diff --git a/pkg/api/annotations.go b/pkg/api/annotations.go index ad09335177a..b86508acdc6 100644 --- a/pkg/api/annotations.go +++ b/pkg/api/annotations.go @@ -278,6 +278,11 @@ func (hs *HTTPServer) UpdateAnnotation(c *models.ReqContext) response.Response { EpochEnd: cmd.TimeEnd, Text: cmd.Text, Tags: cmd.Tags, + Data: annotation.Data, + } + + if cmd.Data != nil { + item.Data = cmd.Data } if err := hs.annotationsRepo.Update(c.Req.Context(), &item); err != nil { @@ -328,6 +333,7 @@ func (hs *HTTPServer) PatchAnnotation(c *models.ReqContext) response.Response { EpochEnd: annotation.TimeEnd, Text: annotation.Text, Tags: annotation.Tags, + Data: annotation.Data, } if cmd.Tags != nil { @@ -346,6 +352,10 @@ func (hs *HTTPServer) PatchAnnotation(c *models.ReqContext) response.Response { existing.EpochEnd = cmd.TimeEnd } + if cmd.Data != nil { + existing.Data = cmd.Data + } + if err := hs.annotationsRepo.Update(c.Req.Context(), &existing); err != nil { return response.ErrOrFallback(500, "Failed to update annotation", err) } diff --git a/pkg/api/dtos/annotations.go b/pkg/api/dtos/annotations.go index 1235e077bea..c154620df4a 100644 --- a/pkg/api/dtos/annotations.go +++ b/pkg/api/dtos/annotations.go @@ -15,19 +15,21 @@ type PostAnnotationsCmd struct { } type UpdateAnnotationsCmd struct { - Id int64 `json:"id"` - Time int64 `json:"time"` - TimeEnd int64 `json:"timeEnd,omitempty"` // Optional - Text string `json:"text"` - Tags []string `json:"tags"` + Id int64 `json:"id"` + Time int64 `json:"time"` + TimeEnd int64 `json:"timeEnd,omitempty"` // Optional + Text string `json:"text"` + Tags []string `json:"tags"` + Data *simplejson.Json `json:"data"` } type PatchAnnotationsCmd struct { - Id int64 `json:"id"` - Time int64 `json:"time"` - TimeEnd int64 `json:"timeEnd,omitempty"` // Optional - Text string `json:"text"` - Tags []string `json:"tags"` + Id int64 `json:"id"` + Time int64 `json:"time"` + TimeEnd int64 `json:"timeEnd,omitempty"` // Optional + Text string `json:"text"` + Tags []string `json:"tags"` + Data *simplejson.Json `json:"data"` } type MassDeleteAnnotationsCmd struct { diff --git a/pkg/services/annotations/annotationsimpl/xorm_store.go b/pkg/services/annotations/annotationsimpl/xorm_store.go index 661d16f1ba8..16983586753 100644 --- a/pkg/services/annotations/annotationsimpl/xorm_store.go +++ b/pkg/services/annotations/annotationsimpl/xorm_store.go @@ -162,6 +162,10 @@ func (r *xormRepositoryImpl) Update(ctx context.Context, item *annotations.Item) existing.EpochEnd = item.EpochEnd } + if item.Data != nil { + existing.Data = item.Data + } + if item.Tags != nil { tags, err := r.tagService.EnsureTagsExist(ctx, tag.ParseTagPairs(item.Tags)) if err != nil { @@ -183,7 +187,7 @@ func (r *xormRepositoryImpl) Update(ctx context.Context, item *annotations.Item) return err } - _, err = sess.Table("annotation").ID(existing.Id).Cols("epoch", "text", "epoch_end", "updated", "tags").Update(existing) + _, err = sess.Table("annotation").ID(existing.Id).Cols("epoch", "text", "epoch_end", "updated", "tags", "data").Update(existing) return err }) } diff --git a/pkg/services/annotations/annotationsimpl/xorm_store_test.go b/pkg/services/annotations/annotationsimpl/xorm_store_test.go index 44239e50f95..907f1e94839 100644 --- a/pkg/services/annotations/annotationsimpl/xorm_store_test.go +++ b/pkg/services/annotations/annotationsimpl/xorm_store_test.go @@ -90,6 +90,7 @@ func TestIntegrationAnnotations(t *testing.T) { Type: "alert", Epoch: 10, Tags: []string{"outage", "error", "type:outage", "server:server-1"}, + Data: simplejson.NewFromAny(map[string]interface{}{"data1": "I am a cool data", "data2": "I am another cool data"}), } err = repo.Add(context.Background(), annotation) require.NoError(t, err) @@ -321,6 +322,9 @@ func TestIntegrationAnnotations(t *testing.T) { assert.Equal(t, annotationId, items[0].Id) assert.Empty(t, items[0].Tags) assert.Equal(t, "something new", items[0].Text) + data, err := items[0].Data.Map() + assert.NoError(t, err) + assert.Equal(t, data, map[string]interface{}{"data1": "I am a cool data", "data2": "I am another cool data"}) }) t.Run("Can update annotation with new tags", func(t *testing.T) { @@ -352,6 +356,38 @@ func TestIntegrationAnnotations(t *testing.T) { assert.Greater(t, items[0].Updated, items[0].Created) }) + t.Run("Can update annotations with data", func(t *testing.T) { + query := &annotations.ItemQuery{ + OrgId: 1, + DashboardId: 1, + From: 0, + To: 15, + SignedInUser: testUser, + } + items, err := repo.Get(context.Background(), query) + require.NoError(t, err) + + annotationId := items[0].Id + data := simplejson.NewFromAny(map[string]interface{}{"data": "I am a data", "data2": "I am also a data"}) + err = repo.Update(context.Background(), &annotations.Item{ + Id: annotationId, + OrgId: 1, + Text: "something new", + Tags: []string{"newtag1", "newtag2"}, + Data: data, + }) + require.NoError(t, err) + + items, err = repo.Get(context.Background(), query) + require.NoError(t, err) + + assert.Equal(t, annotationId, items[0].Id) + assert.Equal(t, []string{"newtag1", "newtag2"}, items[0].Tags) + assert.Equal(t, "something new", items[0].Text) + assert.Greater(t, items[0].Updated, items[0].Created) + assert.Equal(t, data, items[0].Data) + }) + t.Run("Can delete annotation", func(t *testing.T) { query := &annotations.ItemQuery{ OrgId: 1,