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 (
|
|
|
|
AlertStatePending AlertStateType = "pending"
|
|
|
|
AlertStateFiring AlertStateType = "firing"
|
|
|
|
AlertStateOK AlertStateType = "ok"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s AlertStateType) IsValid() bool {
|
|
|
|
return s == AlertStatePending || s == AlertStateFiring || s == AlertStateOK
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
AlertSeverityCritical AlertSeverityType = "critical"
|
|
|
|
AlertSeverityWarning AlertSeverityType = "warning"
|
|
|
|
AlertSeverityInfo AlertSeverityType = "info"
|
2016-08-01 16:07:00 +08:00
|
|
|
AlertSeverityOK AlertSeverityType = "ok"
|
2016-07-22 19:14:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func (s AlertSeverityType) IsValid() bool {
|
|
|
|
return s == AlertSeverityCritical || s == AlertSeverityInfo || s == AlertSeverityWarning
|
|
|
|
}
|
|
|
|
|
2016-06-11 16:26:48 +08:00
|
|
|
type Alert struct {
|
2016-06-10 04:21:28 +08:00
|
|
|
Id int64
|
|
|
|
OrgId int64
|
|
|
|
DashboardId int64
|
|
|
|
PanelId int64
|
|
|
|
Name string
|
|
|
|
Description string
|
2016-07-22 19:14:09 +08:00
|
|
|
Severity AlertSeverityType
|
|
|
|
State AlertStateType
|
2016-06-13 21:58:22 +08:00
|
|
|
Handler int64
|
2016-06-11 16:13:33 +08:00
|
|
|
Enabled bool
|
2016-06-17 14:27:38 +08:00
|
|
|
Frequency int64
|
2016-06-10 04:21:28 +08:00
|
|
|
|
2016-07-14 19:32:16 +08:00
|
|
|
CreatedBy int64
|
|
|
|
UpdatedBy int64
|
|
|
|
|
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-05-30 23:50:35 +08:00
|
|
|
result = result || this.Description != other.Description
|
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
|
|
|
|
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-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
|
|
|
}
|