func SetAlertState(cmd *m.SetAlertStateCommand) error { return inTransaction(func(sess *xorm.Session) error { alert := m.Alert{} if has, err := sess.Id(cmd.AlertId).Get(&alert); err != nil { return err } else if !has { return fmt.Errorf("Could not find alert") } alert.State = cmd.State alert.StateChanges += 1 alert.NewStateDate = time.Now() alert.EvalData = cmd.EvalData if cmd.Error == "" { alert.ExecutionError = " " //without this space, xorm skips updating this field } else { alert.ExecutionError = cmd.Error } sess.Id(alert.Id).Update(&alert) return nil }) }
func upsertAlerts(existingAlerts []*m.Alert, cmd *m.SaveAlertsCommand, sess *xorm.Session) error { for _, alert := range cmd.Alerts { update := false var alertToUpdate *m.Alert for _, k := range existingAlerts { if alert.PanelId == k.PanelId { update = true alert.Id = k.Id alertToUpdate = k break } } if update { if alertToUpdate.ContainsUpdates(alert) { alert.Updated = time.Now() alert.State = alertToUpdate.State _, err := sess.Id(alert.Id).Update(alert) if err != nil { return err } sqlog.Debug("Alert updated", "name", alert.Name, "id", alert.Id) } } else { alert.Updated = time.Now() alert.Created = time.Now() alert.State = m.AlertStateNoData alert.NewStateDate = time.Now() _, err := sess.Insert(alert) if err != nil { return err } sqlog.Debug("Alert inserted", "name", alert.Name, "id", alert.Id) } } return nil }
func PauseAlertRule(cmd *m.PauseAlertCommand) error { return inTransaction(func(sess *xorm.Session) error { alert := m.Alert{} if has, err := sess.Id(cmd.AlertId).Get(&alert); err != nil { return err } else if !has { return fmt.Errorf("Could not find alert") } var newState m.AlertStateType if cmd.Paused { newState = m.AlertStatePaused } else { newState = m.AlertStateNoData } alert.State = newState sess.Id(alert.Id).Update(&alert) return nil }) }
func PauseAlertRule(cmd *m.PauseAlertCommand) error { return inTransaction(func(sess *xorm.Session) error { alert := m.Alert{} has, err := x.Where("id = ? AND org_id=?", cmd.AlertId, cmd.OrgId).Get(&alert) if err != nil { return err } else if !has { return fmt.Errorf("Could not find alert") } var newState m.AlertStateType if cmd.Paused { newState = m.AlertStatePaused } else { newState = m.AlertStatePending } alert.State = newState sess.Id(alert.Id).Update(&alert) return nil }) }