mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Alerting: Fix scheduler to group folders by the unique key (orgID and UID) (#81303)
This commit is contained in:
@@ -514,6 +514,7 @@ func (st DBstore) GetAlertRulesKeysForScheduling(ctx context.Context) ([]ngmodel
|
||||
// GetAlertRulesForScheduling returns a short version of all alert rules except those that belong to an excluded list of organizations
|
||||
func (st DBstore) GetAlertRulesForScheduling(ctx context.Context, query *ngmodels.GetAlertRulesForSchedulingQuery) error {
|
||||
var folders []struct {
|
||||
OrgId int64
|
||||
Uid string
|
||||
Title string
|
||||
ParentUid string
|
||||
@@ -565,7 +566,7 @@ func (st DBstore) GetAlertRulesForScheduling(ctx context.Context, query *ngmodel
|
||||
query.ResultRules = rules
|
||||
|
||||
if query.PopulateFolders {
|
||||
foldersSql := sess.Table("folder").Alias("d").Select("d.uid, d.title, d.parent_uid").
|
||||
foldersSql := sess.Table("folder").Alias("d").Select("d.org_id, d.uid, d.title, d.parent_uid").
|
||||
Where(`EXISTS (SELECT 1 FROM alert_rule a WHERE d.uid = a.namespace_uid AND d.org_id = a.org_id)`)
|
||||
if len(disabledOrgs) > 0 {
|
||||
foldersSql.NotIn("org_id", disabledOrgs)
|
||||
@@ -574,9 +575,13 @@ func (st DBstore) GetAlertRulesForScheduling(ctx context.Context, query *ngmodel
|
||||
if err := foldersSql.Find(&folders); err != nil {
|
||||
return fmt.Errorf("failed to fetch a list of folders that contain alert rules: %w", err)
|
||||
}
|
||||
query.ResultFoldersTitles = make(map[string]string, len(folders))
|
||||
for _, folder := range folders {
|
||||
query.ResultFoldersTitles[folder.Uid] = ngmodels.GetNamespaceKey(folder.ParentUid, folder.Title)
|
||||
query.ResultFoldersTitles = make(map[ngmodels.FolderKey]string, len(folders))
|
||||
for _, f := range folders {
|
||||
key := ngmodels.FolderKey{
|
||||
OrgID: f.OrgId,
|
||||
UID: f.Uid,
|
||||
}
|
||||
query.ResultFoldersTitles[key] = ngmodels.GetNamespaceKey(f.ParentUid, f.Title)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -352,7 +352,7 @@ func TestIntegration_GetAlertRulesForScheduling(t *testing.T) {
|
||||
rules []string
|
||||
ruleGroups []string
|
||||
disabledOrgs []int64
|
||||
folders map[string]string
|
||||
folders map[models.FolderKey]string
|
||||
}{
|
||||
{
|
||||
name: "without a rule group filter, it returns all created rules",
|
||||
@@ -371,13 +371,13 @@ func TestIntegration_GetAlertRulesForScheduling(t *testing.T) {
|
||||
{
|
||||
name: "with populate folders enabled, it returns them",
|
||||
rules: []string{rule1.Title, rule2.Title},
|
||||
folders: map[string]string{rule1.NamespaceUID: models.GetNamespaceKey(parentFolderUid, rule1.Title), rule2.NamespaceUID: rule2.Title},
|
||||
folders: map[models.FolderKey]string{rule1.GetFolderKey(): models.GetNamespaceKey(parentFolderUid, rule1.Title), rule2.GetFolderKey(): rule2.Title},
|
||||
},
|
||||
{
|
||||
name: "with populate folders enabled and a filter on orgs, it only returns selected information",
|
||||
rules: []string{rule1.Title},
|
||||
disabledOrgs: []int64{rule2.OrgID},
|
||||
folders: map[string]string{rule1.NamespaceUID: models.GetNamespaceKey(parentFolderUid, rule1.Title)},
|
||||
folders: map[models.FolderKey]string{rule1.GetFolderKey(): models.GetNamespaceKey(parentFolderUid, rule1.Title)},
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user