Alerting: scheduler to use short version of model for alert rule (#48916)
* scheduler to use a short version of alert rule model
This commit is contained in:
@@ -37,7 +37,7 @@ type RuleStore interface {
|
||||
DeleteAlertRulesByUID(ctx context.Context, orgID int64, ruleUID ...string) error
|
||||
DeleteAlertInstancesByRuleUID(ctx context.Context, orgID int64, ruleUID string) error
|
||||
GetAlertRuleByUID(ctx context.Context, query *ngmodels.GetAlertRuleByUIDQuery) error
|
||||
GetAlertRulesForScheduling(ctx context.Context, query *ngmodels.ListAlertRulesQuery) error
|
||||
GetAlertRulesForScheduling(ctx context.Context, query *ngmodels.GetAlertRulesForSchedulingQuery) error
|
||||
ListAlertRules(ctx context.Context, query *ngmodels.ListAlertRulesQuery) error
|
||||
// GetRuleGroups returns the unique rule groups across all organizations.
|
||||
GetRuleGroups(ctx context.Context, query *ngmodels.ListRuleGroupsQuery) error
|
||||
@@ -344,16 +344,19 @@ func (st DBstore) GetNamespaceByTitle(ctx context.Context, namespace string, org
|
||||
return folder, nil
|
||||
}
|
||||
|
||||
// GetAlertRulesForScheduling returns alert rule info (identifier, interval, version state)
|
||||
// that is useful for it's scheduling.
|
||||
func (st DBstore) GetAlertRulesForScheduling(ctx context.Context, query *ngmodels.ListAlertRulesQuery) error {
|
||||
// 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 {
|
||||
return st.SQLStore.WithDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
||||
alerts := make([]*ngmodels.AlertRule, 0)
|
||||
q := "SELECT uid, org_id, interval_seconds, version FROM alert_rule"
|
||||
if len(query.ExcludeOrgs) > 0 {
|
||||
q = fmt.Sprintf("%s WHERE org_id NOT IN (%s)", q, strings.Join(strings.Split(strings.Trim(fmt.Sprint(query.ExcludeOrgs), "[]"), " "), ","))
|
||||
alerts := make([]*ngmodels.SchedulableAlertRule, 0)
|
||||
q := sess.Table("alert_rule")
|
||||
if len(query.ExcludeOrgIDs) > 0 {
|
||||
excludeOrgs := make([]interface{}, 0, len(query.ExcludeOrgIDs))
|
||||
for _, orgID := range query.ExcludeOrgIDs {
|
||||
excludeOrgs = append(excludeOrgs, orgID)
|
||||
}
|
||||
q = q.NotIn("org_id", excludeOrgs...)
|
||||
}
|
||||
if err := sess.SQL(q).Find(&alerts); err != nil {
|
||||
if err := q.Find(&alerts); err != nil {
|
||||
return err
|
||||
}
|
||||
query.Result = alerts
|
||||
|
||||
@@ -153,7 +153,7 @@ func (f *FakeRuleStore) GetAlertRuleByUID(_ context.Context, q *models.GetAlertR
|
||||
}
|
||||
|
||||
// For now, we're not implementing namespace filtering.
|
||||
func (f *FakeRuleStore) GetAlertRulesForScheduling(_ context.Context, q *models.ListAlertRulesQuery) error {
|
||||
func (f *FakeRuleStore) GetAlertRulesForScheduling(_ context.Context, q *models.GetAlertRulesForSchedulingQuery) error {
|
||||
f.mtx.Lock()
|
||||
defer f.mtx.Unlock()
|
||||
f.RecordedOps = append(f.RecordedOps, *q)
|
||||
@@ -161,7 +161,14 @@ func (f *FakeRuleStore) GetAlertRulesForScheduling(_ context.Context, q *models.
|
||||
return err
|
||||
}
|
||||
for _, rules := range f.Rules {
|
||||
q.Result = append(q.Result, rules...)
|
||||
for _, rule := range rules {
|
||||
q.Result = append(q.Result, &models.SchedulableAlertRule{
|
||||
UID: rule.UID,
|
||||
OrgID: rule.OrgID,
|
||||
IntervalSeconds: rule.IntervalSeconds,
|
||||
Version: rule.Version,
|
||||
})
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user