Alerting: Update ListAlertRulesQuery to take a slice of RuleGroups (#88385)

* Change ListAlertRulesQuery to take RuleGroup slice instead

* Change func name

* Change func name

* Fix fakes

* Fix function arg
This commit is contained in:
Fayzal Ghantiwala 2024-05-29 11:50:33 +01:00 committed by GitHub
parent 4c8ce76929
commit 543f0ae37e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 29 additions and 36 deletions

View File

@ -663,7 +663,7 @@ func (srv RulerSrv) getAuthorizedRuleGroup(ctx context.Context, c *contextmodel.
q := ngmodels.ListAlertRulesQuery{
OrgID: ruleGroupKey.OrgID,
NamespaceUIDs: []string{ruleGroupKey.NamespaceUID},
RuleGroup: ruleGroupKey.RuleGroup,
RuleGroups: []string{ruleGroupKey.RuleGroup},
}
rules, err := srv.store.ListAlertRules(ctx, &q)
if err != nil {

View File

@ -611,7 +611,7 @@ type ListAlertRulesQuery struct {
OrgID int64
NamespaceUIDs []string
ExcludeOrgs []int64
RuleGroup string
RuleGroups []string
// DashboardUID and PanelID are optional and allow filtering rules
// to return just those for a dashboard and panel.

View File

@ -266,7 +266,7 @@ func (service *AlertRuleService) GetRuleGroup(ctx context.Context, user identity
q := models.ListAlertRulesQuery{
OrgID: user.GetOrgID(),
NamespaceUIDs: []string{namespaceUID},
RuleGroup: group,
RuleGroups: []string{group},
}
ruleList, err := service.ruleStore.ListAlertRules(ctx, &q)
if err != nil {
@ -308,7 +308,7 @@ func (service *AlertRuleService) UpdateRuleGroup(ctx context.Context, user ident
query := &models.ListAlertRulesQuery{
OrgID: user.GetOrgID(),
NamespaceUIDs: []string{namespaceUID},
RuleGroup: ruleGroup,
RuleGroups: []string{ruleGroup},
}
ruleList, err := service.ruleStore.ListAlertRules(ctx, query)
if err != nil {
@ -436,7 +436,7 @@ func (service *AlertRuleService) calcDelta(ctx context.Context, user identity.Re
listRulesQuery := models.ListAlertRulesQuery{
OrgID: user.GetOrgID(),
NamespaceUIDs: []string{group.FolderUID},
RuleGroup: group.Title,
RuleGroups: []string{group.Title},
}
ruleList, err := service.ruleStore.ListAlertRules(ctx, &listRulesQuery)
if err != nil {

View File

@ -363,17 +363,13 @@ func (st DBstore) ListAlertRules(ctx context.Context, query *ngmodels.ListAlertR
}
if len(query.NamespaceUIDs) > 0 {
args := make([]any, 0, len(query.NamespaceUIDs))
in := make([]string, 0, len(query.NamespaceUIDs))
for _, namespaceUID := range query.NamespaceUIDs {
args = append(args, namespaceUID)
in = append(in, "?")
}
args, in := getINSubQueryArgs(query.NamespaceUIDs)
q = q.Where(fmt.Sprintf("namespace_uid IN (%s)", strings.Join(in, ",")), args...)
}
if query.RuleGroup != "" {
q = q.Where("rule_group = ?", query.RuleGroup)
if len(query.RuleGroups) > 0 {
args, in := getINSubQueryArgs(query.RuleGroups)
q = q.Where(fmt.Sprintf("rule_group IN (%s)", strings.Join(in, ",")), args...)
}
if query.ReceiverName != "" {
@ -789,3 +785,14 @@ func (st DBstore) GetNamespacesByRuleUID(ctx context.Context, orgID int64, uids
})
return result, err
}
func getINSubQueryArgs[T any](inputSlice []T) ([]any, []string) {
args := make([]any, 0, len(inputSlice))
in := make([]string, 0, len(inputSlice))
for _, t := range inputSlice {
args = append(args, t)
in = append(in, "?")
}
return args, in
}

View File

@ -63,7 +63,7 @@ func CalculateChanges(ctx context.Context, ruleReader RuleReader, groupKey model
q := &models.ListAlertRulesQuery{
OrgID: groupKey.OrgID,
NamespaceUIDs: []string{groupKey.NamespaceUID},
RuleGroup: groupKey.RuleGroup,
RuleGroups: []string{groupKey.RuleGroup},
}
existingGroupRules, err := ruleReader.ListAlertRules(ctx, q)
if err != nil {
@ -203,7 +203,7 @@ func CalculateRuleUpdate(ctx context.Context, ruleReader RuleReader, rule *model
q := &models.ListAlertRulesQuery{
OrgID: rule.OrgID,
NamespaceUIDs: []string{rule.NamespaceUID},
RuleGroup: rule.RuleGroup,
RuleGroups: []string{rule.RuleGroup},
}
existingGroupRules, err := ruleReader.ListAlertRules(ctx, q)
if err != nil {
@ -232,7 +232,7 @@ func CalculateRuleGroupDelete(ctx context.Context, ruleReader RuleReader, groupK
q := models.ListAlertRulesQuery{
OrgID: groupKey.OrgID,
NamespaceUIDs: []string{groupKey.NamespaceUID},
RuleGroup: groupKey.RuleGroup,
RuleGroups: []string{groupKey.RuleGroup},
}
ruleList, err := ruleReader.ListAlertRules(ctx, &q)
if err != nil {
@ -288,7 +288,7 @@ func CalculateRuleCreate(ctx context.Context, ruleReader RuleReader, rule *model
q := &models.ListAlertRulesQuery{
OrgID: rule.OrgID,
NamespaceUIDs: []string{rule.NamespaceUID},
RuleGroup: rule.RuleGroup,
RuleGroups: []string{rule.RuleGroup},
}
group, err := ruleReader.ListAlertRules(ctx, q)
if err != nil {

View File

@ -4,6 +4,7 @@ import (
"context"
"fmt"
"math/rand"
"slices"
"sync"
"testing"
"time"
@ -198,33 +199,18 @@ func (f *RuleStore) ListAlertRules(_ context.Context, q *models.ListAlertRulesQu
return true
}
hasNamespace := func(r *models.AlertRule, namespaceUIDs []string) bool {
if len(namespaceUIDs) > 0 {
var ok bool
for _, uid := range q.NamespaceUIDs {
if uid == r.NamespaceUID {
ok = true
break
}
}
if !ok {
return false
}
}
return true
}
ruleList := models.RulesGroup{}
for _, r := range f.Rules[q.OrgID] {
if !hasDashboard(r, q.DashboardUID, q.PanelID) {
continue
}
if !hasNamespace(r, q.NamespaceUIDs) {
if len(q.NamespaceUIDs) > 0 && !slices.Contains(q.NamespaceUIDs, r.NamespaceUID) {
continue
}
if q.RuleGroup != "" && r.RuleGroup != q.RuleGroup {
if len(q.RuleGroups) > 0 && !slices.Contains(q.RuleGroups, r.RuleGroup) {
continue
}
ruleList = append(ruleList, r)
}

View File

@ -141,7 +141,7 @@ func CreateTestAlertRuleWithLabels(t testing.TB, ctx context.Context, dbstore *s
q := models.ListAlertRulesQuery{
OrgID: orgID,
NamespaceUIDs: []string{folderUID},
RuleGroup: ruleGroup,
RuleGroups: []string{ruleGroup},
}
ruleList, err := dbstore.ListAlertRules(ctx, &q)
require.NoError(t, err)