mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
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:
parent
4c8ce76929
commit
543f0ae37e
@ -663,7 +663,7 @@ func (srv RulerSrv) getAuthorizedRuleGroup(ctx context.Context, c *contextmodel.
|
|||||||
q := ngmodels.ListAlertRulesQuery{
|
q := ngmodels.ListAlertRulesQuery{
|
||||||
OrgID: ruleGroupKey.OrgID,
|
OrgID: ruleGroupKey.OrgID,
|
||||||
NamespaceUIDs: []string{ruleGroupKey.NamespaceUID},
|
NamespaceUIDs: []string{ruleGroupKey.NamespaceUID},
|
||||||
RuleGroup: ruleGroupKey.RuleGroup,
|
RuleGroups: []string{ruleGroupKey.RuleGroup},
|
||||||
}
|
}
|
||||||
rules, err := srv.store.ListAlertRules(ctx, &q)
|
rules, err := srv.store.ListAlertRules(ctx, &q)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -611,7 +611,7 @@ type ListAlertRulesQuery struct {
|
|||||||
OrgID int64
|
OrgID int64
|
||||||
NamespaceUIDs []string
|
NamespaceUIDs []string
|
||||||
ExcludeOrgs []int64
|
ExcludeOrgs []int64
|
||||||
RuleGroup string
|
RuleGroups []string
|
||||||
|
|
||||||
// DashboardUID and PanelID are optional and allow filtering rules
|
// DashboardUID and PanelID are optional and allow filtering rules
|
||||||
// to return just those for a dashboard and panel.
|
// to return just those for a dashboard and panel.
|
||||||
|
@ -266,7 +266,7 @@ func (service *AlertRuleService) GetRuleGroup(ctx context.Context, user identity
|
|||||||
q := models.ListAlertRulesQuery{
|
q := models.ListAlertRulesQuery{
|
||||||
OrgID: user.GetOrgID(),
|
OrgID: user.GetOrgID(),
|
||||||
NamespaceUIDs: []string{namespaceUID},
|
NamespaceUIDs: []string{namespaceUID},
|
||||||
RuleGroup: group,
|
RuleGroups: []string{group},
|
||||||
}
|
}
|
||||||
ruleList, err := service.ruleStore.ListAlertRules(ctx, &q)
|
ruleList, err := service.ruleStore.ListAlertRules(ctx, &q)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -308,7 +308,7 @@ func (service *AlertRuleService) UpdateRuleGroup(ctx context.Context, user ident
|
|||||||
query := &models.ListAlertRulesQuery{
|
query := &models.ListAlertRulesQuery{
|
||||||
OrgID: user.GetOrgID(),
|
OrgID: user.GetOrgID(),
|
||||||
NamespaceUIDs: []string{namespaceUID},
|
NamespaceUIDs: []string{namespaceUID},
|
||||||
RuleGroup: ruleGroup,
|
RuleGroups: []string{ruleGroup},
|
||||||
}
|
}
|
||||||
ruleList, err := service.ruleStore.ListAlertRules(ctx, query)
|
ruleList, err := service.ruleStore.ListAlertRules(ctx, query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -436,7 +436,7 @@ func (service *AlertRuleService) calcDelta(ctx context.Context, user identity.Re
|
|||||||
listRulesQuery := models.ListAlertRulesQuery{
|
listRulesQuery := models.ListAlertRulesQuery{
|
||||||
OrgID: user.GetOrgID(),
|
OrgID: user.GetOrgID(),
|
||||||
NamespaceUIDs: []string{group.FolderUID},
|
NamespaceUIDs: []string{group.FolderUID},
|
||||||
RuleGroup: group.Title,
|
RuleGroups: []string{group.Title},
|
||||||
}
|
}
|
||||||
ruleList, err := service.ruleStore.ListAlertRules(ctx, &listRulesQuery)
|
ruleList, err := service.ruleStore.ListAlertRules(ctx, &listRulesQuery)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -363,17 +363,13 @@ func (st DBstore) ListAlertRules(ctx context.Context, query *ngmodels.ListAlertR
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(query.NamespaceUIDs) > 0 {
|
if len(query.NamespaceUIDs) > 0 {
|
||||||
args := make([]any, 0, len(query.NamespaceUIDs))
|
args, in := getINSubQueryArgs(query.NamespaceUIDs)
|
||||||
in := make([]string, 0, len(query.NamespaceUIDs))
|
|
||||||
for _, namespaceUID := range query.NamespaceUIDs {
|
|
||||||
args = append(args, namespaceUID)
|
|
||||||
in = append(in, "?")
|
|
||||||
}
|
|
||||||
q = q.Where(fmt.Sprintf("namespace_uid IN (%s)", strings.Join(in, ",")), args...)
|
q = q.Where(fmt.Sprintf("namespace_uid IN (%s)", strings.Join(in, ",")), args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
if query.RuleGroup != "" {
|
if len(query.RuleGroups) > 0 {
|
||||||
q = q.Where("rule_group = ?", query.RuleGroup)
|
args, in := getINSubQueryArgs(query.RuleGroups)
|
||||||
|
q = q.Where(fmt.Sprintf("rule_group IN (%s)", strings.Join(in, ",")), args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
if query.ReceiverName != "" {
|
if query.ReceiverName != "" {
|
||||||
@ -789,3 +785,14 @@ func (st DBstore) GetNamespacesByRuleUID(ctx context.Context, orgID int64, uids
|
|||||||
})
|
})
|
||||||
return result, err
|
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
|
||||||
|
}
|
||||||
|
@ -63,7 +63,7 @@ func CalculateChanges(ctx context.Context, ruleReader RuleReader, groupKey model
|
|||||||
q := &models.ListAlertRulesQuery{
|
q := &models.ListAlertRulesQuery{
|
||||||
OrgID: groupKey.OrgID,
|
OrgID: groupKey.OrgID,
|
||||||
NamespaceUIDs: []string{groupKey.NamespaceUID},
|
NamespaceUIDs: []string{groupKey.NamespaceUID},
|
||||||
RuleGroup: groupKey.RuleGroup,
|
RuleGroups: []string{groupKey.RuleGroup},
|
||||||
}
|
}
|
||||||
existingGroupRules, err := ruleReader.ListAlertRules(ctx, q)
|
existingGroupRules, err := ruleReader.ListAlertRules(ctx, q)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -203,7 +203,7 @@ func CalculateRuleUpdate(ctx context.Context, ruleReader RuleReader, rule *model
|
|||||||
q := &models.ListAlertRulesQuery{
|
q := &models.ListAlertRulesQuery{
|
||||||
OrgID: rule.OrgID,
|
OrgID: rule.OrgID,
|
||||||
NamespaceUIDs: []string{rule.NamespaceUID},
|
NamespaceUIDs: []string{rule.NamespaceUID},
|
||||||
RuleGroup: rule.RuleGroup,
|
RuleGroups: []string{rule.RuleGroup},
|
||||||
}
|
}
|
||||||
existingGroupRules, err := ruleReader.ListAlertRules(ctx, q)
|
existingGroupRules, err := ruleReader.ListAlertRules(ctx, q)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -232,7 +232,7 @@ func CalculateRuleGroupDelete(ctx context.Context, ruleReader RuleReader, groupK
|
|||||||
q := models.ListAlertRulesQuery{
|
q := models.ListAlertRulesQuery{
|
||||||
OrgID: groupKey.OrgID,
|
OrgID: groupKey.OrgID,
|
||||||
NamespaceUIDs: []string{groupKey.NamespaceUID},
|
NamespaceUIDs: []string{groupKey.NamespaceUID},
|
||||||
RuleGroup: groupKey.RuleGroup,
|
RuleGroups: []string{groupKey.RuleGroup},
|
||||||
}
|
}
|
||||||
ruleList, err := ruleReader.ListAlertRules(ctx, &q)
|
ruleList, err := ruleReader.ListAlertRules(ctx, &q)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -288,7 +288,7 @@ func CalculateRuleCreate(ctx context.Context, ruleReader RuleReader, rule *model
|
|||||||
q := &models.ListAlertRulesQuery{
|
q := &models.ListAlertRulesQuery{
|
||||||
OrgID: rule.OrgID,
|
OrgID: rule.OrgID,
|
||||||
NamespaceUIDs: []string{rule.NamespaceUID},
|
NamespaceUIDs: []string{rule.NamespaceUID},
|
||||||
RuleGroup: rule.RuleGroup,
|
RuleGroups: []string{rule.RuleGroup},
|
||||||
}
|
}
|
||||||
group, err := ruleReader.ListAlertRules(ctx, q)
|
group, err := ruleReader.ListAlertRules(ctx, q)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"slices"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@ -198,33 +199,18 @@ func (f *RuleStore) ListAlertRules(_ context.Context, q *models.ListAlertRulesQu
|
|||||||
return true
|
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{}
|
ruleList := models.RulesGroup{}
|
||||||
for _, r := range f.Rules[q.OrgID] {
|
for _, r := range f.Rules[q.OrgID] {
|
||||||
if !hasDashboard(r, q.DashboardUID, q.PanelID) {
|
if !hasDashboard(r, q.DashboardUID, q.PanelID) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if !hasNamespace(r, q.NamespaceUIDs) {
|
if len(q.NamespaceUIDs) > 0 && !slices.Contains(q.NamespaceUIDs, r.NamespaceUID) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if q.RuleGroup != "" && r.RuleGroup != q.RuleGroup {
|
if len(q.RuleGroups) > 0 && !slices.Contains(q.RuleGroups, r.RuleGroup) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
ruleList = append(ruleList, r)
|
ruleList = append(ruleList, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -141,7 +141,7 @@ func CreateTestAlertRuleWithLabels(t testing.TB, ctx context.Context, dbstore *s
|
|||||||
q := models.ListAlertRulesQuery{
|
q := models.ListAlertRulesQuery{
|
||||||
OrgID: orgID,
|
OrgID: orgID,
|
||||||
NamespaceUIDs: []string{folderUID},
|
NamespaceUIDs: []string{folderUID},
|
||||||
RuleGroup: ruleGroup,
|
RuleGroups: []string{ruleGroup},
|
||||||
}
|
}
|
||||||
ruleList, err := dbstore.ListAlertRules(ctx, &q)
|
ruleList, err := dbstore.ListAlertRules(ctx, &q)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
Loading…
Reference in New Issue
Block a user