mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
feat(alerting): add basic tables for alerting definitions
This commit is contained in:
parent
6b8921c8c5
commit
91a1a823e2
@ -149,6 +149,19 @@ func PostDashboard(c *middleware.Context, cmd m.SaveDashboardCommand) {
|
||||
return
|
||||
}
|
||||
|
||||
saveAlertCommand := m.SaveAlertsCommand{
|
||||
DashboardId: dash.Id,
|
||||
OrgId: c.OrgId,
|
||||
UserId: c.UserId,
|
||||
Alerts: cmd.GetAlertModels(),
|
||||
}
|
||||
|
||||
err = bus.Dispatch(&saveAlertCommand)
|
||||
if err != nil {
|
||||
c.JsonApiErr(500, "Failed to save alerts", err)
|
||||
return
|
||||
}
|
||||
|
||||
metrics.M_Api_Dashboard_Post.Inc(1)
|
||||
|
||||
c.JSON(200, util.DynMap{"status": "success", "slug": cmd.Result.Slug, "version": cmd.Result.Version})
|
||||
|
50
pkg/models/alerts.go
Normal file
50
pkg/models/alerts.go
Normal file
@ -0,0 +1,50 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
//"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
)
|
||||
|
||||
type Alert struct {
|
||||
Id int64
|
||||
DashboardId int64
|
||||
PanelId int64
|
||||
Query string
|
||||
QueryRefId string
|
||||
WarnLevel int64
|
||||
ErrorLevel int64
|
||||
CheckInterval string
|
||||
Title string
|
||||
Description string
|
||||
QueryRange string
|
||||
}
|
||||
|
||||
func (cmd *SaveDashboardCommand) GetAlertModels() *[]Alert {
|
||||
dash := NewDashboardFromJson(cmd.Dashboard)
|
||||
|
||||
alerts := make([]Alert, 0)
|
||||
|
||||
alerts = append(alerts, Alert{
|
||||
DashboardId: dash.Id,
|
||||
Id: 1,
|
||||
PanelId: 1,
|
||||
Query: "",
|
||||
QueryRefId: "",
|
||||
WarnLevel: 0,
|
||||
ErrorLevel: 0,
|
||||
CheckInterval: "5s",
|
||||
Title: dash.Title + " Alert",
|
||||
Description: dash.Title + " Description",
|
||||
QueryRange: "10m",
|
||||
})
|
||||
|
||||
return &alerts
|
||||
}
|
||||
|
||||
// Commands
|
||||
type SaveAlertsCommand struct {
|
||||
DashboardId int64
|
||||
UserId int64
|
||||
OrgId int64
|
||||
|
||||
Alerts *[]Alert
|
||||
}
|
28
pkg/services/sqlstore/alerting.go
Normal file
28
pkg/services/sqlstore/alerting.go
Normal file
@ -0,0 +1,28 @@
|
||||
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", SaveAlerts)
|
||||
}
|
||||
|
||||
func SaveAlerts(cmd *m.SaveAlertsCommand) error {
|
||||
return inTransaction(func(sess *xorm.Session) error {
|
||||
fmt.Printf("Saving alerts for dashboard %v\n", cmd.DashboardId)
|
||||
|
||||
for _, alert := range *cmd.Alerts {
|
||||
_, err := x.Insert(&alert)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
27
pkg/services/sqlstore/migrations/alert_mig.go
Normal file
27
pkg/services/sqlstore/migrations/alert_mig.go
Normal file
@ -0,0 +1,27 @@
|
||||
package migrations
|
||||
|
||||
import . "github.com/grafana/grafana/pkg/services/sqlstore/migrator"
|
||||
|
||||
func addAlertMigrations(mg *Migrator) {
|
||||
mg.AddMigration("Drop old table alert table", NewDropTableMigration("alert"))
|
||||
|
||||
alertV1 := Table{
|
||||
Name: "alert",
|
||||
Columns: []*Column{
|
||||
{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
|
||||
{Name: "dashboard_id", Type: DB_BigInt, Nullable: false},
|
||||
{Name: "panel_id", Type: DB_BigInt, Nullable: false},
|
||||
{Name: "query", Type: DB_Text, Nullable: false},
|
||||
{Name: "query_ref_id", Type: DB_NVarchar, Length: 255, Nullable: false},
|
||||
{Name: "warn_level", Type: DB_NVarchar, Length: 255, Nullable: false},
|
||||
{Name: "error_level", Type: DB_NVarchar, Length: 255, Nullable: false},
|
||||
{Name: "check_interval", Type: DB_NVarchar, Length: 255, Nullable: false},
|
||||
{Name: "title", Type: DB_NVarchar, Length: 255, Nullable: false},
|
||||
{Name: "description", Type: DB_NVarchar, Length: 255, Nullable: false},
|
||||
{Name: "query_range", Type: DB_NVarchar, Length: 255, Nullable: false},
|
||||
},
|
||||
}
|
||||
|
||||
// create table
|
||||
mg.AddMigration("create alert table v1", NewAddTableMigration(alertV1))
|
||||
}
|
@ -22,6 +22,7 @@ func AddMigrations(mg *Migrator) {
|
||||
addSessionMigration(mg)
|
||||
addPlaylistMigrations(mg)
|
||||
addPreferencesMigrations(mg)
|
||||
addAlertMigrations(mg)
|
||||
}
|
||||
|
||||
func addMigrationLogMigrations(mg *Migrator) {
|
||||
|
Loading…
Reference in New Issue
Block a user