2016-04-13 16:33:45 +08:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2016-04-25 22:18:28 +08:00
|
|
|
"time"
|
2016-06-10 04:21:28 +08:00
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2016-04-13 16:33:45 +08:00
|
|
|
)
|
|
|
|
|
2016-07-22 19:14:09 +08:00
|
|
|
type AlertStateType string
|
|
|
|
type AlertSeverityType string
|
|
|
|
|
|
|
|
const (
|
2016-09-13 21:09:55 +08:00
|
|
|
AlertStateNoData AlertStateType = "no_data"
|
|
|
|
AlertStateExecError AlertStateType = "execution_error"
|
|
|
|
AlertStatePaused AlertStateType = "paused"
|
|
|
|
AlertStateAlerting AlertStateType = "alerting"
|
|
|
|
AlertStateOK AlertStateType = "ok"
|
2016-07-22 19:14:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func (s AlertStateType) IsValid() bool {
|
2016-09-13 21:09:55 +08:00
|
|
|
return s == AlertStateOK || s == AlertStateNoData || s == AlertStateExecError || s == AlertStatePaused
|
2016-07-22 19:14:09 +08:00
|
|
|
}
|
|
|
|
|
2016-06-11 16:26:48 +08:00
|
|
|
type Alert struct {
|
2016-08-15 21:12:43 +08:00
|
|
|
Id int64
|
2016-08-16 15:52:45 +08:00
|
|
|
Version int64
|
2016-08-15 21:12:43 +08:00
|
|
|
OrgId int64
|
|
|
|
DashboardId int64
|
|
|
|
PanelId int64
|
|
|
|
Name string
|
|
|
|
Message string
|
2016-09-13 21:09:55 +08:00
|
|
|
Severity string
|
2016-08-15 21:12:43 +08:00
|
|
|
State AlertStateType
|
|
|
|
Handler int64
|
|
|
|
Silenced bool
|
|
|
|
ExecutionError string
|
|
|
|
Frequency int64
|
|
|
|
|
2016-08-16 15:52:45 +08:00
|
|
|
EvalData *simplejson.Json
|
|
|
|
EvalDate time.Time
|
|
|
|
NewStateDate time.Time
|
|
|
|
StateChanges int
|
2016-07-14 19:32:16 +08:00
|
|
|
|
2016-06-10 04:21:28 +08:00
|
|
|
Created time.Time
|
|
|
|
Updated time.Time
|
|
|
|
|
2016-06-13 21:18:19 +08:00
|
|
|
Settings *simplejson.Json
|
2016-04-13 16:33:45 +08:00
|
|
|
}
|
|
|
|
|
2016-06-11 16:26:48 +08:00
|
|
|
func (alert *Alert) ValidToSave() bool {
|
2016-06-13 21:01:07 +08:00
|
|
|
return alert.DashboardId != 0 && alert.OrgId != 0 && alert.PanelId != 0
|
2016-06-10 16:00:00 +08:00
|
|
|
}
|
|
|
|
|
2016-07-22 19:14:09 +08:00
|
|
|
func (alert *Alert) ShouldUpdateState(newState AlertStateType) bool {
|
2016-06-15 15:19:22 +08:00
|
|
|
return alert.State != newState
|
|
|
|
}
|
|
|
|
|
2016-06-11 16:26:48 +08:00
|
|
|
func (this *Alert) ContainsUpdates(other *Alert) bool {
|
2016-05-30 23:50:35 +08:00
|
|
|
result := false
|
2016-06-06 23:11:46 +08:00
|
|
|
result = result || this.Name != other.Name
|
2016-08-12 16:12:04 +08:00
|
|
|
result = result || this.Message != other.Message
|
2016-06-10 04:21:28 +08:00
|
|
|
|
2016-06-13 21:18:19 +08:00
|
|
|
if this.Settings != nil && other.Settings != nil {
|
|
|
|
json1, err1 := this.Settings.Encode()
|
|
|
|
json2, err2 := other.Settings.Encode()
|
2016-06-10 04:21:28 +08:00
|
|
|
|
2016-06-10 17:37:03 +08:00
|
|
|
if err1 != nil || err2 != nil {
|
|
|
|
return false
|
|
|
|
}
|
2016-06-10 04:21:28 +08:00
|
|
|
|
2016-06-10 17:37:03 +08:00
|
|
|
result = result || string(json1) != string(json2)
|
|
|
|
}
|
2016-06-10 04:21:28 +08:00
|
|
|
|
2016-05-30 23:50:35 +08:00
|
|
|
//don't compare .State! That would be insane.
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2016-05-23 20:51:41 +08:00
|
|
|
type AlertingClusterInfo struct {
|
|
|
|
ServerId string
|
|
|
|
ClusterSize int
|
|
|
|
UptimePosition int
|
|
|
|
}
|
|
|
|
|
2016-06-09 16:00:34 +08:00
|
|
|
type HeartBeat struct {
|
|
|
|
Id int64
|
|
|
|
ServerId string
|
|
|
|
Updated time.Time
|
|
|
|
Created time.Time
|
|
|
|
}
|
|
|
|
|
2016-05-23 20:51:41 +08:00
|
|
|
type HeartBeatCommand struct {
|
2016-05-23 16:02:17 +08:00
|
|
|
ServerId string
|
2016-06-11 16:26:48 +08:00
|
|
|
Result AlertingClusterInfo
|
2016-05-23 16:02:17 +08:00
|
|
|
}
|
|
|
|
|
2016-04-13 16:33:45 +08:00
|
|
|
type SaveAlertsCommand struct {
|
|
|
|
DashboardId int64
|
|
|
|
UserId int64
|
|
|
|
OrgId int64
|
|
|
|
|
2016-06-11 16:26:48 +08:00
|
|
|
Alerts []*Alert
|
2016-04-13 16:33:45 +08:00
|
|
|
}
|
2016-04-26 23:36:50 +08:00
|
|
|
|
2016-07-22 19:14:09 +08:00
|
|
|
type SetAlertStateCommand struct {
|
|
|
|
AlertId int64
|
|
|
|
OrgId int64
|
|
|
|
State AlertStateType
|
2016-08-17 15:27:29 +08:00
|
|
|
Error string
|
2016-07-22 19:14:09 +08:00
|
|
|
Timestamp time.Time
|
|
|
|
}
|
|
|
|
|
2016-05-02 22:07:19 +08:00
|
|
|
type DeleteAlertCommand struct {
|
|
|
|
AlertId int64
|
|
|
|
}
|
|
|
|
|
2016-04-26 23:36:50 +08:00
|
|
|
//Queries
|
|
|
|
type GetAlertsQuery struct {
|
2016-05-10 15:45:56 +08:00
|
|
|
OrgId int64
|
|
|
|
State []string
|
|
|
|
DashboardId int64
|
|
|
|
PanelId int64
|
2016-09-14 14:36:44 +08:00
|
|
|
Limit int64
|
2016-04-26 23:36:50 +08:00
|
|
|
|
2016-06-11 16:26:48 +08:00
|
|
|
Result []*Alert
|
2016-04-26 23:36:50 +08:00
|
|
|
}
|
|
|
|
|
2016-06-03 21:24:53 +08:00
|
|
|
type GetAllAlertsQuery struct {
|
2016-06-11 16:26:48 +08:00
|
|
|
Result []*Alert
|
2016-05-16 21:39:09 +08:00
|
|
|
}
|
|
|
|
|
2016-04-28 14:23:50 +08:00
|
|
|
type GetAlertByIdQuery struct {
|
2016-04-26 23:36:50 +08:00
|
|
|
Id int64
|
|
|
|
|
2016-06-11 16:26:48 +08:00
|
|
|
Result *Alert
|
2016-04-26 23:36:50 +08:00
|
|
|
}
|