2016-06-23 05:57:10 -05:00
|
|
|
package alerting
|
|
|
|
|
2016-07-21 10:31:46 -05:00
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
"github.com/grafana/grafana/pkg/log"
|
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
|
|
|
"github.com/grafana/grafana/pkg/services/alerting/alertstates"
|
|
|
|
)
|
2016-06-23 05:57:10 -05:00
|
|
|
|
|
|
|
type ResultHandler interface {
|
2016-07-19 15:36:59 -05:00
|
|
|
Handle(result *AlertResultContext)
|
2016-06-23 05:57:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type ResultHandlerImpl struct {
|
|
|
|
notifier Notifier
|
|
|
|
log log.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewResultHandler() *ResultHandlerImpl {
|
|
|
|
return &ResultHandlerImpl{
|
2016-07-19 15:36:59 -05:00
|
|
|
log: log.New("alerting.responseHandler"),
|
|
|
|
//notifier: NewNotifier(),
|
2016-06-23 05:57:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-19 15:36:59 -05:00
|
|
|
func (handler *ResultHandlerImpl) Handle(result *AlertResultContext) {
|
2016-07-21 10:31:46 -05:00
|
|
|
newState := alertstates.Ok
|
|
|
|
if result.Triggered {
|
|
|
|
newState = result.Rule.Severity
|
|
|
|
}
|
|
|
|
|
|
|
|
handler.log.Info("Handle result", "newState", newState)
|
|
|
|
handler.log.Info("Handle result", "triggered", result.Triggered)
|
|
|
|
|
|
|
|
if handler.shouldUpdateState(result, newState) {
|
|
|
|
cmd := &m.UpdateAlertStateCommand{
|
2016-07-21 14:54:12 -05:00
|
|
|
AlertId: result.Rule.Id,
|
|
|
|
Info: result.Description,
|
|
|
|
OrgId: result.Rule.OrgId,
|
|
|
|
State: newState,
|
2016-07-21 10:31:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := bus.Dispatch(cmd); err != nil {
|
|
|
|
handler.log.Error("Failed to save state", "error", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
//handler.log.Debug("will notify about new state", "new state", result.State)
|
|
|
|
//handler.notifier.Notify(result)
|
|
|
|
}
|
2016-06-23 05:57:10 -05:00
|
|
|
}
|
|
|
|
|
2016-07-21 10:31:46 -05:00
|
|
|
func (handler *ResultHandlerImpl) shouldUpdateState(result *AlertResultContext, newState string) bool {
|
|
|
|
query := &m.GetLastAlertStateQuery{
|
|
|
|
AlertId: result.Rule.Id,
|
|
|
|
OrgId: result.Rule.OrgId,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := bus.Dispatch(query); err != nil {
|
|
|
|
log.Error2("Failed to read last alert state", "error", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if query.Result == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
lastExecution := query.Result.Created
|
|
|
|
asdf := result.StartTime.Add(time.Minute * -15)
|
|
|
|
olderThen15Min := lastExecution.Before(asdf)
|
|
|
|
changedState := query.Result.State != newState
|
|
|
|
|
|
|
|
return changedState || olderThen15Min
|
2016-06-23 05:57:10 -05:00
|
|
|
}
|