Remove redundant queries in GetAlertRules and GetOrgAlertRules and replace with ListAlertRules (#48108)
This commit is contained in:
@@ -55,9 +55,9 @@ func (srv RulerSrv) RouteDeleteAlertRules(c *models.ReqContext) response.Respons
|
||||
"namespace",
|
||||
namespace.Title,
|
||||
}
|
||||
var ruleGroup *string
|
||||
var ruleGroup string
|
||||
if group, ok := web.Params(c.Req)[":Groupname"]; ok {
|
||||
ruleGroup = &group
|
||||
ruleGroup = group
|
||||
loggerCtx = append(loggerCtx, "group", group)
|
||||
}
|
||||
logger := srv.log.New(loggerCtx...)
|
||||
@@ -68,12 +68,12 @@ func (srv RulerSrv) RouteDeleteAlertRules(c *models.ReqContext) response.Respons
|
||||
|
||||
var canDelete, cannotDelete []string
|
||||
err = srv.xactManager.InTransaction(c.Req.Context(), func(ctx context.Context) error {
|
||||
q := ngmodels.GetAlertRulesQuery{
|
||||
OrgID: c.SignedInUser.OrgId,
|
||||
NamespaceUID: namespace.Uid,
|
||||
RuleGroup: ruleGroup,
|
||||
q := ngmodels.ListAlertRulesQuery{
|
||||
OrgID: c.SignedInUser.OrgId,
|
||||
NamespaceUIDs: []string{namespace.Uid},
|
||||
RuleGroup: ruleGroup,
|
||||
}
|
||||
if err = srv.store.GetAlertRules(ctx, &q); err != nil {
|
||||
if err = srv.store.ListAlertRules(ctx, &q); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -128,11 +128,11 @@ func (srv RulerSrv) RouteGetNamespaceRulesConfig(c *models.ReqContext) response.
|
||||
return toNamespaceErrorResponse(err)
|
||||
}
|
||||
|
||||
q := ngmodels.GetAlertRulesQuery{
|
||||
OrgID: c.SignedInUser.OrgId,
|
||||
NamespaceUID: namespace.Uid,
|
||||
q := ngmodels.ListAlertRulesQuery{
|
||||
OrgID: c.SignedInUser.OrgId,
|
||||
NamespaceUIDs: []string{namespace.Uid},
|
||||
}
|
||||
if err := srv.store.GetAlertRules(c.Req.Context(), &q); err != nil {
|
||||
if err := srv.store.ListAlertRules(c.Req.Context(), &q); err != nil {
|
||||
return ErrResp(http.StatusInternalServerError, err, "failed to update rule group")
|
||||
}
|
||||
|
||||
@@ -178,12 +178,12 @@ func (srv RulerSrv) RouteGetRulegGroupConfig(c *models.ReqContext) response.Resp
|
||||
}
|
||||
|
||||
ruleGroup := web.Params(c.Req)[":Groupname"]
|
||||
q := ngmodels.GetAlertRulesQuery{
|
||||
OrgID: c.SignedInUser.OrgId,
|
||||
NamespaceUID: namespace.Uid,
|
||||
RuleGroup: &ruleGroup,
|
||||
q := ngmodels.ListAlertRulesQuery{
|
||||
OrgID: c.SignedInUser.OrgId,
|
||||
NamespaceUIDs: []string{namespace.Uid},
|
||||
RuleGroup: ruleGroup,
|
||||
}
|
||||
if err := srv.store.GetAlertRules(c.Req.Context(), &q); err != nil {
|
||||
if err := srv.store.ListAlertRules(c.Req.Context(), &q); err != nil {
|
||||
return ErrResp(http.StatusInternalServerError, err, "failed to get group alert rules")
|
||||
}
|
||||
|
||||
@@ -245,7 +245,7 @@ func (srv RulerSrv) RouteGetRulesConfig(c *models.ReqContext) response.Response
|
||||
PanelID: panelID,
|
||||
}
|
||||
|
||||
if err := srv.store.GetOrgAlertRules(c.Req.Context(), &q); err != nil {
|
||||
if err := srv.store.ListAlertRules(c.Req.Context(), &q); err != nil {
|
||||
return ErrResp(http.StatusInternalServerError, err, "failed to get alert rules")
|
||||
}
|
||||
|
||||
@@ -493,12 +493,12 @@ func (c *changes) isEmpty() bool {
|
||||
// calculateChanges calculates the difference between rules in the group in the database and the submitted rules. If a submitted rule has UID it tries to find it in the database (in other groups).
|
||||
// returns a list of rules that need to be added, updated and deleted. Deleted considered rules in the database that belong to the group but do not exist in the list of submitted rules.
|
||||
func calculateChanges(ctx context.Context, ruleStore store.RuleStore, orgId int64, namespace *models.Folder, ruleGroupName string, submittedRules []*ngmodels.AlertRule) (*changes, error) {
|
||||
q := &ngmodels.GetAlertRulesQuery{
|
||||
OrgID: orgId,
|
||||
NamespaceUID: namespace.Uid,
|
||||
RuleGroup: &ruleGroupName,
|
||||
q := &ngmodels.ListAlertRulesQuery{
|
||||
OrgID: orgId,
|
||||
NamespaceUIDs: []string{namespace.Uid},
|
||||
RuleGroup: ruleGroupName,
|
||||
}
|
||||
if err := ruleStore.GetAlertRules(ctx, q); err != nil {
|
||||
if err := ruleStore.ListAlertRules(ctx, q); err != nil {
|
||||
return nil, fmt.Errorf("failed to query database for rules in the group %s: %w", ruleGroupName, err)
|
||||
}
|
||||
existingGroupRules := q.Result
|
||||
|
||||
Reference in New Issue
Block a user