浏览代码

feat(alerting): notification query

bergquist 9 年之前
父节点
当前提交
8b91e57ef6

+ 44 - 0
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
+}

+ 2 - 0
pkg/services/alerting/alert_rule.go

@@ -26,6 +26,8 @@ type AlertRule struct {
 	Transform       string
 	TransformParams simplejson.Json
 	Transformer     transformers.Transformer
+
+	NotificationGroups []int64
 }
 
 var (

+ 56 - 0
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 {
+
+}*/

+ 36 - 0
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)
+			}) */
+	})
+}

+ 15 - 0
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))
 }