mirror of https://github.com/grafana/grafana
parent
6a5ecb3fca
commit
e7be7d2835
@ -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 |
||||
} |
@ -0,0 +1,39 @@ |
||||
package sqlstore |
||||
|
||||
import ( |
||||
"fmt" |
||||
"github.com/go-xorm/xorm" |
||||
"github.com/grafana/grafana/pkg/bus" |
||||
m "github.com/grafana/grafana/pkg/models" |
||||
) |
||||
|
||||
func init() { |
||||
bus.AddHandler("sql", SetNewAlertState) |
||||
} |
||||
|
||||
func SetNewAlertState(cmd *m.UpdateAlertStateCommand) error { |
||||
return inTransaction(func(sess *xorm.Session) error { |
||||
if !cmd.IsValidState() { |
||||
return fmt.Errorf("new state is invalid") |
||||
} |
||||
|
||||
alert := m.AlertRule{} |
||||
has, err := sess.Id(cmd.AlertId).Get(&alert) |
||||
if !has { |
||||
return fmt.Errorf("Could not find alert") |
||||
} |
||||
|
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
alert.State = cmd.NewState |
||||
sess.Id(alert.Id).Update(&alert) |
||||
//update alert
|
||||
|
||||
//insert alert state log
|
||||
|
||||
cmd.Result = &alert |
||||
return nil |
||||
}) |
||||
} |
@ -0,0 +1,85 @@ |
||||
package sqlstore |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
m "github.com/grafana/grafana/pkg/models" |
||||
. "github.com/smartystreets/goconvey/convey" |
||||
) |
||||
|
||||
func TestAlertingStateAccess(t *testing.T) { |
||||
Convey("Test alerting state changes", t, func() { |
||||
InitTestDB(t) |
||||
|
||||
//setup alert
|
||||
testDash := insertTestDashboard("dashboard with alerts", 1, "alert") |
||||
|
||||
items := []m.AlertRule{ |
||||
{ |
||||
PanelId: 1, |
||||
DashboardId: testDash.Id, |
||||
OrgId: testDash.OrgId, |
||||
Query: "Query", |
||||
QueryRefId: "A", |
||||
WarnLevel: "> 30", |
||||
CritLevel: "> 50", |
||||
Interval: "10", |
||||
Title: "Alerting title", |
||||
Description: "Alerting description", |
||||
QueryRange: "5m", |
||||
Aggregator: "avg", |
||||
}, |
||||
} |
||||
|
||||
cmd := m.SaveAlertsCommand{ |
||||
Alerts: &items, |
||||
DashboardId: testDash.Id, |
||||
OrgId: 1, |
||||
UserId: 1, |
||||
} |
||||
|
||||
err := SaveAlerts(&cmd) |
||||
So(err, ShouldBeNil) |
||||
|
||||
Convey("Cannot insert invalid states", func() { |
||||
err = SetNewAlertState(&m.UpdateAlertStateCommand{ |
||||
AlertId: 1, |
||||
NewState: "maybe ok", |
||||
Info: "Shit just hit the fan", |
||||
}) |
||||
|
||||
So(err, ShouldNotBeNil) |
||||
}) |
||||
|
||||
Convey("Changes state to alert", func() { |
||||
|
||||
err = SetNewAlertState(&m.UpdateAlertStateCommand{ |
||||
AlertId: 1, |
||||
NewState: "ALERT", |
||||
Info: "Shit just hit the fan", |
||||
}) |
||||
|
||||
Convey("can get new state for alert", func() { |
||||
query := &m.GetAlertByIdQuery{Id: 1} |
||||
err := GetAlertById(query) |
||||
So(err, ShouldBeNil) |
||||
So(query.Result.State, ShouldEqual, "ALERT") |
||||
}) |
||||
|
||||
Convey("Changes state to ok", func() { |
||||
err = SetNewAlertState(&m.UpdateAlertStateCommand{ |
||||
AlertId: 1, |
||||
NewState: "OK", |
||||
Info: "Shit just hit the fan", |
||||
}) |
||||
|
||||
Convey("get ok state for alert", func() { |
||||
query := &m.GetAlertByIdQuery{Id: 1} |
||||
err := GetAlertById(query) |
||||
So(err, ShouldBeNil) |
||||
So(query.Result.State, ShouldEqual, "OK") |
||||
}) |
||||
}) |
||||
}) |
||||
}) |
||||
} |
Loading…
Reference in new issue