grafana/pkg/services/alerting/reader.go

49 lines
1006 B
Go
Raw Normal View History

2016-06-06 07:24:14 -05:00
package alerting
import (
"sync"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/metrics"
"github.com/grafana/grafana/pkg/models"
2016-06-06 07:24:14 -05:00
)
type ruleReader interface {
fetch() []*Rule
2016-06-06 07:24:14 -05:00
}
type defaultRuleReader struct {
2016-06-06 07:24:14 -05:00
sync.RWMutex
log log.Logger
2016-06-06 07:24:14 -05:00
}
func newRuleReader() *defaultRuleReader {
ruleReader := &defaultRuleReader{
log: log.New("alerting.ruleReader"),
}
2016-06-06 07:24:14 -05:00
return ruleReader
}
func (arr *defaultRuleReader) fetch() []*Rule {
cmd := &models.GetAllAlertsQuery{}
2016-06-06 07:24:14 -05:00
if err := bus.Dispatch(cmd); 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 cmd.Result {
2016-07-27 09:29:28 -05:00
if model, err := NewRuleFromDBAlert(ruleDef); 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
}
2017-09-06 04:23:52 -05:00
metrics.M_Alerting_Active_Alerts.Set(float64(len(res)))
2016-06-06 07:24:14 -05:00
return res
}