mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
convert epoch to milliseconds
This commit is contained in:
parent
a2bbd89a9e
commit
20353db966
@ -2,7 +2,6 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/api/dtos"
|
"github.com/grafana/grafana/pkg/api/dtos"
|
||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||||
@ -15,8 +14,8 @@ import (
|
|||||||
func GetAnnotations(c *m.ReqContext) Response {
|
func GetAnnotations(c *m.ReqContext) Response {
|
||||||
|
|
||||||
query := &annotations.ItemQuery{
|
query := &annotations.ItemQuery{
|
||||||
From: c.QueryInt64("from") / 1000,
|
From: c.QueryInt64("from"),
|
||||||
To: c.QueryInt64("to") / 1000,
|
To: c.QueryInt64("to"),
|
||||||
OrgId: c.OrgId,
|
OrgId: c.OrgId,
|
||||||
UserId: c.QueryInt64("userId"),
|
UserId: c.QueryInt64("userId"),
|
||||||
AlertId: c.QueryInt64("alertId"),
|
AlertId: c.QueryInt64("alertId"),
|
||||||
@ -38,7 +37,7 @@ func GetAnnotations(c *m.ReqContext) Response {
|
|||||||
if item.Email != "" {
|
if item.Email != "" {
|
||||||
item.AvatarUrl = dtos.GetGravatarUrl(item.Email)
|
item.AvatarUrl = dtos.GetGravatarUrl(item.Email)
|
||||||
}
|
}
|
||||||
item.Time = item.Time * 1000
|
item.Time = item.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
return Json(200, items)
|
return Json(200, items)
|
||||||
@ -69,16 +68,12 @@ func PostAnnotation(c *m.ReqContext, cmd dtos.PostAnnotationsCmd) Response {
|
|||||||
UserId: c.UserId,
|
UserId: c.UserId,
|
||||||
DashboardId: cmd.DashboardId,
|
DashboardId: cmd.DashboardId,
|
||||||
PanelId: cmd.PanelId,
|
PanelId: cmd.PanelId,
|
||||||
Epoch: cmd.Time / 1000,
|
Epoch: cmd.Time,
|
||||||
Text: cmd.Text,
|
Text: cmd.Text,
|
||||||
Data: cmd.Data,
|
Data: cmd.Data,
|
||||||
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)
|
||||||
}
|
}
|
||||||
@ -98,7 +93,7 @@ func PostAnnotation(c *m.ReqContext, cmd dtos.PostAnnotationsCmd) Response {
|
|||||||
}
|
}
|
||||||
|
|
||||||
item.Id = 0
|
item.Id = 0
|
||||||
item.Epoch = cmd.TimeEnd / 1000
|
item.Epoch = cmd.TimeEnd
|
||||||
|
|
||||||
if err := repo.Save(&item); err != nil {
|
if err := repo.Save(&item); err != nil {
|
||||||
return ApiError(500, "Failed save annotation for region end time", err)
|
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)
|
return ApiError(500, "Failed to save Graphite annotation", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if cmd.When == 0 {
|
|
||||||
cmd.When = time.Now().Unix()
|
|
||||||
}
|
|
||||||
text := formatGraphiteAnnotation(cmd.What, cmd.Data)
|
text := formatGraphiteAnnotation(cmd.What, cmd.Data)
|
||||||
|
|
||||||
// Support tags in prior to Graphite 0.10.0 format (string of tags separated by space)
|
// 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,
|
OrgId: c.OrgId,
|
||||||
UserId: c.UserId,
|
UserId: c.UserId,
|
||||||
Id: annotationID,
|
Id: annotationID,
|
||||||
Epoch: cmd.Time / 1000,
|
Epoch: cmd.Time,
|
||||||
Text: cmd.Text,
|
Text: cmd.Text,
|
||||||
Tags: cmd.Tags,
|
Tags: cmd.Tags,
|
||||||
}
|
}
|
||||||
@ -204,7 +196,7 @@ func UpdateAnnotation(c *m.ReqContext, cmd dtos.UpdateAnnotationsCmd) Response {
|
|||||||
if cmd.IsRegion {
|
if cmd.IsRegion {
|
||||||
itemRight := item
|
itemRight := item
|
||||||
itemRight.RegionId = item.Id
|
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
|
// 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> ...
|
// ... WHERE region_id = <item.RegionId> AND id != <item.RegionId> ...
|
||||||
|
@ -23,6 +23,10 @@ func (r *SqlAnnotationRepo) Save(item *annotations.Item) error {
|
|||||||
item.Tags = models.JoinTagPairs(tags)
|
item.Tags = models.JoinTagPairs(tags)
|
||||||
item.Created = time.Now().UnixNano() / int64(time.Millisecond)
|
item.Created = time.Now().UnixNano() / int64(time.Millisecond)
|
||||||
item.Updated = item.Created
|
item.Updated = item.Created
|
||||||
|
if item.Epoch == 0 {
|
||||||
|
item.Epoch = item.Created
|
||||||
|
}
|
||||||
|
|
||||||
if _, err := sess.Table("annotation").Insert(item); err != nil {
|
if _, err := sess.Table("annotation").Insert(item); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -70,7 +74,6 @@ func (r *SqlAnnotationRepo) Update(item *annotations.Item) error {
|
|||||||
err error
|
err error
|
||||||
)
|
)
|
||||||
existing := new(annotations.Item)
|
existing := new(annotations.Item)
|
||||||
item.Updated = time.Now().UnixNano() / int64(time.Millisecond)
|
|
||||||
|
|
||||||
if item.Id == 0 && item.RegionId != 0 {
|
if item.Id == 0 && item.RegionId != 0 {
|
||||||
// Update region end time
|
// Update region end time
|
||||||
@ -86,6 +89,7 @@ func (r *SqlAnnotationRepo) Update(item *annotations.Item) error {
|
|||||||
return errors.New("Annotation not found")
|
return errors.New("Annotation not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
existing.Updated = time.Now().UnixNano() / int64(time.Millisecond)
|
||||||
existing.Epoch = item.Epoch
|
existing.Epoch = item.Epoch
|
||||||
existing.Text = item.Text
|
existing.Text = item.Text
|
||||||
if item.RegionId != 0 {
|
if item.RegionId != 0 {
|
||||||
@ -185,8 +189,7 @@ func (r *SqlAnnotationRepo) Find(query *annotations.ItemQuery) ([]*annotations.I
|
|||||||
|
|
||||||
if query.Type == "alert" {
|
if query.Type == "alert" {
|
||||||
sql.WriteString(` AND annotation.alert_id > 0`)
|
sql.WriteString(` AND annotation.alert_id > 0`)
|
||||||
}
|
} else if query.Type == "annotation" {
|
||||||
if query.Type == "annotation" {
|
|
||||||
sql.WriteString(` AND annotation.alert_id = 0`)
|
sql.WriteString(` AND annotation.alert_id = 0`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -106,4 +106,13 @@ func addAnnotationMig(mg *Migrator) {
|
|||||||
mg.AddMigration("Add index for updated in annotation table", NewAddIndexMigration(table, &Index{
|
mg.AddMigration("Add index for updated in annotation table", NewAddIndexMigration(table, &Index{
|
||||||
Cols: []string{"org_id", "updated"}, Type: IndexType,
|
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))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user