Skip to content

Commit 55da665

Browse files
committed
alerting model poc
1 parent 30645a6 commit 55da665

File tree

11 files changed

+84
-47
lines changed

11 files changed

+84
-47
lines changed

pkg/api/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ func Register(r *macaron.Macaron) {
217217
// Dashboard
218218
r.Group("/dashboards", func() {
219219
r.Combo("/db/:slug").Get(GetDashboard).Delete(DeleteDashboard)
220-
r.Post("/db", reqEditorRole, bind(m.SaveDashboardCommand{}), wrap(PostDashboard))
220+
r.Post("/db", reqEditorRole, bind(dtos.SaveDashboardCmd{}), wrap(PostDashboard))
221221
r.Get("/file/:file", GetDashboardFromJsonFile)
222222
r.Get("/home", wrap(GetHomeDashboard))
223223
r.Get("/tags", GetDashboardTags)

pkg/api/dashboard.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,21 @@ func DeleteDashboard(c *middleware.Context) {
111111
c.JSON(200, resp)
112112
}
113113

114-
func PostDashboard(c *middleware.Context, cmd m.SaveDashboardCommand) Response {
115-
cmd.OrgId = c.OrgId
114+
func PostDashboard(c *middleware.Context, apiCmd dtos.SaveDashboardCmd) Response {
115+
116+
internalSaveDashCmd := m.SaveDashboardCommand{
117+
Dashboard: apiCmd.Dashboard,
118+
Overwrite: apiCmd.Overwrite,
119+
OrgId: c.OrgId,
120+
}
116121

117122
if !c.IsSignedIn {
118-
cmd.UserId = -1
123+
internalSaveDashCmd.UserId = -1
119124
} else {
120-
cmd.UserId = c.UserId
125+
internalSaveDashCmd.UserId = c.UserId
121126
}
122127

123-
dash := cmd.GetDashboardModel()
128+
dash := internalSaveDashCmd.GetDashboardModel()
124129
if dash.Id == 0 {
125130
limitReached, err := middleware.QuotaReached(c, "dashboard")
126131
if err != nil {
@@ -131,7 +136,7 @@ func PostDashboard(c *middleware.Context, cmd m.SaveDashboardCommand) Response {
131136
}
132137
}
133138

134-
err := bus.Dispatch(&cmd)
139+
err := bus.Dispatch(&internalSaveDashCmd)
135140
if err != nil {
136141
if err == m.ErrDashboardWithSameNameExists {
137142
return Json(412, util.DynMap{"status": "name-exists", "message": err.Error()})
@@ -153,18 +158,23 @@ func PostDashboard(c *middleware.Context, cmd m.SaveDashboardCommand) Response {
153158
return ApiError(500, "Failed to save dashboard", err)
154159
}
155160

156-
alertCmd := alerting.UpdateDashboardAlertsCommand{
157-
OrgId: c.OrgId,
158-
UserId: c.UserId,
159-
Dashboard: cmd.Result,
161+
saveAlertsCmd := alerting.SaveDashboardAlertsCommand{
162+
OrgId: c.OrgId,
163+
UserId: c.UserId,
164+
DashboardId: internalSaveDashCmd.Result.Id,
165+
Alerts: apiCmd.Alerts,
160166
}
161167

162-
if err := bus.Dispatch(&alertCmd); err != nil {
168+
if err := bus.Dispatch(&saveAlertsCmd); err != nil {
163169
return ApiError(500, "Failed to save alerts", err)
164170
}
165171

166172
c.TimeRequest(metrics.M_Api_Dashboard_Save)
167-
return Json(200, util.DynMap{"status": "success", "slug": cmd.Result.Slug, "version": cmd.Result.Version})
173+
return Json(200, util.DynMap{
174+
"status": "success",
175+
"slug": internalSaveDashCmd.Result.Slug,
176+
"version": internalSaveDashCmd.Result.Version,
177+
})
168178
}
169179

170180
func canEditDashboard(role m.RoleType) bool {

pkg/api/dtos/dashboard.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package dtos
2+
3+
import "github.com/grafana/grafana/pkg/components/simplejson"
4+
5+
type SaveDashboardCmd struct {
6+
Dashboard *simplejson.Json `json:"dashboard" binding:"Required"`
7+
Alerts []*simplejson.Json `json:"alerts"`
8+
Overwrite bool `json:"overwrite"`
9+
}

pkg/models/dashboards.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,12 @@ func (dash *Dashboard) UpdateSlug() {
125125
//
126126

127127
type SaveDashboardCommand struct {
128-
Dashboard *simplejson.Json `json:"dashboard" binding:"Required"`
129-
UserId int64 `json:"userId"`
130-
OrgId int64 `json:"-"`
131-
Overwrite bool `json:"overwrite"`
132-
PluginId string `json:"-"`
128+
Dashboard *simplejson.Json `json:"dashboard" binding:"Required"`
129+
Alerts []*simplejson.Json `json:"alerts"`
130+
UserId int64 `json:"userId"`
131+
OrgId int64 `json:"-"`
132+
Overwrite bool `json:"overwrite"`
133+
PluginId string `json:"-"`
133134

134135
Result *Dashboard
135136
}

pkg/services/alerting/commands.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,22 @@ package alerting
22

33
import (
44
"github.com/grafana/grafana/pkg/bus"
5+
"github.com/grafana/grafana/pkg/components/simplejson"
56
m "github.com/grafana/grafana/pkg/models"
67
)
78

8-
type UpdateDashboardAlertsCommand struct {
9-
UserId int64
10-
OrgId int64
11-
Dashboard *m.Dashboard
9+
type SaveDashboardAlertsCommand struct {
10+
UserId int64
11+
OrgId int64
12+
DashboardId int64
13+
Alerts []*simplejson.Json
1214
}
1315

1416
func init() {
15-
bus.AddHandler("alerting", updateDashboardAlerts)
17+
bus.AddHandler("alerting", saveDashboardAlertsHandler)
1618
}
1719

18-
func updateDashboardAlerts(cmd *UpdateDashboardAlertsCommand) error {
20+
func saveDashboardAlertsHandler(cmd *UpdateDashboardAlertsCommand) error {
1921
saveAlerts := m.SaveAlertsCommand{
2022
OrgId: cmd.OrgId,
2123
UserId: cmd.UserId,

public/app/core/services/backend_srv.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,8 @@ export class BackendSrv {
173173
return this.get('/api/dashboards/' + type + '/' + slug);
174174
}
175175

176-
saveDashboard(dash, options) {
177-
options = (options || {});
178-
return this.post('/api/dashboards/db/', {dashboard: dash, overwrite: options.overwrite === true});
176+
saveDashboard(options) {
177+
return this.post('/api/dashboards/db/', options);
179178
}
180179
}
181180

public/app/features/alerting/alert_tab_ctrl.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,12 @@ export class AlertTabCtrl {
129129
}
130130

131131
initModel() {
132-
var alert = this.alert = this.panel.alert;
133-
if (!alert) {
132+
if (!this.alert) {
134133
return;
135134
}
136135

136+
var alert = this.alert;
137+
137138
alert.conditions = alert.conditions || [];
138139
if (alert.conditions.length === 0) {
139140
alert.conditions.push(this.buildDefaultCondition());
@@ -152,7 +153,7 @@ export class AlertTabCtrl {
152153
return memo;
153154
}, []);
154155

155-
ThresholdMapper.alertToGraphThresholds(this.panel);
156+
//ThresholdMapper.alertToGraphThresholds(this.panel);
156157

157158
for (let addedNotification of alert.notifications) {
158159
var model = _.find(this.notifications, {id: addedNotification.id});
@@ -322,12 +323,17 @@ export class AlertTabCtrl {
322323
}
323324

324325
enable() {
325-
this.panel.alert = {};
326+
this.panel.alert = {id: 0};
327+
this.alert = {
328+
panelId: this.panel.id
329+
};
330+
326331
this.initModel();
332+
this.dashboardSrv.addAlert(this.alert);
327333
}
328334

329335
evaluatorParamsChanged() {
330-
ThresholdMapper.alertToGraphThresholds(this.panel);
336+
//ThresholdMapper.alertToGraphThresholds(this.panel);
331337
this.panelCtrl.render();
332338
}
333339

public/app/features/dashboard/dashboard_ctrl.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ export class DashboardCtrl {
1616
dashboardKeybindings,
1717
timeSrv,
1818
variableSrv,
19-
alertingSrv,
2019
dashboardSrv,
2120
unsavedChangesSrv,
2221
dynamicDashboardSrv,
@@ -39,12 +38,10 @@ export class DashboardCtrl {
3938
};
4039

4140
$scope.setupDashboardInternal = function(data) {
42-
var dashboard = dashboardSrv.create(data.dashboard, data.meta);
43-
dashboardSrv.setCurrent(dashboard);
41+
var dashboard = dashboardSrv.init(data);
4442

4543
// init services
4644
timeSrv.init(dashboard);
47-
alertingSrv.init(dashboard, data.alerts);
4845

4946
// template values service needs to initialize completely before
5047
// the rest of the dashboard can load

public/app/features/dashboard/dashboard_srv.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {DashboardModel} from './model';
66

77
export class DashboardSrv {
88
dash: any;
9+
alerts: any[];
910

1011
/** @ngInject */
1112
constructor(private backendSrv, private $rootScope, private $location) {
@@ -15,22 +16,30 @@ export class DashboardSrv {
1516
return new DashboardModel(dashboard, meta);
1617
}
1718

18-
setCurrent(dashboard) {
19-
this.dash = dashboard;
19+
init(data) {
20+
this.dash = this.create(data.dashboard, data.meta);
21+
this.alerts = [];
22+
23+
return this.dash;
2024
}
2125

2226
getCurrent() {
2327
return this.dash;
2428
}
2529

30+
addAlert(alert) {
31+
this.alerts.push(alert);
32+
}
33+
2634
saveDashboard(options) {
27-
if (!this.dash.meta.canSave && options.makeEditable !== true) {
35+
if (this.dash.meta.canSave === false && options.makeEditable !== true) {
2836
return Promise.resolve();
2937
}
3038

31-
var clone = this.dash.getSaveModelClone();
39+
options.dashboard = this.dash.getSaveModelClone();
40+
options.alerts = this.alerts;
3241

33-
return this.backendSrv.saveDashboard(clone, options).then(data => {
42+
return this.backendSrv.saveDashboard(options).then(data => {
3443
this.dash.version = data.version;
3544

3645
this.$rootScope.appEvent('dashboard-saved', this.dash);
@@ -40,7 +49,7 @@ export class DashboardSrv {
4049
this.$location.url(dashboardUrl);
4150
}
4251

43-
this.$rootScope.appEvent('alert-success', ['Dashboard saved', 'Saved as ' + clone.title]);
52+
this.$rootScope.appEvent('alert-success', ['Dashboard saved', 'Saved as ' + options.dashboard.title]);
4453
}).catch(this.handleSaveDashboardError.bind(this));
4554
}
4655

public/app/features/dashboard/dashnav/dashnav.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ export class DashNavCtrl {
7777
});
7878
};
7979

80-
$scope.saveDashboard = function(options) {
81-
return dashboardSrv.saveDashboard(options);
80+
$scope.saveDashboard = function() {
81+
return dashboardSrv.saveDashboard({overwrite: false});
8282
};
8383

8484
$scope.deleteDashboard = function() {

public/app/features/dashboard/saveDashboardAsCtrl.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function (angular) {
2525
};
2626

2727
function saveDashboard(options) {
28-
return backendSrv.saveDashboard($scope.clone, options).then(function(result) {
28+
return backendSrv.saveDashboard(options).then(function(result) {
2929
$scope.appEvent('alert-success', ['Dashboard saved', 'Saved as ' + $scope.clone.title]);
3030

3131
$location.url('/dashboard/db/' + result.slug);
@@ -42,7 +42,11 @@ function (angular) {
4242
};
4343

4444
$scope.saveClone = function() {
45-
saveDashboard({overwrite: false}).then(null, function(err) {
45+
saveDashboard({
46+
dashboard: $scope.clone,
47+
overwrite: false
48+
}).catch(function(err) {
49+
4650
if (err.data && err.data.status === "name-exists") {
4751
err.isHandled = true;
4852

@@ -53,7 +57,7 @@ function (angular) {
5357
yesText: "Save & Overwrite",
5458
icon: "fa-warning",
5559
onConfirm: function() {
56-
saveDashboard({overwrite: true});
60+
saveDashboard({dashboard: $scope.clone, overwrite: true});
5761
}
5862
});
5963
}

0 commit comments

Comments
 (0)