mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Alerting: Persist rule position in the group (#50051)
Migrations: * add a new column alert_group_idx to alert_rule table * add a new column alert_group_idx to alert_rule_version table * re-index existing rules during migration API: * set group index on update. Use the natural order of items in the array as group index * sort rules in the group on GET * update the version of all rules of all affected groups. This will make optimistic lock work in the case of multiple concurrent request touching the same groups. UI: * update UI to keep the order of alerts in a group
This commit is contained in:
@@ -419,6 +419,49 @@ func TestRouteGetRuleStatuses(t *testing.T) {
|
||||
`, folder.Title), string(r.Body()))
|
||||
})
|
||||
|
||||
t.Run("with many rules in a group", func(t *testing.T) {
|
||||
t.Run("should return sorted", func(t *testing.T) {
|
||||
ruleStore := store.NewFakeRuleStore(t)
|
||||
fakeAIM := NewFakeAlertInstanceManager(t)
|
||||
groupKey := ngmodels.GenerateGroupKey(orgID)
|
||||
_, rules := ngmodels.GenerateUniqueAlertRules(rand.Intn(5)+5, ngmodels.AlertRuleGen(withGroupKey(groupKey), ngmodels.WithUniqueGroupIndex()))
|
||||
ruleStore.PutRule(context.Background(), rules...)
|
||||
|
||||
api := PrometheusSrv{
|
||||
log: log.NewNopLogger(),
|
||||
manager: fakeAIM,
|
||||
store: ruleStore,
|
||||
ac: acmock.New().WithDisabled(),
|
||||
}
|
||||
|
||||
response := api.RouteGetRuleStatuses(c)
|
||||
require.Equal(t, http.StatusOK, response.Status())
|
||||
result := &apimodels.RuleResponse{}
|
||||
require.NoError(t, json.Unmarshal(response.Body(), result))
|
||||
|
||||
ngmodels.RulesGroup(rules).SortByGroupIndex()
|
||||
|
||||
require.Len(t, result.Data.RuleGroups, 1)
|
||||
group := result.Data.RuleGroups[0]
|
||||
require.Equal(t, groupKey.RuleGroup, group.Name)
|
||||
require.Len(t, group.Rules, len(rules))
|
||||
for i, actual := range group.Rules {
|
||||
expected := rules[i]
|
||||
if actual.Name != expected.Title {
|
||||
var actualNames []string
|
||||
var expectedNames []string
|
||||
for _, rule := range group.Rules {
|
||||
actualNames = append(actualNames, rule.Name)
|
||||
}
|
||||
for _, rule := range rules {
|
||||
expectedNames = append(expectedNames, rule.Title)
|
||||
}
|
||||
require.Fail(t, fmt.Sprintf("rules are not sorted by group index. Expected: %v. Actual: %v", expectedNames, actualNames))
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("when fine-grained access is enabled", func(t *testing.T) {
|
||||
t.Run("should return only rules if the user can query all data sources", func(t *testing.T) {
|
||||
ruleStore := store.NewFakeRuleStore(t)
|
||||
|
||||
Reference in New Issue
Block a user