Sfoglia il codice sorgente

feat(alerting): enables deletes for alert notifications

bergquist 9 anni fa
parent
commit
b907ce341c

+ 13 - 0
pkg/api/alerting.go

@@ -201,3 +201,16 @@ func UpdateAlertNotification(c *middleware.Context, cmd models.UpdateAlertNotifi
 
 	return Json(200, cmd.Result)
 }
+
+func DeleteAlertNotification(c *middleware.Context) Response {
+	cmd := models.DeleteAlertNotificationCommand{
+		OrgId: c.OrgId,
+		Id:    c.ParamsInt64("notificationId"),
+	}
+
+	if err := bus.Dispatch(&cmd); err != nil {
+		return ApiError(500, "Failed to delete alert notification", err)
+	}
+
+	return Json(200, map[string]interface{}{"notificationId": cmd.Id})
+}

+ 1 - 0
pkg/api/api.go

@@ -257,6 +257,7 @@ func Register(r *macaron.Macaron) {
 				r.Post("/", bind(m.CreateAlertNotificationCommand{}), wrap(CreateAlertNotification))
 				r.Put("/:notificationId", bind(m.UpdateAlertNotificationCommand{}), wrap(UpdateAlertNotification))
 				r.Get("/:notificationId", wrap(GetAlertNotificationById))
+				r.Delete("/:notificationId", wrap(DeleteAlertNotification))
 			})
 
 			r.Get("/changes", wrap(GetAlertChanges))

+ 5 - 0
pkg/models/alert_notifications.go

@@ -35,6 +35,11 @@ type UpdateAlertNotificationCommand struct {
 	Result *AlertNotification
 }
 
+type DeleteAlertNotificationCommand struct {
+	Id    int64
+	OrgId int64
+}
+
 type GetAlertNotificationQuery struct {
 	Name  string
 	Id    int64

+ 14 - 0
pkg/services/sqlstore/alert_notification.go

@@ -15,6 +15,20 @@ func init() {
 	bus.AddHandler("sql", AlertNotificationQuery)
 	bus.AddHandler("sql", CreateAlertNotificationCommand)
 	bus.AddHandler("sql", UpdateAlertNotification)
+	bus.AddHandler("sql", DeleteAlertNotification)
+}
+
+func DeleteAlertNotification(cmd *m.DeleteAlertNotificationCommand) error {
+	return inTransaction(func(sess *xorm.Session) error {
+		sql := "DELETE FROM alert_notification WHERE alert_notification.org_id = ? AND alert_notification.id = ?"
+		_, err := sess.Exec(sql, cmd.OrgId, cmd.Id)
+
+		if err != nil {
+			return err
+		}
+
+		return nil
+	})
 }
 
 func AlertNotificationQuery(query *m.GetAlertNotificationQuery) error {

+ 7 - 3
public/app/features/alerting/notification_edit_ctrl.ts

@@ -10,7 +10,7 @@ export class AlertNotificationEditCtrl {
   notification: any;
 
   /** @ngInject */
-  constructor(private $routeParams, private backendSrv) {
+  constructor(private $routeParams, private backendSrv, private $scope) {
     if ($routeParams.notificationId) {
       this.loadNotification($routeParams.notificationId);
     }
@@ -33,13 +33,17 @@ export class AlertNotificationEditCtrl {
       this.backendSrv.put(`/api/alerts/notification/${this.notification.id}`, this.notification)
         .then(result => {
           this.notification = result;
-          console.log('updated notification', result);
+          this.$scope.appEvent('alert-success', ['Notification created!', '']);
+        }, () => {
+          this.$scope.appEvent('alert-error', ['Unable to create notification.', '']);
         });
     } else {
       this.backendSrv.post(`/api/alerts/notification`, this.notification)
         .then(result => {
           this.notification = result;
-          console.log('created new notification', result);
+          this.$scope.appEvent('alert-success', ['Notification updated!', '']);
+        }, () => {
+          this.$scope.appEvent('alert-error', ['Unable to update notification.', '']);
         });
     }
   }

+ 14 - 1
public/app/features/alerting/notifications_list_ctrl.ts

@@ -10,7 +10,7 @@ export class AlertNotificationsListCtrl {
   notifications: any;
 
   /** @ngInject */
-  constructor(private backendSrv) {
+  constructor(private backendSrv, private $scope) {
     this.loadNotifications();
   }
 
@@ -19,7 +19,20 @@ export class AlertNotificationsListCtrl {
       this.notifications = result;
     });
   }
+
+  deleteNotification(notificationId) {
+    this.backendSrv.delete(`/api/alerts/notification/${notificationId}`)
+      .then(() => {
+        this.notifications = this.notifications.filter(notification => {
+          return notification.id !== notificationId;
+        });
+        this.$scope.appEvent('alert-success', ['Notification deleted', '']);
+      }, () => {
+        this.$scope.appEvent('alert-error', ['Unable to delete notification', '']);
+      });
+  }
 }
 
 coreModule.controller('AlertNotificationsListCtrl', AlertNotificationsListCtrl);
 
+

+ 10 - 7
public/app/features/alerting/partials/notifications_list.html

@@ -4,32 +4,35 @@
 <div class="page-container" >
 	<div class="page-header">
 		<h1>Alert notifications</h1>
-    <button class="btn btn-success pull-right">
+    <a href="alerting/notification/new" class="btn btn-success pull-right">
       <i class="fa fa-plus"></i>
       New Notification
-    </button>
+    </a>
   </div>
 
-	<table class="grafana-options-table">
+	<table class="grafana-options-table" style="/*width: 600px;*/">
 		<thead>
 			<th style="min-width: 200px"><strong>Name</strong></th>
-			<th style="width: 1%">Type</th>
+			<th style="min-width: 100px">Type</th>
 			<th style="width: 1%"></th>
 		</thead>
 		<tr ng-repeat="notification in ctrl.notifications">
 			<td>
 				<a href="alerting/notification{{notification.id}}/edit">
-					{{alert.name}}
+					{{notification.name}}
 				</a>
 			</td>
-			<td class="text-center">
+			<td>
 				{{notification.type}}
 			</td>
-			<td class="text-center">
+			<td>
 				<a href="alerting/notification/{{notification.id}}/edit" class="btn btn-inverse btn-small">
 					<i class="fa fa-edit"></i>
 					edit
 				</a>
+				<a ng-click="ctrl.deleteNotification(notification.id)" class="btn btn-danger btn-small">
+          <i class="fa fa-remove"></i>
+        </a>
 			</td>
 		</tr>
 	</table>