tech: avoid alias for importing models in alerting (#17041)

ref #14679
This commit is contained in:
Carl Bergquist
2019-05-14 08:15:05 +02:00
committed by GitHub
parent a87a763d83
commit 13f137a17d
47 changed files with 261 additions and 259 deletions
+4 -4
View File
@@ -2,7 +2,7 @@ package alerting
import (
"github.com/grafana/grafana/pkg/bus"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
)
func init() {
@@ -10,14 +10,14 @@ func init() {
bus.AddHandler("alerting", validateDashboardAlerts)
}
func validateDashboardAlerts(cmd *m.ValidateDashboardAlertsCommand) error {
func validateDashboardAlerts(cmd *models.ValidateDashboardAlertsCommand) error {
extractor := NewDashAlertExtractor(cmd.Dashboard, cmd.OrgId, cmd.User)
return extractor.ValidateAlerts()
}
func updateDashboardAlerts(cmd *m.UpdateDashboardAlertsCommand) error {
saveAlerts := m.SaveAlertsCommand{
func updateDashboardAlerts(cmd *models.UpdateDashboardAlertsCommand) error {
saveAlerts := models.SaveAlertsCommand{
OrgId: cmd.OrgId,
UserId: cmd.User.UserId,
DashboardId: cmd.Dashboard.Id,
+3 -3
View File
@@ -10,7 +10,7 @@ import (
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/components/null"
"github.com/grafana/grafana/pkg/components/simplejson"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/alerting"
"github.com/grafana/grafana/pkg/tsdb"
)
@@ -100,7 +100,7 @@ func (c *QueryCondition) Eval(context *alerting.EvalContext) (*alerting.Conditio
}
func (c *QueryCondition) executeQuery(context *alerting.EvalContext, timeRange *tsdb.TimeRange) (tsdb.TimeSeriesSlice, error) {
getDsInfo := &m.GetDataSourceByIdQuery{
getDsInfo := &models.GetDataSourceByIdQuery{
Id: c.Query.DatasourceId,
OrgId: context.Rule.OrgId,
}
@@ -139,7 +139,7 @@ func (c *QueryCondition) executeQuery(context *alerting.EvalContext, timeRange *
return result, nil
}
func (c *QueryCondition) getRequestForAlertRule(datasource *m.DataSource, timeRange *tsdb.TimeRange) *tsdb.TsdbQuery {
func (c *QueryCondition) getRequestForAlertRule(datasource *models.DataSource, timeRange *tsdb.TimeRange) *tsdb.TsdbQuery {
req := &tsdb.TsdbQuery{
TimeRange: timeRange,
Queries: []*tsdb.Query{
@@ -7,7 +7,7 @@ import (
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/components/null"
"github.com/grafana/grafana/pkg/components/simplejson"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/alerting"
"github.com/grafana/grafana/pkg/tsdb"
. "github.com/smartystreets/goconvey/convey"
@@ -168,7 +168,7 @@ func (ctx *queryConditionTestContext) exec() (*alerting.ConditionResult, error)
ctx.condition = condition
condition.HandleRequest = func(context context.Context, dsInfo *m.DataSource, req *tsdb.TsdbQuery) (*tsdb.Response, error) {
condition.HandleRequest = func(context context.Context, dsInfo *models.DataSource, req *tsdb.TsdbQuery) (*tsdb.Response, error) {
return &tsdb.Response{
Results: map[string]*tsdb.QueryResult{
"A": {Series: ctx.series},
@@ -182,8 +182,8 @@ func (ctx *queryConditionTestContext) exec() (*alerting.ConditionResult, error)
func queryConditionScenario(desc string, fn queryConditionScenarioFunc) {
Convey(desc, func() {
bus.AddHandler("test", func(query *m.GetDataSourceByIdQuery) error {
query.Result = &m.DataSource{Id: 1, Type: "graphite"}
bus.AddHandler("test", func(query *models.GetDataSourceByIdQuery) error {
query.Result = &models.DataSource{Id: 1, Type: "graphite"}
return nil
})
+22 -22
View File
@@ -7,7 +7,7 @@ import (
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/infra/log"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/setting"
)
@@ -23,12 +23,12 @@ type EvalContext struct {
Rule *Rule
log log.Logger
dashboardRef *m.DashboardRef
dashboardRef *models.DashboardRef
ImagePublicUrl string
ImageOnDiskPath string
NoDataFound bool
PrevAlertState m.AlertStateType
PrevAlertState models.AlertStateType
Ctx context.Context
}
@@ -53,22 +53,22 @@ type StateDescription struct {
func (c *EvalContext) GetStateModel() *StateDescription {
switch c.Rule.State {
case m.AlertStateOK:
case models.AlertStateOK:
return &StateDescription{
Color: "#36a64f",
Text: "OK",
}
case m.AlertStateNoData:
case models.AlertStateNoData:
return &StateDescription{
Color: "#888888",
Text: "No Data",
}
case m.AlertStateAlerting:
case models.AlertStateAlerting:
return &StateDescription{
Color: "#D63232",
Text: "Alerting",
}
case m.AlertStateUnknown:
case models.AlertStateUnknown:
return &StateDescription{
Color: "#888888",
Text: "Unknown",
@@ -90,12 +90,12 @@ func (c *EvalContext) GetNotificationTitle() string {
return "[" + c.GetStateModel().Text + "] " + c.Rule.Name
}
func (c *EvalContext) GetDashboardUID() (*m.DashboardRef, error) {
func (c *EvalContext) GetDashboardUID() (*models.DashboardRef, error) {
if c.dashboardRef != nil {
return c.dashboardRef, nil
}
uidQuery := &m.GetDashboardRefByIdQuery{Id: c.Rule.DashboardId}
uidQuery := &models.GetDashboardRefByIdQuery{Id: c.Rule.DashboardId}
if err := bus.Dispatch(uidQuery); err != nil {
return nil, err
}
@@ -115,29 +115,29 @@ func (c *EvalContext) GetRuleUrl() (string, error) {
if err != nil {
return "", err
}
return fmt.Sprintf(urlFormat, m.GetFullDashboardUrl(ref.Uid, ref.Slug), c.Rule.PanelId, c.Rule.OrgId), nil
return fmt.Sprintf(urlFormat, models.GetFullDashboardUrl(ref.Uid, ref.Slug), c.Rule.PanelId, c.Rule.OrgId), nil
}
// GetNewState returns the new state from the alert rule evaluation
func (c *EvalContext) GetNewState() m.AlertStateType {
func (c *EvalContext) GetNewState() models.AlertStateType {
ns := getNewStateInternal(c)
if ns != m.AlertStateAlerting || c.Rule.For == 0 {
if ns != models.AlertStateAlerting || c.Rule.For == 0 {
return ns
}
since := time.Since(c.Rule.LastStateChange)
if c.PrevAlertState == m.AlertStatePending && since > c.Rule.For {
return m.AlertStateAlerting
if c.PrevAlertState == models.AlertStatePending && since > c.Rule.For {
return models.AlertStateAlerting
}
if c.PrevAlertState == m.AlertStateAlerting {
return m.AlertStateAlerting
if c.PrevAlertState == models.AlertStateAlerting {
return models.AlertStateAlerting
}
return m.AlertStatePending
return models.AlertStatePending
}
func getNewStateInternal(c *EvalContext) m.AlertStateType {
func getNewStateInternal(c *EvalContext) models.AlertStateType {
if c.Error != nil {
c.log.Error("Alert Rule Result Error",
"ruleId", c.Rule.Id,
@@ -145,14 +145,14 @@ func getNewStateInternal(c *EvalContext) m.AlertStateType {
"error", c.Error,
"changing state to", c.Rule.ExecutionErrorState.ToAlertState())
if c.Rule.ExecutionErrorState == m.ExecutionErrorKeepState {
if c.Rule.ExecutionErrorState == models.ExecutionErrorKeepState {
return c.PrevAlertState
}
return c.Rule.ExecutionErrorState.ToAlertState()
}
if c.Firing {
return m.AlertStateAlerting
return models.AlertStateAlerting
}
if c.NoDataFound {
@@ -161,11 +161,11 @@ func getNewStateInternal(c *EvalContext) m.AlertStateType {
"name", c.Rule.Name,
"changing state to", c.Rule.NoDataState.ToAlertState())
if c.Rule.NoDataState == m.NoDataKeepState {
if c.Rule.NoDataState == models.NoDataKeepState {
return c.PrevAlertState
}
return c.Rule.NoDataState.ToAlertState()
}
return m.AlertStateOK
return models.AlertStateOK
}
+18 -18
View File
@@ -8,19 +8,19 @@ import (
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/infra/log"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
)
// DashAlertExtractor extracts alerts from the dashboard json
type DashAlertExtractor struct {
User *m.SignedInUser
Dash *m.Dashboard
User *models.SignedInUser
Dash *models.Dashboard
OrgID int64
log log.Logger
}
// NewDashAlertExtractor returns a new DashAlertExtractor
func NewDashAlertExtractor(dash *m.Dashboard, orgID int64, user *m.SignedInUser) *DashAlertExtractor {
func NewDashAlertExtractor(dash *models.Dashboard, orgID int64, user *models.SignedInUser) *DashAlertExtractor {
return &DashAlertExtractor{
User: user,
Dash: dash,
@@ -29,9 +29,9 @@ func NewDashAlertExtractor(dash *m.Dashboard, orgID int64, user *m.SignedInUser)
}
}
func (e *DashAlertExtractor) lookupDatasourceID(dsName string) (*m.DataSource, error) {
func (e *DashAlertExtractor) lookupDatasourceID(dsName string) (*models.DataSource, error) {
if dsName == "" {
query := &m.GetDataSourcesQuery{OrgId: e.OrgID}
query := &models.GetDataSourcesQuery{OrgId: e.OrgID}
if err := bus.Dispatch(query); err != nil {
return nil, err
}
@@ -42,7 +42,7 @@ func (e *DashAlertExtractor) lookupDatasourceID(dsName string) (*m.DataSource, e
}
}
} else {
query := &m.GetDataSourceByNameQuery{Name: dsName, OrgId: e.OrgID}
query := &models.GetDataSourceByNameQuery{Name: dsName, OrgId: e.OrgID}
if err := bus.Dispatch(query); err != nil {
return nil, err
}
@@ -73,8 +73,8 @@ func copyJSON(in *simplejson.Json) (*simplejson.Json, error) {
return simplejson.NewJson(rawJSON)
}
func (e *DashAlertExtractor) getAlertFromPanels(jsonWithPanels *simplejson.Json, validateAlertFunc func(*m.Alert) bool) ([]*m.Alert, error) {
alerts := make([]*m.Alert, 0)
func (e *DashAlertExtractor) getAlertFromPanels(jsonWithPanels *simplejson.Json, validateAlertFunc func(*models.Alert) bool) ([]*models.Alert, error) {
alerts := make([]*models.Alert, 0)
for _, panelObj := range jsonWithPanels.Get("panels").MustArray() {
panel := simplejson.NewFromAny(panelObj)
@@ -124,7 +124,7 @@ func (e *DashAlertExtractor) getAlertFromPanels(jsonWithPanels *simplejson.Json,
}
}
alert := &m.Alert{
alert := &models.Alert{
DashboardId: e.Dash.Id,
OrgId: e.OrgID,
PanelId: panelID,
@@ -161,9 +161,9 @@ func (e *DashAlertExtractor) getAlertFromPanels(jsonWithPanels *simplejson.Json,
return nil, ValidationError{Reason: fmt.Sprintf("Data source used by alert rule not found, alertName=%v, datasource=%s", alert.Name, dsName)}
}
dsFilterQuery := m.DatasourcesPermissionFilterQuery{
dsFilterQuery := models.DatasourcesPermissionFilterQuery{
User: e.User,
Datasources: []*m.DataSource{datasource},
Datasources: []*models.DataSource{datasource},
}
if err := bus.Dispatch(&dsFilterQuery); err != nil {
@@ -172,7 +172,7 @@ func (e *DashAlertExtractor) getAlertFromPanels(jsonWithPanels *simplejson.Json,
}
} else {
if len(dsFilterQuery.Result) == 0 {
return nil, m.ErrDataSourceAccessDenied
return nil, models.ErrDataSourceAccessDenied
}
}
@@ -203,22 +203,22 @@ func (e *DashAlertExtractor) getAlertFromPanels(jsonWithPanels *simplejson.Json,
return alerts, nil
}
func validateAlertRule(alert *m.Alert) bool {
func validateAlertRule(alert *models.Alert) bool {
return alert.ValidToSave()
}
// GetAlerts extracts alerts from the dashboard json and does full validation on the alert json data
func (e *DashAlertExtractor) GetAlerts() ([]*m.Alert, error) {
func (e *DashAlertExtractor) GetAlerts() ([]*models.Alert, error) {
return e.extractAlerts(validateAlertRule)
}
func (e *DashAlertExtractor) extractAlerts(validateFunc func(alert *m.Alert) bool) ([]*m.Alert, error) {
func (e *DashAlertExtractor) extractAlerts(validateFunc func(alert *models.Alert) bool) ([]*models.Alert, error) {
dashboardJSON, err := copyJSON(e.Dash.Data)
if err != nil {
return nil, err
}
alerts := make([]*m.Alert, 0)
alerts := make([]*models.Alert, 0)
// We extract alerts from rows to be backwards compatible
// with the old dashboard json model.
@@ -249,6 +249,6 @@ func (e *DashAlertExtractor) extractAlerts(validateFunc func(alert *m.Alert) boo
// ValidateAlerts validates alerts in the dashboard json but does not require a valid dashboard id
// in the first validation pass
func (e *DashAlertExtractor) ValidateAlerts() error {
_, err := e.extractAlerts(func(alert *m.Alert) bool { return alert.OrgId != 0 && alert.PanelId != 0 })
_, err := e.extractAlerts(func(alert *models.Alert) bool { return alert.OrgId != 0 && alert.PanelId != 0 })
return err
}
+18 -18
View File
@@ -7,7 +7,7 @@ import (
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/components/simplejson"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/sqlstore"
. "github.com/smartystreets/goconvey/convey"
)
@@ -21,17 +21,17 @@ func TestAlertRuleExtraction(t *testing.T) {
})
// mock data
defaultDs := &m.DataSource{Id: 12, OrgId: 1, Name: "I am default", IsDefault: true}
graphite2Ds := &m.DataSource{Id: 15, OrgId: 1, Name: "graphite2"}
influxDBDs := &m.DataSource{Id: 16, OrgId: 1, Name: "InfluxDB"}
prom := &m.DataSource{Id: 17, OrgId: 1, Name: "Prometheus"}
defaultDs := &models.DataSource{Id: 12, OrgId: 1, Name: "I am default", IsDefault: true}
graphite2Ds := &models.DataSource{Id: 15, OrgId: 1, Name: "graphite2"}
influxDBDs := &models.DataSource{Id: 16, OrgId: 1, Name: "InfluxDB"}
prom := &models.DataSource{Id: 17, OrgId: 1, Name: "Prometheus"}
bus.AddHandler("test", func(query *m.GetDataSourcesQuery) error {
query.Result = []*m.DataSource{defaultDs, graphite2Ds}
bus.AddHandler("test", func(query *models.GetDataSourcesQuery) error {
query.Result = []*models.DataSource{defaultDs, graphite2Ds}
return nil
})
bus.AddHandler("test", func(query *m.GetDataSourceByNameQuery) error {
bus.AddHandler("test", func(query *models.GetDataSourceByNameQuery) error {
if query.Name == defaultDs.Name {
query.Result = defaultDs
}
@@ -55,7 +55,7 @@ func TestAlertRuleExtraction(t *testing.T) {
dashJson, err := simplejson.NewJson(json)
So(err, ShouldBeNil)
dash := m.NewDashboardFromJson(dashJson)
dash := models.NewDashboardFromJson(dashJson)
getTarget := func(j *simplejson.Json) string {
rowObj := j.Get("rows").MustArray()[0]
@@ -84,7 +84,7 @@ func TestAlertRuleExtraction(t *testing.T) {
dashJson, err := simplejson.NewJson(json)
So(err, ShouldBeNil)
dash := m.NewDashboardFromJson(dashJson)
dash := models.NewDashboardFromJson(dashJson)
extractor := NewDashAlertExtractor(dash, 1, nil)
alerts, err := extractor.GetAlerts()
@@ -152,7 +152,7 @@ func TestAlertRuleExtraction(t *testing.T) {
dashJson, err := simplejson.NewJson(panelWithoutId)
So(err, ShouldBeNil)
dash := m.NewDashboardFromJson(dashJson)
dash := models.NewDashboardFromJson(dashJson)
extractor := NewDashAlertExtractor(dash, 1, nil)
_, err = extractor.GetAlerts()
@@ -168,7 +168,7 @@ func TestAlertRuleExtraction(t *testing.T) {
dashJson, err := simplejson.NewJson(panelWithIdZero)
So(err, ShouldBeNil)
dash := m.NewDashboardFromJson(dashJson)
dash := models.NewDashboardFromJson(dashJson)
extractor := NewDashAlertExtractor(dash, 1, nil)
_, err = extractor.GetAlerts()
@@ -184,7 +184,7 @@ func TestAlertRuleExtraction(t *testing.T) {
dashJson, err := simplejson.NewJson(json)
So(err, ShouldBeNil)
dash := m.NewDashboardFromJson(dashJson)
dash := models.NewDashboardFromJson(dashJson)
extractor := NewDashAlertExtractor(dash, 1, nil)
alerts, err := extractor.GetAlerts()
@@ -200,10 +200,10 @@ func TestAlertRuleExtraction(t *testing.T) {
Convey("Alert notifications are in DB", func() {
sqlstore.InitTestDB(t)
firstNotification := m.CreateAlertNotificationCommand{Uid: "notifier1", OrgId: 1, Name: "1"}
firstNotification := models.CreateAlertNotificationCommand{Uid: "notifier1", OrgId: 1, Name: "1"}
err = sqlstore.CreateAlertNotificationCommand(&firstNotification)
So(err, ShouldBeNil)
secondNotification := m.CreateAlertNotificationCommand{Uid: "notifier2", OrgId: 1, Name: "2"}
secondNotification := models.CreateAlertNotificationCommand{Uid: "notifier2", OrgId: 1, Name: "2"}
err = sqlstore.CreateAlertNotificationCommand(&secondNotification)
So(err, ShouldBeNil)
@@ -213,7 +213,7 @@ func TestAlertRuleExtraction(t *testing.T) {
dashJson, err := simplejson.NewJson(json)
So(err, ShouldBeNil)
dash := m.NewDashboardFromJson(dashJson)
dash := models.NewDashboardFromJson(dashJson)
extractor := NewDashAlertExtractor(dash, 1, nil)
alerts, err := extractor.GetAlerts()
@@ -243,7 +243,7 @@ func TestAlertRuleExtraction(t *testing.T) {
dashJson, err := simplejson.NewJson(json)
So(err, ShouldBeNil)
dash := m.NewDashboardFromJson(dashJson)
dash := models.NewDashboardFromJson(dashJson)
extractor := NewDashAlertExtractor(dash, 1, nil)
alerts, err := extractor.GetAlerts()
@@ -263,7 +263,7 @@ func TestAlertRuleExtraction(t *testing.T) {
dashJSON, err := simplejson.NewJson(json)
So(err, ShouldBeNil)
dash := m.NewDashboardFromJson(dashJSON)
dash := models.NewDashboardFromJson(dashJSON)
extractor := NewDashAlertExtractor(dash, 1, nil)
err = extractor.ValidateAlerts()
+9 -10
View File
@@ -8,10 +8,9 @@ import (
"github.com/grafana/grafana/pkg/components/imguploader"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/metrics"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/rendering"
"github.com/grafana/grafana/pkg/setting"
m "github.com/grafana/grafana/pkg/models"
)
type NotifierPlugin struct {
@@ -73,7 +72,7 @@ func (n *notificationService) sendAndMarkAsComplete(evalContext *EvalContext, no
return nil
}
cmd := &m.SetAlertNotificationStateToCompleteCommand{
cmd := &models.SetAlertNotificationStateToCompleteCommand{
Id: notifierState.state.Id,
Version: notifierState.state.Version,
}
@@ -83,14 +82,14 @@ func (n *notificationService) sendAndMarkAsComplete(evalContext *EvalContext, no
func (n *notificationService) sendNotification(evalContext *EvalContext, notifierState *notifierState) error {
if !evalContext.IsTestRun {
setPendingCmd := &m.SetAlertNotificationStateToPendingCommand{
setPendingCmd := &models.SetAlertNotificationStateToPendingCommand{
Id: notifierState.state.Id,
Version: notifierState.state.Version,
AlertRuleStateUpdatedVersion: evalContext.Rule.StateChanges,
}
err := bus.DispatchCtx(evalContext.Ctx, setPendingCmd)
if err == m.ErrAlertNotificationStateVersionConflict {
if err == models.ErrAlertNotificationStateVersionConflict {
return nil
}
@@ -128,7 +127,7 @@ func (n *notificationService) uploadImage(context *EvalContext) (err error) {
Height: 500,
Timeout: setting.AlertingEvaluationTimeout,
OrgId: context.Rule.OrgId,
OrgRole: m.ROLE_ADMIN,
OrgRole: models.ROLE_ADMIN,
ConcurrentLimit: setting.AlertingRenderLimit,
}
@@ -158,7 +157,7 @@ func (n *notificationService) uploadImage(context *EvalContext) (err error) {
}
func (n *notificationService) getNeededNotifiers(orgId int64, notificationUids []string, evalContext *EvalContext) (notifierStateSlice, error) {
query := &m.GetAlertNotificationsWithUidToSendQuery{OrgId: orgId, Uids: notificationUids}
query := &models.GetAlertNotificationsWithUidToSendQuery{OrgId: orgId, Uids: notificationUids}
if err := bus.Dispatch(query); err != nil {
return nil, err
@@ -172,7 +171,7 @@ func (n *notificationService) getNeededNotifiers(orgId int64, notificationUids [
continue
}
query := &m.GetOrCreateNotificationStateQuery{
query := &models.GetOrCreateNotificationStateQuery{
NotifierId: notification.Id,
AlertId: evalContext.Rule.Id,
OrgId: evalContext.Rule.OrgId,
@@ -196,7 +195,7 @@ func (n *notificationService) getNeededNotifiers(orgId int64, notificationUids [
}
// InitNotifier instantiate a new notifier based on the model
func InitNotifier(model *m.AlertNotification) (Notifier, error) {
func InitNotifier(model *models.AlertNotification) (Notifier, error) {
notifierPlugin, found := notifierFactories[model.Type]
if !found {
return nil, errors.New("Unsupported notification type")
@@ -205,7 +204,7 @@ func InitNotifier(model *m.AlertNotification) (Notifier, error) {
return notifierPlugin.Factory(model)
}
type NotifierFactory func(notification *m.AlertNotification) (Notifier, error)
type NotifierFactory func(notification *models.AlertNotification) (Notifier, error)
var notifierFactories = make(map[string]*NotifierPlugin)
@@ -7,7 +7,7 @@ import (
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/infra/log"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/alerting"
)
@@ -27,7 +27,7 @@ func init() {
})
}
func NewAlertmanagerNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
func NewAlertmanagerNotifier(model *models.AlertNotification) (alerting.Notifier, error) {
url := model.Settings.Get("url").MustString()
if url == "" {
return nil, alerting.ValidationError{Reason: "Could not find url property in settings"}
@@ -46,24 +46,24 @@ type AlertmanagerNotifier struct {
log log.Logger
}
func (this *AlertmanagerNotifier) ShouldNotify(ctx context.Context, evalContext *alerting.EvalContext, notificationState *m.AlertNotificationState) bool {
func (this *AlertmanagerNotifier) ShouldNotify(ctx context.Context, evalContext *alerting.EvalContext, notificationState *models.AlertNotificationState) bool {
this.log.Debug("Should notify", "ruleId", evalContext.Rule.Id, "state", evalContext.Rule.State, "previousState", evalContext.PrevAlertState)
// Do not notify when we become OK for the first time.
if (evalContext.PrevAlertState == m.AlertStatePending) && (evalContext.Rule.State == m.AlertStateOK) {
if (evalContext.PrevAlertState == models.AlertStatePending) && (evalContext.Rule.State == models.AlertStateOK) {
return false
}
// Notify on Alerting -> OK to resolve before alertmanager timeout.
if (evalContext.PrevAlertState == m.AlertStateAlerting) && (evalContext.Rule.State == m.AlertStateOK) {
if (evalContext.PrevAlertState == models.AlertStateAlerting) && (evalContext.Rule.State == models.AlertStateOK) {
return true
}
return evalContext.Rule.State == m.AlertStateAlerting
return evalContext.Rule.State == models.AlertStateAlerting
}
func (this *AlertmanagerNotifier) createAlert(evalContext *alerting.EvalContext, match *alerting.EvalMatch, ruleUrl string) *simplejson.Json {
alertJSON := simplejson.New()
alertJSON.Set("startsAt", evalContext.StartTime.UTC().Format(time.RFC3339))
if evalContext.Rule.State == m.AlertStateOK {
if evalContext.Rule.State == models.AlertStateOK {
alertJSON.Set("endsAt", time.Now().UTC().Format(time.RFC3339))
}
alertJSON.Set("generatorURL", ruleUrl)
@@ -128,7 +128,7 @@ func (this *AlertmanagerNotifier) Notify(evalContext *alerting.EvalContext) erro
bodyJSON := simplejson.NewFromAny(alerts)
body, _ := bodyJSON.MarshalJSON()
cmd := &m.SendWebhookSync{
cmd := &models.SendWebhookSync{
Url: this.Url + "/api/v1/alerts",
HttpMethod: "POST",
Body: string(body),
@@ -6,36 +6,37 @@ import (
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/infra/log"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/alerting"
. "github.com/smartystreets/goconvey/convey"
)
func TestWhenAlertManagerShouldNotify(t *testing.T) {
tcs := []struct {
prevState m.AlertStateType
newState m.AlertStateType
prevState models.AlertStateType
newState models.AlertStateType
expect bool
}{
{
prevState: m.AlertStatePending,
newState: m.AlertStateOK,
prevState: models.AlertStatePending,
newState: models.AlertStateOK,
expect: false,
},
{
prevState: m.AlertStateAlerting,
newState: m.AlertStateOK,
prevState: models.AlertStateAlerting,
newState: models.AlertStateOK,
expect: true,
},
{
prevState: m.AlertStateOK,
newState: m.AlertStatePending,
prevState: models.AlertStateOK,
newState: models.AlertStatePending,
expect: false,
},
{
prevState: m.AlertStateUnknown,
newState: m.AlertStatePending,
prevState: models.AlertStateUnknown,
newState: models.AlertStatePending,
expect: false,
},
}
@@ -48,7 +49,7 @@ func TestWhenAlertManagerShouldNotify(t *testing.T) {
evalContext.Rule.State = tc.newState
res := am.ShouldNotify(context.TODO(), evalContext, &m.AlertNotificationState{})
res := am.ShouldNotify(context.TODO(), evalContext, &models.AlertNotificationState{})
if res != tc.expect {
t.Errorf("got %v expected %v", res, tc.expect)
}
@@ -63,7 +64,7 @@ func TestAlertmanagerNotifier(t *testing.T) {
json := `{ }`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{
model := &models.AlertNotification{
Name: "alertmanager",
Type: "alertmanager",
Settings: settingsJSON,
@@ -77,7 +78,7 @@ func TestAlertmanagerNotifier(t *testing.T) {
json := `{ "url": "http://127.0.0.1:9093/" }`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{
model := &models.AlertNotification{
Name: "alertmanager",
Type: "alertmanager",
Settings: settingsJSON,
+3 -3
View File
@@ -8,7 +8,7 @@ import (
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/infra/log"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/alerting"
)
@@ -36,7 +36,7 @@ func init() {
}
func NewDingDingNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
func NewDingDingNotifier(model *models.AlertNotification) (alerting.Notifier, error) {
url := model.Settings.Get("url").MustString()
if url == "" {
return nil, alerting.ValidationError{Reason: "Could not find url property in settings"}
@@ -129,7 +129,7 @@ func (this *DingDingNotifier) Notify(evalContext *alerting.EvalContext) error {
return err
}
cmd := &m.SendWebhookSync{
cmd := &models.SendWebhookSync{
Url: this.Url,
Body: string(body),
}
@@ -4,7 +4,7 @@ import (
"testing"
"github.com/grafana/grafana/pkg/components/simplejson"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
. "github.com/smartystreets/goconvey/convey"
)
@@ -14,7 +14,7 @@ func TestDingDingNotifier(t *testing.T) {
json := `{ }`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{
model := &models.AlertNotification{
Name: "dingding_testing",
Type: "dingding",
Settings: settingsJSON,
@@ -28,7 +28,7 @@ func TestDingDingNotifier(t *testing.T) {
json := `{ "url": "https://www.google.com" }`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{
model := &models.AlertNotification{
Name: "dingding_testing",
Type: "dingding",
Settings: settingsJSON,
+4 -4
View File
@@ -11,7 +11,7 @@ import (
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/infra/log"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/alerting"
"github.com/grafana/grafana/pkg/setting"
)
@@ -32,7 +32,7 @@ func init() {
})
}
func NewDiscordNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
func NewDiscordNotifier(model *models.AlertNotification) (alerting.Notifier, error) {
url := model.Settings.Get("url").MustString()
if url == "" {
return nil, alerting.ValidationError{Reason: "Could not find webhook url property in settings"}
@@ -111,7 +111,7 @@ func (this *DiscordNotifier) Notify(evalContext *alerting.EvalContext) error {
json, _ := bodyJSON.MarshalJSON()
cmd := &m.SendWebhookSync{
cmd := &models.SendWebhookSync{
Url: this.WebhookURL,
HttpMethod: "POST",
ContentType: "application/json",
@@ -135,7 +135,7 @@ func (this *DiscordNotifier) Notify(evalContext *alerting.EvalContext) error {
return nil
}
func (this *DiscordNotifier) embedImage(cmd *m.SendWebhookSync, imagePath string, existingJSONBody []byte) error {
func (this *DiscordNotifier) embedImage(cmd *models.SendWebhookSync, imagePath string, existingJSONBody []byte) error {
f, err := os.Open(imagePath)
defer f.Close()
if err != nil {
@@ -4,7 +4,7 @@ import (
"testing"
"github.com/grafana/grafana/pkg/components/simplejson"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
. "github.com/smartystreets/goconvey/convey"
)
@@ -16,7 +16,7 @@ func TestDiscordNotifier(t *testing.T) {
json := `{ }`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{
model := &models.AlertNotification{
Name: "discord_testing",
Type: "discord",
Settings: settingsJSON,
@@ -33,7 +33,7 @@ func TestDiscordNotifier(t *testing.T) {
}`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{
model := &models.AlertNotification{
Name: "discord_testing",
Type: "discord",
Settings: settingsJSON,
+5 -4
View File
@@ -6,7 +6,8 @@ import (
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/infra/log"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/alerting"
"github.com/grafana/grafana/pkg/setting"
)
@@ -35,7 +36,7 @@ type EmailNotifier struct {
log log.Logger
}
func NewEmailNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
func NewEmailNotifier(model *models.AlertNotification) (alerting.Notifier, error) {
addressesString := model.Settings.Get("addresses").MustString()
if addressesString == "" {
@@ -72,8 +73,8 @@ func (this *EmailNotifier) Notify(evalContext *alerting.EvalContext) error {
error = evalContext.Error.Error()
}
cmd := &m.SendEmailCommandSync{
SendEmailCommand: m.SendEmailCommand{
cmd := &models.SendEmailCommandSync{
SendEmailCommand: models.SendEmailCommand{
Subject: evalContext.GetNotificationTitle(),
Data: map[string]interface{}{
"Title": evalContext.GetNotificationTitle(),
@@ -4,7 +4,7 @@ import (
"testing"
"github.com/grafana/grafana/pkg/components/simplejson"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
. "github.com/smartystreets/goconvey/convey"
)
@@ -16,7 +16,7 @@ func TestEmailNotifier(t *testing.T) {
json := `{ }`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{
model := &models.AlertNotification{
Name: "ops",
Type: "email",
Settings: settingsJSON,
@@ -33,7 +33,7 @@ func TestEmailNotifier(t *testing.T) {
}`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{
model := &models.AlertNotification{
Name: "ops",
Type: "email",
Settings: settingsJSON,
@@ -57,7 +57,7 @@ func TestEmailNotifier(t *testing.T) {
settingsJSON, err := simplejson.NewJson([]byte(json))
So(err, ShouldBeNil)
model := &m.AlertNotification{
model := &models.AlertNotification{
Name: "ops",
Type: "email",
Settings: settingsJSON,
@@ -7,7 +7,7 @@ import (
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/infra/log"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/alerting"
"github.com/grafana/grafana/pkg/setting"
)
@@ -29,7 +29,7 @@ func init() {
})
}
func NewGoogleChatNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
func NewGoogleChatNotifier(model *models.AlertNotification) (alerting.Notifier, error) {
url := model.Settings.Get("url").MustString()
if url == "" {
return nil, alerting.ValidationError{Reason: "Could not find url property in settings"}
@@ -199,7 +199,7 @@ func (this *GoogleChatNotifier) Notify(evalContext *alerting.EvalContext) error
}
body, _ := json.Marshal(res1D)
cmd := &m.SendWebhookSync{
cmd := &models.SendWebhookSync{
Url: this.Url,
HttpMethod: "POST",
HttpHeader: headers,
@@ -4,7 +4,7 @@ import (
"testing"
"github.com/grafana/grafana/pkg/components/simplejson"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
. "github.com/smartystreets/goconvey/convey"
)
@@ -16,7 +16,7 @@ func TestGoogleChatNotifier(t *testing.T) {
json := `{ }`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{
model := &models.AlertNotification{
Name: "ops",
Type: "googlechat",
Settings: settingsJSON,
@@ -33,7 +33,7 @@ func TestGoogleChatNotifier(t *testing.T) {
}`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{
model := &models.AlertNotification{
Name: "ops",
Type: "googlechat",
Settings: settingsJSON,
@@ -4,7 +4,7 @@ import (
"testing"
"github.com/grafana/grafana/pkg/components/simplejson"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
. "github.com/smartystreets/goconvey/convey"
)
@@ -16,7 +16,7 @@ func TestHipChatNotifier(t *testing.T) {
json := `{ }`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{
model := &models.AlertNotification{
Name: "ops",
Type: "hipchat",
Settings: settingsJSON,
@@ -33,7 +33,7 @@ func TestHipChatNotifier(t *testing.T) {
}`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{
model := &models.AlertNotification{
Name: "ops",
Type: "hipchat",
Settings: settingsJSON,
@@ -59,7 +59,7 @@ func TestHipChatNotifier(t *testing.T) {
}`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{
model := &models.AlertNotification{
Name: "ops",
Type: "hipchat",
Settings: settingsJSON,
+3 -3
View File
@@ -8,7 +8,7 @@ import (
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/infra/log"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/alerting"
)
@@ -32,7 +32,7 @@ func init() {
})
}
func NewKafkaNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
func NewKafkaNotifier(model *models.AlertNotification) (alerting.Notifier, error) {
endpoint := model.Settings.Get("kafkaRestProxy").MustString()
if endpoint == "" {
return nil, alerting.ValidationError{Reason: "Could not find kafka rest proxy endpoint property in settings"}
@@ -101,7 +101,7 @@ func (this *KafkaNotifier) Notify(evalContext *alerting.EvalContext) error {
topicUrl := this.Endpoint + "/topics/" + this.Topic
cmd := &m.SendWebhookSync{
cmd := &models.SendWebhookSync{
Url: topicUrl,
Body: string(body),
HttpMethod: "POST",
@@ -4,7 +4,7 @@ import (
"testing"
"github.com/grafana/grafana/pkg/components/simplejson"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
. "github.com/smartystreets/goconvey/convey"
)
@@ -16,7 +16,7 @@ func TestKafkaNotifier(t *testing.T) {
json := `{ }`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{
model := &models.AlertNotification{
Name: "kafka_testing",
Type: "kafka",
Settings: settingsJSON,
@@ -34,7 +34,7 @@ func TestKafkaNotifier(t *testing.T) {
}`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{
model := &models.AlertNotification{
Name: "kafka_testing",
Type: "kafka",
Settings: settingsJSON,
+4 -4
View File
@@ -6,7 +6,7 @@ import (
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/infra/log"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/alerting"
)
@@ -32,7 +32,7 @@ const (
lineNotifyUrl string = "https://notify-api.line.me/api/notify"
)
func NewLINENotifier(model *m.AlertNotification) (alerting.Notifier, error) {
func NewLINENotifier(model *models.AlertNotification) (alerting.Notifier, error) {
token := model.Settings.Get("token").MustString()
if token == "" {
return nil, alerting.ValidationError{Reason: "Could not find token in settings"}
@@ -56,7 +56,7 @@ func (this *LineNotifier) Notify(evalContext *alerting.EvalContext) error {
var err error
switch evalContext.Rule.State {
case m.AlertStateAlerting:
case models.AlertStateAlerting:
err = this.createAlert(evalContext)
}
return err
@@ -79,7 +79,7 @@ func (this *LineNotifier) createAlert(evalContext *alerting.EvalContext) error {
form.Add("imageFullsize", evalContext.ImagePublicUrl)
}
cmd := &m.SendWebhookSync{
cmd := &models.SendWebhookSync{
Url: lineNotifyUrl,
HttpMethod: "POST",
HttpHeader: map[string]string{
+3 -3
View File
@@ -4,7 +4,7 @@ import (
"testing"
"github.com/grafana/grafana/pkg/components/simplejson"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
. "github.com/smartystreets/goconvey/convey"
)
@@ -14,7 +14,7 @@ func TestLineNotifier(t *testing.T) {
json := `{ }`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{
model := &models.AlertNotification{
Name: "line_testing",
Type: "line",
Settings: settingsJSON,
@@ -30,7 +30,7 @@ func TestLineNotifier(t *testing.T) {
"token": "abcdefgh0123456789"
}`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{
model := &models.AlertNotification{
Name: "line_testing",
Type: "line",
Settings: settingsJSON,
+6 -6
View File
@@ -7,7 +7,7 @@ import (
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/infra/log"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/alerting"
)
@@ -44,7 +44,7 @@ var (
opsgenieAlertURL = "https://api.opsgenie.com/v2/alerts"
)
func NewOpsGenieNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
func NewOpsGenieNotifier(model *models.AlertNotification) (alerting.Notifier, error) {
autoClose := model.Settings.Get("autoClose").MustBool(true)
apiKey := model.Settings.Get("apiKey").MustString()
apiUrl := model.Settings.Get("apiUrl").MustString()
@@ -76,11 +76,11 @@ func (this *OpsGenieNotifier) Notify(evalContext *alerting.EvalContext) error {
var err error
switch evalContext.Rule.State {
case m.AlertStateOK:
case models.AlertStateOK:
if this.AutoClose {
err = this.closeAlert(evalContext)
}
case m.AlertStateAlerting:
case models.AlertStateAlerting:
err = this.createAlert(evalContext)
}
return err
@@ -115,7 +115,7 @@ func (this *OpsGenieNotifier) createAlert(evalContext *alerting.EvalContext) err
bodyJSON.Set("details", details)
body, _ := bodyJSON.MarshalJSON()
cmd := &m.SendWebhookSync{
cmd := &models.SendWebhookSync{
Url: this.ApiUrl,
Body: string(body),
HttpMethod: "POST",
@@ -139,7 +139,7 @@ func (this *OpsGenieNotifier) closeAlert(evalContext *alerting.EvalContext) erro
bodyJSON.Set("source", "Grafana")
body, _ := bodyJSON.MarshalJSON()
cmd := &m.SendWebhookSync{
cmd := &models.SendWebhookSync{
Url: fmt.Sprintf("%s/alertId-%d/close?identifierType=alias", this.ApiUrl, evalContext.Rule.Id),
Body: string(body),
HttpMethod: "POST",
@@ -4,7 +4,7 @@ import (
"testing"
"github.com/grafana/grafana/pkg/components/simplejson"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
. "github.com/smartystreets/goconvey/convey"
)
@@ -16,7 +16,7 @@ func TestOpsGenieNotifier(t *testing.T) {
json := `{ }`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{
model := &models.AlertNotification{
Name: "opsgenie_testing",
Type: "opsgenie",
Settings: settingsJSON,
@@ -33,7 +33,7 @@ func TestOpsGenieNotifier(t *testing.T) {
}`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{
model := &models.AlertNotification{
Name: "opsgenie_testing",
Type: "opsgenie",
Settings: settingsJSON,
+5 -5
View File
@@ -10,7 +10,7 @@ import (
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/infra/log"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/alerting"
)
@@ -43,7 +43,7 @@ var (
pagerdutyEventApiUrl = "https://events.pagerduty.com/v2/enqueue"
)
func NewPagerdutyNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
func NewPagerdutyNotifier(model *models.AlertNotification) (alerting.Notifier, error) {
autoResolve := model.Settings.Get("autoResolve").MustBool(false)
key := model.Settings.Get("integrationKey").MustString()
if key == "" {
@@ -67,13 +67,13 @@ type PagerdutyNotifier struct {
func (this *PagerdutyNotifier) Notify(evalContext *alerting.EvalContext) error {
if evalContext.Rule.State == m.AlertStateOK && !this.AutoResolve {
if evalContext.Rule.State == models.AlertStateOK && !this.AutoResolve {
this.log.Info("Not sending a trigger to Pagerduty", "state", evalContext.Rule.State, "auto resolve", this.AutoResolve)
return nil
}
eventType := "trigger"
if evalContext.Rule.State == m.AlertStateOK {
if evalContext.Rule.State == models.AlertStateOK {
eventType = "resolve"
}
customData := triggMetrString
@@ -122,7 +122,7 @@ func (this *PagerdutyNotifier) Notify(evalContext *alerting.EvalContext) error {
body, _ := bodyJSON.MarshalJSON()
cmd := &m.SendWebhookSync{
cmd := &models.SendWebhookSync{
Url: pagerdutyEventApiUrl,
Body: string(body),
HttpMethod: "POST",
@@ -4,7 +4,7 @@ import (
"testing"
"github.com/grafana/grafana/pkg/components/simplejson"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
. "github.com/smartystreets/goconvey/convey"
)
@@ -15,7 +15,7 @@ func TestPagerdutyNotifier(t *testing.T) {
json := `{ }`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{
model := &models.AlertNotification{
Name: "pageduty_testing",
Type: "pagerduty",
Settings: settingsJSON,
@@ -29,7 +29,7 @@ func TestPagerdutyNotifier(t *testing.T) {
json := `{ "integrationKey": "abcdefgh0123456789" }`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{
model := &models.AlertNotification{
Name: "pagerduty_testing",
Type: "pagerduty",
Settings: settingsJSON,
@@ -53,7 +53,7 @@ func TestPagerdutyNotifier(t *testing.T) {
}`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{
model := &models.AlertNotification{
Name: "pagerduty_testing",
Type: "pagerduty",
Settings: settingsJSON,
+4 -4
View File
@@ -10,7 +10,7 @@ import (
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/infra/log"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/alerting"
)
@@ -95,7 +95,7 @@ func init() {
})
}
func NewPushoverNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
func NewPushoverNotifier(model *models.AlertNotification) (alerting.Notifier, error) {
userKey := model.Settings.Get("userKey").MustString()
apiToken := model.Settings.Get("apiToken").MustString()
device := model.Settings.Get("device").MustString()
@@ -169,7 +169,7 @@ func (this *PushoverNotifier) Notify(evalContext *alerting.EvalContext) error {
return err
}
cmd := &m.SendWebhookSync{
cmd := &models.SendWebhookSync{
Url: PUSHOVER_ENDPOINT,
HttpMethod: "POST",
HttpHeader: headers,
@@ -248,7 +248,7 @@ func (this *PushoverNotifier) genPushoverBody(evalContext *alerting.EvalContext,
// Add sound
sound := this.AlertingSound
if evalContext.Rule.State == m.AlertStateOK {
if evalContext.Rule.State == models.AlertStateOK {
sound = this.OkSound
}
if sound != "default" {
@@ -2,12 +2,13 @@ package notifiers
import (
"context"
"github.com/grafana/grafana/pkg/services/alerting"
"strings"
"testing"
"github.com/grafana/grafana/pkg/services/alerting"
"github.com/grafana/grafana/pkg/components/simplejson"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
. "github.com/smartystreets/goconvey/convey"
)
@@ -19,7 +20,7 @@ func TestPushoverNotifier(t *testing.T) {
json := `{ }`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{
model := &models.AlertNotification{
Name: "Pushover",
Type: "pushover",
Settings: settingsJSON,
@@ -40,7 +41,7 @@ func TestPushoverNotifier(t *testing.T) {
}`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{
model := &models.AlertNotification{
Name: "Pushover",
Type: "pushover",
Settings: settingsJSON,
@@ -73,7 +74,7 @@ func TestGenPushoverBody(t *testing.T) {
Convey("When alert is firing - should use siren sound", func() {
evalContext := alerting.NewEvalContext(context.Background(),
&alerting.Rule{
State: m.AlertStateAlerting,
State: models.AlertStateAlerting,
})
_, pushoverBody, err := notifier.genPushoverBody(evalContext, "", "")
@@ -84,7 +85,7 @@ func TestGenPushoverBody(t *testing.T) {
Convey("When alert is ok - should use success sound", func() {
evalContext := alerting.NewEvalContext(context.Background(),
&alerting.Rule{
State: m.AlertStateOK,
State: models.AlertStateOK,
})
_, pushoverBody, err := notifier.genPushoverBody(evalContext, "", "")
+3 -3
View File
@@ -7,7 +7,7 @@ import (
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/infra/log"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/alerting"
)
@@ -44,7 +44,7 @@ func init() {
}
func NewSensuNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
func NewSensuNotifier(model *models.AlertNotification) (alerting.Notifier, error) {
url := model.Settings.Get("url").MustString()
if url == "" {
return nil, alerting.ValidationError{Reason: "Could not find url property in settings"}
@@ -117,7 +117,7 @@ func (this *SensuNotifier) Notify(evalContext *alerting.EvalContext) error {
body, _ := bodyJSON.MarshalJSON()
cmd := &m.SendWebhookSync{
cmd := &models.SendWebhookSync{
Url: this.Url,
User: this.User,
Password: this.Password,
@@ -4,7 +4,7 @@ import (
"testing"
"github.com/grafana/grafana/pkg/components/simplejson"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
. "github.com/smartystreets/goconvey/convey"
)
@@ -16,7 +16,7 @@ func TestSensuNotifier(t *testing.T) {
json := `{ }`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{
model := &models.AlertNotification{
Name: "sensu",
Type: "sensu",
Settings: settingsJSON,
@@ -35,7 +35,7 @@ func TestSensuNotifier(t *testing.T) {
}`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{
model := &models.AlertNotification{
Name: "sensu",
Type: "sensu",
Settings: settingsJSON,
+5 -5
View File
@@ -11,7 +11,7 @@ import (
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/infra/log"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/alerting"
"github.com/grafana/grafana/pkg/setting"
)
@@ -99,7 +99,7 @@ func init() {
}
func NewSlackNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
func NewSlackNotifier(model *models.AlertNotification) (alerting.Notifier, error) {
url := model.Settings.Get("url").MustString()
if url == "" {
return nil, alerting.ValidationError{Reason: "Could not find url property in settings"}
@@ -171,7 +171,7 @@ func (this *SlackNotifier) Notify(evalContext *alerting.EvalContext) error {
}
message := this.Mention
if evalContext.Rule.State != m.AlertStateOK { //don't add message when going back to alert state ok.
if evalContext.Rule.State != models.AlertStateOK { //don't add message when going back to alert state ok.
message += " " + evalContext.Rule.Message
}
image_url := ""
@@ -212,7 +212,7 @@ func (this *SlackNotifier) Notify(evalContext *alerting.EvalContext) error {
body["icon_url"] = this.IconUrl
}
data, _ := json.Marshal(&body)
cmd := &m.SendWebhookSync{Url: this.Url, Body: string(data)}
cmd := &models.SendWebhookSync{Url: this.Url, Body: string(data)}
if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
this.log.Error("Failed to send slack notification", "error", err, "webhook", this.Name)
return err
@@ -235,7 +235,7 @@ func SlackFileUpload(evalContext *alerting.EvalContext, log log.Logger, url stri
if err != nil {
return err
}
cmd := &m.SendWebhookSync{Url: url, Body: uploadBody.String(), HttpHeader: headers, HttpMethod: "POST"}
cmd := &models.SendWebhookSync{Url: url, Body: uploadBody.String(), HttpHeader: headers, HttpMethod: "POST"}
if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
log.Error("Failed to upload slack image", "error", err, "webhook", "file.upload")
return err
@@ -4,7 +4,7 @@ import (
"testing"
"github.com/grafana/grafana/pkg/components/simplejson"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
. "github.com/smartystreets/goconvey/convey"
)
@@ -16,7 +16,7 @@ func TestSlackNotifier(t *testing.T) {
json := `{ }`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{
model := &models.AlertNotification{
Name: "ops",
Type: "slack",
Settings: settingsJSON,
@@ -33,7 +33,7 @@ func TestSlackNotifier(t *testing.T) {
}`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{
model := &models.AlertNotification{
Name: "ops",
Type: "slack",
Settings: settingsJSON,
@@ -67,7 +67,7 @@ func TestSlackNotifier(t *testing.T) {
}`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{
model := &models.AlertNotification{
Name: "ops",
Type: "slack",
Settings: settingsJSON,
+4 -4
View File
@@ -5,7 +5,7 @@ import (
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/infra/log"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/alerting"
)
@@ -26,7 +26,7 @@ func init() {
}
func NewTeamsNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
func NewTeamsNotifier(model *models.AlertNotification) (alerting.Notifier, error) {
url := model.Settings.Get("url").MustString()
if url == "" {
return nil, alerting.ValidationError{Reason: "Could not find url property in settings"}
@@ -74,7 +74,7 @@ func (this *TeamsNotifier) Notify(evalContext *alerting.EvalContext) error {
}
message := ""
if evalContext.Rule.State != m.AlertStateOK { //don't add message when going back to alert state ok.
if evalContext.Rule.State != models.AlertStateOK { //don't add message when going back to alert state ok.
message = evalContext.Rule.Message
}
@@ -126,7 +126,7 @@ func (this *TeamsNotifier) Notify(evalContext *alerting.EvalContext) error {
}
data, _ := json.Marshal(&body)
cmd := &m.SendWebhookSync{Url: this.Url, Body: string(data)}
cmd := &models.SendWebhookSync{Url: this.Url, Body: string(data)}
if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
this.log.Error("Failed to send teams notification", "error", err, "webhook", this.Name)
@@ -4,7 +4,7 @@ import (
"testing"
"github.com/grafana/grafana/pkg/components/simplejson"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
. "github.com/smartystreets/goconvey/convey"
)
@@ -16,7 +16,7 @@ func TestTeamsNotifier(t *testing.T) {
json := `{ }`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{
model := &models.AlertNotification{
Name: "ops",
Type: "teams",
Settings: settingsJSON,
@@ -33,7 +33,7 @@ func TestTeamsNotifier(t *testing.T) {
}`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{
model := &models.AlertNotification{
Name: "ops",
Type: "teams",
Settings: settingsJSON,
@@ -55,7 +55,7 @@ func TestTeamsNotifier(t *testing.T) {
}`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{
model := &models.AlertNotification{
Name: "ops",
Type: "teams",
Settings: settingsJSON,
+8 -8
View File
@@ -9,7 +9,7 @@ import (
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/infra/log"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/alerting"
)
@@ -60,7 +60,7 @@ type TelegramNotifier struct {
log log.Logger
}
func NewTelegramNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
func NewTelegramNotifier(model *models.AlertNotification) (alerting.Notifier, error) {
if model.Settings == nil {
return nil, alerting.ValidationError{Reason: "No Settings Supplied"}
}
@@ -86,7 +86,7 @@ func NewTelegramNotifier(model *m.AlertNotification) (alerting.Notifier, error)
}, nil
}
func (this *TelegramNotifier) buildMessage(evalContext *alerting.EvalContext, sendImageInline bool) *m.SendWebhookSync {
func (this *TelegramNotifier) buildMessage(evalContext *alerting.EvalContext, sendImageInline bool) *models.SendWebhookSync {
if sendImageInline {
cmd, err := this.buildMessageInlineImage(evalContext)
if err == nil {
@@ -98,7 +98,7 @@ func (this *TelegramNotifier) buildMessage(evalContext *alerting.EvalContext, se
return this.buildMessageLinkedImage(evalContext)
}
func (this *TelegramNotifier) buildMessageLinkedImage(evalContext *alerting.EvalContext) *m.SendWebhookSync {
func (this *TelegramNotifier) buildMessageLinkedImage(evalContext *alerting.EvalContext) *models.SendWebhookSync {
message := fmt.Sprintf("<b>%s</b>\nState: %s\nMessage: %s\n", evalContext.GetNotificationTitle(), evalContext.Rule.Name, evalContext.Rule.Message)
ruleUrl, err := evalContext.GetRuleUrl()
@@ -122,7 +122,7 @@ func (this *TelegramNotifier) buildMessageLinkedImage(evalContext *alerting.Eval
return cmd
}
func (this *TelegramNotifier) buildMessageInlineImage(evalContext *alerting.EvalContext) (*m.SendWebhookSync, error) {
func (this *TelegramNotifier) buildMessageInlineImage(evalContext *alerting.EvalContext) (*models.SendWebhookSync, error) {
var imageFile *os.File
var err error
@@ -153,7 +153,7 @@ func (this *TelegramNotifier) buildMessageInlineImage(evalContext *alerting.Eval
return cmd, nil
}
func (this *TelegramNotifier) generateTelegramCmd(message string, messageField string, apiAction string, extraConf func(writer *multipart.Writer)) *m.SendWebhookSync {
func (this *TelegramNotifier) generateTelegramCmd(message string, messageField string, apiAction string, extraConf func(writer *multipart.Writer)) *models.SendWebhookSync {
var body bytes.Buffer
w := multipart.NewWriter(&body)
@@ -170,7 +170,7 @@ func (this *TelegramNotifier) generateTelegramCmd(message string, messageField s
this.log.Info("Sending telegram notification", "chat_id", this.ChatID, "bot_token", this.BotToken, "apiAction", apiAction)
url := fmt.Sprintf(telegramApiUrl, this.BotToken, apiAction)
cmd := &m.SendWebhookSync{
cmd := &models.SendWebhookSync{
Url: url,
Body: body.String(),
HttpMethod: "POST",
@@ -227,7 +227,7 @@ func appendIfPossible(message string, extra string, sizeLimit int) string {
}
func (this *TelegramNotifier) Notify(evalContext *alerting.EvalContext) error {
var cmd *m.SendWebhookSync
var cmd *models.SendWebhookSync
if evalContext.ImagePublicUrl == "" && this.UploadImage {
cmd = this.buildMessage(evalContext, true)
} else {
@@ -5,7 +5,7 @@ import (
"testing"
"github.com/grafana/grafana/pkg/components/simplejson"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/alerting"
. "github.com/smartystreets/goconvey/convey"
)
@@ -18,7 +18,7 @@ func TestTelegramNotifier(t *testing.T) {
json := `{ }`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{
model := &models.AlertNotification{
Name: "telegram_testing",
Type: "telegram",
Settings: settingsJSON,
@@ -36,7 +36,7 @@ func TestTelegramNotifier(t *testing.T) {
}`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{
model := &models.AlertNotification{
Name: "telegram_testing",
Type: "telegram",
Settings: settingsJSON,
@@ -57,7 +57,7 @@ func TestTelegramNotifier(t *testing.T) {
&alerting.Rule{
Name: "This is an alarm",
Message: "Some kind of message.",
State: m.AlertStateOK,
State: models.AlertStateOK,
})
caption := generateImageCaption(evalContext, "http://grafa.url/abcdef", "")
@@ -74,7 +74,7 @@ func TestTelegramNotifier(t *testing.T) {
&alerting.Rule{
Name: "This is an alarm",
Message: "Some kind of message.",
State: m.AlertStateOK,
State: models.AlertStateOK,
})
caption := generateImageCaption(evalContext,
@@ -92,7 +92,7 @@ func TestTelegramNotifier(t *testing.T) {
&alerting.Rule{
Name: "This is an alarm",
Message: "Some kind of message that is too long for appending to our pretty little message, this line is actually exactly 197 chars long and I will get there in the end I promise I will. Yes siree that's it. But suddenly Telegram increased the length so now we need some lorem ipsum to fix this test. Here we go: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus consectetur molestie cursus. Donec suscipit egestas nisi. Proin ut efficitur ex. Mauris mi augue, volutpat a nisi vel, euismod dictum arcu. Sed quis tempor eros, sed malesuada dolor. Ut orci augue, viverra sit amet blandit quis, faucibus sit amet ex. Duis condimentum efficitur lectus, id dignissim quam tempor id. Morbi sollicitudin rhoncus diam, id tincidunt lectus scelerisque vitae. Etiam imperdiet semper sem, vel eleifend ligula mollis eget. Etiam ultrices fringilla lacus, sit amet pharetra ex blandit quis. Suspendisse in egestas neque, et posuere lectus. Vestibulum eu ex dui. Sed molestie nulla a lobortis scelerisque. Nulla ipsum ex, iaculis vitae vehicula sit amet, fermentum eu eros.",
State: m.AlertStateOK,
State: models.AlertStateOK,
})
caption := generateImageCaption(evalContext,
@@ -109,7 +109,7 @@ func TestTelegramNotifier(t *testing.T) {
&alerting.Rule{
Name: "This is an alarm",
Message: "Some kind of message that is too long for appending to our pretty little message, this line is actually exactly 197 chars long and I will get there in the end I promise I will. Yes siree that's it. But suddenly Telegram increased the length so now we need some lorem ipsum to fix this test. Here we go: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus consectetur molestie cursus. Donec suscipit egestas nisi. Proin ut efficitur ex. Mauris mi augue, volutpat a nisi vel, euismod dictum arcu. Sed quis tempor eros, sed malesuada dolor. Ut orci augue, viverra sit amet blandit quis, faucibus sit amet ex. Duis condimentum efficitur lectus, id dignissim quam tempor id. Morbi sollicitudin rhoncus diam, id tincidunt lectus scelerisque vitae. Etiam imperdiet semper sem, vel eleifend ligula mollis eget. Etiam ultrices fringilla lacus, sit amet pharetra ex blandit quis. Suspendisse in egestas neque, et posuere lectus. Vestibulum eu ex dui. Sed molestie nulla a lobortis sceleri",
State: m.AlertStateOK,
State: models.AlertStateOK,
})
caption := generateImageCaption(evalContext,
+6 -6
View File
@@ -7,7 +7,7 @@ import (
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/infra/log"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/alerting"
)
@@ -76,7 +76,7 @@ type ThreemaNotifier struct {
log log.Logger
}
func NewThreemaNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
func NewThreemaNotifier(model *models.AlertNotification) (alerting.Notifier, error) {
if model.Settings == nil {
return nil, alerting.ValidationError{Reason: "No Settings Supplied"}
}
@@ -127,11 +127,11 @@ func (notifier *ThreemaNotifier) Notify(evalContext *alerting.EvalContext) error
// Determine emoji
stateEmoji := ""
switch evalContext.Rule.State {
case m.AlertStateOK:
case models.AlertStateOK:
stateEmoji = "\u2705 " // White Heavy Check Mark
case m.AlertStateNoData:
case models.AlertStateNoData:
stateEmoji = "\u2753 " // Black Question Mark Ornament
case m.AlertStateAlerting:
case models.AlertStateAlerting:
stateEmoji = "\u26A0 " // Warning sign
}
@@ -154,7 +154,7 @@ func (notifier *ThreemaNotifier) Notify(evalContext *alerting.EvalContext) error
headers := map[string]string{
"Content-Type": "application/x-www-form-urlencoded",
}
cmd := &m.SendWebhookSync{
cmd := &models.SendWebhookSync{
Url: url,
Body: body,
HttpMethod: "POST",
@@ -4,7 +4,7 @@ import (
"testing"
"github.com/grafana/grafana/pkg/components/simplejson"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/alerting"
. "github.com/smartystreets/goconvey/convey"
)
@@ -17,7 +17,7 @@ func TestThreemaNotifier(t *testing.T) {
json := `{ }`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{
model := &models.AlertNotification{
Name: "threema_testing",
Type: "threema",
Settings: settingsJSON,
@@ -36,7 +36,7 @@ func TestThreemaNotifier(t *testing.T) {
}`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{
model := &models.AlertNotification{
Name: "threema_testing",
Type: "threema",
Settings: settingsJSON,
@@ -63,7 +63,7 @@ func TestThreemaNotifier(t *testing.T) {
}`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{
model := &models.AlertNotification{
Name: "threema_testing",
Type: "threema",
Settings: settingsJSON,
@@ -83,7 +83,7 @@ func TestThreemaNotifier(t *testing.T) {
}`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{
model := &models.AlertNotification{
Name: "threema_testing",
Type: "threema",
Settings: settingsJSON,
@@ -103,7 +103,7 @@ func TestThreemaNotifier(t *testing.T) {
}`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{
model := &models.AlertNotification{
Name: "threema_testing",
Type: "threema",
Settings: settingsJSON,
@@ -4,7 +4,7 @@ import (
"testing"
"github.com/grafana/grafana/pkg/components/simplejson"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
. "github.com/smartystreets/goconvey/convey"
)
@@ -16,7 +16,7 @@ func TestVictoropsNotifier(t *testing.T) {
json := `{ }`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{
model := &models.AlertNotification{
Name: "victorops_testing",
Type: "victorops",
Settings: settingsJSON,
@@ -33,7 +33,7 @@ func TestVictoropsNotifier(t *testing.T) {
}`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{
model := &models.AlertNotification{
Name: "victorops_testing",
Type: "victorops",
Settings: settingsJSON,
+3 -3
View File
@@ -4,7 +4,7 @@ import (
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/infra/log"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/alerting"
)
@@ -40,7 +40,7 @@ func init() {
}
func NewWebHookNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
func NewWebHookNotifier(model *models.AlertNotification) (alerting.Notifier, error) {
url := model.Settings.Get("url").MustString()
if url == "" {
return nil, alerting.ValidationError{Reason: "Could not find url property in settings"}
@@ -90,7 +90,7 @@ func (this *WebhookNotifier) Notify(evalContext *alerting.EvalContext) error {
body, _ := bodyJSON.MarshalJSON()
cmd := &m.SendWebhookSync{
cmd := &models.SendWebhookSync{
Url: this.Url,
User: this.User,
Password: this.Password,
@@ -4,7 +4,7 @@ import (
"testing"
"github.com/grafana/grafana/pkg/components/simplejson"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
. "github.com/smartystreets/goconvey/convey"
)
@@ -16,7 +16,7 @@ func TestWebhookNotifier(t *testing.T) {
json := `{ }`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{
model := &models.AlertNotification{
Name: "ops",
Type: "webhook",
Settings: settingsJSON,
@@ -33,7 +33,7 @@ func TestWebhookNotifier(t *testing.T) {
}`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &m.AlertNotification{
model := &models.AlertNotification{
Name: "ops",
Type: "webhook",
Settings: settingsJSON,
+2 -3
View File
@@ -7,7 +7,7 @@ import (
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/metrics"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
)
type RuleReader interface {
@@ -16,7 +16,6 @@ type RuleReader interface {
type DefaultRuleReader struct {
sync.RWMutex
//serverID string
serverPosition int
clusterSize int
log log.Logger
@@ -40,7 +39,7 @@ func (arr *DefaultRuleReader) initReader() {
}
func (arr *DefaultRuleReader) Fetch() []*Rule {
cmd := &m.GetAllAlertsQuery{}
cmd := &models.GetAllAlertsQuery{}
if err := bus.Dispatch(cmd); err != nil {
arr.log.Error("Could not load alerts", "error", err)
+5 -4
View File
@@ -7,7 +7,8 @@ import (
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/metrics"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/annotations"
"github.com/grafana/grafana/pkg/services/rendering"
)
@@ -47,7 +48,7 @@ func (handler *DefaultResultHandler) Handle(evalContext *EvalContext) error {
if evalContext.ShouldUpdateAlertState() {
handler.log.Info("New state change", "alertId", evalContext.Rule.Id, "newState", evalContext.Rule.State, "prev state", evalContext.PrevAlertState)
cmd := &m.SetAlertStateCommand{
cmd := &models.SetAlertStateCommand{
AlertId: evalContext.Rule.Id,
OrgId: evalContext.Rule.OrgId,
State: evalContext.Rule.State,
@@ -56,12 +57,12 @@ func (handler *DefaultResultHandler) Handle(evalContext *EvalContext) error {
}
if err := bus.Dispatch(cmd); err != nil {
if err == m.ErrCannotChangeStateOnPausedAlert {
if err == models.ErrCannotChangeStateOnPausedAlert {
handler.log.Error("Cannot change state on alert that's paused", "error", err)
return err
}
if err == m.ErrRequiresNewState {
if err == models.ErrRequiresNewState {
handler.log.Info("Alert already updated")
return nil
}
+7 -7
View File
@@ -8,7 +8,7 @@ import (
"time"
"github.com/grafana/grafana/pkg/components/simplejson"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
)
var (
@@ -26,9 +26,9 @@ type Rule struct {
Message string
LastStateChange time.Time
For time.Duration
NoDataState m.NoDataOption
ExecutionErrorState m.ExecutionErrorOption
State m.AlertStateType
NoDataState models.NoDataOption
ExecutionErrorState models.ExecutionErrorOption
State models.AlertStateType
Conditions []Condition
Notifications []string
@@ -103,7 +103,7 @@ func getTimeDurationStringToSeconds(str string) (int64, error) {
return int64(value * multiplier), nil
}
func NewRuleFromDBAlert(ruleDef *m.Alert) (*Rule, error) {
func NewRuleFromDBAlert(ruleDef *models.Alert) (*Rule, error) {
model := &Rule{}
model.Id = ruleDef.Id
model.OrgId = ruleDef.OrgId
@@ -114,8 +114,8 @@ func NewRuleFromDBAlert(ruleDef *m.Alert) (*Rule, error) {
model.State = ruleDef.State
model.LastStateChange = ruleDef.NewStateDate
model.For = ruleDef.For
model.NoDataState = m.NoDataOption(ruleDef.Settings.Get("noDataState").MustString("no_data"))
model.ExecutionErrorState = m.ExecutionErrorOption(ruleDef.Settings.Get("executionErrorState").MustString("alerting"))
model.NoDataState = models.NoDataOption(ruleDef.Settings.Get("noDataState").MustString("no_data"))
model.ExecutionErrorState = models.ExecutionErrorOption(ruleDef.Settings.Get("executionErrorState").MustString("alerting"))
model.StateChanges = ruleDef.StateChanges
model.Frequency = ruleDef.Frequency
+6 -6
View File
@@ -4,7 +4,7 @@ import (
"testing"
"github.com/grafana/grafana/pkg/components/simplejson"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/sqlstore"
. "github.com/smartystreets/goconvey/convey"
)
@@ -60,10 +60,10 @@ func TestAlertRuleModel(t *testing.T) {
})
Convey("can construct alert rule model", func() {
firstNotification := m.CreateAlertNotificationCommand{OrgId: 1, Name: "1"}
firstNotification := models.CreateAlertNotificationCommand{OrgId: 1, Name: "1"}
err := sqlstore.CreateAlertNotificationCommand(&firstNotification)
So(err, ShouldBeNil)
secondNotification := m.CreateAlertNotificationCommand{Uid: "notifier2", OrgId: 1, Name: "2"}
secondNotification := models.CreateAlertNotificationCommand{Uid: "notifier2", OrgId: 1, Name: "2"}
err = sqlstore.CreateAlertNotificationCommand(&secondNotification)
So(err, ShouldBeNil)
@@ -92,7 +92,7 @@ func TestAlertRuleModel(t *testing.T) {
alertJSON, jsonErr := simplejson.NewJson([]byte(json))
So(jsonErr, ShouldBeNil)
alert := &m.Alert{
alert := &models.Alert{
Id: 1,
OrgId: 1,
DashboardId: 1,
@@ -129,7 +129,7 @@ func TestAlertRuleModel(t *testing.T) {
alertJSON, jsonErr := simplejson.NewJson([]byte(json))
So(jsonErr, ShouldBeNil)
alert := &m.Alert{
alert := &models.Alert{
Id: 1,
OrgId: 1,
DashboardId: 1,
@@ -167,7 +167,7 @@ func TestAlertRuleModel(t *testing.T) {
alertJSON, jsonErr := simplejson.NewJson([]byte(json))
So(jsonErr, ShouldBeNil)
alert := &m.Alert{
alert := &models.Alert{
Id: 1,
OrgId: 1,
DashboardId: 1,
+4 -4
View File
@@ -8,11 +8,11 @@ import (
"github.com/grafana/grafana/pkg/components/null"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/infra/log"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
)
type NotificationTestCommand struct {
State m.AlertStateType
State models.AlertStateType
Name string
Type string
Settings *simplejson.Json
@@ -29,7 +29,7 @@ func init() {
func handleNotificationTestCommand(cmd *NotificationTestCommand) error {
notifier := NewNotificationService(nil).(*notificationService)
model := &m.AlertNotification{
model := &models.AlertNotification{
Name: cmd.Name,
Type: cmd.Type,
Settings: cmd.Settings,
@@ -51,7 +51,7 @@ func createTestEvalContext(cmd *NotificationTestCommand) *EvalContext {
PanelId: 1,
Name: "Test notification",
Message: "Someone is testing the alert notification within grafana.",
State: m.AlertStateAlerting,
State: models.AlertStateAlerting,
}
ctx := NewEvalContext(context.Background(), testRule)
+3 -3
View File
@@ -6,14 +6,14 @@ import (
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/components/simplejson"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/models"
)
type AlertTestCommand struct {
Dashboard *simplejson.Json
PanelId int64
OrgId int64
User *m.SignedInUser
User *models.SignedInUser
Result *EvalContext
}
@@ -24,7 +24,7 @@ func init() {
func handleAlertTestCommand(cmd *AlertTestCommand) error {
dash := m.NewDashboardFromJson(cmd.Dashboard)
dash := models.NewDashboardFromJson(cmd.Dashboard)
extractor := NewDashAlertExtractor(dash, cmd.OrgId, cmd.User)
alerts, err := extractor.GetAlerts()