| 
									
										
										
										
											2016-04-26 23:36:50 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2016-07-21 16:29:11 +08:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2018-06-01 20:36:40 +08:00
										 |  |  | 	"strconv" | 
					
						
							| 
									
										
										
										
											2016-07-21 16:29:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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" | 
					
						
							| 
									
										
										
										
											2018-03-07 06:59:45 +08:00
										 |  |  | 	m "github.com/grafana/grafana/pkg/models" | 
					
						
							| 
									
										
										
										
											2016-07-20 22:13:36 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/alerting" | 
					
						
							| 
									
										
										
										
											2018-01-30 21:41:25 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/guardian" | 
					
						
							| 
									
										
										
										
											2018-06-01 20:36:40 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/search" | 
					
						
							| 
									
										
										
										
											2016-04-26 23:36:50 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-08 00:54:50 +08:00
										 |  |  | func ValidateOrgAlert(c *m.ReqContext) { | 
					
						
							| 
									
										
										
										
											2016-05-02 22:07:19 +08:00
										 |  |  | 	id := c.ParamsInt64(":alertId") | 
					
						
							| 
									
										
										
										
											2018-03-07 06:59:45 +08:00
										 |  |  | 	query := m.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 | 
					
						
							| 
									
										
										
										
											2017-10-23 15:56:52 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-04-26 23:36:50 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-08 00:54:50 +08:00
										 |  |  | func GetAlertStatesForDashboard(c *m.ReqContext) Response { | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 	dashboardID := c.QueryInt64("dashboardId") | 
					
						
							| 
									
										
										
										
											2016-09-30 23:37:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 	if dashboardID == 0 { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		return Error(400, "Missing query parameter dashboardId", nil) | 
					
						
							| 
									
										
										
										
											2016-09-30 23:37:47 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-07 06:59:45 +08:00
										 |  |  | 	query := m.GetAlertStatesForDashboardQuery{ | 
					
						
							| 
									
										
										
										
											2016-09-30 23:37:47 +08:00
										 |  |  | 		OrgId:       c.OrgId, | 
					
						
							|  |  |  | 		DashboardId: c.QueryInt64("dashboardId"), | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := bus.Dispatch(&query); err != nil { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		return Error(500, "Failed to fetch alert states", err) | 
					
						
							| 
									
										
										
										
											2016-09-30 23:37:47 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 	return JSON(200, query.Result) | 
					
						
							| 
									
										
										
										
											2016-09-30 23:37:47 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-14 14:36:44 +08:00
										 |  |  | // GET /api/alerts
 | 
					
						
							| 
									
										
										
										
											2018-03-08 00:54:50 +08:00
										 |  |  | func GetAlerts(c *m.ReqContext) Response { | 
					
						
							| 
									
										
										
										
											2018-06-01 20:36:40 +08:00
										 |  |  | 	dashboardQuery := c.Query("dashboardQuery") | 
					
						
							|  |  |  | 	dashboardTags := c.QueryStrings("dashboardTag") | 
					
						
							|  |  |  | 	stringDashboardIDs := c.QueryStrings("dashboardId") | 
					
						
							|  |  |  | 	stringFolderIDs := c.QueryStrings("folderId") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	dashboardIDs := make([]int64, 0) | 
					
						
							|  |  |  | 	for _, id := range stringDashboardIDs { | 
					
						
							|  |  |  | 		dashboardID, err := strconv.ParseInt(id, 10, 64) | 
					
						
							|  |  |  | 		if err == nil { | 
					
						
							|  |  |  | 			dashboardIDs = append(dashboardIDs, dashboardID) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if dashboardQuery != "" || len(dashboardTags) > 0 || len(stringFolderIDs) > 0 { | 
					
						
							|  |  |  | 		folderIDs := make([]int64, 0) | 
					
						
							|  |  |  | 		for _, id := range stringFolderIDs { | 
					
						
							|  |  |  | 			folderID, err := strconv.ParseInt(id, 10, 64) | 
					
						
							|  |  |  | 			if err == nil { | 
					
						
							|  |  |  | 				folderIDs = append(folderIDs, folderID) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		searchQuery := search.Query{ | 
					
						
							|  |  |  | 			Title:        dashboardQuery, | 
					
						
							|  |  |  | 			Tags:         dashboardTags, | 
					
						
							|  |  |  | 			SignedInUser: c.SignedInUser, | 
					
						
							|  |  |  | 			Limit:        1000, | 
					
						
							|  |  |  | 			OrgId:        c.OrgId, | 
					
						
							|  |  |  | 			DashboardIds: dashboardIDs, | 
					
						
							|  |  |  | 			Type:         string(search.DashHitDB), | 
					
						
							|  |  |  | 			FolderIds:    folderIDs, | 
					
						
							| 
									
										
										
										
											2018-06-01 18:39:44 +08:00
										 |  |  | 			Permission:   m.PERMISSION_VIEW, | 
					
						
							| 
									
										
										
										
											2018-06-01 20:36:40 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		err := bus.Dispatch(&searchQuery) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return Error(500, "List alerts failed", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for _, d := range searchQuery.Result { | 
					
						
							|  |  |  | 			if d.Type == search.DashHitDB && d.Id > 0 { | 
					
						
							|  |  |  | 				dashboardIDs = append(dashboardIDs, d.Id) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// if we didn't find any dashboards, return empty result
 | 
					
						
							|  |  |  | 		if len(dashboardIDs) == 0 { | 
					
						
							|  |  |  | 			return JSON(200, []*m.AlertListItemDTO{}) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-07 06:59:45 +08:00
										 |  |  | 	query := m.GetAlertsQuery{ | 
					
						
							| 
									
										
										
										
											2018-06-01 20:36:40 +08:00
										 |  |  | 		OrgId:        c.OrgId, | 
					
						
							|  |  |  | 		DashboardIDs: dashboardIDs, | 
					
						
							|  |  |  | 		PanelId:      c.QueryInt64("panelId"), | 
					
						
							|  |  |  | 		Limit:        c.QueryInt64("limit"), | 
					
						
							|  |  |  | 		User:         c.SignedInUser, | 
					
						
							|  |  |  | 		Query:        c.Query("query"), | 
					
						
							| 
									
										
										
										
											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 { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		return Error(500, "List alerts failed", err) | 
					
						
							| 
									
										
										
										
											2016-04-26 23:36:50 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-16 20:56:04 +08:00
										 |  |  | 	for _, alert := range query.Result { | 
					
						
							| 
									
										
										
										
											2018-03-07 06:59:45 +08:00
										 |  |  | 		alert.Url = m.GetDashboardUrl(alert.DashboardUid, alert.DashboardSlug) | 
					
						
							| 
									
										
										
										
											2018-01-31 17:47:31 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 	return JSON(200, query.Result) | 
					
						
							| 
									
										
										
										
											2016-04-26 23:36:50 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-20 22:13:36 +08:00
										 |  |  | // POST /api/alerts/test
 | 
					
						
							| 
									
										
										
										
											2018-03-08 00:54:50 +08:00
										 |  |  | func AlertTest(c *m.ReqContext, dto dtos.AlertTestCommand) Response { | 
					
						
							| 
									
										
										
										
											2016-11-23 21:55:10 +08:00
										 |  |  | 	if _, idErr := dto.Dashboard.Get("id").Int64(); idErr != nil { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		return Error(400, "The dashboard needs to be saved at least once before you can test an alert rule", nil) | 
					
						
							| 
									
										
										
										
											2016-11-23 21:55:10 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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, | 
					
						
							| 
									
										
										
										
											2018-11-05 21:25:19 +08:00
										 |  |  | 		User:      c.SignedInUser, | 
					
						
							| 
									
										
										
										
											2016-07-20 22:13:36 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := bus.Dispatch(&backendCmd); err != nil { | 
					
						
							| 
									
										
										
										
											2016-07-27 22:29:28 +08:00
										 |  |  | 		if validationErr, ok := err.(alerting.ValidationError); ok { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 			return Error(422, validationErr.Error(), nil) | 
					
						
							| 
									
										
										
										
											2016-07-21 19:09:12 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-11-05 21:25:19 +08:00
										 |  |  | 		if err == m.ErrDataSourceAccessDenied { | 
					
						
							|  |  |  | 			return Error(403, "Access denied to datasource", err) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		return Error(500, "Failed to test rule", err) | 
					
						
							| 
									
										
										
										
											2016-07-20 22:13:36 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +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
 | 
					
						
							| 
									
										
										
										
											2018-03-08 00:54:50 +08:00
										 |  |  | func GetAlert(c *m.ReqContext) Response { | 
					
						
							| 
									
										
										
										
											2016-05-03 00:00:10 +08:00
										 |  |  | 	id := c.ParamsInt64(":alertId") | 
					
						
							| 
									
										
										
										
											2018-03-07 06:59:45 +08:00
										 |  |  | 	query := m.GetAlertByIdQuery{Id: id} | 
					
						
							| 
									
										
										
										
											2016-04-26 23:36:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if err := bus.Dispatch(&query); err != nil { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		return Error(500, "List alerts failed", err) | 
					
						
							| 
									
										
										
										
											2016-04-26 23:36:50 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 	return JSON(200, &query.Result) | 
					
						
							| 
									
										
										
										
											2016-04-26 23:36:50 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-04-28 14:23:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-08 00:54:50 +08:00
										 |  |  | func GetAlertNotifiers(c *m.ReqContext) Response { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 	return JSON(200, alerting.GetNotifiers()) | 
					
						
							| 
									
										
										
										
											2017-01-06 19:04:25 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-08 00:54:50 +08:00
										 |  |  | func GetAlertNotifications(c *m.ReqContext) Response { | 
					
						
							| 
									
										
										
										
											2018-03-07 06:59:45 +08:00
										 |  |  | 	query := &m.GetAllAlertNotificationsQuery{OrgId: c.OrgId} | 
					
						
							| 
									
										
										
										
											2016-06-15 16:48:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if err := bus.Dispatch(query); err != nil { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		return Error(500, "Failed to get alert notifications", err) | 
					
						
							| 
									
										
										
										
											2016-06-15 16:48:04 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 { | 
					
						
							| 
									
										
										
										
											2018-06-05 04:19:27 +08:00
										 |  |  | 		result = append(result, dtos.NewAlertNotification(notification)) | 
					
						
							| 
									
										
										
										
											2016-06-20 17:31:20 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 	return JSON(200, result) | 
					
						
							| 
									
										
										
										
											2016-06-15 16:48:04 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | func GetAlertNotificationByID(c *m.ReqContext) Response { | 
					
						
							| 
									
										
										
										
											2018-03-07 06:59:45 +08:00
										 |  |  | 	query := &m.GetAlertNotificationsQuery{ | 
					
						
							| 
									
										
										
										
											2016-07-22 22:45:17 +08:00
										 |  |  | 		OrgId: c.OrgId, | 
					
						
							| 
									
										
										
										
											2016-06-16 20:29:20 +08:00
										 |  |  | 		Id:    c.ParamsInt64("notificationId"), | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := bus.Dispatch(query); err != nil { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		return Error(500, "Failed to get alert notifications", err) | 
					
						
							| 
									
										
										
										
											2016-06-16 20:29:20 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-14 03:30:20 +08:00
										 |  |  | 	if query.Result == nil { | 
					
						
							|  |  |  | 		return Error(404, "Alert notification not found", nil) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-05 04:19:27 +08:00
										 |  |  | 	return JSON(200, dtos.NewAlertNotification(query.Result)) | 
					
						
							| 
									
										
										
										
											2016-06-16 20:29:20 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-08 00:54:50 +08:00
										 |  |  | func CreateAlertNotification(c *m.ReqContext, cmd m.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 { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		return Error(500, "Failed to create alert notification", err) | 
					
						
							| 
									
										
										
										
											2016-06-15 16:48:04 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-05 04:19:27 +08:00
										 |  |  | 	return JSON(200, dtos.NewAlertNotification(cmd.Result)) | 
					
						
							| 
									
										
										
										
											2016-06-15 16:48:04 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-08 00:54:50 +08:00
										 |  |  | func UpdateAlertNotification(c *m.ReqContext, cmd m.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 { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		return Error(500, "Failed to update alert notification", err) | 
					
						
							| 
									
										
										
										
											2016-06-15 16:48:04 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-05 04:19:27 +08:00
										 |  |  | 	return JSON(200, dtos.NewAlertNotification(cmd.Result)) | 
					
						
							| 
									
										
										
										
											2016-06-15 16:48:04 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-06-16 21:21:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-08 00:54:50 +08:00
										 |  |  | func DeleteAlertNotification(c *m.ReqContext) Response { | 
					
						
							| 
									
										
										
										
											2018-03-07 06:59:45 +08:00
										 |  |  | 	cmd := m.DeleteAlertNotificationCommand{ | 
					
						
							| 
									
										
										
										
											2016-06-16 21:21:44 +08:00
										 |  |  | 		OrgId: c.OrgId, | 
					
						
							|  |  |  | 		Id:    c.ParamsInt64("notificationId"), | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := bus.Dispatch(&cmd); err != nil { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		return Error(500, "Failed to delete alert notification", err) | 
					
						
							| 
									
										
										
										
											2016-06-16 21:21:44 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 	return Success("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
 | 
					
						
							| 
									
										
										
										
											2018-03-08 00:54:50 +08:00
										 |  |  | func NotificationTest(c *m.ReqContext, dto dtos.NotificationTestCommand) Response { | 
					
						
							| 
									
										
										
										
											2016-09-05 20:43:53 +08:00
										 |  |  | 	cmd := &alerting.NotificationTestCommand{ | 
					
						
							|  |  |  | 		Name:     dto.Name, | 
					
						
							|  |  |  | 		Type:     dto.Type, | 
					
						
							|  |  |  | 		Settings: dto.Settings, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := bus.Dispatch(cmd); err != nil { | 
					
						
							| 
									
										
										
										
											2018-03-07 06:59:45 +08:00
										 |  |  | 		if err == m.ErrSmtpNotEnabled { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 			return Error(412, err.Error(), err) | 
					
						
							| 
									
										
										
										
											2017-04-25 19:16:37 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		return Error(500, "Failed to send alert notifications", err) | 
					
						
							| 
									
										
										
										
											2016-09-05 20:43:53 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 	return Success("Test notification sent") | 
					
						
							| 
									
										
										
										
											2016-09-05 20:43:53 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-14 19:06:29 +08:00
										 |  |  | //POST /api/alerts/:alertId/pause
 | 
					
						
							| 
									
										
										
										
											2018-03-08 00:54:50 +08:00
										 |  |  | func PauseAlert(c *m.ReqContext, dto dtos.PauseAlertCommand) Response { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 	alertID := c.ParamsInt64("alertId") | 
					
						
							| 
									
										
										
										
											2018-01-30 21:41:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 	query := m.GetAlertByIdQuery{Id: alertID} | 
					
						
							| 
									
										
										
										
											2018-01-30 21:41:25 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if err := bus.Dispatch(&query); err != nil { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		return Error(500, "Get Alert failed", err) | 
					
						
							| 
									
										
										
										
											2018-01-30 21:41:25 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-19 18:12:56 +08:00
										 |  |  | 	guardian := guardian.New(query.Result.DashboardId, c.OrgId, c.SignedInUser) | 
					
						
							| 
									
										
										
										
											2018-01-30 21:41:25 +08:00
										 |  |  | 	if canEdit, err := guardian.CanEdit(); err != nil || !canEdit { | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 			return Error(500, "Error while checking permissions for Alert", err) | 
					
						
							| 
									
										
										
										
											2018-01-30 21:41:25 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		return Error(403, "Access denied to this dashboard and alert", nil) | 
					
						
							| 
									
										
										
										
											2018-01-30 21:41:25 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-07 06:59:45 +08:00
										 |  |  | 	cmd := m.PauseAlertCommand{ | 
					
						
							| 
									
										
										
										
											2016-12-19 23:07:55 +08:00
										 |  |  | 		OrgId:    c.OrgId, | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		AlertIds: []int64{alertID}, | 
					
						
							| 
									
										
										
										
											2016-12-19 23:07:55 +08:00
										 |  |  | 		Paused:   dto.Paused, | 
					
						
							| 
									
										
										
										
											2016-10-10 20:26:09 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := bus.Dispatch(&cmd); err != nil { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		return Error(500, "", err) | 
					
						
							| 
									
										
										
										
											2016-10-10 20:26:09 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-05 17:23:43 +08:00
										 |  |  | 	var response m.AlertStateType = m.AlertStateUnknown | 
					
						
							| 
									
										
										
										
											2017-12-31 21:16:19 +08:00
										 |  |  | 	pausedState := "un-paused" | 
					
						
							| 
									
										
										
										
											2016-10-10 20:26:09 +08:00
										 |  |  | 	if cmd.Paused { | 
					
						
							| 
									
										
										
										
											2018-03-07 06:59:45 +08:00
										 |  |  | 		response = m.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{}{ | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		"alertId": alertID, | 
					
						
							| 
									
										
										
										
											2016-10-10 21:17:14 +08:00
										 |  |  | 		"state":   response, | 
					
						
							| 
									
										
										
										
											2017-12-31 21:16:19 +08:00
										 |  |  | 		"message": "Alert " + pausedState, | 
					
						
							| 
									
										
										
										
											2016-10-10 21:17:14 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +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
 | 
					
						
							| 
									
										
										
										
											2018-03-08 00:54:50 +08:00
										 |  |  | func PauseAllAlerts(c *m.ReqContext, dto dtos.PauseAllAlertsCommand) Response { | 
					
						
							| 
									
										
										
										
											2018-03-07 06:59:45 +08:00
										 |  |  | 	updateCmd := m.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 { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		return Error(500, "Failed to pause alerts", err) | 
					
						
							| 
									
										
										
										
											2016-12-16 00:01:45 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-07 06:59:45 +08:00
										 |  |  | 	var response m.AlertStateType = m.AlertStatePending | 
					
						
							| 
									
										
										
										
											2016-12-16 00:01:45 +08:00
										 |  |  | 	pausedState := "un paused" | 
					
						
							|  |  |  | 	if updateCmd.Paused { | 
					
						
							| 
									
										
										
										
											2018-03-07 06:59:45 +08:00
										 |  |  | 		response = m.AlertStatePaused | 
					
						
							| 
									
										
										
										
											2016-12-16 00:01:45 +08:00
										 |  |  | 		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, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 	return JSON(200, result) | 
					
						
							| 
									
										
										
										
											2016-12-16 00:01:45 +08:00
										 |  |  | } |