feat(alerting): adds basic page for listing alerts

This commit is contained in:
bergquist 2016-04-27 13:02:28 +02:00
parent 3cf6532515
commit 6a5ecb3fca
7 changed files with 129 additions and 2 deletions

View File

@ -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

View File

@ -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)

18
pkg/api/dtos/alerting.go Normal file
View File

@ -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"`
}

View File

@ -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'

View File

@ -0,0 +1,26 @@
///<reference path="../../headers/common.d.ts" />
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);

View File

@ -0,0 +1,2 @@
import './alerts_ctrl';

View File

@ -0,0 +1,34 @@
<navbar icon="fa fa-fw fa-list" title="Alerts" title-url="alerts">
</navbar>
<div class="page-container" >
<div class="page-header">
<h1>Alerts</h1>
</div>
<table class="filter-table">
<thead>
<th><strong>Name</strong></th>
<th style="width: 68px"></th>
<th style="width: 28px"></th>
</thead>
<tr ng-repeat="alert in ctrl.alerts">
<td>
{{alert.title}}
</td>
<td class="text-center">
<a href="/dashboard/{{alert.dashboardUri}}" class="btn btn-inverse btn-small">
Go to dashboard
</a>
</td>
<td class="text-right">
<a ng-click="ctrl.deleteAlert(alert)" class="btn btn-danger btn-small">
<i class="fa fa-remove"></i>
</a>
</td>
</tr>
</table>
</div>