From d553498a33299aeb62931cf553e36e1f1190e959 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Mon, 10 Apr 2017 20:22:58 +0300 Subject: [PATCH] graph(add annotation): initial backend implementation #1286 --- pkg/api/annotations.go | 21 +++++++++++++++++++ pkg/api/api.go | 1 + pkg/api/dtos/annotations.go | 8 +++++++ pkg/services/annotations/annotations.go | 8 +++++++ .../features/annotations/annotations_srv.ts | 13 ++++++++---- 5 files changed, 47 insertions(+), 4 deletions(-) diff --git a/pkg/api/annotations.go b/pkg/api/annotations.go index 48bf6c327ad..af72b9d3876 100644 --- a/pkg/api/annotations.go +++ b/pkg/api/annotations.go @@ -45,6 +45,27 @@ func GetAnnotations(c *middleware.Context) Response { return Json(200, result) } +func PostAnnotation(c *middleware.Context, cmd dtos.PostAnnotationsCmd) Response { + repo := annotations.GetRepository() + + item := annotations.Item{ + OrgId: c.OrgId, + DashboardId: cmd.DashboardId, + PanelId: cmd.PanelId, + Epoch: cmd.Time / 1000, + Title: cmd.Title, + Text: cmd.Text, + } + + err := repo.Save(&item) + + if err != nil { + return ApiError(500, "Failed to save annotation", err) + } + + return ApiSuccess("Annotation added") +} + func DeleteAnnotations(c *middleware.Context, cmd dtos.DeleteAnnotationsCmd) Response { repo := annotations.GetRepository() diff --git a/pkg/api/api.go b/pkg/api/api.go index 843b68eb915..0b9a8acf851 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -277,6 +277,7 @@ func (hs *HttpServer) registerRoutes() { }, reqEditorRole) r.Get("/annotations", wrap(GetAnnotations)) + r.Post("/annotations", bind(dtos.PostAnnotationsCmd{}), wrap(PostAnnotation)) r.Post("/annotations/mass-delete", reqOrgAdmin, bind(dtos.DeleteAnnotationsCmd{}), wrap(DeleteAnnotations)) // error test diff --git a/pkg/api/dtos/annotations.go b/pkg/api/dtos/annotations.go index 45415978ee1..cd8c0c1d523 100644 --- a/pkg/api/dtos/annotations.go +++ b/pkg/api/dtos/annotations.go @@ -16,6 +16,14 @@ type Annotation struct { Data *simplejson.Json `json:"data"` } +type PostAnnotationsCmd struct { + DashboardId int64 `json:"dashboardId"` + PanelId int64 `json:"panelId"` + Time int64 `json:"time"` + Title string `json:"title"` + Text string `json:"text"` +} + type DeleteAnnotationsCmd struct { AlertId int64 `json:"alertId"` DashboardId int64 `json:"dashboardId"` diff --git a/pkg/services/annotations/annotations.go b/pkg/services/annotations/annotations.go index d9d15bca34b..a308f546c8a 100644 --- a/pkg/services/annotations/annotations.go +++ b/pkg/services/annotations/annotations.go @@ -21,6 +21,14 @@ type ItemQuery struct { Limit int64 `json:"limit"` } +type PostParams struct { + DashboardId int64 `json:"dashboardId"` + PanelId int64 `json:"panelId"` + Epoch int64 `json:"epoch"` + Title string `json:"title"` + Text string `json:"text"` +} + type DeleteParams struct { AlertId int64 `json:"alertId"` DashboardId int64 `json:"dashboardId"` diff --git a/public/app/features/annotations/annotations_srv.ts b/public/app/features/annotations/annotations_srv.ts index f50fce8e08a..d3b83982f51 100644 --- a/public/app/features/annotations/annotations_srv.ts +++ b/public/app/features/annotations/annotations_srv.ts @@ -126,13 +126,18 @@ export class AnnotationsSrv { return this.globalAnnotationsPromise; } - postAnnotation(annotation) { - console.log("POST /api/annotations\n", annotation); + postAnnotation(annotations) { + console.log("POST /api/annotations\n", annotations); // Not implemented yet - let implemented = false; + let implemented = true; if (implemented) { - return this.backendSrv.post('/api/annotations', annotation); + return Promise.all(_.map(annotations, annotation => { + return this.backendSrv.post('/api/annotations', annotation); + })) + .catch(error => { + console.log(error); + }); } else { return Promise.resolve("Not implemented"); }