Alerting: Add GetRuleGroups to RuleStore (#48036)

This commit adds a new method GetRuleGroups to RuleStore which returns the set of rule groups across all organizations.
This commit is contained in:
George Robinson 2022-04-21 17:59:22 +01:00 committed by GitHub
parent 9595b56f0d
commit d66fc6ed1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 0 deletions

View File

@ -265,7 +265,14 @@ type GetAlertRulesQuery struct {
Result []*AlertRule
}
// ListRuleGroupsQuery is the query for listing unique rule groups
// across all organizations
type ListRuleGroupsQuery struct {
Result []string
}
// ListOrgRuleGroupsQuery is the query for listing unique rule groups
// for an organization
type ListOrgRuleGroupsQuery struct {
OrgID int64
NamespaceUIDs []string

View File

@ -39,6 +39,8 @@ type RuleStore interface {
GetAlertRuleByUID(ctx context.Context, query *ngmodels.GetAlertRuleByUIDQuery) error
GetAlertRulesForScheduling(ctx context.Context, query *ngmodels.ListAlertRulesQuery) error
GetOrgAlertRules(ctx context.Context, query *ngmodels.ListAlertRulesQuery) error
// GetRuleGroups returns the unique rule groups across all organizations.
GetRuleGroups(ctx context.Context, query *ngmodels.ListRuleGroupsQuery) error
GetAlertRules(ctx context.Context, query *ngmodels.GetAlertRulesQuery) error
GetUserVisibleNamespaces(context.Context, int64, *models.SignedInUser) (map[string]*models.Folder, error)
GetNamespaceByTitle(context.Context, string, int64, *models.SignedInUser, bool) (*models.Folder, error)
@ -261,6 +263,17 @@ func (st DBstore) GetOrgAlertRules(ctx context.Context, query *ngmodels.ListAler
})
}
func (st DBstore) GetRuleGroups(ctx context.Context, query *ngmodels.ListRuleGroupsQuery) error {
return st.SQLStore.WithDbSession(ctx, func(sess *sqlstore.DBSession) error {
ruleGroups := make([]string, 0)
if err := sess.Table("alert_rule").Distinct("rule_group").Find(&ruleGroups); err != nil {
return err
}
query.Result = ruleGroups
return nil
})
}
// GetAlertRules is a handler for retrieving rule group alert rules of specific organisation.
func (st DBstore) GetAlertRules(ctx context.Context, query *ngmodels.GetAlertRulesQuery) error {
return st.SQLStore.WithDbSession(ctx, func(sess *sqlstore.DBSession) error {

View File

@ -178,6 +178,26 @@ func (f *FakeRuleStore) GetOrgAlertRules(_ context.Context, q *models.ListAlertR
q.Result = rules
return nil
}
func (f *FakeRuleStore) GetRuleGroups(_ context.Context, q *models.ListRuleGroupsQuery) error {
f.mtx.Lock()
defer f.mtx.Unlock()
f.RecordedOps = append(f.RecordedOps, *q)
m := make(map[string]struct{})
for _, rules := range f.Rules {
for _, rule := range rules {
m[rule.RuleGroup] = struct{}{}
}
}
for s := range m {
q.Result = append(q.Result, s)
}
return nil
}
func (f *FakeRuleStore) GetAlertRules(_ context.Context, q *models.GetAlertRulesQuery) error {
f.mtx.Lock()
defer f.mtx.Unlock()