feat(alerting): more work on handling result and saving state

This commit is contained in:
Torkel Ödegaard
2016-07-21 17:31:46 +02:00
parent f0fc336e88
commit b073fe0eba
6 changed files with 88 additions and 58 deletions
+54 -39
View File
@@ -1,6 +1,14 @@
package alerting
import "github.com/grafana/grafana/pkg/log"
import (
"time"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/log"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/alerting/alertstates"
)
type ResultHandler interface {
Handle(result *AlertResultContext)
@@ -19,44 +27,51 @@ func NewResultHandler() *ResultHandlerImpl {
}
func (handler *ResultHandlerImpl) Handle(result *AlertResultContext) {
// if handler.shouldUpdateState(result) {
// cmd := &m.UpdateAlertStateCommand{
// AlertId: result.Rule.Id,
// State: result.Rule.Severity,
// Info: result.Description,
// OrgId: result.Rule.OrgId,
// TriggeredAlerts: simplejson.NewFromAny(result.Details),
// }
//
// 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)
// }
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{
AlertId: result.Rule.Id,
Info: result.Description,
OrgId: result.Rule.OrgId,
State: newState,
TriggeredAlerts: simplejson.NewFromAny(result.Details),
}
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)
}
}
func (handler *ResultHandlerImpl) shouldUpdateState(result *AlertResultContext) bool {
// query := &m.GetLastAlertStateQuery{
// AlertId: result.AlertJob.Rule.Id,
// OrgId: result.AlertJob.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 != result.State
//
// return changedState || olderThen15Min
return false
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
}