grafana/pkg/services/alerting/reader.go

52 lines
1.1 KiB
Go
Raw Normal View History

2016-06-06 07:24:14 -05:00
package alerting
import (
"context"
2016-06-06 07:24:14 -05:00
"sync"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/metrics"
"github.com/grafana/grafana/pkg/services/alerting/models"
2016-06-06 07:24:14 -05:00
)
type ruleReader interface {
fetch(context.Context) []*Rule
2016-06-06 07:24:14 -05:00
}
type defaultRuleReader struct {
2016-06-06 07:24:14 -05:00
sync.RWMutex
sqlStore AlertStore
log log.Logger
2016-06-06 07:24:14 -05:00
}
func newRuleReader(sqlStore AlertStore) *defaultRuleReader {
ruleReader := &defaultRuleReader{
sqlStore: sqlStore,
log: log.New("alerting.ruleReader"),
}
2016-06-06 07:24:14 -05:00
return ruleReader
}
func (arr *defaultRuleReader) fetch(ctx context.Context) []*Rule {
cmd := &models.GetAllAlertsQuery{}
2016-06-06 07:24:14 -05:00
alerts, err := arr.sqlStore.GetAllAlertQueryHandler(ctx, cmd)
if err != nil {
arr.log.Error("Could not load alerts", "error", err)
2016-07-27 09:29:28 -05:00
return []*Rule{}
2016-06-06 07:24:14 -05:00
}
2016-07-27 09:29:28 -05:00
res := make([]*Rule, 0)
for _, ruleDef := range alerts {
if model, err := NewRuleFromDBAlert(ctx, arr.sqlStore, ruleDef, false); err != nil {
arr.log.Error("Could not build alert model for rule", "ruleId", ruleDef.ID, "error", err)
} else {
res = append(res, model)
}
2016-06-06 07:24:14 -05:00
}
metrics.MAlertingActiveAlerts.Set(float64(len(res)))
2016-06-06 07:24:14 -05:00
return res
}