|
@@ -1,7 +1,6 @@
|
|
|
package api
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
|
- "fmt"
|
|
|
|
|
"strings"
|
|
"strings"
|
|
|
"time"
|
|
"time"
|
|
|
|
|
|
|
@@ -41,9 +40,22 @@ func GetAnnotations(c *middleware.Context) Response {
|
|
|
return Json(200, items)
|
|
return Json(200, items)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+type CreateAnnotationError struct {
|
|
|
|
|
+ message string
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func (e *CreateAnnotationError) Error() string {
|
|
|
|
|
+ return e.message
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
func PostAnnotation(c *middleware.Context, cmd dtos.PostAnnotationsCmd) Response {
|
|
func PostAnnotation(c *middleware.Context, cmd dtos.PostAnnotationsCmd) Response {
|
|
|
repo := annotations.GetRepository()
|
|
repo := annotations.GetRepository()
|
|
|
|
|
|
|
|
|
|
+ if cmd.Text == "" {
|
|
|
|
|
+ err := &CreateAnnotationError{"text field should not be empty"}
|
|
|
|
|
+ return ApiError(500, "Failed to save annotation", err)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
item := annotations.Item{
|
|
item := annotations.Item{
|
|
|
OrgId: c.OrgId,
|
|
OrgId: c.OrgId,
|
|
|
UserId: c.UserId,
|
|
UserId: c.UserId,
|
|
@@ -55,6 +67,10 @@ func PostAnnotation(c *middleware.Context, cmd dtos.PostAnnotationsCmd) Response
|
|
|
Tags: cmd.Tags,
|
|
Tags: cmd.Tags,
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ if item.Epoch == 0 {
|
|
|
|
|
+ item.Epoch = time.Now().Unix()
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
if err := repo.Save(&item); err != nil {
|
|
if err := repo.Save(&item); err != nil {
|
|
|
return ApiError(500, "Failed to save annotation", err)
|
|
return ApiError(500, "Failed to save annotation", err)
|
|
|
}
|
|
}
|
|
@@ -82,21 +98,22 @@ func PostAnnotation(c *middleware.Context, cmd dtos.PostAnnotationsCmd) Response
|
|
|
return ApiSuccess("Annotation added")
|
|
return ApiSuccess("Annotation added")
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-type GraphiteAnnotationError struct {
|
|
|
|
|
- message string
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-func (e *GraphiteAnnotationError) Error() string {
|
|
|
|
|
- return e.message
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
func formatGraphiteAnnotation(what string, data string) string {
|
|
func formatGraphiteAnnotation(what string, data string) string {
|
|
|
- return fmt.Sprintf("%s\n%s", what, data)
|
|
|
|
|
|
|
+ text := what
|
|
|
|
|
+ if data != "" {
|
|
|
|
|
+ text = text + "\n" + data
|
|
|
|
|
+ }
|
|
|
|
|
+ return text
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func PostGraphiteAnnotation(c *middleware.Context, cmd dtos.PostGraphiteAnnotationsCmd) Response {
|
|
func PostGraphiteAnnotation(c *middleware.Context, cmd dtos.PostGraphiteAnnotationsCmd) Response {
|
|
|
repo := annotations.GetRepository()
|
|
repo := annotations.GetRepository()
|
|
|
|
|
|
|
|
|
|
+ if cmd.What == "" {
|
|
|
|
|
+ err := &CreateAnnotationError{"what field should not be empty"}
|
|
|
|
|
+ return ApiError(500, "Failed to save Graphite annotation", err)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
if cmd.When == 0 {
|
|
if cmd.When == 0 {
|
|
|
cmd.When = time.Now().Unix()
|
|
cmd.When = time.Now().Unix()
|
|
|
}
|
|
}
|
|
@@ -106,18 +123,22 @@ func PostGraphiteAnnotation(c *middleware.Context, cmd dtos.PostGraphiteAnnotati
|
|
|
var tagsArray []string
|
|
var tagsArray []string
|
|
|
switch tags := cmd.Tags.(type) {
|
|
switch tags := cmd.Tags.(type) {
|
|
|
case string:
|
|
case string:
|
|
|
- tagsArray = strings.Split(tags, " ")
|
|
|
|
|
|
|
+ if tags != "" {
|
|
|
|
|
+ tagsArray = strings.Split(tags, " ")
|
|
|
|
|
+ } else {
|
|
|
|
|
+ tagsArray = []string{}
|
|
|
|
|
+ }
|
|
|
case []interface{}:
|
|
case []interface{}:
|
|
|
for _, t := range tags {
|
|
for _, t := range tags {
|
|
|
if tagStr, ok := t.(string); ok {
|
|
if tagStr, ok := t.(string); ok {
|
|
|
tagsArray = append(tagsArray, tagStr)
|
|
tagsArray = append(tagsArray, tagStr)
|
|
|
} else {
|
|
} else {
|
|
|
- err := &GraphiteAnnotationError{"tag should be a string"}
|
|
|
|
|
|
|
+ err := &CreateAnnotationError{"tag should be a string"}
|
|
|
return ApiError(500, "Failed to save Graphite annotation", err)
|
|
return ApiError(500, "Failed to save Graphite annotation", err)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
default:
|
|
default:
|
|
|
- err := &GraphiteAnnotationError{"unsupported tags format"}
|
|
|
|
|
|
|
+ err := &CreateAnnotationError{"unsupported tags format"}
|
|
|
return ApiError(500, "Failed to save Graphite annotation", err)
|
|
return ApiError(500, "Failed to save Graphite annotation", err)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -133,7 +154,7 @@ func PostGraphiteAnnotation(c *middleware.Context, cmd dtos.PostGraphiteAnnotati
|
|
|
return ApiError(500, "Failed to save Graphite annotation", err)
|
|
return ApiError(500, "Failed to save Graphite annotation", err)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- return ApiSuccess("Graphite Annotation added")
|
|
|
|
|
|
|
+ return ApiSuccess("Graphite annotation added")
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func UpdateAnnotation(c *middleware.Context, cmd dtos.UpdateAnnotationsCmd) Response {
|
|
func UpdateAnnotation(c *middleware.Context, cmd dtos.UpdateAnnotationsCmd) Response {
|