diff --git a/pkg/models/alert.go b/pkg/models/alert.go index 240f8509179..5d907291eae 100644 --- a/pkg/models/alert.go +++ b/pkg/models/alert.go @@ -122,6 +122,11 @@ type DeleteAlertCommand struct { AlertId int64 } +type SaveExecutionErrorCommand struct { + AlertId int64 + ExecutionError string +} + //Queries type GetAlertsQuery struct { OrgId int64 diff --git a/pkg/services/alerting/notifiers/slack.go b/pkg/services/alerting/notifiers/slack.go index d6335221b63..e099c7da690 100644 --- a/pkg/services/alerting/notifiers/slack.go +++ b/pkg/services/alerting/notifiers/slack.go @@ -64,20 +64,20 @@ func (this *SlackNotifier) Notify(context *alerting.EvalContext) { body := map[string]interface{}{ "attachments": []map[string]interface{}{ { - "color": context.GetColor(), + "color": context.GetColor(), + "title": context.GetNotificationTitle(), + "title_link": ruleUrl, + "text": context.Rule.Message, + "fields": fields, + "image_url": context.ImagePublicUrl, + "footer": "Grafana v" + setting.BuildVersion, + "footer_icon": "http://grafana.org/assets/img/fav32.png", + "ts": time.Now().Unix(), //"pretext": "Optional text that appears above the attachment block", // "author_name": "Bobby Tables", // "author_link": "http://flickr.com/bobby/", // "author_icon": "http://flickr.com/icons/bobby.jpg", - "title": context.GetNotificationTitle(), - "title_link": ruleUrl, - "text": context.Rule.Message, - "fields": fields, - "image_url": context.ImagePublicUrl, // "thumb_url": "http://example.com/path/to/thumb.png", - "footer": "Grafana v" + setting.BuildVersion, - "footer_icon": "http://grafana.org/assets/img/fav32.png", - "ts": time.Now().Unix(), }, }, } diff --git a/pkg/services/sqlstore/alert.go b/pkg/services/sqlstore/alert.go index 32c118d51b7..37825054a54 100644 --- a/pkg/services/sqlstore/alert.go +++ b/pkg/services/sqlstore/alert.go @@ -18,6 +18,24 @@ func init() { bus.AddHandler("sql", DeleteAlertById) bus.AddHandler("sql", GetAllAlertQueryHandler) bus.AddHandler("sql", SetAlertState) + bus.AddHandler("sql", SaveExecutionErrorForAlert) +} + +func SaveExecutionErrorForAlert(cmd *m.SaveExecutionErrorCommand) 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.ExecutionError = cmd.ExecutionError + + sess.Id(alert.Id).Update(&alert) + return nil + }) } func GetAlertById(query *m.GetAlertByIdQuery) error { diff --git a/pkg/services/sqlstore/alert_test.go b/pkg/services/sqlstore/alert_test.go index e22b1c48c47..5340f102fdd 100644 --- a/pkg/services/sqlstore/alert_test.go +++ b/pkg/services/sqlstore/alert_test.go @@ -175,5 +175,39 @@ func TestAlertingDataAccess(t *testing.T) { So(len(query.Result), ShouldEqual, 0) }) }) + + Convey("Can set new execution error", func() { + items := []*m.Alert{ + { + PanelId: 1, + DashboardId: testDash.Id, + Name: "Alerting title", + Message: "Alerting message", + }, + } + + cmd := m.SaveAlertsCommand{ + Alerts: items, + DashboardId: testDash.Id, + OrgId: 1, + UserId: 1, + } + + SaveAlerts(&cmd) + + So(SaveExecutionErrorForAlert(&m.SaveExecutionErrorCommand{ + AlertId: 1, + ExecutionError: "the slacker is broken", + }), ShouldBeNil) + + Convey("Alerts should be removed", func() { + query := &m.GetAlertByIdQuery{Id: 1} + err2 := GetAlertById(query) + + So(testDash.Id, ShouldEqual, 1) + So(err2, ShouldBeNil) + So(query.Result.ExecutionError, ShouldEqual, "the slacker is broken") + }) + }) }) }