diff --git a/pkg/models/alert_notifications.go b/pkg/models/alert_notifications.go new file mode 100644 index 00000000000..b0dcfcfb82e --- /dev/null +++ b/pkg/models/alert_notifications.go @@ -0,0 +1,44 @@ +package models + +import ( + "time" + + "github.com/grafana/grafana/pkg/components/simplejson" +) + +type AlertNotification struct { + Id int64 + OrgId int64 + Name string + Type string + Settings *simplejson.Json + + Created time.Time + Updated time.Time +} + +type CreateAlertNotificationCommand struct { + Name string + Type string + OrgID int64 + Settings *simplejson.Json + + Result *AlertNotification +} + +type UpdateAlertNotificationCommand struct { + Name string + Type string + OrgID int64 + Settings *simplejson.Json + + Result *AlertNotification +} + +type GetAlertNotificationQuery struct { + Name string + ID int64 + OrgID int64 + + Result []*AlertNotification +} diff --git a/pkg/services/alerting/alert_rule.go b/pkg/services/alerting/alert_rule.go index 6e460a65dfd..25284b755a7 100644 --- a/pkg/services/alerting/alert_rule.go +++ b/pkg/services/alerting/alert_rule.go @@ -26,6 +26,8 @@ type AlertRule struct { Transform string TransformParams simplejson.Json Transformer transformers.Transformer + + NotificationGroups []int64 } var ( diff --git a/pkg/services/sqlstore/alert_notification.go b/pkg/services/sqlstore/alert_notification.go new file mode 100644 index 00000000000..5a173c548b7 --- /dev/null +++ b/pkg/services/sqlstore/alert_notification.go @@ -0,0 +1,56 @@ +package sqlstore + +import ( + "bytes" + + "github.com/grafana/grafana/pkg/bus" + m "github.com/grafana/grafana/pkg/models" +) + +func init() { + bus.AddHandler("sql", GetAlertNotifications) +} + +func GetAlertNotifications(query *m.GetAlertNotificationQuery) error { + var sql bytes.Buffer + params := make([]interface{}, 0) + + sql.WriteString(`SELECT + alert_notification.id, + alert_notification.org_id, + alert_notification.name, + alert_notification.type, + alert_notification.created, + alert_notification.updated, + alert_notification.settings + FROM alert_notification + `) + + sql.WriteString(` WHERE alert_notification.org_id = ?`) + params = append(params, query.OrgID) + + if query.Name != "" { + sql.WriteString(` AND alert_notification.name = ?`) + params = append(params, query.Name) + } + + var result []*m.AlertNotification + if err := x.Sql(sql.String(), params...).Find(&result); err != nil { + return err + } + + query.Result = result + return nil +} + +/* +func CreateAlertNotification(cmd *m.CreateAlertNotificationCommand) error { + return inTransaction(func(sess *xorm.Session) error { + + + }) +} + +func UpdateAlertNotification(cmd *m.UpdateAlertNotificationCommand) error { + +}*/ diff --git a/pkg/services/sqlstore/alert_notification_test.go b/pkg/services/sqlstore/alert_notification_test.go new file mode 100644 index 00000000000..332394e97be --- /dev/null +++ b/pkg/services/sqlstore/alert_notification_test.go @@ -0,0 +1,36 @@ +package sqlstore + +import ( + "fmt" + "testing" + + m "github.com/grafana/grafana/pkg/models" + . "github.com/smartystreets/goconvey/convey" +) + +func TestAlertNotificationSQLAccess(t *testing.T) { + Convey("Testing Alert notification sql access", t, func() { + InitTestDB(t) + + Convey("Alert notifications should be empty", func() { + cmd := &m.GetAlertNotificationQuery{ + OrgID: FakeOrgId, + Name: "email", + } + + err := GetAlertNotifications(cmd) + fmt.Printf("errror %v", err) + So(err, ShouldBeNil) + So(len(cmd.Result), ShouldEqual, 0) + }) + /* + Convey("Can save Alert Notification", func() { + cmd := &m.CreateAlertNotificationCommand{} + + var err error + err = CreateAlertNotification(cmd) + + So(err, ShouldBeNil) + }) */ + }) +} diff --git a/pkg/services/sqlstore/migrations/alert_mig.go b/pkg/services/sqlstore/migrations/alert_mig.go index ff9ad5abf51..2ff7712f8bb 100644 --- a/pkg/services/sqlstore/migrations/alert_mig.go +++ b/pkg/services/sqlstore/migrations/alert_mig.go @@ -65,4 +65,19 @@ func addAlertMigrations(mg *Migrator) { } mg.AddMigration("create alert_heartbeat table v1", NewAddTableMigration(alert_heartbeat)) + + alert_notification := Table{ + Name: "alert_notification", + Columns: []*Column{ + {Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true}, + {Name: "org_id", Type: DB_BigInt, Nullable: false}, + {Name: "name", Type: DB_NVarchar, Length: 255, Nullable: false}, + {Name: "type", Type: DB_NVarchar, Length: 255, Nullable: false}, + {Name: "settings", Type: DB_Text, Nullable: false}, + {Name: "created", Type: DB_DateTime, Nullable: false}, + {Name: "updated", Type: DB_DateTime, Nullable: false}, + }, + } + + mg.AddMigration("create alert_notification table v1", NewAddTableMigration(alert_notification)) }