Skip to content

Commit c9d7eb0

Browse files
committed
feat(alerting): added clear history button to alert state history view, grafana#6244
1 parent aeb0c5c commit c9d7eb0

File tree

9 files changed

+82
-3
lines changed

9 files changed

+82
-3
lines changed

pkg/api/annotations.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,19 @@ func GetAnnotations(c *middleware.Context) Response {
4444

4545
return Json(200, result)
4646
}
47+
48+
func DeleteAnnotations(c *middleware.Context, cmd dtos.DeleteAnnotationsCmd) Response {
49+
repo := annotations.GetRepository()
50+
51+
err := repo.Delete(&annotations.DeleteParams{
52+
AlertId: cmd.PanelId,
53+
DashboardId: cmd.DashboardId,
54+
PanelId: cmd.PanelId,
55+
})
56+
57+
if err != nil {
58+
return ApiError(500, "Failed to delete annotations", err)
59+
}
60+
61+
return ApiSuccess("Annotations deleted")
62+
}

pkg/api/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ func Register(r *macaron.Macaron) {
269269
}, reqOrgAdmin)
270270

271271
r.Get("/annotations", wrap(GetAnnotations))
272+
r.Post("/annotations/mass-delete", reqOrgAdmin, bind(dtos.DeleteAnnotationsCmd{}), wrap(DeleteAnnotations))
272273

273274
// error test
274275
r.Get("/metrics/error", wrap(GenerateError))

pkg/api/dtos/annotations.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,9 @@ type Annotation struct {
1515

1616
Data *simplejson.Json `json:"data"`
1717
}
18+
19+
type DeleteAnnotationsCmd struct {
20+
AlertId int64 `json:"alertId"`
21+
DashboardId int64 `json:"dashboardId"`
22+
PanelId int64 `json:"panelId"`
23+
}

pkg/services/annotations/annotations.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import "github.com/grafana/grafana/pkg/components/simplejson"
55
type Repository interface {
66
Save(item *Item) error
77
Find(query *ItemQuery) ([]*Item, error)
8+
Delete(params *DeleteParams) error
89
}
910

1011
type ItemQuery struct {
@@ -20,6 +21,12 @@ type ItemQuery struct {
2021
Limit int64 `json:"alertId"`
2122
}
2223

24+
type DeleteParams struct {
25+
AlertId int64 `json:"alertId"`
26+
DashboardId int64 `json:"dashboardId"`
27+
PanelId int64 `json:"panelId"`
28+
}
29+
2330
var repositoryInstance Repository
2431

2532
func GetRepository() Repository {

pkg/services/sqlstore/annotation.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,17 @@ func (r *SqlAnnotationRepo) Find(query *annotations.ItemQuery) ([]*annotations.I
8484

8585
return items, nil
8686
}
87+
88+
func (r *SqlAnnotationRepo) Delete(params *annotations.DeleteParams) error {
89+
return inTransaction(func(sess *xorm.Session) error {
90+
91+
sql := "DELETE FROM annotation WHERE dashboard_id = ? AND panel_id = ?"
92+
93+
_, err := sess.Exec(sql, params.DashboardId, params.PanelId)
94+
if err != nil {
95+
return err
96+
}
97+
98+
return nil
99+
})
100+
}

public/app/features/alerting/alert_tab_ctrl.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export class AlertTabCtrl {
5959
this.panelCtrl.render();
6060
});
6161

62-
// build notification model
62+
// build notification model
6363
this.notifications = [];
6464
this.alertNotifications = [];
6565
this.alertHistory = [];
@@ -352,6 +352,24 @@ export class AlertTabCtrl {
352352
this.evaluatorParamsChanged();
353353
}
354354

355+
clearHistory() {
356+
appEvents.emit('confirm-modal', {
357+
title: 'Delete Alert History',
358+
text: 'Are you sure you want to remove all history & annotations for this alert?',
359+
icon: 'fa-trash',
360+
yesText: 'Yes',
361+
onConfirm: () => {
362+
this.backendSrv.post('/api/annotations/mass-delete', {
363+
dashboardId: this.panelCtrl.dashboard.id,
364+
panelId: this.panel.id,
365+
}).then(res => {
366+
this.alertHistory = [];
367+
this.panelCtrl.refresh();
368+
});
369+
}
370+
});
371+
}
372+
355373
test() {
356374
this.testing = true;
357375

public/app/features/alerting/partials/alert_tab.html

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,16 @@ <h5 class="section-heading">Notifications</h5>
125125
</div>
126126

127127
<div class="gf-form-group" style="max-width: 720px;" ng-if="ctrl.subTabIndex === 2">
128-
<h5 class="section-heading">State history <span class="muted small">(last 50 state changes)</span></h5>
128+
<button class="btn btn-mini btn-danger pull-right" ng-click="ctrl.clearHistory()"><i class="fa fa-trash"></i>&nbsp;Clear history</button>
129+
<h5 class="section-heading" style="whitespace: nowrap">
130+
State history <span class="muted small">(last 50 state changes)</span>
131+
</h5>
132+
133+
<div ng-show="ctrl.alertHistory.length === 0">
134+
<br>
135+
<i>No state changes recorded</i>
136+
</div>
137+
129138
<section class="card-section card-list-layout-list">
130139
<ol class="card-list" >
131140
<li class="card-item-wrapper" ng-repeat="ah in ctrl.alertHistory">

public/app/plugins/panel/graph/graph.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,17 +392,21 @@ module.directive('grafanaGraph', function($rootScope, timeSrv) {
392392
position: 'BOTTOM',
393393
markerSize: 5,
394394
};
395+
395396
types['$__ok'] = {
396397
color: 'rgba(11, 237, 50, 1)',
397398
position: 'BOTTOM',
398399
markerSize: 5,
399400
};
400-
types['$__nodata'] = {
401+
402+
types['$__no_data'] = {
401403
color: 'rgba(150, 150, 150, 1)',
402404
position: 'BOTTOM',
403405
markerSize: 5,
404406
};
405407

408+
types['$__execution_error'] = ['$__no_data'];
409+
406410
for (var i = 0; i < annotations.length; i++) {
407411
var item = annotations[i];
408412
if (item.newState) {

public/sass/components/edit_sidemenu.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
flex-direction: row;
55
}
66

7+
.edit-tab-content {
8+
flex-grow: 1;
9+
}
10+
711
.edit-sidemenu-aside {
812
width: 16rem;
913
}

0 commit comments

Comments
 (0)