@ -1,6 +1,10 @@
package api
import (
"fmt"
"strings"
"time"
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/middleware"
@ -78,6 +82,60 @@ func PostAnnotation(c *middleware.Context, cmd dtos.PostAnnotationsCmd) Response
return ApiSuccess ( "Annotation added" )
}
type GraphiteAnnotationError struct {
message string
}
func ( e * GraphiteAnnotationError ) Error ( ) string {
return e . message
}
func formatGraphiteAnnotation ( what string , data string ) string {
return fmt . Sprintf ( "%s\n%s" , what , data )
}
func PostGraphiteAnnotation ( c * middleware . Context , cmd dtos . PostGraphiteAnnotationsCmd ) Response {
repo := annotations . GetRepository ( )
if cmd . When == 0 {
cmd . When = time . Now ( ) . Unix ( )
}
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 :
tagsArray = strings . Split ( tags , " " )
case [ ] interface { } :
for _ , t := range tags {
if tagStr , ok := t . ( string ) ; ok {
tagsArray = append ( tagsArray , tagStr )
} else {
err := & GraphiteAnnotationError { "tag should be a string" }
return ApiError ( 500 , "Failed to save Graphite annotation" , err )
}
}
default :
err := & GraphiteAnnotationError { "unsupported tags format" }
return ApiError ( 500 , "Failed to save Graphite annotation" , err )
}
item := annotations . Item {
OrgId : c . OrgId ,
UserId : c . UserId ,
Epoch : cmd . When ,
Text : text ,
Tags : tagsArray ,
}
if err := repo . Save ( & item ) ; err != nil {
return ApiError ( 500 , "Failed to save Graphite annotation" , err )
}
return ApiSuccess ( "Graphite Annotation added" )
}
func UpdateAnnotation ( c * middleware . Context , cmd dtos . UpdateAnnotationsCmd ) Response {
annotationId := c . ParamsInt64 ( ":annotationId" )