Alerting: reduce database calls in prometheus-comptible rules API (#47080)

* move validation at the beginning of method
* remove usage of GetOrgRuleGroups because it is not necessary. All information is already available in memory.
* remove unused method
This commit is contained in:
Yuriy Tseretyan
2022-04-11 10:54:29 -04:00
committed by GitHub
parent 7be8fe027f
commit 48519f9ebb
4 changed files with 45 additions and 134 deletions
+2 -30
View File
@@ -219,8 +219,8 @@ func (f *FakeRuleStore) GetUserVisibleNamespaces(_ context.Context, orgID int64,
return namespacesMap, nil
}
for _, rule := range f.Rules[orgID] {
namespacesMap[rule.NamespaceUID] = &models2.Folder{}
for _, folder := range f.Folders[orgID] {
namespacesMap[folder.Uid] = folder
}
return namespacesMap, nil
}
@@ -233,34 +233,6 @@ func (f *FakeRuleStore) GetNamespaceByTitle(_ context.Context, title string, org
}
return nil, fmt.Errorf("not found")
}
func (f *FakeRuleStore) GetOrgRuleGroups(_ context.Context, q *models.ListOrgRuleGroupsQuery) error {
f.mtx.Lock()
defer f.mtx.Unlock()
f.RecordedOps = append(f.RecordedOps, *q)
if err := f.Hook(*q); err != nil {
return err
}
// If we have namespaces, we want to try and retrieve the list of rules stored.
if len(q.NamespaceUIDs) != 0 {
rules, ok := f.Rules[q.OrgID]
if !ok {
return nil
}
var ruleGroups [][]string
for _, rule := range rules {
for _, namespace := range q.NamespaceUIDs {
if rule.NamespaceUID == namespace { // if they match, they should go in.
ruleGroups = append(ruleGroups, []string{rule.RuleGroup, rule.NamespaceUID, rule.NamespaceUID})
}
}
}
q.Result = ruleGroups
}
return nil
}
func (f *FakeRuleStore) UpsertAlertRules(_ context.Context, q []UpsertRule) error {
f.mtx.Lock()