feat(alerting): added few fields to alert rule

This commit is contained in:
Torkel Ödegaard 2016-05-16 15:39:09 +02:00
parent 2ce9d4571c
commit 77b7cdfadb
3 changed files with 60 additions and 9 deletions

View File

@ -0,0 +1,34 @@
package alerting
import (
"time"
m "github.com/grafana/grafana/pkg/models"
)
func Init() {
go dispatcher()
}
func dispatcher() {
ticker := time.NewTicker(time.Second)
for {
select {
case <-ticker.C:
scheduleJobs()
}
}
}
func scheduleJobs() {
}
type Scheduler interface {
}
type Executor interface {
Execute(rule *m.AlertRule)
}

View File

@ -1,13 +1,15 @@
package models
import (
"github.com/grafana/grafana/pkg/components/simplejson"
"time"
"github.com/grafana/grafana/pkg/components/simplejson"
)
type AlertRule struct {
Id int64 `json:"id"`
OrgId int64 `json:"-"`
DataSourceId int64 `json:"datasourceId"`
DashboardId int64 `json:"dashboardId"`
PanelId int64 `json:"panelId"`
Query string `json:"query"`
@ -17,11 +19,15 @@ type AlertRule struct {
WarnOperator string `json:"warnOperator"`
CritOperator string `json:"critOperator"`
Interval string `json:"interval"`
Frequency int64 `json:"frequency"`
Title string `json:"title"`
Description string `json:"description"`
QueryRange string `json:"queryRange"`
Aggregator string `json:"aggregator"`
State string `json:"state"`
Created time.Time `json:"created"`
Updated time.Time `json:"updated"`
}
type AlertRuleChange struct {
@ -32,7 +38,7 @@ type AlertRuleChange struct {
Created time.Time `json:"created"`
}
func (cmd *SaveDashboardCommand) GetAlertModels() *[]AlertRule {
func (cmd *SaveDashboardCommand) GetAlertModels() []AlertRule {
alerts := make([]AlertRule, 0)
for _, rowObj := range cmd.Dashboard.Get("rows").MustArray() {
@ -77,7 +83,7 @@ func (cmd *SaveDashboardCommand) GetAlertModels() *[]AlertRule {
}
}
return &alerts
return alerts
}
// Commands
@ -86,7 +92,7 @@ type SaveAlertsCommand struct {
UserId int64
OrgId int64
Alerts *[]AlertRule
Alerts []AlertRule
}
type DeleteAlertCommand struct {
@ -103,6 +109,12 @@ type GetAlertsQuery struct {
Result []AlertRule
}
type GetAlertsForExecutionQuery struct {
Timestamp int64
Result []AlertRule
}
type GetAlertByIdQuery struct {
Id int64

View File

@ -3,10 +3,12 @@ package sqlstore
import (
"bytes"
"fmt"
"strings"
"time"
"github.com/go-xorm/xorm"
"github.com/grafana/grafana/pkg/bus"
m "github.com/grafana/grafana/pkg/models"
"strings"
)
func init() {
@ -134,8 +136,8 @@ func SaveAlerts(cmd *m.SaveAlertsCommand) error {
})
}
func upsertAlerts(alerts []m.AlertRule, posted *[]m.AlertRule, sess *xorm.Session) error {
for _, alert := range *posted {
func upsertAlerts(alerts []m.AlertRule, posted []m.AlertRule, sess *xorm.Session) error {
for _, alert := range posted {
update := false
var alertToUpdate m.AlertRule
@ -149,6 +151,7 @@ func upsertAlerts(alerts []m.AlertRule, posted *[]m.AlertRule, sess *xorm.Sessio
if update {
if alertIsDifferent(alertToUpdate, alert) {
alert.Updated = time.Now()
alert.State = alertToUpdate.State
_, err := sess.Id(alert.Id).Update(&alert)
if err != nil {
@ -159,6 +162,8 @@ func upsertAlerts(alerts []m.AlertRule, posted *[]m.AlertRule, sess *xorm.Sessio
}
} else {
alert.Updated = time.Now()
alert.Created = time.Now()
alert.State = "OK"
_, err := sess.Insert(&alert)
if err != nil {
@ -171,11 +176,11 @@ func upsertAlerts(alerts []m.AlertRule, posted *[]m.AlertRule, sess *xorm.Sessio
return nil
}
func deleteMissingAlerts(alerts []m.AlertRule, posted *[]m.AlertRule, sess *xorm.Session) error {
func deleteMissingAlerts(alerts []m.AlertRule, posted []m.AlertRule, sess *xorm.Session) error {
for _, missingAlert := range alerts {
missing := true
for _, k := range *posted {
for _, k := range posted {
if missingAlert.PanelId == k.PanelId {
missing = false
}