From c0efccf867da20c27ba9f4f902d67275ad50d190 Mon Sep 17 00:00:00 2001 From: AleksandrMatsko Date: Fri, 5 Jul 2024 16:35:33 +0700 Subject: [PATCH 1/8] refactor: add default values for TTL and Schedule in dto.TriggerModel --- api/dto/triggers.go | 28 ++++++++++++++++++++++++++++ api/dto/triggers_test.go | 1 + api/handler/triggers_test.go | 3 +++ 3 files changed, 32 insertions(+) diff --git a/api/dto/triggers.go b/api/dto/triggers.go index ae33863e0..9140495b1 100644 --- a/api/dto/triggers.go +++ b/api/dto/triggers.go @@ -22,6 +22,10 @@ var targetNameRegex = regexp.MustCompile("t(\\d+)") // TODO(litleleprikon): Remove after https://github.com/moira-alert/moira/issues/550 will be resolved. var asteriskPattern = "*" +const ( + DefaultTTL = 600 +) + type TriggersList struct { Page *int64 `json:"page,omitempty" format:"int64" extensions:"x-nullable"` Size *int64 `json:"size,omitempty" format:"int64" extensions:"x-nullable"` @@ -202,6 +206,9 @@ func (trigger *Trigger) Bind(request *http.Request) error { return err } + if trigger.TTL == 0 { + trigger.TTL = 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 = getDefaultScheduleData() + } + middleware.SetTimeSeriesNames(request, metricsDataNames) if _, err := triggerExpression.Evaluate(); err != nil { @@ -289,6 +300,23 @@ func resolvePatterns(trigger *Trigger, expressionValues *expression.TriggerExpre return metricsDataNames, nil } +func getDefaultScheduleData() *moira.ScheduleData { + return &moira.ScheduleData{ + Days: []moira.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: 0, + StartOffset: 0, + EndOffset: 0, + } +} + func checkWarnErrorExpression(trigger *Trigger) error { if trigger.WarnValue == nil && trigger.ErrorValue == nil && trigger.Expression == "" { return fmt.Errorf("at least one of error_value, warn_value or expression is required") diff --git a/api/dto/triggers_test.go b/api/dto/triggers_test.go index 6b4d317aa..ef9f7a799 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: getDefaultScheduleData(), TriggerSource: moira.GraphiteLocal, ClusterId: moira.DefaultCluster, MuteNewMetrics: false, diff --git a/api/handler/triggers_test.go b/api/handler/triggers_test.go index f77c48a8b..2ca6b63f2 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 = dto.DefaultTTL + trigger, err := getTriggerFromRequest(request) + So(err, ShouldBeNil) So(trigger, ShouldResemble, &triggerDTO) }) From 3489a9240c9aa434a6ffc060768d6711ce2b1b7f Mon Sep 17 00:00:00 2001 From: AleksandrMatsko Date: Fri, 5 Jul 2024 16:41:41 +0700 Subject: [PATCH 2/8] style: rename receiver for TriggerModel --- api/dto/triggers.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api/dto/triggers.go b/api/dto/triggers.go index 9140495b1..14d7d09df 100644 --- a/api/dto/triggers.go +++ b/api/dto/triggers.go @@ -94,8 +94,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. From 548b0be6ad3e1157960044aba1b0498ab6ebf3ed Mon Sep 17 00:00:00 2001 From: AleksandrMatsko Date: Fri, 5 Jul 2024 19:07:51 +0700 Subject: [PATCH 3/8] refactor: move default ttl value and helper function to other package --- api/dto/triggers.go | 25 ++----------------------- api/dto/triggers_test.go | 2 +- api/handler/triggers_test.go | 2 +- datatypes.go | 21 +++++++++++++++++++++ 4 files changed, 25 insertions(+), 25 deletions(-) diff --git a/api/dto/triggers.go b/api/dto/triggers.go index 14d7d09df..d0011aadb 100644 --- a/api/dto/triggers.go +++ b/api/dto/triggers.go @@ -22,10 +22,6 @@ var targetNameRegex = regexp.MustCompile("t(\\d+)") // TODO(litleleprikon): Remove after https://github.com/moira-alert/moira/issues/550 will be resolved. var asteriskPattern = "*" -const ( - DefaultTTL = 600 -) - type TriggersList struct { Page *int64 `json:"page,omitempty" format:"int64" extensions:"x-nullable"` Size *int64 `json:"size,omitempty" format:"int64" extensions:"x-nullable"` @@ -207,7 +203,7 @@ func (trigger *Trigger) Bind(request *http.Request) error { } if trigger.TTL == 0 { - trigger.TTL = DefaultTTL + trigger.TTL = moira.DefaultTTL } if err := checkTTLSanity(trigger, metricsSource); err != nil { return api.ErrInvalidRequestContent{ValidationError: err} @@ -226,7 +222,7 @@ func (trigger *Trigger) Bind(request *http.Request) error { } if trigger.Schedule == nil { - trigger.Schedule = getDefaultScheduleData() + trigger.Schedule = moira.GetDefaultScheduleData() } middleware.SetTimeSeriesNames(request, metricsDataNames) @@ -300,23 +296,6 @@ func resolvePatterns(trigger *Trigger, expressionValues *expression.TriggerExpre return metricsDataNames, nil } -func getDefaultScheduleData() *moira.ScheduleData { - return &moira.ScheduleData{ - Days: []moira.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: 0, - StartOffset: 0, - EndOffset: 0, - } -} - func checkWarnErrorExpression(trigger *Trigger) error { if trigger.WarnValue == nil && trigger.ErrorValue == nil && trigger.Expression == "" { return fmt.Errorf("at least one of error_value, warn_value or expression is required") diff --git a/api/dto/triggers_test.go b/api/dto/triggers_test.go index ef9f7a799..caca881c0 100644 --- a/api/dto/triggers_test.go +++ b/api/dto/triggers_test.go @@ -46,7 +46,7 @@ func TestTriggerValidation(t *testing.T) { Tags: tags, TTLState: &moira.TTLStateNODATA, TTL: 600, - Schedule: getDefaultScheduleData(), + Schedule: moira.GetDefaultScheduleData(), TriggerSource: moira.GraphiteLocal, ClusterId: moira.DefaultCluster, MuteNewMetrics: false, diff --git a/api/handler/triggers_test.go b/api/handler/triggers_test.go index 2ca6b63f2..fd39a922d 100644 --- a/api/handler/triggers_test.go +++ b/api/handler/triggers_test.go @@ -97,7 +97,7 @@ func TestGetTriggerFromRequest(t *testing.T) { request = request.WithContext(middleware.SetContextValueForTest(request.Context(), "metricSourceProvider", sourceProvider)) Convey("It should be parsed successfully", func() { - triggerDTO.TTL = dto.DefaultTTL + triggerDTO.TTL = moira.DefaultTTL trigger, err := getTriggerFromRequest(request) diff --git a/datatypes.go b/datatypes.go index 34fe869da..5e7ce7ee8 100644 --- a/datatypes.go +++ b/datatypes.go @@ -247,6 +247,23 @@ type ScheduleDataDay struct { Name string `json:"name,omitempty" example:"Mon"` } +func GetDefaultScheduleData() *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: 0, + StartOffset: 0, + EndOffset: 0, + } +} + // ScheduledNotification represent notification object. type ScheduledNotification struct { Event NotificationEvent `json:"event"` @@ -355,6 +372,10 @@ type Trigger struct { UpdatedBy string `json:"updated_by"` } +const ( + 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) From 7bb91135c87e4ffd4e2937ea63502381c1f620ca Mon Sep 17 00:00:00 2001 From: AleksandrMatsko Date: Fri, 5 Jul 2024 20:54:38 +0700 Subject: [PATCH 4/8] docs: for new constant and function --- datatypes.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/datatypes.go b/datatypes.go index 5e7ce7ee8..0d0975a31 100644 --- a/datatypes.go +++ b/datatypes.go @@ -247,6 +247,7 @@ type ScheduleDataDay struct { Name string `json:"name,omitempty" example:"Mon"` } +// GetDefaultScheduleData returns the default ScheduleData which can be used in Trigger func GetDefaultScheduleData() *ScheduleData { return &ScheduleData{ Days: []ScheduleDataDay{ @@ -373,6 +374,7 @@ type Trigger struct { } const ( + // DefaultTTL is a default value for Trigger.TTL. DefaultTTL = 600 ) From 853da5f80bca9bdeec8b8e95282d3bd8295f0d36 Mon Sep 17 00:00:00 2001 From: AleksandrMatsko Date: Fri, 5 Jul 2024 20:56:05 +0700 Subject: [PATCH 5/8] docs: fix dot --- datatypes.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/datatypes.go b/datatypes.go index 0d0975a31..15fb3d560 100644 --- a/datatypes.go +++ b/datatypes.go @@ -247,7 +247,7 @@ type ScheduleDataDay struct { Name string `json:"name,omitempty" example:"Mon"` } -// GetDefaultScheduleData returns the default ScheduleData which can be used in Trigger +// GetDefaultScheduleData returns the default ScheduleData which can be used in Trigger. func GetDefaultScheduleData() *ScheduleData { return &ScheduleData{ Days: []ScheduleDataDay{ From 3d42d77211d850328a17b57c3e9ed2ccba0faa44 Mon Sep 17 00:00:00 2001 From: AleksandrMatsko Date: Mon, 8 Jul 2024 10:48:54 +0700 Subject: [PATCH 6/8] refactor: default values used in GetDefaultScheduleData --- datatypes.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/datatypes.go b/datatypes.go index 15fb3d560..9a990af4a 100644 --- a/datatypes.go +++ b/datatypes.go @@ -247,6 +247,15 @@ type ScheduleDataDay struct { Name string `json:"name,omitempty" example:"Mon"` } +const ( + // DefaultTimezoneOffset is a default value for timezone offset for (GMT+3) used in GetDefaultScheduleData. + DefaultTimezoneOffset = -180 + // DefaultStartOffset is a default value for start offset for (GMT+3) used in GetDefaultScheduleData. + DefaultStartOffset = 0 + // DefaultEndOffset is a default value for end offset for (GMT+3) used in GetDefaultScheduleData. + DefaultEndOffset = 1439 +) + // GetDefaultScheduleData returns the default ScheduleData which can be used in Trigger. func GetDefaultScheduleData() *ScheduleData { return &ScheduleData{ @@ -259,9 +268,9 @@ func GetDefaultScheduleData() *ScheduleData { {Name: "Sat", Enabled: true}, {Name: "Sun", Enabled: true}, }, - TimezoneOffset: 0, - StartOffset: 0, - EndOffset: 0, + TimezoneOffset: DefaultTimezoneOffset, + StartOffset: DefaultStartOffset, + EndOffset: DefaultEndOffset, } } From 45cc38673b661fcaa3cdee8c54bacac8cdc5b015 Mon Sep 17 00:00:00 2001 From: AleksandrMatsko Date: Mon, 8 Jul 2024 17:47:07 +0700 Subject: [PATCH 7/8] refactor: default value for trigger.TTLState --- api/dto/triggers.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/api/dto/triggers.go b/api/dto/triggers.go index d0011aadb..d457fd1d2 100644 --- a/api/dto/triggers.go +++ b/api/dto/triggers.go @@ -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, From 5b97d58a6505939e75ce844ab78f9e6ae5b38639 Mon Sep 17 00:00:00 2001 From: AleksandrMatsko Date: Mon, 8 Jul 2024 22:08:12 +0700 Subject: [PATCH 8/8] refactor: rename constructor --- api/dto/triggers.go | 2 +- api/dto/triggers_test.go | 2 +- datatypes.go | 10 +++++----- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/api/dto/triggers.go b/api/dto/triggers.go index d457fd1d2..59521c89d 100644 --- a/api/dto/triggers.go +++ b/api/dto/triggers.go @@ -226,7 +226,7 @@ func (trigger *Trigger) Bind(request *http.Request) error { } if trigger.Schedule == nil { - trigger.Schedule = moira.GetDefaultScheduleData() + trigger.Schedule = moira.NewDefaultScheduleData() } middleware.SetTimeSeriesNames(request, metricsDataNames) diff --git a/api/dto/triggers_test.go b/api/dto/triggers_test.go index caca881c0..78279a9fc 100644 --- a/api/dto/triggers_test.go +++ b/api/dto/triggers_test.go @@ -46,7 +46,7 @@ func TestTriggerValidation(t *testing.T) { Tags: tags, TTLState: &moira.TTLStateNODATA, TTL: 600, - Schedule: moira.GetDefaultScheduleData(), + Schedule: moira.NewDefaultScheduleData(), TriggerSource: moira.GraphiteLocal, ClusterId: moira.DefaultCluster, MuteNewMetrics: false, diff --git a/datatypes.go b/datatypes.go index 9a990af4a..1e82631f8 100644 --- a/datatypes.go +++ b/datatypes.go @@ -248,16 +248,16 @@ type ScheduleDataDay struct { } const ( - // DefaultTimezoneOffset is a default value for timezone offset for (GMT+3) used in GetDefaultScheduleData. + // 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 GetDefaultScheduleData. + // 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 GetDefaultScheduleData. + // DefaultEndOffset is a default value for end offset for (GMT+3) used in NewDefaultScheduleData. DefaultEndOffset = 1439 ) -// GetDefaultScheduleData returns the default ScheduleData which can be used in Trigger. -func GetDefaultScheduleData() *ScheduleData { +// NewDefaultScheduleData returns the default ScheduleData which can be used in Trigger. +func NewDefaultScheduleData() *ScheduleData { return &ScheduleData{ Days: []ScheduleDataDay{ {Name: "Mon", Enabled: true},