mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
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:
parent
9595b56f0d
commit
d66fc6ed1a
@ -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
|
||||
|
@ -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 {
|
||||
|
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user