mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
feat(alerting): add api endpoint for alert state
This commit is contained in:
32
pkg/models/alerting_state.go
Normal file
32
pkg/models/alerting_state.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package models
|
||||
|
||||
import "time"
|
||||
|
||||
type AlertStateLog struct {
|
||||
Id int64 `json:"id"`
|
||||
OrgId int64 `json:"-"`
|
||||
AlertId int64 `json:"alertId"`
|
||||
State string `json:"type"`
|
||||
Created time.Time `json:"created"`
|
||||
Acknowledged time.Time `json:"acknowledged"`
|
||||
Deleted time.Time `json:"deleted"`
|
||||
}
|
||||
|
||||
var (
|
||||
ALERT_STATE_OK = "OK"
|
||||
ALERT_STATE_ALERT = "ALERT"
|
||||
ALERT_STATE_WARN = "WARN"
|
||||
ALERT_STATE_ACKNOWLEDGED = "ACKNOWLEDGED"
|
||||
)
|
||||
|
||||
func (this *UpdateAlertStateCommand) IsValidState() bool {
|
||||
return this.NewState == ALERT_STATE_OK || this.NewState == ALERT_STATE_WARN || this.NewState == ALERT_STATE_ALERT || this.NewState == ALERT_STATE_ACKNOWLEDGED
|
||||
}
|
||||
|
||||
type UpdateAlertStateCommand struct {
|
||||
AlertId int64 `json:"alertId" binding:"Required"`
|
||||
NewState string `json:"newState" binding:"Required"`
|
||||
Info string `json:"info"`
|
||||
|
||||
Result *AlertRule
|
||||
}
|
||||
@@ -6,20 +6,20 @@ import (
|
||||
)
|
||||
|
||||
type AlertRule struct {
|
||||
Id int64 `json:"id"`
|
||||
OrgId int64 `json:"-"`
|
||||
DashboardId int64 `json:"dashboardId"`
|
||||
PanelId int64 `json:"panelId"`
|
||||
Query string `json:"query"`
|
||||
QueryRefId string `json:"queryRefId"`
|
||||
WarnLevel string `json:"warnLevel"`
|
||||
CritLevel string `json:"critLevel"`
|
||||
Interval string `json:"interval"`
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
QueryRange string `json:"queryRange"`
|
||||
Aggregator string `json:"aggregator"`
|
||||
DatasourceName string `json:"-"`
|
||||
Id int64 `json:"id"`
|
||||
OrgId int64 `json:"-"`
|
||||
DashboardId int64 `json:"dashboardId"`
|
||||
PanelId int64 `json:"panelId"`
|
||||
Query string `json:"query"`
|
||||
QueryRefId string `json:"queryRefId"`
|
||||
WarnLevel string `json:"warnLevel"`
|
||||
CritLevel string `json:"critLevel"`
|
||||
Interval string `json:"interval"`
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
QueryRange string `json:"queryRange"`
|
||||
Aggregator string `json:"aggregator"`
|
||||
State string `json:"state"`
|
||||
}
|
||||
|
||||
type AlertRuleChange struct {
|
||||
@@ -40,19 +40,18 @@ func (cmd *SaveDashboardCommand) GetAlertModels() *[]AlertRule {
|
||||
|
||||
alerting := panel.Get("alerting")
|
||||
alert := AlertRule{
|
||||
DashboardId: cmd.Result.Id,
|
||||
OrgId: cmd.Result.OrgId,
|
||||
PanelId: panel.Get("id").MustInt64(),
|
||||
DatasourceName: panel.Get("datasource").MustString(),
|
||||
Id: alerting.Get("id").MustInt64(),
|
||||
QueryRefId: alerting.Get("queryRef").MustString(),
|
||||
WarnLevel: alerting.Get("warnLevel").MustString(),
|
||||
CritLevel: alerting.Get("critLevel").MustString(),
|
||||
Interval: alerting.Get("interval").MustString(),
|
||||
Title: alerting.Get("title").MustString(),
|
||||
Description: alerting.Get("description").MustString(),
|
||||
QueryRange: alerting.Get("queryRange").MustString(),
|
||||
Aggregator: alerting.Get("aggregator").MustString(),
|
||||
DashboardId: cmd.Result.Id,
|
||||
OrgId: cmd.Result.OrgId,
|
||||
PanelId: panel.Get("id").MustInt64(),
|
||||
Id: alerting.Get("id").MustInt64(),
|
||||
QueryRefId: alerting.Get("queryRef").MustString(),
|
||||
WarnLevel: alerting.Get("warnLevel").MustString(),
|
||||
CritLevel: alerting.Get("critLevel").MustString(),
|
||||
Interval: alerting.Get("interval").MustString(),
|
||||
Title: alerting.Get("title").MustString(),
|
||||
Description: alerting.Get("description").MustString(),
|
||||
QueryRange: alerting.Get("queryRange").MustString(),
|
||||
Aggregator: alerting.Get("aggregator").MustString(),
|
||||
}
|
||||
|
||||
for _, targetsObj := range panel.Get("targets").MustArray() {
|
||||
@@ -92,7 +91,7 @@ type GetAlertsQuery struct {
|
||||
Result []AlertRule
|
||||
}
|
||||
|
||||
type GetAlertById struct {
|
||||
type GetAlertByIdQuery struct {
|
||||
Id int64
|
||||
|
||||
Result AlertRule
|
||||
|
||||
@@ -371,9 +371,6 @@ func TestAlertModel(t *testing.T) {
|
||||
|
||||
So(alerts[0].Query, ShouldEqual, `{"refId":"A","target":"aliasByNode(statsd.fakesite.counters.session_start.desktop.count, 4)"}`)
|
||||
So(alerts[1].Query, ShouldEqual, `{"refId":"A","target":"aliasByNode(statsd.fakesite.counters.session_start.mobile.count, 4)"}`)
|
||||
|
||||
So(alerts[0].DatasourceName, ShouldEqual, "")
|
||||
So(alerts[1].DatasourceName, ShouldEqual, "graphite2")
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user