Browse Source

convert epoch to milliseconds

ryan 7 years ago
parent
commit
20353db966

+ 7 - 15
pkg/api/annotations.go

@@ -2,7 +2,6 @@ package api
 
 import (
 	"strings"
-	"time"
 
 	"github.com/grafana/grafana/pkg/api/dtos"
 	"github.com/grafana/grafana/pkg/components/simplejson"
@@ -15,8 +14,8 @@ import (
 func GetAnnotations(c *m.ReqContext) Response {
 
 	query := &annotations.ItemQuery{
-		From:        c.QueryInt64("from") / 1000,
-		To:          c.QueryInt64("to") / 1000,
+		From:        c.QueryInt64("from"),
+		To:          c.QueryInt64("to"),
 		OrgId:       c.OrgId,
 		UserId:      c.QueryInt64("userId"),
 		AlertId:     c.QueryInt64("alertId"),
@@ -38,7 +37,7 @@ func GetAnnotations(c *m.ReqContext) Response {
 		if item.Email != "" {
 			item.AvatarUrl = dtos.GetGravatarUrl(item.Email)
 		}
-		item.Time = item.Time * 1000
+		item.Time = item.Time
 	}
 
 	return Json(200, items)
@@ -69,16 +68,12 @@ func PostAnnotation(c *m.ReqContext, cmd dtos.PostAnnotationsCmd) Response {
 		UserId:      c.UserId,
 		DashboardId: cmd.DashboardId,
 		PanelId:     cmd.PanelId,
-		Epoch:       cmd.Time / 1000,
+		Epoch:       cmd.Time,
 		Text:        cmd.Text,
 		Data:        cmd.Data,
 		Tags:        cmd.Tags,
 	}
 
-	if item.Epoch == 0 {
-		item.Epoch = time.Now().Unix()
-	}
-
 	if err := repo.Save(&item); err != nil {
 		return ApiError(500, "Failed to save annotation", err)
 	}
@@ -98,7 +93,7 @@ func PostAnnotation(c *m.ReqContext, cmd dtos.PostAnnotationsCmd) Response {
 		}
 
 		item.Id = 0
-		item.Epoch = cmd.TimeEnd / 1000
+		item.Epoch = cmd.TimeEnd
 
 		if err := repo.Save(&item); err != nil {
 			return ApiError(500, "Failed save annotation for region end time", err)
@@ -133,9 +128,6 @@ func PostGraphiteAnnotation(c *m.ReqContext, cmd dtos.PostGraphiteAnnotationsCmd
 		return ApiError(500, "Failed to save Graphite annotation", err)
 	}
 
-	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)
@@ -192,7 +184,7 @@ func UpdateAnnotation(c *m.ReqContext, cmd dtos.UpdateAnnotationsCmd) Response {
 		OrgId:  c.OrgId,
 		UserId: c.UserId,
 		Id:     annotationID,
-		Epoch:  cmd.Time / 1000,
+		Epoch:  cmd.Time,
 		Text:   cmd.Text,
 		Tags:   cmd.Tags,
 	}
@@ -204,7 +196,7 @@ func UpdateAnnotation(c *m.ReqContext, cmd dtos.UpdateAnnotationsCmd) Response {
 	if cmd.IsRegion {
 		itemRight := item
 		itemRight.RegionId = item.Id
-		itemRight.Epoch = cmd.TimeEnd / 1000
+		itemRight.Epoch = cmd.TimeEnd
 
 		// We don't know id of region right event, so set it to 0 and find then using query like
 		// ... WHERE region_id = <item.RegionId> AND id != <item.RegionId> ...

+ 6 - 3
pkg/services/sqlstore/annotation.go

@@ -23,6 +23,10 @@ func (r *SqlAnnotationRepo) Save(item *annotations.Item) error {
 		item.Tags = models.JoinTagPairs(tags)
 		item.Created = time.Now().UnixNano() / int64(time.Millisecond)
 		item.Updated = item.Created
+		if item.Epoch == 0 {
+			item.Epoch = item.Created
+		}
+
 		if _, err := sess.Table("annotation").Insert(item); err != nil {
 			return err
 		}
@@ -70,7 +74,6 @@ func (r *SqlAnnotationRepo) Update(item *annotations.Item) error {
 			err     error
 		)
 		existing := new(annotations.Item)
-		item.Updated = time.Now().UnixNano() / int64(time.Millisecond)
 
 		if item.Id == 0 && item.RegionId != 0 {
 			// Update region end time
@@ -86,6 +89,7 @@ func (r *SqlAnnotationRepo) Update(item *annotations.Item) error {
 			return errors.New("Annotation not found")
 		}
 
+		existing.Updated = time.Now().UnixNano() / int64(time.Millisecond)
 		existing.Epoch = item.Epoch
 		existing.Text = item.Text
 		if item.RegionId != 0 {
@@ -185,8 +189,7 @@ func (r *SqlAnnotationRepo) Find(query *annotations.ItemQuery) ([]*annotations.I
 
 	if query.Type == "alert" {
 		sql.WriteString(` AND annotation.alert_id > 0`)
-	}
-	if query.Type == "annotation" {
+	} else if query.Type == "annotation" {
 		sql.WriteString(` AND annotation.alert_id = 0`)
 	}
 

+ 9 - 0
pkg/services/sqlstore/migrations/annotation_mig.go

@@ -106,4 +106,13 @@ func addAnnotationMig(mg *Migrator) {
 	mg.AddMigration("Add index for updated in annotation table", NewAddIndexMigration(table, &Index{
 		Cols: []string{"org_id", "updated"}, Type: IndexType,
 	}))
+
+	//
+	// Convert epoch saved as seconds to miliseconds
+	//
+	updateEpochSql := "UPDATE annotation SET epoch = (epoch*1000)"
+	mg.AddMigration("Convert existing annotations from seconds to miliseconds", new(RawSqlMigration).
+		Sqlite(updateEpochSql).
+		Postgres(updateEpochSql).
+		Mysql(updateEpochSql))
 }