mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Alerting: Update DbStore to use disabled orgs from the config (#52156)
* update DbStore to use UnifiedAlerting settings * remove disabled orgs from scheduler and use config in db store instead * remove test
This commit is contained in:
@@ -403,9 +403,9 @@ func (st DBstore) GetAlertRulesForScheduling(ctx context.Context, query *ngmodel
|
||||
return st.SQLStore.WithDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
||||
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 {
|
||||
if len(st.Cfg.DisabledOrgs) > 0 {
|
||||
excludeOrgs := make([]interface{}, 0, len(st.Cfg.DisabledOrgs))
|
||||
for orgID := range st.Cfg.DisabledOrgs {
|
||||
excludeOrgs = append(excludeOrgs, orgID)
|
||||
}
|
||||
q = q.NotIn("org_id", excludeOrgs...)
|
||||
@@ -449,7 +449,7 @@ func (st DBstore) validateAlertRule(alertRule ngmodels.AlertRule) error {
|
||||
return fmt.Errorf("%w: title is empty", ngmodels.ErrAlertRuleFailedValidation)
|
||||
}
|
||||
|
||||
if err := ngmodels.ValidateRuleGroupInterval(alertRule.IntervalSeconds, int64(st.BaseInterval.Seconds())); err != nil {
|
||||
if err := ngmodels.ValidateRuleGroupInterval(alertRule.IntervalSeconds, int64(st.Cfg.BaseInterval.Seconds())); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@@ -12,18 +12,21 @@ import (
|
||||
|
||||
"github.com/grafana/grafana/pkg/services/ngalert/models"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
)
|
||||
|
||||
func TestUpdateAlertRules(t *testing.T) {
|
||||
sqlStore := sqlstore.InitTestDB(t)
|
||||
store := DBstore{
|
||||
SQLStore: sqlStore,
|
||||
BaseInterval: time.Duration(rand.Int63n(100)) * time.Second,
|
||||
SQLStore: sqlStore,
|
||||
Cfg: setting.UnifiedAlertingSettings{
|
||||
BaseInterval: time.Duration(rand.Int63n(100)) * time.Second,
|
||||
},
|
||||
}
|
||||
createRule := func(t *testing.T) *models.AlertRule {
|
||||
t.Helper()
|
||||
rule := models.AlertRuleGen(withIntervalMatching(store.BaseInterval))()
|
||||
rule := models.AlertRuleGen(withIntervalMatching(store.Cfg.BaseInterval))()
|
||||
err := sqlStore.WithDbSession(context.Background(), func(sess *sqlstore.DBSession) error {
|
||||
_, err := sess.Table(models.AlertRule{}).InsertOne(rule)
|
||||
if err != nil {
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/services/dashboards"
|
||||
"github.com/grafana/grafana/pkg/services/ngalert/models"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
)
|
||||
|
||||
// TimeNow makes it possible to test usage of time
|
||||
@@ -28,10 +29,7 @@ type AlertingStore interface {
|
||||
|
||||
// DBstore stores the alert definitions and instances in the database.
|
||||
type DBstore struct {
|
||||
// the base scheduler tick rate; it's used for validating definition interval
|
||||
BaseInterval time.Duration
|
||||
// default alert definiiton interval
|
||||
DefaultInterval time.Duration
|
||||
Cfg setting.UnifiedAlertingSettings
|
||||
SQLStore *sqlstore.SQLStore
|
||||
Logger log.Logger
|
||||
FolderService dashboards.FolderService
|
||||
|
||||
Reference in New Issue
Block a user