| 
									
										
										
										
											2016-09-08 17:25:45 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2017-10-12 16:12:15 +08:00
										 |  |  | 	"strings" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 17:25:45 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/api/dtos" | 
					
						
							| 
									
										
										
										
											2017-10-07 16:31:39 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/components/simplejson" | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 	m "github.com/grafana/grafana/pkg/models" | 
					
						
							| 
									
										
										
										
											2016-09-08 17:25:45 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/annotations" | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/guardian" | 
					
						
							| 
									
										
										
										
											2017-11-16 21:24:56 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/util" | 
					
						
							| 
									
										
										
										
											2016-09-08 17:25:45 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-08 00:54:50 +08:00
										 |  |  | func GetAnnotations(c *m.ReqContext) Response { | 
					
						
							| 
									
										
										
										
											2016-09-08 17:25:45 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	query := &annotations.ItemQuery{ | 
					
						
							| 
									
										
										
										
											2018-09-13 21:15:42 +08:00
										 |  |  | 		From:        c.QueryInt64("from"), | 
					
						
							|  |  |  | 		To:          c.QueryInt64("to"), | 
					
						
							|  |  |  | 		OrgId:       c.OrgId, | 
					
						
							|  |  |  | 		UserId:      c.QueryInt64("userId"), | 
					
						
							|  |  |  | 		AlertId:     c.QueryInt64("alertId"), | 
					
						
							|  |  |  | 		DashboardId: c.QueryInt64("dashboardId"), | 
					
						
							|  |  |  | 		PanelId:     c.QueryInt64("panelId"), | 
					
						
							|  |  |  | 		Limit:       c.QueryInt64("limit"), | 
					
						
							|  |  |  | 		Tags:        c.QueryStrings("tags"), | 
					
						
							|  |  |  | 		Type:        c.Query("type"), | 
					
						
							|  |  |  | 		MatchAny:    c.QueryBool("matchAny"), | 
					
						
							| 
									
										
										
										
											2016-09-08 17:25:45 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	repo := annotations.GetRepository() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	items, err := repo.Find(query) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		return Error(500, "Failed to get annotations", err) | 
					
						
							| 
									
										
										
										
											2016-09-08 17:25:45 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, item := range items { | 
					
						
							| 
									
										
										
										
											2017-10-07 16:31:39 +08:00
										 |  |  | 		if item.Email != "" { | 
					
						
							|  |  |  | 			item.AvatarUrl = dtos.GetGravatarUrl(item.Email) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-09-08 17:25:45 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 	return JSON(200, items) | 
					
						
							| 
									
										
										
										
											2016-09-08 17:25:45 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-10-14 15:33:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-18 16:13:02 +08:00
										 |  |  | type CreateAnnotationError struct { | 
					
						
							|  |  |  | 	message string | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (e *CreateAnnotationError) Error() string { | 
					
						
							|  |  |  | 	return e.message | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-08 00:54:50 +08:00
										 |  |  | func PostAnnotation(c *m.ReqContext, cmd dtos.PostAnnotationsCmd) Response { | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 	if canSave, err := canSaveByDashboardID(c, cmd.DashboardId); err != nil || !canSave { | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 		return dashboardGuardianResponse(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-11 01:22:58 +08:00
										 |  |  | 	repo := annotations.GetRepository() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-18 16:13:02 +08:00
										 |  |  | 	if cmd.Text == "" { | 
					
						
							|  |  |  | 		err := &CreateAnnotationError{"text field should not be empty"} | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		return Error(500, "Failed to save annotation", err) | 
					
						
							| 
									
										
										
										
											2017-10-18 16:13:02 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-11 01:22:58 +08:00
										 |  |  | 	item := annotations.Item{ | 
					
						
							|  |  |  | 		OrgId:       c.OrgId, | 
					
						
							| 
									
										
										
										
											2017-10-07 16:31:39 +08:00
										 |  |  | 		UserId:      c.UserId, | 
					
						
							| 
									
										
										
										
											2017-04-11 01:22:58 +08:00
										 |  |  | 		DashboardId: cmd.DashboardId, | 
					
						
							|  |  |  | 		PanelId:     cmd.PanelId, | 
					
						
							| 
									
										
										
										
											2018-03-22 23:21:47 +08:00
										 |  |  | 		Epoch:       cmd.Time, | 
					
						
							| 
									
										
										
										
											2017-04-11 01:22:58 +08:00
										 |  |  | 		Text:        cmd.Text, | 
					
						
							| 
									
										
										
										
											2017-10-07 16:31:39 +08:00
										 |  |  | 		Data:        cmd.Data, | 
					
						
							|  |  |  | 		Tags:        cmd.Tags, | 
					
						
							| 
									
										
										
										
											2017-04-11 01:22:58 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-12 22:26:34 +08:00
										 |  |  | 	if err := repo.Save(&item); err != nil { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		return Error(500, "Failed to save annotation", err) | 
					
						
							| 
									
										
										
										
											2017-04-11 01:22:58 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-16 21:24:56 +08:00
										 |  |  | 	startID := item.Id | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-12 22:26:34 +08:00
										 |  |  | 	// handle regions
 | 
					
						
							|  |  |  | 	if cmd.IsRegion { | 
					
						
							| 
									
										
										
										
											2017-11-16 21:24:56 +08:00
										 |  |  | 		item.RegionId = startID | 
					
						
							| 
									
										
										
										
											2017-04-12 22:26:34 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-07 16:31:39 +08:00
										 |  |  | 		if item.Data == nil { | 
					
						
							|  |  |  | 			item.Data = simplejson.New() | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-12 22:26:34 +08:00
										 |  |  | 		if err := repo.Update(&item); err != nil { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 			return Error(500, "Failed set regionId on annotation", err) | 
					
						
							| 
									
										
										
										
											2017-04-12 22:26:34 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		item.Id = 0 | 
					
						
							| 
									
										
										
										
											2018-03-22 23:21:47 +08:00
										 |  |  | 		item.Epoch = cmd.TimeEnd | 
					
						
							| 
									
										
										
										
											2017-04-12 22:26:34 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if err := repo.Save(&item); err != nil { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 			return Error(500, "Failed save annotation for region end time", err) | 
					
						
							| 
									
										
										
										
											2017-04-12 22:26:34 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-11-16 21:24:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		return JSON(200, util.DynMap{ | 
					
						
							| 
									
										
										
										
											2017-11-16 21:24:56 +08:00
										 |  |  | 			"message": "Annotation added", | 
					
						
							|  |  |  | 			"id":      startID, | 
					
						
							|  |  |  | 			"endId":   item.Id, | 
					
						
							|  |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2017-04-12 22:26:34 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 	return JSON(200, util.DynMap{ | 
					
						
							| 
									
										
										
										
											2017-11-16 21:24:56 +08:00
										 |  |  | 		"message": "Annotation added", | 
					
						
							|  |  |  | 		"id":      startID, | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2017-04-11 01:22:58 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-12 16:12:15 +08:00
										 |  |  | func formatGraphiteAnnotation(what string, data string) string { | 
					
						
							| 
									
										
										
										
											2017-10-18 16:13:02 +08:00
										 |  |  | 	text := what | 
					
						
							|  |  |  | 	if data != "" { | 
					
						
							|  |  |  | 		text = text + "\n" + data | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return text | 
					
						
							| 
									
										
										
										
											2017-10-12 16:12:15 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-08 00:54:50 +08:00
										 |  |  | func PostGraphiteAnnotation(c *m.ReqContext, cmd dtos.PostGraphiteAnnotationsCmd) Response { | 
					
						
							| 
									
										
										
										
											2017-10-12 16:12:15 +08:00
										 |  |  | 	repo := annotations.GetRepository() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-18 16:13:02 +08:00
										 |  |  | 	if cmd.What == "" { | 
					
						
							|  |  |  | 		err := &CreateAnnotationError{"what field should not be empty"} | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		return Error(500, "Failed to save Graphite annotation", err) | 
					
						
							| 
									
										
										
										
											2017-10-18 16:13:02 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-12 16:12:15 +08:00
										 |  |  | 	text := formatGraphiteAnnotation(cmd.What, cmd.Data) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Support tags in prior to Graphite 0.10.0 format (string of tags separated by space)
 | 
					
						
							|  |  |  | 	var tagsArray []string | 
					
						
							|  |  |  | 	switch tags := cmd.Tags.(type) { | 
					
						
							|  |  |  | 	case string: | 
					
						
							| 
									
										
										
										
											2017-10-18 16:13:02 +08:00
										 |  |  | 		if tags != "" { | 
					
						
							|  |  |  | 			tagsArray = strings.Split(tags, " ") | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			tagsArray = []string{} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-10-12 16:12:15 +08:00
										 |  |  | 	case []interface{}: | 
					
						
							|  |  |  | 		for _, t := range tags { | 
					
						
							|  |  |  | 			if tagStr, ok := t.(string); ok { | 
					
						
							|  |  |  | 				tagsArray = append(tagsArray, tagStr) | 
					
						
							|  |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2017-10-18 16:13:02 +08:00
										 |  |  | 				err := &CreateAnnotationError{"tag should be a string"} | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 				return Error(500, "Failed to save Graphite annotation", err) | 
					
						
							| 
									
										
										
										
											2017-10-12 16:12:15 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	default: | 
					
						
							| 
									
										
										
										
											2017-10-18 16:13:02 +08:00
										 |  |  | 		err := &CreateAnnotationError{"unsupported tags format"} | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		return Error(500, "Failed to save Graphite annotation", err) | 
					
						
							| 
									
										
										
										
											2017-10-12 16:12:15 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	item := annotations.Item{ | 
					
						
							|  |  |  | 		OrgId:  c.OrgId, | 
					
						
							|  |  |  | 		UserId: c.UserId, | 
					
						
							| 
									
										
										
										
											2018-04-09 19:58:09 +08:00
										 |  |  | 		Epoch:  cmd.When * 1000, | 
					
						
							| 
									
										
										
										
											2017-10-12 16:12:15 +08:00
										 |  |  | 		Text:   text, | 
					
						
							|  |  |  | 		Tags:   tagsArray, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := repo.Save(&item); err != nil { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		return Error(500, "Failed to save Graphite annotation", err) | 
					
						
							| 
									
										
										
										
											2017-10-12 16:12:15 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 	return JSON(200, util.DynMap{ | 
					
						
							| 
									
										
										
										
											2017-11-16 21:24:56 +08:00
										 |  |  | 		"message": "Graphite annotation added", | 
					
						
							|  |  |  | 		"id":      item.Id, | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2017-10-12 16:12:15 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-08 00:54:50 +08:00
										 |  |  | func UpdateAnnotation(c *m.ReqContext, cmd dtos.UpdateAnnotationsCmd) Response { | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 	annotationID := c.ParamsInt64(":annotationId") | 
					
						
							| 
									
										
										
										
											2017-10-07 16:31:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	repo := annotations.GetRepository() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 	if resp := canSave(c, repo, annotationID); resp != nil { | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 		return resp | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-07 16:31:39 +08:00
										 |  |  | 	item := annotations.Item{ | 
					
						
							|  |  |  | 		OrgId:  c.OrgId, | 
					
						
							|  |  |  | 		UserId: c.UserId, | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 		Id:     annotationID, | 
					
						
							| 
									
										
										
										
											2018-03-22 23:21:47 +08:00
										 |  |  | 		Epoch:  cmd.Time, | 
					
						
							| 
									
										
										
										
											2017-10-07 16:31:39 +08:00
										 |  |  | 		Text:   cmd.Text, | 
					
						
							|  |  |  | 		Tags:   cmd.Tags, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := repo.Update(&item); err != nil { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		return Error(500, "Failed to update annotation", err) | 
					
						
							| 
									
										
										
										
											2017-10-07 16:31:39 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if cmd.IsRegion { | 
					
						
							|  |  |  | 		itemRight := item | 
					
						
							|  |  |  | 		itemRight.RegionId = item.Id | 
					
						
							| 
									
										
										
										
											2018-03-22 23:21:47 +08:00
										 |  |  | 		itemRight.Epoch = cmd.TimeEnd | 
					
						
							| 
									
										
										
										
											2017-10-07 16:31:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// We don't know id of region right event, so set it to 0 and find then using query like
 | 
					
						
							|  |  |  | 		// ... WHERE region_id = <item.RegionId> AND id != <item.RegionId> ...
 | 
					
						
							|  |  |  | 		itemRight.Id = 0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if err := repo.Update(&itemRight); err != nil { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 			return Error(500, "Failed to update annotation for region end time", err) | 
					
						
							| 
									
										
										
										
											2017-10-07 16:31:39 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 	return Success("Annotation updated") | 
					
						
							| 
									
										
										
										
											2017-10-07 16:31:39 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-08 00:54:50 +08:00
										 |  |  | func DeleteAnnotations(c *m.ReqContext, cmd dtos.DeleteAnnotationsCmd) Response { | 
					
						
							| 
									
										
										
										
											2016-10-14 15:33:16 +08:00
										 |  |  | 	repo := annotations.GetRepository() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	err := repo.Delete(&annotations.DeleteParams{ | 
					
						
							| 
									
										
										
										
											2018-06-25 22:02:34 +08:00
										 |  |  | 		OrgId:       c.OrgId, | 
					
						
							| 
									
										
										
										
											2018-06-25 19:58:49 +08:00
										 |  |  | 		Id:          cmd.AnnotationId, | 
					
						
							|  |  |  | 		RegionId:    cmd.RegionId, | 
					
						
							| 
									
										
										
										
											2016-10-14 15:33:16 +08:00
										 |  |  | 		DashboardId: cmd.DashboardId, | 
					
						
							|  |  |  | 		PanelId:     cmd.PanelId, | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		return Error(500, "Failed to delete annotations", err) | 
					
						
							| 
									
										
										
										
											2016-10-14 15:33:16 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 	return Success("Annotations deleted") | 
					
						
							| 
									
										
										
										
											2016-10-14 15:33:16 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-10-07 16:31:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | func DeleteAnnotationByID(c *m.ReqContext) Response { | 
					
						
							| 
									
										
										
										
											2017-10-07 16:31:39 +08:00
										 |  |  | 	repo := annotations.GetRepository() | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 	annotationID := c.ParamsInt64(":annotationId") | 
					
						
							| 
									
										
										
										
											2017-10-07 16:31:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 	if resp := canSave(c, repo, annotationID); resp != nil { | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 		return resp | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-07 16:31:39 +08:00
										 |  |  | 	err := repo.Delete(&annotations.DeleteParams{ | 
					
						
							| 
									
										
										
										
											2018-06-25 22:02:34 +08:00
										 |  |  | 		OrgId: c.OrgId, | 
					
						
							|  |  |  | 		Id:    annotationID, | 
					
						
							| 
									
										
										
										
											2017-10-07 16:31:39 +08:00
										 |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		return Error(500, "Failed to delete annotation", err) | 
					
						
							| 
									
										
										
										
											2017-10-07 16:31:39 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 	return Success("Annotation deleted") | 
					
						
							| 
									
										
										
										
											2017-10-07 16:31:39 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-08 00:54:50 +08:00
										 |  |  | func DeleteAnnotationRegion(c *m.ReqContext) Response { | 
					
						
							| 
									
										
										
										
											2017-10-07 16:31:39 +08:00
										 |  |  | 	repo := annotations.GetRepository() | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 	regionID := c.ParamsInt64(":regionId") | 
					
						
							| 
									
										
										
										
											2017-10-07 16:31:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 	if resp := canSave(c, repo, regionID); resp != nil { | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 		return resp | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-07 16:31:39 +08:00
										 |  |  | 	err := repo.Delete(&annotations.DeleteParams{ | 
					
						
							| 
									
										
										
										
											2018-06-25 22:02:34 +08:00
										 |  |  | 		OrgId:    c.OrgId, | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 		RegionId: regionID, | 
					
						
							| 
									
										
										
										
											2017-10-07 16:31:39 +08:00
										 |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		return Error(500, "Failed to delete annotation region", err) | 
					
						
							| 
									
										
										
										
											2017-10-07 16:31:39 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 	return Success("Annotation region deleted") | 
					
						
							| 
									
										
										
										
											2017-10-07 16:31:39 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | func canSaveByDashboardID(c *m.ReqContext, dashboardID int64) (bool, error) { | 
					
						
							|  |  |  | 	if dashboardID == 0 && !c.SignedInUser.HasRole(m.ROLE_EDITOR) { | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 		return false, nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-26 20:12:20 +08:00
										 |  |  | 	if dashboardID != 0 { | 
					
						
							|  |  |  | 		guard := guardian.New(dashboardID, c.OrgId, c.SignedInUser) | 
					
						
							|  |  |  | 		if canEdit, err := guard.CanEdit(); err != nil || !canEdit { | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 			return false, err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return true, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | func canSave(c *m.ReqContext, repo annotations.Repository, annotationID int64) Response { | 
					
						
							|  |  |  | 	items, err := repo.Find(&annotations.ItemQuery{AnnotationId: annotationID, OrgId: c.OrgId}) | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if err != nil || len(items) == 0 { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		return Error(500, "Could not find annotation to update", err) | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 	dashboardID := items[0].DashboardId | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 	if canSave, err := canSaveByDashboardID(c, dashboardID); err != nil || !canSave { | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 		return dashboardGuardianResponse(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } |