| 
									
										
										
										
											2016-04-26 23:36:50 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2016-07-21 16:29:11 +08:00
										 |  |  | 	"fmt" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-27 19:02:28 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/api/dtos" | 
					
						
							| 
									
										
										
										
											2016-04-26 23:36:50 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/bus" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/middleware" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/models" | 
					
						
							| 
									
										
										
										
											2016-07-20 22:13:36 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/alerting" | 
					
						
							| 
									
										
										
										
											2016-04-26 23:36:50 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func ValidateOrgAlert(c *middleware.Context) { | 
					
						
							| 
									
										
										
										
											2016-05-02 22:07:19 +08:00
										 |  |  | 	id := c.ParamsInt64(":alertId") | 
					
						
							| 
									
										
										
										
											2016-04-28 14:23:50 +08:00
										 |  |  | 	query := models.GetAlertByIdQuery{Id: id} | 
					
						
							| 
									
										
										
										
											2016-04-26 23:36:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if err := bus.Dispatch(&query); err != nil { | 
					
						
							|  |  |  | 		c.JsonApiErr(404, "Alert not found", nil) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if c.OrgId != query.Result.OrgId { | 
					
						
							|  |  |  | 		c.JsonApiErr(403, "You are not allowed to edit/view alert", nil) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-30 23:37:47 +08:00
										 |  |  | func GetAlertStatesForDashboard(c *middleware.Context) Response { | 
					
						
							|  |  |  | 	dashboardId := c.QueryInt64("dashboardId") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if dashboardId == 0 { | 
					
						
							|  |  |  | 		return ApiError(400, "Missing query parameter dashboardId", nil) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	query := models.GetAlertStatesForDashboardQuery{ | 
					
						
							|  |  |  | 		OrgId:       c.OrgId, | 
					
						
							|  |  |  | 		DashboardId: c.QueryInt64("dashboardId"), | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := bus.Dispatch(&query); err != nil { | 
					
						
							|  |  |  | 		return ApiError(500, "Failed to fetch alert states", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return Json(200, query.Result) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-14 14:36:44 +08:00
										 |  |  | // GET /api/alerts
 | 
					
						
							| 
									
										
										
										
											2016-04-26 23:36:50 +08:00
										 |  |  | func GetAlerts(c *middleware.Context) Response { | 
					
						
							|  |  |  | 	query := models.GetAlertsQuery{ | 
					
						
							| 
									
										
										
										
											2016-05-10 15:45:56 +08:00
										 |  |  | 		OrgId:       c.OrgId, | 
					
						
							|  |  |  | 		DashboardId: c.QueryInt64("dashboardId"), | 
					
						
							|  |  |  | 		PanelId:     c.QueryInt64("panelId"), | 
					
						
							| 
									
										
										
										
											2016-09-14 14:36:44 +08:00
										 |  |  | 		Limit:       c.QueryInt64("limit"), | 
					
						
							| 
									
										
										
										
											2016-04-26 23:36:50 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-14 20:12:19 +08:00
										 |  |  | 	states := c.QueryStrings("state") | 
					
						
							|  |  |  | 	if len(states) > 0 { | 
					
						
							|  |  |  | 		query.State = states | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-26 23:36:50 +08:00
										 |  |  | 	if err := bus.Dispatch(&query); err != nil { | 
					
						
							|  |  |  | 		return ApiError(500, "List alerts failed", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-27 19:02:28 +08:00
										 |  |  | 	dashboardIds := make([]int64, 0) | 
					
						
							| 
									
										
										
										
											2016-07-22 22:45:17 +08:00
										 |  |  | 	alertDTOs := make([]*dtos.AlertRule, 0) | 
					
						
							| 
									
										
										
										
											2016-04-27 19:02:28 +08:00
										 |  |  | 	for _, alert := range query.Result { | 
					
						
							|  |  |  | 		dashboardIds = append(dashboardIds, alert.DashboardId) | 
					
						
							| 
									
										
										
										
											2016-07-22 22:45:17 +08:00
										 |  |  | 		alertDTOs = append(alertDTOs, &dtos.AlertRule{ | 
					
						
							| 
									
										
										
										
											2016-08-16 15:52:45 +08:00
										 |  |  | 			Id:             alert.Id, | 
					
						
							|  |  |  | 			DashboardId:    alert.DashboardId, | 
					
						
							|  |  |  | 			PanelId:        alert.PanelId, | 
					
						
							|  |  |  | 			Name:           alert.Name, | 
					
						
							|  |  |  | 			Message:        alert.Message, | 
					
						
							|  |  |  | 			State:          alert.State, | 
					
						
							|  |  |  | 			NewStateDate:   alert.NewStateDate, | 
					
						
							|  |  |  | 			ExecutionError: alert.ExecutionError, | 
					
						
							| 
									
										
										
										
											2017-01-17 01:01:08 +08:00
										 |  |  | 			EvalData:       alert.EvalData, | 
					
						
							| 
									
										
										
										
											2016-04-27 19:02:28 +08:00
										 |  |  | 		}) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	dashboardsQuery := models.GetDashboardsQuery{ | 
					
						
							|  |  |  | 		DashboardIds: dashboardIds, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-09 20:30:28 +08:00
										 |  |  | 	if len(alertDTOs) > 0 { | 
					
						
							|  |  |  | 		if err := bus.Dispatch(&dashboardsQuery); err != nil { | 
					
						
							|  |  |  | 			return ApiError(500, "List alerts failed", err) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-04-27 19:02:28 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	//TODO: should be possible to speed this up with lookup table
 | 
					
						
							|  |  |  | 	for _, alert := range alertDTOs { | 
					
						
							| 
									
										
										
										
											2016-06-23 22:30:12 +08:00
										 |  |  | 		for _, dash := range dashboardsQuery.Result { | 
					
						
							| 
									
										
										
										
											2016-04-27 19:02:28 +08:00
										 |  |  | 			if alert.DashboardId == dash.Id { | 
					
						
							|  |  |  | 				alert.DashbboardUri = "db/" + dash.Slug | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return Json(200, alertDTOs) | 
					
						
							| 
									
										
										
										
											2016-04-26 23:36:50 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-20 22:13:36 +08:00
										 |  |  | // POST /api/alerts/test
 | 
					
						
							| 
									
										
										
										
											2016-07-21 16:29:11 +08:00
										 |  |  | func AlertTest(c *middleware.Context, dto dtos.AlertTestCommand) Response { | 
					
						
							| 
									
										
										
										
											2016-11-23 21:55:10 +08:00
										 |  |  | 	if _, idErr := dto.Dashboard.Get("id").Int64(); idErr != nil { | 
					
						
							|  |  |  | 		return ApiError(400, "The dashboard needs to be saved at least once before you can test an alert rule", nil) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-21 16:29:11 +08:00
										 |  |  | 	backendCmd := alerting.AlertTestCommand{ | 
					
						
							| 
									
										
										
										
											2016-07-20 22:13:36 +08:00
										 |  |  | 		OrgId:     c.OrgId, | 
					
						
							|  |  |  | 		Dashboard: dto.Dashboard, | 
					
						
							|  |  |  | 		PanelId:   dto.PanelId, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := bus.Dispatch(&backendCmd); err != nil { | 
					
						
							| 
									
										
										
										
											2016-07-27 22:29:28 +08:00
										 |  |  | 		if validationErr, ok := err.(alerting.ValidationError); ok { | 
					
						
							| 
									
										
										
										
											2016-07-21 19:09:12 +08:00
										 |  |  | 			return ApiError(422, validationErr.Error(), nil) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-07-20 22:13:36 +08:00
										 |  |  | 		return ApiError(500, "Failed to test rule", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-21 16:29:11 +08:00
										 |  |  | 	res := backendCmd.Result | 
					
						
							|  |  |  | 	dtoRes := &dtos.AlertTestResult{ | 
					
						
							| 
									
										
										
										
											2016-11-17 22:48:15 +08:00
										 |  |  | 		Firing:         res.Firing, | 
					
						
							|  |  |  | 		ConditionEvals: res.ConditionEvals, | 
					
						
							| 
									
										
										
										
											2017-01-13 17:24:40 +08:00
										 |  |  | 		State:          res.Rule.State, | 
					
						
							| 
									
										
										
										
											2016-07-21 16:29:11 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if res.Error != nil { | 
					
						
							|  |  |  | 		dtoRes.Error = res.Error.Error() | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-21 19:09:12 +08:00
										 |  |  | 	for _, log := range res.Logs { | 
					
						
							|  |  |  | 		dtoRes.Logs = append(dtoRes.Logs, &dtos.AlertTestResultLog{Message: log.Message, Data: log.Data}) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-08-18 17:22:24 +08:00
										 |  |  | 	for _, match := range res.EvalMatches { | 
					
						
							|  |  |  | 		dtoRes.EvalMatches = append(dtoRes.EvalMatches, &dtos.EvalMatch{Metric: match.Metric, Value: match.Value}) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-07-21 19:09:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-26 18:29:52 +08:00
										 |  |  | 	dtoRes.TimeMs = fmt.Sprintf("%1.3fms", res.GetDurationMs()) | 
					
						
							| 
									
										
										
										
											2016-07-21 16:29:11 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return Json(200, dtoRes) | 
					
						
							| 
									
										
										
										
											2016-07-20 22:13:36 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-28 14:23:50 +08:00
										 |  |  | // GET /api/alerts/:id
 | 
					
						
							| 
									
										
										
										
											2016-04-26 23:36:50 +08:00
										 |  |  | func GetAlert(c *middleware.Context) Response { | 
					
						
							| 
									
										
										
										
											2016-05-03 00:00:10 +08:00
										 |  |  | 	id := c.ParamsInt64(":alertId") | 
					
						
							| 
									
										
										
										
											2016-04-28 14:23:50 +08:00
										 |  |  | 	query := models.GetAlertByIdQuery{Id: id} | 
					
						
							| 
									
										
										
										
											2016-04-26 23:36:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if err := bus.Dispatch(&query); err != nil { | 
					
						
							|  |  |  | 		return ApiError(500, "List alerts failed", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return Json(200, &query.Result) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-04-28 14:23:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-02 22:07:19 +08:00
										 |  |  | // DEL /api/alerts/:id
 | 
					
						
							|  |  |  | func DelAlert(c *middleware.Context) Response { | 
					
						
							|  |  |  | 	alertId := c.ParamsInt64(":alertId") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if alertId == 0 { | 
					
						
							|  |  |  | 		return ApiError(401, "Failed to parse alertid", nil) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	cmd := models.DeleteAlertCommand{AlertId: alertId} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := bus.Dispatch(&cmd); err != nil { | 
					
						
							|  |  |  | 		return ApiError(500, "Failed to delete alert", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var resp = map[string]interface{}{"alertId": alertId} | 
					
						
							|  |  |  | 	return Json(200, resp) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-06 19:04:25 +08:00
										 |  |  | func GetAlertNotifiers(c *middleware.Context) Response { | 
					
						
							|  |  |  | 	return Json(200, alerting.GetNotifiers()) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-15 16:48:04 +08:00
										 |  |  | func GetAlertNotifications(c *middleware.Context) Response { | 
					
						
							| 
									
										
										
										
											2016-09-06 14:42:35 +08:00
										 |  |  | 	query := &models.GetAllAlertNotificationsQuery{OrgId: c.OrgId} | 
					
						
							| 
									
										
										
										
											2016-06-15 16:48:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if err := bus.Dispatch(query); err != nil { | 
					
						
							|  |  |  | 		return ApiError(500, "Failed to get alert notifications", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-06 22:40:02 +08:00
										 |  |  | 	result := make([]*dtos.AlertNotification, 0) | 
					
						
							| 
									
										
										
										
											2016-06-20 17:31:20 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	for _, notification := range query.Result { | 
					
						
							| 
									
										
										
										
											2016-11-06 22:40:02 +08:00
										 |  |  | 		result = append(result, &dtos.AlertNotification{ | 
					
						
							| 
									
										
										
										
											2016-09-06 03:33:05 +08:00
										 |  |  | 			Id:        notification.Id, | 
					
						
							|  |  |  | 			Name:      notification.Name, | 
					
						
							|  |  |  | 			Type:      notification.Type, | 
					
						
							|  |  |  | 			IsDefault: notification.IsDefault, | 
					
						
							|  |  |  | 			Created:   notification.Created, | 
					
						
							|  |  |  | 			Updated:   notification.Updated, | 
					
						
							| 
									
										
										
										
											2016-06-20 17:31:20 +08:00
										 |  |  | 		}) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return Json(200, result) | 
					
						
							| 
									
										
										
										
											2016-06-15 16:48:04 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-16 20:29:20 +08:00
										 |  |  | func GetAlertNotificationById(c *middleware.Context) Response { | 
					
						
							| 
									
										
										
										
											2016-07-22 22:45:17 +08:00
										 |  |  | 	query := &models.GetAlertNotificationsQuery{ | 
					
						
							|  |  |  | 		OrgId: c.OrgId, | 
					
						
							| 
									
										
										
										
											2016-06-16 20:29:20 +08:00
										 |  |  | 		Id:    c.ParamsInt64("notificationId"), | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := bus.Dispatch(query); err != nil { | 
					
						
							|  |  |  | 		return ApiError(500, "Failed to get alert notifications", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-06 14:42:35 +08:00
										 |  |  | 	return Json(200, query.Result) | 
					
						
							| 
									
										
										
										
											2016-06-16 20:29:20 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func CreateAlertNotification(c *middleware.Context, cmd models.CreateAlertNotificationCommand) Response { | 
					
						
							| 
									
										
										
										
											2016-07-22 22:45:17 +08:00
										 |  |  | 	cmd.OrgId = c.OrgId | 
					
						
							| 
									
										
										
										
											2016-06-15 16:48:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-16 20:29:20 +08:00
										 |  |  | 	if err := bus.Dispatch(&cmd); err != nil { | 
					
						
							| 
									
										
										
										
											2016-06-15 16:48:04 +08:00
										 |  |  | 		return ApiError(500, "Failed to create alert notification", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return Json(200, cmd.Result) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-16 20:29:20 +08:00
										 |  |  | func UpdateAlertNotification(c *middleware.Context, cmd models.UpdateAlertNotificationCommand) Response { | 
					
						
							| 
									
										
										
										
											2016-07-22 22:45:17 +08:00
										 |  |  | 	cmd.OrgId = c.OrgId | 
					
						
							| 
									
										
										
										
											2016-06-15 16:48:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-16 20:29:20 +08:00
										 |  |  | 	if err := bus.Dispatch(&cmd); err != nil { | 
					
						
							| 
									
										
										
										
											2016-06-15 16:48:04 +08:00
										 |  |  | 		return ApiError(500, "Failed to update alert notification", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return Json(200, cmd.Result) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-06-16 21:21:44 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 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) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-22 22:45:17 +08:00
										 |  |  | 	return ApiSuccess("Notification deleted") | 
					
						
							| 
									
										
										
										
											2016-06-16 21:21:44 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-08-30 15:32:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-05 20:43:53 +08:00
										 |  |  | //POST /api/alert-notifications/test
 | 
					
						
							|  |  |  | func NotificationTest(c *middleware.Context, dto dtos.NotificationTestCommand) Response { | 
					
						
							|  |  |  | 	cmd := &alerting.NotificationTestCommand{ | 
					
						
							|  |  |  | 		Name:     dto.Name, | 
					
						
							|  |  |  | 		Type:     dto.Type, | 
					
						
							|  |  |  | 		Settings: dto.Settings, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := bus.Dispatch(cmd); err != nil { | 
					
						
							| 
									
										
										
										
											2017-04-25 19:16:37 +08:00
										 |  |  | 		if err == models.ErrSmtpNotEnabled { | 
					
						
							|  |  |  | 			return ApiError(412, err.Error(), err) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-09-05 20:43:53 +08:00
										 |  |  | 		return ApiError(500, "Failed to send alert notifications", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return ApiSuccess("Test notification sent") | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-14 19:06:29 +08:00
										 |  |  | //POST /api/alerts/:alertId/pause
 | 
					
						
							| 
									
										
										
										
											2016-10-11 14:40:38 +08:00
										 |  |  | func PauseAlert(c *middleware.Context, dto dtos.PauseAlertCommand) Response { | 
					
						
							| 
									
										
										
										
											2016-12-19 23:07:55 +08:00
										 |  |  | 	alertId := c.ParamsInt64("alertId") | 
					
						
							| 
									
										
										
										
											2016-10-11 14:40:38 +08:00
										 |  |  | 	cmd := models.PauseAlertCommand{ | 
					
						
							| 
									
										
										
										
											2016-12-19 23:07:55 +08:00
										 |  |  | 		OrgId:    c.OrgId, | 
					
						
							|  |  |  | 		AlertIds: []int64{alertId}, | 
					
						
							|  |  |  | 		Paused:   dto.Paused, | 
					
						
							| 
									
										
										
										
											2016-10-10 20:26:09 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := bus.Dispatch(&cmd); err != nil { | 
					
						
							|  |  |  | 		return ApiError(500, "", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-01 22:58:38 +08:00
										 |  |  | 	var response models.AlertStateType = models.AlertStatePending | 
					
						
							| 
									
										
										
										
											2016-10-11 15:45:38 +08:00
										 |  |  | 	pausedState := "un paused" | 
					
						
							| 
									
										
										
										
											2016-10-10 20:26:09 +08:00
										 |  |  | 	if cmd.Paused { | 
					
						
							| 
									
										
										
										
											2016-10-10 21:17:14 +08:00
										 |  |  | 		response = models.AlertStatePaused | 
					
						
							| 
									
										
										
										
											2016-10-11 15:45:38 +08:00
										 |  |  | 		pausedState = "paused" | 
					
						
							| 
									
										
										
										
											2016-10-10 20:26:09 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-10 21:17:14 +08:00
										 |  |  | 	result := map[string]interface{}{ | 
					
						
							| 
									
										
										
										
											2016-12-19 23:07:55 +08:00
										 |  |  | 		"alertId": alertId, | 
					
						
							| 
									
										
										
										
											2016-10-10 21:17:14 +08:00
										 |  |  | 		"state":   response, | 
					
						
							| 
									
										
										
										
											2016-10-11 15:45:38 +08:00
										 |  |  | 		"message": "alert " + pausedState, | 
					
						
							| 
									
										
										
										
											2016-10-10 21:17:14 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return Json(200, result) | 
					
						
							| 
									
										
										
										
											2016-10-10 20:26:09 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-12-16 00:01:45 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-19 21:36:44 +08:00
										 |  |  | //POST /api/admin/pause-all-alerts
 | 
					
						
							|  |  |  | func PauseAllAlerts(c *middleware.Context, dto dtos.PauseAllAlertsCommand) Response { | 
					
						
							| 
									
										
										
										
											2016-12-19 20:24:45 +08:00
										 |  |  | 	updateCmd := models.PauseAllAlertCommand{ | 
					
						
							| 
									
										
										
										
											2016-12-16 23:07:23 +08:00
										 |  |  | 		Paused: dto.Paused, | 
					
						
							| 
									
										
										
										
											2016-12-16 00:01:45 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := bus.Dispatch(&updateCmd); err != nil { | 
					
						
							| 
									
										
										
										
											2016-12-16 22:19:13 +08:00
										 |  |  | 		return ApiError(500, "Failed to pause alerts", err) | 
					
						
							| 
									
										
										
										
											2016-12-16 00:01:45 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var response models.AlertStateType = models.AlertStatePending | 
					
						
							|  |  |  | 	pausedState := "un paused" | 
					
						
							|  |  |  | 	if updateCmd.Paused { | 
					
						
							|  |  |  | 		response = models.AlertStatePaused | 
					
						
							|  |  |  | 		pausedState = "paused" | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	result := map[string]interface{}{ | 
					
						
							|  |  |  | 		"state":          response, | 
					
						
							| 
									
										
										
										
											2016-12-19 23:44:59 +08:00
										 |  |  | 		"message":        "alerts " + pausedState, | 
					
						
							| 
									
										
										
										
											2016-12-16 00:01:45 +08:00
										 |  |  | 		"alertsAffected": updateCmd.ResultCount, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return Json(200, result) | 
					
						
							|  |  |  | } |