diff --git a/api/dto/triggers.go b/api/dto/triggers.go index ae33863e0..59521c89d 100644 --- a/api/dto/triggers.go +++ b/api/dto/triggers.go @@ -90,8 +90,8 @@ type TriggerModel struct { } // ClusterKey returns cluster key composed of trigger source and cluster id associated with the trigger. -func (trigger *TriggerModel) ClusterKey() moira.ClusterKey { - return moira.MakeClusterKey(trigger.TriggerSource, trigger.ClusterId) +func (model *TriggerModel) ClusterKey() moira.ClusterKey { + return moira.MakeClusterKey(model.TriggerSource, model.ClusterId) } // ToMoiraTrigger transforms TriggerModel to moira.Trigger. @@ -184,6 +184,10 @@ func (trigger *Trigger) Bind(request *http.Request) error { } } + if trigger.TTLState == nil { + trigger.TTLState = &moira.TTLStateNODATA + } + triggerExpression := expression.TriggerExpression{ AdditionalTargetsValues: make(map[string]float64), WarnValue: trigger.WarnValue, @@ -202,6 +206,9 @@ func (trigger *Trigger) Bind(request *http.Request) error { return err } + if trigger.TTL == 0 { + trigger.TTL = moira.DefaultTTL + } if err := checkTTLSanity(trigger, metricsSource); err != nil { return api.ErrInvalidRequestContent{ValidationError: err} } @@ -218,6 +225,10 @@ func (trigger *Trigger) Bind(request *http.Request) error { } } + if trigger.Schedule == nil { + trigger.Schedule = moira.NewDefaultScheduleData() + } + middleware.SetTimeSeriesNames(request, metricsDataNames) if _, err := triggerExpression.Evaluate(); err != nil { diff --git a/api/dto/triggers_test.go b/api/dto/triggers_test.go index 6b4d317aa..78279a9fc 100644 --- a/api/dto/triggers_test.go +++ b/api/dto/triggers_test.go @@ -46,6 +46,7 @@ func TestTriggerValidation(t *testing.T) { Tags: tags, TTLState: &moira.TTLStateNODATA, TTL: 600, + Schedule: moira.NewDefaultScheduleData(), TriggerSource: moira.GraphiteLocal, ClusterId: moira.DefaultCluster, MuteNewMetrics: false, diff --git a/api/handler/triggers_test.go b/api/handler/triggers_test.go index f77c48a8b..fd39a922d 100644 --- a/api/handler/triggers_test.go +++ b/api/handler/triggers_test.go @@ -97,7 +97,10 @@ func TestGetTriggerFromRequest(t *testing.T) { request = request.WithContext(middleware.SetContextValueForTest(request.Context(), "metricSourceProvider", sourceProvider)) Convey("It should be parsed successfully", func() { + triggerDTO.TTL = moira.DefaultTTL + trigger, err := getTriggerFromRequest(request) + So(err, ShouldBeNil) So(trigger, ShouldResemble, &triggerDTO) }) diff --git a/datatypes.go b/datatypes.go index 34fe869da..1e82631f8 100644 --- a/datatypes.go +++ b/datatypes.go @@ -247,6 +247,33 @@ type ScheduleDataDay struct { Name string `json:"name,omitempty" example:"Mon"` } +const ( + // DefaultTimezoneOffset is a default value for timezone offset for (GMT+3) used in NewDefaultScheduleData. + DefaultTimezoneOffset = -180 + // DefaultStartOffset is a default value for start offset for (GMT+3) used in NewDefaultScheduleData. + DefaultStartOffset = 0 + // DefaultEndOffset is a default value for end offset for (GMT+3) used in NewDefaultScheduleData. + DefaultEndOffset = 1439 +) + +// NewDefaultScheduleData returns the default ScheduleData which can be used in Trigger. +func NewDefaultScheduleData() *ScheduleData { + return &ScheduleData{ + Days: []ScheduleDataDay{ + {Name: "Mon", Enabled: true}, + {Name: "Tue", Enabled: true}, + {Name: "Wed", Enabled: true}, + {Name: "Thu", Enabled: true}, + {Name: "Fri", Enabled: true}, + {Name: "Sat", Enabled: true}, + {Name: "Sun", Enabled: true}, + }, + TimezoneOffset: DefaultTimezoneOffset, + StartOffset: DefaultStartOffset, + EndOffset: DefaultEndOffset, + } +} + // ScheduledNotification represent notification object. type ScheduledNotification struct { Event NotificationEvent `json:"event"` @@ -355,6 +382,11 @@ type Trigger struct { UpdatedBy string `json:"updated_by"` } +const ( + // DefaultTTL is a default value for Trigger.TTL. + DefaultTTL = 600 +) + // ClusterKey returns cluster key composed of trigger source and cluster id associated with the trigger. func (trigger *Trigger) ClusterKey() ClusterKey { return MakeClusterKey(trigger.TriggerSource, trigger.ClusterId)