From f934081bcb306067758fd1ac46270a509dbac36a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Tue, 16 Aug 2016 09:52:45 +0200 Subject: [PATCH] feat(alerting): making progress on alerting list, #5784 --- pkg/api/alerting.go | 17 +++-- pkg/api/dtos/alerting.go | 20 +++--- pkg/api/index.go | 4 +- pkg/models/alert.go | 10 +-- pkg/services/sqlstore/alert.go | 6 +- pkg/services/sqlstore/migrations/alert_mig.go | 9 +-- .../sqlstore/migrations/annotation_mig.go | 1 + public/app/core/routes/routes.ts | 2 +- public/app/features/alerting/alert_def.ts | 33 +++++---- .../{alerts_ctrl.ts => alert_list_ctrl.ts} | 22 ++---- public/app/features/alerting/all.ts | 2 +- .../alerting/partials/alert_list.html | 72 ++++++++++--------- .../features/alerting/partials/alert_tab.html | 2 +- .../plugins/partials/plugin_list.html | 4 -- 14 files changed, 105 insertions(+), 99 deletions(-) rename public/app/features/alerting/{alerts_ctrl.ts => alert_list_ctrl.ts} (60%) diff --git a/pkg/api/alerting.go b/pkg/api/alerting.go index 8546a4e439f..c829c1ff71c 100644 --- a/pkg/api/alerting.go +++ b/pkg/api/alerting.go @@ -43,13 +43,16 @@ func GetAlerts(c *middleware.Context) Response { for _, alert := range query.Result { dashboardIds = append(dashboardIds, alert.DashboardId) alertDTOs = append(alertDTOs, &dtos.AlertRule{ - Id: alert.Id, - DashboardId: alert.DashboardId, - PanelId: alert.PanelId, - Name: alert.Name, - Message: alert.Message, - State: alert.State, - Severity: alert.Severity, + Id: alert.Id, + DashboardId: alert.DashboardId, + PanelId: alert.PanelId, + Name: alert.Name, + Message: alert.Message, + State: alert.State, + Severity: alert.Severity, + EvalDate: alert.EvalDate, + NewStateDate: alert.NewStateDate, + ExecutionError: alert.ExecutionError, }) } diff --git a/pkg/api/dtos/alerting.go b/pkg/api/dtos/alerting.go index ce8995eb2f6..bab4eb196ba 100644 --- a/pkg/api/dtos/alerting.go +++ b/pkg/api/dtos/alerting.go @@ -8,15 +8,17 @@ import ( ) type AlertRule struct { - Id int64 `json:"id"` - DashboardId int64 `json:"dashboardId"` - PanelId int64 `json:"panelId"` - Name string `json:"name"` - Message string `json:"message"` - State m.AlertStateType `json:"state"` - Severity m.AlertSeverityType `json:"severity"` - - DashbboardUri string `json:"dashboardUri"` + Id int64 `json:"id"` + DashboardId int64 `json:"dashboardId"` + PanelId int64 `json:"panelId"` + Name string `json:"name"` + Message string `json:"message"` + State m.AlertStateType `json:"state"` + Severity m.AlertSeverityType `json:"severity"` + NewStateDate time.Time `json:"newStateDate"` + EvalDate time.Time `json:"evalDate"` + ExecutionError string `json:"executionError"` + DashbboardUri string `json:"dashboardUri"` } type AlertNotification struct { diff --git a/pkg/api/index.go b/pkg/api/index.go index 056bf1f11e1..46dfc813259 100644 --- a/pkg/api/index.go +++ b/pkg/api/index.go @@ -93,14 +93,14 @@ func setIndexViewData(c *middleware.Context) (*dtos.IndexViewData, error) { if setting.AlertingEnabled && (c.OrgRole == m.ROLE_ADMIN || c.OrgRole == m.ROLE_EDITOR) { alertChildNavs := []*dtos.NavLink{ - {Text: "Home", Url: setting.AppSubUrl + "/alerting"}, + {Text: "Alert List", Url: setting.AppSubUrl + "/alerting/list"}, {Text: "Notifications", Url: setting.AppSubUrl + "/alerting/notifications"}, } data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{ Text: "Alerting", Icon: "icon-gf icon-gf-monitoring", - Url: setting.AppSubUrl + "/alerting", + Url: setting.AppSubUrl + "/alerting/list", Children: alertChildNavs, }) } diff --git a/pkg/models/alert.go b/pkg/models/alert.go index 8d7d338ca06..240f8509179 100644 --- a/pkg/models/alert.go +++ b/pkg/models/alert.go @@ -32,6 +32,7 @@ func (s AlertSeverityType) IsValid() bool { type Alert struct { Id int64 + Version int64 OrgId int64 DashboardId int64 PanelId int64 @@ -45,11 +46,10 @@ type Alert struct { ExecutionError string Frequency int64 - LastEvalData *simplejson.Json - LastEvalTime time.Time - - CreatedBy int64 - UpdatedBy int64 + EvalData *simplejson.Json + EvalDate time.Time + NewStateDate time.Time + StateChanges int Created time.Time Updated time.Time diff --git a/pkg/services/sqlstore/alert.go b/pkg/services/sqlstore/alert.go index 1a19b17aafa..d47e742c82d 100644 --- a/pkg/services/sqlstore/alert.go +++ b/pkg/services/sqlstore/alert.go @@ -161,8 +161,6 @@ func upsertAlerts(existingAlerts []*m.Alert, cmd *m.SaveAlertsCommand, sess *xor alert.Updated = time.Now() alert.Created = time.Now() alert.State = m.AlertStatePending - alert.CreatedBy = cmd.UserId - alert.UpdatedBy = cmd.UserId _, err := sess.Insert(alert) if err != nil { @@ -222,8 +220,10 @@ func SetAlertState(cmd *m.SetAlertStateCommand) error { } alert.State = cmd.State - sess.Id(alert.Id).Update(&alert) + alert.StateChanges += 1 + alert.NewStateDate = time.Now() + sess.Id(alert.Id).Update(&alert) return nil }) } diff --git a/pkg/services/sqlstore/migrations/alert_mig.go b/pkg/services/sqlstore/migrations/alert_mig.go index 342c2282933..62817e3f543 100644 --- a/pkg/services/sqlstore/migrations/alert_mig.go +++ b/pkg/services/sqlstore/migrations/alert_mig.go @@ -10,6 +10,7 @@ func addAlertMigrations(mg *Migrator) { Name: "alert", Columns: []*Column{ {Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true}, + {Name: "version", Type: DB_BigInt, Nullable: false}, {Name: "dashboard_id", Type: DB_BigInt, Nullable: false}, {Name: "panel_id", Type: DB_BigInt, Nullable: false}, {Name: "org_id", Type: DB_BigInt, Nullable: false}, @@ -23,12 +24,12 @@ func addAlertMigrations(mg *Migrator) { {Name: "paused", Type: DB_Bool, Nullable: false}, {Name: "silenced", Type: DB_Bool, Nullable: false}, {Name: "execution_error", Type: DB_Text, Nullable: false}, - {Name: "last_eval_data", Type: DB_Text, Nullable: false}, - {Name: "last_eval_time", Type: DB_DateTime, Nullable: false}, + {Name: "eval_data", Type: DB_Text, Nullable: true}, + {Name: "eval_date", Type: DB_DateTime, Nullable: true}, + {Name: "new_state_date", Type: DB_DateTime, Nullable: false}, + {Name: "state_changes", Type: DB_Int, Nullable: false}, {Name: "created", Type: DB_DateTime, Nullable: false}, {Name: "updated", Type: DB_DateTime, Nullable: false}, - {Name: "updated_by", Type: DB_BigInt, Nullable: false}, - {Name: "created_by", Type: DB_BigInt, Nullable: false}, }, Indices: []*Index{ {Cols: []string{"org_id", "id"}, Type: IndexType}, diff --git a/pkg/services/sqlstore/migrations/annotation_mig.go b/pkg/services/sqlstore/migrations/annotation_mig.go index af8d1cf0a03..11b4eeed629 100644 --- a/pkg/services/sqlstore/migrations/annotation_mig.go +++ b/pkg/services/sqlstore/migrations/annotation_mig.go @@ -32,6 +32,7 @@ func addAnnotationMig(mg *Migrator) { // create indices mg.AddMigration("add index annotation org_id & alert_id ", NewAddIndexMigration(table, table.Indices[0])) + mg.AddMigration("add index annotation org_id & type", NewAddIndexMigration(table, table.Indices[1])) mg.AddMigration("add index annotation timestamp", NewAddIndexMigration(table, table.Indices[2])) } diff --git a/public/app/core/routes/routes.ts b/public/app/core/routes/routes.ts index 547a22dfd7a..afe8dca2534 100644 --- a/public/app/core/routes/routes.ts +++ b/public/app/core/routes/routes.ts @@ -194,7 +194,7 @@ function setupAngularRoutes($routeProvider, $locationProvider) { controllerAs: 'ctrl', templateUrl: 'public/app/features/styleguide/styleguide.html', }) - .when('/alerting', { + .when('/alerting/list', { templateUrl: 'public/app/features/alerting/partials/alert_list.html', controller: 'AlertListCtrl', controllerAs: 'ctrl', diff --git a/public/app/features/alerting/alert_def.ts b/public/app/features/alerting/alert_def.ts index 69f639d04c4..bcac982f5ea 100644 --- a/public/app/features/alerting/alert_def.ts +++ b/public/app/features/alerting/alert_def.ts @@ -1,14 +1,5 @@ /// -var alertSeverityIconMap = { - "ok": "icon-gf-online alert-icon-online", - "warning": "icon-gf-warn alert-icon-warn", - "critical": "icon-gf-critical alert-icon-critical", -}; - -function getSeverityIconClass(alertState) { - return alertSeverityIconMap[alertState]; -} import { QueryPartDef, @@ -50,14 +41,28 @@ function createReducerPart(model) { return new QueryPart(model, def); } -var severityLevels = [ - {text: 'Critical', value: 'critical'}, - {text: 'Warning', value: 'warning'}, -]; +var severityLevels = { + 'critical': {text: 'Critical', iconClass: 'icon-gf-critical alert-icon-critical'}, + 'warning': {text: 'Warning', iconClass: 'icon-gf-warn alert-icon-warn'}, +}; + +function getStateDisplayModel(state, severity) { + var model = { + text: 'OK', + iconClass: 'icon-gf-online alert-icon-online' + }; + + if (state === 'firing') { + model.text = severityLevels[severity].text; + model.iconClass = severityLevels[severity].iconClass; + } + + return model; +} export default { alertQueryDef: alertQueryDef, - getSeverityIconClass: getSeverityIconClass, + getStateDisplayModel: getStateDisplayModel, conditionTypes: conditionTypes, evalFunctions: evalFunctions, severityLevels: severityLevels, diff --git a/public/app/features/alerting/alerts_ctrl.ts b/public/app/features/alerting/alert_list_ctrl.ts similarity index 60% rename from public/app/features/alerting/alerts_ctrl.ts rename to public/app/features/alerting/alert_list_ctrl.ts index 17d048cee85..6948242c729 100644 --- a/public/app/features/alerting/alerts_ctrl.ts +++ b/public/app/features/alerting/alert_list_ctrl.ts @@ -3,23 +3,20 @@ import angular from 'angular'; import _ from 'lodash'; import coreModule from '../../core/core_module'; -import config from 'app/core/config'; +import moment from 'moment'; import alertDef from './alert_def'; export class AlertListCtrl { alerts: any; - filter = { - ok: false, - warn: false, - critical: false, - acknowleged: false + filters = { + state: 'OK' }; /** @ngInject */ constructor(private backendSrv, private $route) { _.each($route.current.params.state, state => { - this.filter[state.toLowerCase()] = true; + this.filters[state.toLowerCase()] = true; }); this.loadAlerts(); @@ -27,10 +24,6 @@ export class AlertListCtrl { updateFilter() { var stats = []; - this.filter.ok && stats.push('OK'); - this.filter.warn && stats.push('Warn'); - this.filter.critical && stats.push('critical'); - this.$route.current.params.state = stats; this.$route.updateParams(); } @@ -38,17 +31,14 @@ export class AlertListCtrl { loadAlerts() { var stats = []; - this.filter.ok && stats.push('OK'); - this.filter.warn && stats.push('Warn'); - this.filter.critical && stats.push('critical'); - var params = { state: stats }; this.backendSrv.get('/api/alerts', params).then(result => { this.alerts = _.map(result, alert => { - alert.severityClass = alertDef.getSeverityIconClass(alert.severity); + alert.stateModel = alertDef.getStateDisplayModel(alert.state, alert.severity); + alert.newStateDateAgo = moment(alert.newStateDate).fromNow().replace(" ago", ""); return alert; }); }); diff --git a/public/app/features/alerting/all.ts b/public/app/features/alerting/all.ts index c7e2264c1c8..feb297c0867 100644 --- a/public/app/features/alerting/all.ts +++ b/public/app/features/alerting/all.ts @@ -1,4 +1,4 @@ -import './alerts_ctrl'; +import './alert_list_ctrl'; import './alert_log_ctrl'; import './notifications_list_ctrl'; import './notification_edit_ctrl'; diff --git a/public/app/features/alerting/partials/alert_list.html b/public/app/features/alerting/partials/alert_list.html index dbae224a0cc..e0b0896f3bd 100644 --- a/public/app/features/alerting/partials/alert_list.html +++ b/public/app/features/alerting/partials/alert_list.html @@ -6,38 +6,46 @@

Alerting

-
- - - -
+
+
+
+ +
+ +
+
+
+
+ +
+ +
    +
  1. + +
    +
    ACTIVE
    +
    + Execution Error +
    +
    +
    +
    +
    {{alert.name}}
    +
    +
    + + {{alert.stateModel.text}} + for + {{alert.newStateDateAgo}} +
    +
    +
    +
    +
    +
  2. +
+
- - - - - - - - - - - - - -
NameStateSeverity
- - {{alert.name}} - - - {{alert.state}} - - {{alert.severity}} - - - - edit - -
diff --git a/public/app/features/alerting/partials/alert_tab.html b/public/app/features/alerting/partials/alert_tab.html index 43ac9abaad4..39a76b2a6a3 100644 --- a/public/app/features/alerting/partials/alert_tab.html +++ b/public/app/features/alerting/partials/alert_tab.html @@ -34,7 +34,7 @@
Severity
-
diff --git a/public/app/features/plugins/partials/plugin_list.html b/public/app/features/plugins/partials/plugin_list.html index c276faae93a..0870b8727ec 100644 --- a/public/app/features/plugins/partials/plugin_list.html +++ b/public/app/features/plugins/partials/plugin_list.html @@ -5,10 +5,6 @@