From 6a5ecb3fcae37e98ba59747cfa4ff35c70700c54 Mon Sep 17 00:00:00 2001 From: bergquist Date: Wed, 27 Apr 2016 13:02:28 +0200 Subject: [PATCH] feat(alerting): adds basic page for listing alerts --- pkg/api/alerting.go | 42 ++++++++++++++++++- pkg/api/api.go | 2 + pkg/api/dtos/alerting.go | 18 ++++++++ public/app/core/routes/routes.ts | 7 ++++ public/app/features/alerts/alerts_ctrl.ts | 26 ++++++++++++ public/app/features/alerts/all.ts | 2 + .../features/alerts/partials/alerts_page.html | 34 +++++++++++++++ 7 files changed, 129 insertions(+), 2 deletions(-) create mode 100644 pkg/api/dtos/alerting.go create mode 100644 public/app/features/alerts/alerts_ctrl.ts create mode 100644 public/app/features/alerts/all.ts create mode 100644 public/app/features/alerts/partials/alerts_page.html diff --git a/pkg/api/alerting.go b/pkg/api/alerting.go index 1443f395ea2..29ca7a6758f 100644 --- a/pkg/api/alerting.go +++ b/pkg/api/alerting.go @@ -1,6 +1,7 @@ package api import ( + "github.com/grafana/grafana/pkg/api/dtos" "github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/middleware" "github.com/grafana/grafana/pkg/models" @@ -21,7 +22,7 @@ func ValidateOrgAlert(c *middleware.Context) { } } -// GET /api/alert_rule +// GET /api/alert_rule/changes func GetAlertChanges(c *middleware.Context) Response { query := models.GetAlertChangesQuery{ OrgId: c.OrgId, @@ -44,7 +45,44 @@ func GetAlerts(c *middleware.Context) Response { return ApiError(500, "List alerts failed", err) } - return Json(200, query.Result) + dashboardIds := make([]int64, 0) + alertDTOs := make([]*dtos.AlertRuleDTO, 0) + for _, alert := range query.Result { + dashboardIds = append(dashboardIds, alert.DashboardId) + alertDTOs = append(alertDTOs, &dtos.AlertRuleDTO{ + Id: alert.Id, + DashboardId: alert.DashboardId, + PanelId: alert.PanelId, + Query: alert.Query, + QueryRefId: alert.QueryRefId, + WarnLevel: alert.WarnLevel, + CritLevel: alert.CritLevel, + Interval: alert.Interval, + Title: alert.Title, + Description: alert.Description, + QueryRange: alert.QueryRange, + Aggregator: alert.Aggregator, + }) + } + + dashboardsQuery := models.GetDashboardsQuery{ + DashboardIds: dashboardIds, + } + + if err := bus.Dispatch(&dashboardsQuery); err != nil { + return ApiError(500, "List alerts failed", err) + } + + //TODO: should be possible to speed this up with lookup table + for _, alert := range alertDTOs { + for _, dash := range *dashboardsQuery.Result { + if alert.DashboardId == dash.Id { + alert.DashbboardUri = "db/" + dash.Slug + } + } + } + + return Json(200, alertDTOs) } // GET /api/alert_rule/:id diff --git a/pkg/api/api.go b/pkg/api/api.go index 4ad6c1665a1..081a85adba0 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -58,6 +58,8 @@ func Register(r *macaron.Macaron) { r.Get("/playlists/", reqSignedIn, Index) r.Get("/playlists/*", reqSignedIn, Index) + r.Get("/alerts/", reqSignedIn, Index) + r.Get("/alerts/*", reqSignedIn, Index) // sign up r.Get("/signup", Index) diff --git a/pkg/api/dtos/alerting.go b/pkg/api/dtos/alerting.go new file mode 100644 index 00000000000..695d11c0b66 --- /dev/null +++ b/pkg/api/dtos/alerting.go @@ -0,0 +1,18 @@ +package dtos + +type AlertRuleDTO struct { + Id int64 `json:"id"` + DashboardId int64 `json:"dashboardId"` + PanelId int64 `json:"panelId"` + Query string `json:"query"` + QueryRefId string `json:"queryRefId"` + WarnLevel string `json:"warnLevel"` + CritLevel string `json:"critLevel"` + Interval string `json:"interval"` + Title string `json:"title"` + Description string `json:"description"` + QueryRange string `json:"queryRange"` + Aggregator string `json:"aggregator"` + + DashbboardUri string `json:"dashboardUri"` +} diff --git a/public/app/core/routes/routes.ts b/public/app/core/routes/routes.ts index 1608a772e87..899310c9c2b 100644 --- a/public/app/core/routes/routes.ts +++ b/public/app/core/routes/routes.ts @@ -13,6 +13,7 @@ function setupAngularRoutes($routeProvider, $locationProvider) { var loadOrgBundle = new BundleLoader('app/features/org/all'); var loadPluginsBundle = new BundleLoader('app/features/plugins/all'); var loadAdminBundle = new BundleLoader('app/features/admin/admin'); + var loadAlertsBundle = new BundleLoader('app/features/alerts/all'); $routeProvider .when('/', { @@ -197,6 +198,12 @@ function setupAngularRoutes($routeProvider, $locationProvider) { controllerAs: 'ctrl', templateUrl: 'public/app/features/styleguide/styleguide.html', }) + .when('/alerts', { + templateUrl: 'public/app/features/alerts/partials/alerts_page.html', + controller: 'AlertPageCtrl', + controllerAs: 'ctrl', + resolve: loadAlertsBundle, + }) .otherwise({ templateUrl: 'public/app/partials/error.html', controller: 'ErrorCtrl' diff --git a/public/app/features/alerts/alerts_ctrl.ts b/public/app/features/alerts/alerts_ctrl.ts new file mode 100644 index 00000000000..7a3fc032281 --- /dev/null +++ b/public/app/features/alerts/alerts_ctrl.ts @@ -0,0 +1,26 @@ +/// + +import angular from 'angular'; +import _ from 'lodash'; +import coreModule from '../../core/core_module'; +import config from 'app/core/config'; + +export class AlertPageCtrl { + + alerts: any; + /** @ngInject */ + constructor(private $scope, private backendSrv) { + console.log('ctor!'); + this.loadAlerts(); + } + + loadAlerts() { + this.backendSrv.get('/api/alert_rule').then(result => { + console.log(result); + this.alerts = result; + }); + } +} + +coreModule.controller('AlertPageCtrl', AlertPageCtrl); + diff --git a/public/app/features/alerts/all.ts b/public/app/features/alerts/all.ts new file mode 100644 index 00000000000..ef94f49e82f --- /dev/null +++ b/public/app/features/alerts/all.ts @@ -0,0 +1,2 @@ +import './alerts_ctrl'; + diff --git a/public/app/features/alerts/partials/alerts_page.html b/public/app/features/alerts/partials/alerts_page.html new file mode 100644 index 00000000000..a34a4552535 --- /dev/null +++ b/public/app/features/alerts/partials/alerts_page.html @@ -0,0 +1,34 @@ + + + +
+ + + + + + + + + + + + + + +
Name
+ {{alert.title}} + + + Go to dashboard + + + + + +
+
+ +