Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(api): add some default vals to TriggerModel #1047

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 {
almostinf marked this conversation as resolved.
Show resolved Hide resolved
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.GetDefaultScheduleData()
}

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.GetDefaultScheduleData(),
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 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 {
AleksandrMatsko marked this conversation as resolved.
Show resolved Hide resolved
AleksandrMatsko marked this conversation as resolved.
Show resolved Hide resolved
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
AleksandrMatsko marked this conversation as resolved.
Show resolved Hide resolved
)

// 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
Loading