func (handler *DefaultResultHandler) Handle(evalContext *EvalContext) error { executionError := "" annotationData := simplejson.New() evalContext.Rule.State = handler.GetStateFromEvaluation(evalContext) if evalContext.Error != nil { executionError = evalContext.Error.Error() annotationData.Set("errorMessage", executionError) } if evalContext.Firing { annotationData = simplejson.NewFromAny(evalContext.EvalMatches) } countStateResult(evalContext.Rule.State) if evalContext.ShouldUpdateAlertState() { handler.log.Info("New state change", "alertId", evalContext.Rule.Id, "newState", evalContext.Rule.State, "prev state", evalContext.PrevAlertState) cmd := &m.SetAlertStateCommand{ AlertId: evalContext.Rule.Id, OrgId: evalContext.Rule.OrgId, State: evalContext.Rule.State, Error: executionError, EvalData: annotationData, } if err := bus.Dispatch(cmd); err != nil { handler.log.Error("Failed to save state", "error", err) } // save annotation item := annotations.Item{ OrgId: evalContext.Rule.OrgId, DashboardId: evalContext.Rule.DashboardId, PanelId: evalContext.Rule.PanelId, Type: annotations.AlertType, AlertId: evalContext.Rule.Id, Title: evalContext.Rule.Name, Text: evalContext.GetStateModel().Text, NewState: string(evalContext.Rule.State), PrevState: string(evalContext.PrevAlertState), Epoch: time.Now().Unix(), Data: annotationData, } annotationRepo := annotations.GetRepository() if err := annotationRepo.Save(&item); err != nil { handler.log.Error("Failed to save annotation for new alert state", "error", err) } if evalContext.ShouldSendNotification() { handler.notifier.Notify(evalContext) } } return nil }
func DeleteAnnotations(c *middleware.Context, cmd dtos.DeleteAnnotationsCmd) Response { repo := annotations.GetRepository() err := repo.Delete(&annotations.DeleteParams{ AlertId: cmd.PanelId, DashboardId: cmd.DashboardId, PanelId: cmd.PanelId, }) if err != nil { return ApiError(500, "Failed to delete annotations", err) } return ApiSuccess("Annotations deleted") }
func GetAnnotations(c *middleware.Context) Response { query := &annotations.ItemQuery{ From: c.QueryInt64("from") / 1000, To: c.QueryInt64("to") / 1000, Type: annotations.ItemType(c.Query("type")), OrgId: c.OrgId, AlertId: c.QueryInt64("alertId"), DashboardId: c.QueryInt64("dashboardId"), PanelId: c.QueryInt64("panelId"), Limit: c.QueryInt64("limit"), NewState: c.QueryStrings("newState"), } repo := annotations.GetRepository() items, err := repo.Find(query) if err != nil { return ApiError(500, "Failed to get annotations", err) } result := make([]dtos.Annotation, 0) for _, item := range items { result = append(result, dtos.Annotation{ AlertId: item.AlertId, Time: item.Epoch * 1000, Data: item.Data, NewState: item.NewState, PrevState: item.PrevState, Text: item.Text, Metric: item.Metric, Title: item.Title, }) } return Json(200, result) }
func (handler *DefaultResultHandler) Handle(evalContext *EvalContext) error { oldState := evalContext.Rule.State executionError := "" annotationData := simplejson.New() if evalContext.Error != nil { handler.log.Error("Alert Rule Result Error", "ruleId", evalContext.Rule.Id, "error", evalContext.Error) evalContext.Rule.State = m.AlertStateExecError executionError = evalContext.Error.Error() annotationData.Set("errorMessage", executionError) } else if evalContext.Firing { evalContext.Rule.State = m.AlertStateAlerting annotationData = simplejson.NewFromAny(evalContext.EvalMatches) } else { if evalContext.NoDataFound { if evalContext.Rule.NoDataState != m.NoDataKeepState { evalContext.Rule.State = evalContext.Rule.NoDataState.ToAlertState() } } else { evalContext.Rule.State = m.AlertStateOK } } countStateResult(evalContext.Rule.State) if handler.shouldUpdateAlertState(evalContext, oldState) { handler.log.Info("New state change", "alertId", evalContext.Rule.Id, "newState", evalContext.Rule.State, "oldState", oldState) cmd := &m.SetAlertStateCommand{ AlertId: evalContext.Rule.Id, OrgId: evalContext.Rule.OrgId, State: evalContext.Rule.State, Error: executionError, EvalData: annotationData, } if err := bus.Dispatch(cmd); err != nil { handler.log.Error("Failed to save state", "error", err) } // save annotation item := annotations.Item{ OrgId: evalContext.Rule.OrgId, DashboardId: evalContext.Rule.DashboardId, PanelId: evalContext.Rule.PanelId, Type: annotations.AlertType, AlertId: evalContext.Rule.Id, Title: evalContext.Rule.Name, Text: evalContext.GetStateModel().Text, NewState: string(evalContext.Rule.State), PrevState: string(oldState), Epoch: time.Now().Unix(), Data: annotationData, } annotationRepo := annotations.GetRepository() if err := annotationRepo.Save(&item); err != nil { handler.log.Error("Failed to save annotation for new alert state", "error", err) } if (oldState == m.AlertStatePending) && (evalContext.Rule.State == m.AlertStateOK) { handler.log.Info("Notfication not sent", "oldState", oldState, "newState", evalContext.Rule.State) } else { handler.notifier.Notify(evalContext) } } return nil }