mirror of
https://github.com/grafana/grafana.git
synced 2024-11-23 09:26:43 -06:00
43b15b92ad
* propagate notificationservice down to the notifiers * replace dispatch in result handler * remove dispatch from the rule reader * remove dispatch from eval context * remove dispatch from alerting usage * remove dispatch from alerting usage * remove dispatch from notifier * attempt to fix tests in alerting * hello linter, my old friend; also disable some tests for now * use mocks to fix the tests * resolving wire providers * make linter happy * remove yet another bus.dispatch * fix tests using store mock
51 lines
1.1 KiB
Go
51 lines
1.1 KiB
Go
package alerting
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
|
"github.com/grafana/grafana/pkg/infra/metrics"
|
|
"github.com/grafana/grafana/pkg/models"
|
|
)
|
|
|
|
type ruleReader interface {
|
|
fetch(context.Context) []*Rule
|
|
}
|
|
|
|
type defaultRuleReader struct {
|
|
sync.RWMutex
|
|
sqlStore AlertStore
|
|
log log.Logger
|
|
}
|
|
|
|
func newRuleReader(sqlStore AlertStore) *defaultRuleReader {
|
|
ruleReader := &defaultRuleReader{
|
|
sqlStore: sqlStore,
|
|
log: log.New("alerting.ruleReader"),
|
|
}
|
|
|
|
return ruleReader
|
|
}
|
|
|
|
func (arr *defaultRuleReader) fetch(ctx context.Context) []*Rule {
|
|
cmd := &models.GetAllAlertsQuery{}
|
|
|
|
if err := arr.sqlStore.GetAllAlertQueryHandler(ctx, cmd); err != nil {
|
|
arr.log.Error("Could not load alerts", "error", err)
|
|
return []*Rule{}
|
|
}
|
|
|
|
res := make([]*Rule, 0)
|
|
for _, ruleDef := range cmd.Result {
|
|
if model, err := NewRuleFromDBAlert(ctx, ruleDef, false); err != nil {
|
|
arr.log.Error("Could not build alert model for rule", "ruleId", ruleDef.Id, "error", err)
|
|
} else {
|
|
res = append(res, model)
|
|
}
|
|
}
|
|
|
|
metrics.MAlertingActiveAlerts.Set(float64(len(res)))
|
|
return res
|
|
}
|