2016-09-08 04:25:45 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2021-03-29 08:47:16 -05:00
|
|
|
"errors"
|
2017-10-12 03:12:15 -05:00
|
|
|
"strings"
|
|
|
|
|
2016-09-08 04:25:45 -05:00
|
|
|
"github.com/grafana/grafana/pkg/api/dtos"
|
2021-01-15 07:43:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/api/response"
|
2020-03-04 05:57:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2016-09-08 04:25:45 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/annotations"
|
2017-12-20 17:52:21 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/guardian"
|
2017-11-16 07:24:56 -06:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
2016-09-08 04:25:45 -05:00
|
|
|
)
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
func GetAnnotations(c *models.ReqContext) response.Response {
|
2016-09-08 04:25:45 -05:00
|
|
|
query := &annotations.ItemQuery{
|
2018-09-13 08:15:42 -05: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 04:25:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
repo := annotations.GetRepository()
|
|
|
|
|
|
|
|
items, err := repo.Find(query)
|
|
|
|
if err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to get annotations", err)
|
2016-09-08 04:25:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range items {
|
2017-10-07 03:31:39 -05:00
|
|
|
if item.Email != "" {
|
|
|
|
item.AvatarUrl = dtos.GetGravatarUrl(item.Email)
|
|
|
|
}
|
2016-09-08 04:25:45 -05:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.JSON(200, items)
|
2016-09-08 04:25:45 -05:00
|
|
|
}
|
2016-10-14 02:33:16 -05:00
|
|
|
|
2017-10-18 03:13:02 -05:00
|
|
|
type CreateAnnotationError struct {
|
|
|
|
message string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *CreateAnnotationError) Error() string {
|
|
|
|
return e.message
|
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
func PostAnnotation(c *models.ReqContext, cmd dtos.PostAnnotationsCmd) response.Response {
|
2018-03-22 06:37:35 -05:00
|
|
|
if canSave, err := canSaveByDashboardID(c, cmd.DashboardId); err != nil || !canSave {
|
2017-12-20 17:52:21 -06:00
|
|
|
return dashboardGuardianResponse(err)
|
|
|
|
}
|
|
|
|
|
2017-04-10 12:22:58 -05:00
|
|
|
repo := annotations.GetRepository()
|
|
|
|
|
2017-10-18 03:13:02 -05:00
|
|
|
if cmd.Text == "" {
|
|
|
|
err := &CreateAnnotationError{"text field should not be empty"}
|
2021-03-29 08:47:16 -05:00
|
|
|
return response.Error(400, "Failed to save annotation", err)
|
2017-10-18 03:13:02 -05:00
|
|
|
}
|
|
|
|
|
2017-04-10 12:22:58 -05:00
|
|
|
item := annotations.Item{
|
|
|
|
OrgId: c.OrgId,
|
2017-10-07 03:31:39 -05:00
|
|
|
UserId: c.UserId,
|
2017-04-10 12:22:58 -05:00
|
|
|
DashboardId: cmd.DashboardId,
|
|
|
|
PanelId: cmd.PanelId,
|
2018-03-22 10:21:47 -05:00
|
|
|
Epoch: cmd.Time,
|
2019-08-16 03:49:30 -05:00
|
|
|
EpochEnd: cmd.TimeEnd,
|
2017-04-10 12:22:58 -05:00
|
|
|
Text: cmd.Text,
|
2017-10-07 03:31:39 -05:00
|
|
|
Data: cmd.Data,
|
|
|
|
Tags: cmd.Tags,
|
2017-04-10 12:22:58 -05:00
|
|
|
}
|
|
|
|
|
2017-04-12 09:26:34 -05:00
|
|
|
if err := repo.Save(&item); err != nil {
|
2021-03-29 08:47:16 -05:00
|
|
|
if errors.Is(err, annotations.ErrTimerangeMissing) {
|
|
|
|
return response.Error(400, "Failed to save annotation", err)
|
|
|
|
}
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to save annotation", err)
|
2017-04-10 12:22:58 -05:00
|
|
|
}
|
|
|
|
|
2017-11-16 07:24:56 -06:00
|
|
|
startID := item.Id
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.JSON(200, util.DynMap{
|
2017-11-16 07:24:56 -06:00
|
|
|
"message": "Annotation added",
|
|
|
|
"id": startID,
|
|
|
|
})
|
2017-04-10 12:22:58 -05:00
|
|
|
}
|
|
|
|
|
2017-10-12 03:12:15 -05:00
|
|
|
func formatGraphiteAnnotation(what string, data string) string {
|
2017-10-18 03:13:02 -05:00
|
|
|
text := what
|
|
|
|
if data != "" {
|
|
|
|
text = text + "\n" + data
|
|
|
|
}
|
|
|
|
return text
|
2017-10-12 03:12:15 -05:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
func PostGraphiteAnnotation(c *models.ReqContext, cmd dtos.PostGraphiteAnnotationsCmd) response.Response {
|
2017-10-12 03:12:15 -05:00
|
|
|
repo := annotations.GetRepository()
|
|
|
|
|
2017-10-18 03:13:02 -05:00
|
|
|
if cmd.What == "" {
|
|
|
|
err := &CreateAnnotationError{"what field should not be empty"}
|
2021-03-29 08:47:16 -05:00
|
|
|
return response.Error(400, "Failed to save Graphite annotation", err)
|
2017-10-18 03:13:02 -05:00
|
|
|
}
|
|
|
|
|
2017-10-12 03:12:15 -05: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 03:13:02 -05:00
|
|
|
if tags != "" {
|
|
|
|
tagsArray = strings.Split(tags, " ")
|
|
|
|
} else {
|
|
|
|
tagsArray = []string{}
|
|
|
|
}
|
2017-10-12 03:12:15 -05:00
|
|
|
case []interface{}:
|
|
|
|
for _, t := range tags {
|
|
|
|
if tagStr, ok := t.(string); ok {
|
|
|
|
tagsArray = append(tagsArray, tagStr)
|
|
|
|
} else {
|
2017-10-18 03:13:02 -05:00
|
|
|
err := &CreateAnnotationError{"tag should be a string"}
|
2021-03-29 08:47:16 -05:00
|
|
|
return response.Error(400, "Failed to save Graphite annotation", err)
|
2017-10-12 03:12:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
2017-10-18 03:13:02 -05:00
|
|
|
err := &CreateAnnotationError{"unsupported tags format"}
|
2021-03-29 08:47:16 -05:00
|
|
|
return response.Error(400, "Failed to save Graphite annotation", err)
|
2017-10-12 03:12:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
item := annotations.Item{
|
|
|
|
OrgId: c.OrgId,
|
|
|
|
UserId: c.UserId,
|
2018-04-09 06:58:09 -05:00
|
|
|
Epoch: cmd.When * 1000,
|
2017-10-12 03:12:15 -05:00
|
|
|
Text: text,
|
|
|
|
Tags: tagsArray,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := repo.Save(&item); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to save Graphite annotation", err)
|
2017-10-12 03:12:15 -05:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.JSON(200, util.DynMap{
|
2017-11-16 07:24:56 -06:00
|
|
|
"message": "Graphite annotation added",
|
|
|
|
"id": item.Id,
|
|
|
|
})
|
2017-10-12 03:12:15 -05:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
func UpdateAnnotation(c *models.ReqContext, cmd dtos.UpdateAnnotationsCmd) response.Response {
|
2018-03-22 06:37:35 -05:00
|
|
|
annotationID := c.ParamsInt64(":annotationId")
|
2017-10-07 03:31:39 -05:00
|
|
|
|
|
|
|
repo := annotations.GetRepository()
|
|
|
|
|
2018-03-22 06:37:35 -05:00
|
|
|
if resp := canSave(c, repo, annotationID); resp != nil {
|
2017-12-20 17:52:21 -06:00
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2017-10-07 03:31:39 -05:00
|
|
|
item := annotations.Item{
|
2019-08-16 03:49:30 -05:00
|
|
|
OrgId: c.OrgId,
|
|
|
|
UserId: c.UserId,
|
|
|
|
Id: annotationID,
|
|
|
|
Epoch: cmd.Time,
|
|
|
|
EpochEnd: cmd.TimeEnd,
|
|
|
|
Text: cmd.Text,
|
|
|
|
Tags: cmd.Tags,
|
2017-10-07 03:31:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := repo.Update(&item); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to update annotation", err)
|
2017-10-07 03:31:39 -05:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Success("Annotation updated")
|
2017-10-07 03:31:39 -05:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
func PatchAnnotation(c *models.ReqContext, cmd dtos.PatchAnnotationsCmd) response.Response {
|
2019-01-27 05:49:22 -06:00
|
|
|
annotationID := c.ParamsInt64(":annotationId")
|
|
|
|
|
|
|
|
repo := annotations.GetRepository()
|
|
|
|
|
|
|
|
if resp := canSave(c, repo, annotationID); resp != nil {
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
items, err := repo.Find(&annotations.ItemQuery{AnnotationId: annotationID, OrgId: c.OrgId})
|
|
|
|
|
|
|
|
if err != nil || len(items) == 0 {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(404, "Could not find annotation to update", err)
|
2019-01-27 05:49:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
existing := annotations.Item{
|
|
|
|
OrgId: c.OrgId,
|
|
|
|
UserId: c.UserId,
|
|
|
|
Id: annotationID,
|
|
|
|
Epoch: items[0].Time,
|
2019-08-16 03:49:30 -05:00
|
|
|
EpochEnd: items[0].TimeEnd,
|
2019-01-27 05:49:22 -06:00
|
|
|
Text: items[0].Text,
|
|
|
|
Tags: items[0].Tags,
|
|
|
|
}
|
|
|
|
|
|
|
|
if cmd.Tags != nil {
|
|
|
|
existing.Tags = cmd.Tags
|
|
|
|
}
|
|
|
|
|
|
|
|
if cmd.Text != "" && cmd.Text != existing.Text {
|
|
|
|
existing.Text = cmd.Text
|
|
|
|
}
|
|
|
|
|
|
|
|
if cmd.Time > 0 && cmd.Time != existing.Epoch {
|
|
|
|
existing.Epoch = cmd.Time
|
|
|
|
}
|
|
|
|
|
2019-08-16 03:49:30 -05:00
|
|
|
if cmd.TimeEnd > 0 && cmd.TimeEnd != existing.EpochEnd {
|
|
|
|
existing.EpochEnd = cmd.TimeEnd
|
2019-01-27 05:49:22 -06:00
|
|
|
}
|
|
|
|
|
2019-08-16 03:49:30 -05:00
|
|
|
if err := repo.Update(&existing); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to update annotation", err)
|
2019-01-27 05:49:22 -06:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Success("Annotation patched")
|
2019-01-27 05:49:22 -06:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
func DeleteAnnotations(c *models.ReqContext, cmd dtos.DeleteAnnotationsCmd) response.Response {
|
2016-10-14 02:33:16 -05:00
|
|
|
repo := annotations.GetRepository()
|
|
|
|
|
|
|
|
err := repo.Delete(&annotations.DeleteParams{
|
2018-06-25 09:02:34 -05:00
|
|
|
OrgId: c.OrgId,
|
2018-06-25 06:58:49 -05:00
|
|
|
Id: cmd.AnnotationId,
|
2016-10-14 02:33:16 -05:00
|
|
|
DashboardId: cmd.DashboardId,
|
|
|
|
PanelId: cmd.PanelId,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to delete annotations", err)
|
2016-10-14 02:33:16 -05:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Success("Annotations deleted")
|
2016-10-14 02:33:16 -05:00
|
|
|
}
|
2017-10-07 03:31:39 -05:00
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
func DeleteAnnotationByID(c *models.ReqContext) response.Response {
|
2017-10-07 03:31:39 -05:00
|
|
|
repo := annotations.GetRepository()
|
2018-03-22 16:13:46 -05:00
|
|
|
annotationID := c.ParamsInt64(":annotationId")
|
2017-10-07 03:31:39 -05:00
|
|
|
|
2018-03-22 16:13:46 -05:00
|
|
|
if resp := canSave(c, repo, annotationID); resp != nil {
|
2017-12-20 17:52:21 -06:00
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2017-10-07 03:31:39 -05:00
|
|
|
err := repo.Delete(&annotations.DeleteParams{
|
2018-06-25 09:02:34 -05:00
|
|
|
OrgId: c.OrgId,
|
|
|
|
Id: annotationID,
|
2017-10-07 03:31:39 -05:00
|
|
|
})
|
|
|
|
if err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to delete annotation", err)
|
2017-10-07 03:31:39 -05:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Success("Annotation deleted")
|
2017-10-07 03:31:39 -05:00
|
|
|
}
|
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
func canSaveByDashboardID(c *models.ReqContext, dashboardID int64) (bool, error) {
|
|
|
|
if dashboardID == 0 && !c.SignedInUser.HasRole(models.ROLE_EDITOR) {
|
2017-12-20 17:52:21 -06:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2018-06-26 07:12:20 -05:00
|
|
|
if dashboardID != 0 {
|
2021-09-23 10:43:32 -05:00
|
|
|
guard := guardian.New(c.Req.Context(), dashboardID, c.OrgId, c.SignedInUser)
|
2018-06-26 07:12:20 -05:00
|
|
|
if canEdit, err := guard.CanEdit(); err != nil || !canEdit {
|
2017-12-20 17:52:21 -06:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
func canSave(c *models.ReqContext, repo annotations.Repository, annotationID int64) response.Response {
|
2018-03-22 16:13:46 -05:00
|
|
|
items, err := repo.Find(&annotations.ItemQuery{AnnotationId: annotationID, OrgId: c.OrgId})
|
2017-12-20 17:52:21 -06:00
|
|
|
if err != nil || len(items) == 0 {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Could not find annotation to update", err)
|
2017-12-20 17:52:21 -06:00
|
|
|
}
|
|
|
|
|
2018-03-22 06:37:35 -05:00
|
|
|
dashboardID := items[0].DashboardId
|
2017-12-20 17:52:21 -06:00
|
|
|
|
2018-03-22 06:37:35 -05:00
|
|
|
if canSave, err := canSaveByDashboardID(c, dashboardID); err != nil || !canSave {
|
2017-12-20 17:52:21 -06:00
|
|
|
return dashboardGuardianResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-06-30 06:42:54 -05:00
|
|
|
|
|
|
|
func GetAnnotationTags(c *models.ReqContext) response.Response {
|
|
|
|
query := &annotations.TagsQuery{
|
|
|
|
OrgID: c.OrgId,
|
|
|
|
Tag: c.Query("tag"),
|
|
|
|
Limit: c.QueryInt64("limit"),
|
|
|
|
}
|
|
|
|
|
|
|
|
repo := annotations.GetRepository()
|
|
|
|
result, err := repo.FindTags(query)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(500, "Failed to find annotation tags", err)
|
|
|
|
}
|
|
|
|
|
2021-07-01 03:23:33 -05:00
|
|
|
return response.JSON(200, annotations.GetAnnotationTagsResponse{Result: result})
|
2021-06-30 06:42:54 -05:00
|
|
|
}
|