feat(alerting): added few fields to alert rule
This commit is contained in:
@@ -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)
|
||||||
|
}
|
||||||
+16
-4
@@ -1,13 +1,15 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||||
)
|
)
|
||||||
|
|
||||||
type AlertRule struct {
|
type AlertRule struct {
|
||||||
Id int64 `json:"id"`
|
Id int64 `json:"id"`
|
||||||
OrgId int64 `json:"-"`
|
OrgId int64 `json:"-"`
|
||||||
|
DataSourceId int64 `json:"datasourceId"`
|
||||||
DashboardId int64 `json:"dashboardId"`
|
DashboardId int64 `json:"dashboardId"`
|
||||||
PanelId int64 `json:"panelId"`
|
PanelId int64 `json:"panelId"`
|
||||||
Query string `json:"query"`
|
Query string `json:"query"`
|
||||||
@@ -17,11 +19,15 @@ type AlertRule struct {
|
|||||||
WarnOperator string `json:"warnOperator"`
|
WarnOperator string `json:"warnOperator"`
|
||||||
CritOperator string `json:"critOperator"`
|
CritOperator string `json:"critOperator"`
|
||||||
Interval string `json:"interval"`
|
Interval string `json:"interval"`
|
||||||
|
Frequency int64 `json:"frequency"`
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
QueryRange string `json:"queryRange"`
|
QueryRange string `json:"queryRange"`
|
||||||
Aggregator string `json:"aggregator"`
|
Aggregator string `json:"aggregator"`
|
||||||
State string `json:"state"`
|
State string `json:"state"`
|
||||||
|
|
||||||
|
Created time.Time `json:"created"`
|
||||||
|
Updated time.Time `json:"updated"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type AlertRuleChange struct {
|
type AlertRuleChange struct {
|
||||||
@@ -32,7 +38,7 @@ type AlertRuleChange struct {
|
|||||||
Created time.Time `json:"created"`
|
Created time.Time `json:"created"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cmd *SaveDashboardCommand) GetAlertModels() *[]AlertRule {
|
func (cmd *SaveDashboardCommand) GetAlertModels() []AlertRule {
|
||||||
alerts := make([]AlertRule, 0)
|
alerts := make([]AlertRule, 0)
|
||||||
|
|
||||||
for _, rowObj := range cmd.Dashboard.Get("rows").MustArray() {
|
for _, rowObj := range cmd.Dashboard.Get("rows").MustArray() {
|
||||||
@@ -77,7 +83,7 @@ func (cmd *SaveDashboardCommand) GetAlertModels() *[]AlertRule {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &alerts
|
return alerts
|
||||||
}
|
}
|
||||||
|
|
||||||
// Commands
|
// Commands
|
||||||
@@ -86,7 +92,7 @@ type SaveAlertsCommand struct {
|
|||||||
UserId int64
|
UserId int64
|
||||||
OrgId int64
|
OrgId int64
|
||||||
|
|
||||||
Alerts *[]AlertRule
|
Alerts []AlertRule
|
||||||
}
|
}
|
||||||
|
|
||||||
type DeleteAlertCommand struct {
|
type DeleteAlertCommand struct {
|
||||||
@@ -103,6 +109,12 @@ type GetAlertsQuery struct {
|
|||||||
Result []AlertRule
|
Result []AlertRule
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type GetAlertsForExecutionQuery struct {
|
||||||
|
Timestamp int64
|
||||||
|
|
||||||
|
Result []AlertRule
|
||||||
|
}
|
||||||
|
|
||||||
type GetAlertByIdQuery struct {
|
type GetAlertByIdQuery struct {
|
||||||
Id int64
|
Id int64
|
||||||
|
|
||||||
|
|||||||
@@ -3,10 +3,12 @@ package sqlstore
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/go-xorm/xorm"
|
"github.com/go-xorm/xorm"
|
||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
m "github.com/grafana/grafana/pkg/models"
|
m "github.com/grafana/grafana/pkg/models"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -134,8 +136,8 @@ func SaveAlerts(cmd *m.SaveAlertsCommand) error {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func upsertAlerts(alerts []m.AlertRule, posted *[]m.AlertRule, sess *xorm.Session) error {
|
func upsertAlerts(alerts []m.AlertRule, posted []m.AlertRule, sess *xorm.Session) error {
|
||||||
for _, alert := range *posted {
|
for _, alert := range posted {
|
||||||
update := false
|
update := false
|
||||||
var alertToUpdate m.AlertRule
|
var alertToUpdate m.AlertRule
|
||||||
|
|
||||||
@@ -149,6 +151,7 @@ func upsertAlerts(alerts []m.AlertRule, posted *[]m.AlertRule, sess *xorm.Sessio
|
|||||||
|
|
||||||
if update {
|
if update {
|
||||||
if alertIsDifferent(alertToUpdate, alert) {
|
if alertIsDifferent(alertToUpdate, alert) {
|
||||||
|
alert.Updated = time.Now()
|
||||||
alert.State = alertToUpdate.State
|
alert.State = alertToUpdate.State
|
||||||
_, err := sess.Id(alert.Id).Update(&alert)
|
_, err := sess.Id(alert.Id).Update(&alert)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -159,6 +162,8 @@ func upsertAlerts(alerts []m.AlertRule, posted *[]m.AlertRule, sess *xorm.Sessio
|
|||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
alert.Updated = time.Now()
|
||||||
|
alert.Created = time.Now()
|
||||||
alert.State = "OK"
|
alert.State = "OK"
|
||||||
_, err := sess.Insert(&alert)
|
_, err := sess.Insert(&alert)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -171,11 +176,11 @@ func upsertAlerts(alerts []m.AlertRule, posted *[]m.AlertRule, sess *xorm.Sessio
|
|||||||
return nil
|
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 {
|
for _, missingAlert := range alerts {
|
||||||
missing := true
|
missing := true
|
||||||
|
|
||||||
for _, k := range *posted {
|
for _, k := range posted {
|
||||||
if missingAlert.PanelId == k.PanelId {
|
if missingAlert.PanelId == k.PanelId {
|
||||||
missing = false
|
missing = false
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user