Skip to content

Commit

Permalink
refactor(api): add some default vals to TriggerModel (#1047)
Browse files Browse the repository at this point in the history
  • Loading branch information
AleksandrMatsko authored Jul 9, 2024
1 parent 973caa5 commit e719b3f
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
15 changes: 13 additions & 2 deletions api/dto/triggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand All @@ -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}
}
Expand All @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions api/dto/triggers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions api/handler/triggers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down
32 changes: 32 additions & 0 deletions datatypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit e719b3f

Please sign in to comment.