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

feat(notifier, api): fix sending notifications on muted and deleted triggers #946

Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions database/redis/last_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,28 @@ func (connector *DbConnector) checkDataScoreChanged(triggerID string, checkData
return oldScore != float64(checkData.Score)
}

// getTriggersLastCheck returns an array of trigger checks by the passed ids, if the trigger does not exist, it is nil
func (connector *DbConnector) getTriggersLastCheck(triggerIDs []string) ([]*moira.CheckData, error) {
ctx := connector.context
pipe := (*connector.client).TxPipeline()

results := make([]*redis.StringCmd, len(triggerIDs))
for i, id := range triggerIDs {
var result *redis.StringCmd
if id != "" {
result = pipe.Get(ctx, metricLastCheckKey(id))
}
results[i] = result
}

_, err := pipe.Exec(ctx)
if err != nil && err != redis.Nil {
return nil, err
}

return reply.Checks(results)
}

var badStateTriggersKey = "moira-bad-state-triggers"
var triggersChecksKey = "moira-triggers-checks"

Expand Down
101 changes: 101 additions & 0 deletions database/redis/last_check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,107 @@ func TestLastCheckErrorConnection(t *testing.T) {
})
}

func TestGetTriggersLastCheck(t *testing.T) {
logger, _ := logging.GetLogger("dataBase")
dataBase := NewTestDatabase(logger)
dataBase.Flush()
defer dataBase.Flush()

_ = dataBase.SetTriggerLastCheck("test1", &moira.CheckData{
Timestamp: 1,
}, moira.TriggerSourceNotSet)

_ = dataBase.SetTriggerLastCheck("test2", &moira.CheckData{
Timestamp: 2,
}, moira.TriggerSourceNotSet)

_ = dataBase.SetTriggerLastCheck("test3", &moira.CheckData{
Timestamp: 3,
}, moira.TriggerSourceNotSet)

Convey("getTriggersLastCheck manipulations", t, func() {
Convey("Test with nil id array", func() {
actual, err := dataBase.getTriggersLastCheck(nil)
So(err, ShouldBeNil)
So(actual, ShouldResemble, []*moira.CheckData{})
})

Convey("Test with correct id array", func() {
actual, err := dataBase.getTriggersLastCheck([]string{"test1", "test2", "test3"})
So(err, ShouldBeNil)
So(actual, ShouldResemble, []*moira.CheckData{
{
Timestamp: 1,
MetricsToTargetRelation: map[string]string{},
},
{
Timestamp: 2,
MetricsToTargetRelation: map[string]string{},
},
{
Timestamp: 3,
MetricsToTargetRelation: map[string]string{},
},
})
})

Convey("Test with deleted trigger", func() {
dataBase.RemoveTriggerLastCheck("test2") //nolint
defer func() {
_ = dataBase.SetTriggerLastCheck("test2", &moira.CheckData{
Timestamp: 2,
}, moira.TriggerSourceNotSet)
}()

actual, err := dataBase.getTriggersLastCheck([]string{"test1", "test2", "test3"})
So(err, ShouldBeNil)
So(actual, ShouldResemble, []*moira.CheckData{
{
Timestamp: 1,
MetricsToTargetRelation: map[string]string{},
},
nil,
{
Timestamp: 3,
MetricsToTargetRelation: map[string]string{},
},
})
})

Convey("Test with a nonexistent trigger id", func() {
actual, err := dataBase.getTriggersLastCheck([]string{"test1", "test2", "test4"})
So(err, ShouldBeNil)
So(actual, ShouldResemble, []*moira.CheckData{
{
Timestamp: 1,
MetricsToTargetRelation: map[string]string{},
},
{
Timestamp: 2,
MetricsToTargetRelation: map[string]string{},
},
nil,
})
})

Convey("Test with an empty trigger id", func() {
actual, err := dataBase.getTriggersLastCheck([]string{"", "test2", "test3"})
So(err, ShouldBeNil)
So(actual, ShouldResemble, []*moira.CheckData{
nil,
{
Timestamp: 2,
MetricsToTargetRelation: map[string]string{},
},
{
Timestamp: 3,
MetricsToTargetRelation: map[string]string{},
},
})
})
})
}

func TestMaintenanceUserSave(t *testing.T) {
logger, _ := logging.GetLogger("dataBase")
dataBase := NewTestDatabase(logger)
Expand Down
Loading