Explorar el Código

adding created column

ryan hace 7 años
padre
commit
3898ea02e6

+ 1 - 0
pkg/api/annotations.go

@@ -24,6 +24,7 @@ func GetAnnotations(c *m.ReqContext) Response {
 		Limit:       c.QueryInt64("limit"),
 		Tags:        c.QueryStrings("tags"),
 		Type:        c.Query("type"),
+		Sort:        c.Query("sort"),
 	}
 
 	repo := annotations.GetRepository()

+ 3 - 0
pkg/services/annotations/annotations.go

@@ -20,6 +20,7 @@ type ItemQuery struct {
 	RegionId     int64    `json:"regionId"`
 	Tags         []string `json:"tags"`
 	Type         string   `json:"type"`
+	Sort         string   `json:"sort"`
 
 	Limit int64 `json:"limit"`
 }
@@ -63,6 +64,7 @@ type Item struct {
 	PrevState   string           `json:"prevState"`
 	NewState    string           `json:"newState"`
 	Epoch       int64            `json:"epoch"`
+	Created     int64            `json:"created"`
 	Tags        []string         `json:"tags"`
 	Data        *simplejson.Json `json:"data"`
 
@@ -80,6 +82,7 @@ type ItemDTO struct {
 	UserId      int64            `json:"userId"`
 	NewState    string           `json:"newState"`
 	PrevState   string           `json:"prevState"`
+	Created     int64            `json:"created"`
 	Time        int64            `json:"time"`
 	Text        string           `json:"text"`
 	RegionId    int64            `json:"regionId"`

+ 14 - 1
pkg/services/sqlstore/annotation.go

@@ -5,6 +5,7 @@ import (
 	"errors"
 	"fmt"
 	"strings"
+	"time"
 
 	"github.com/grafana/grafana/pkg/models"
 	"github.com/grafana/grafana/pkg/services/annotations"
@@ -17,6 +18,7 @@ func (r *SqlAnnotationRepo) Save(item *annotations.Item) error {
 	return inTransaction(func(sess *DBSession) error {
 		tags := models.ParseTagPairs(item.Tags)
 		item.Tags = models.JoinTagPairs(tags)
+		item.Created = time.Now().UnixNano() / int64(time.Millisecond)
 		if _, err := sess.Table("annotation").Insert(item); err != nil {
 			return err
 		}
@@ -127,6 +129,7 @@ func (r *SqlAnnotationRepo) Find(query *annotations.ItemQuery) ([]*annotations.I
 			annotation.text,
 			annotation.tags,
 			annotation.data,
+			annotation.created,
 			usr.email,
 			usr.login,
 			alert.name as alert_name
@@ -205,7 +208,17 @@ func (r *SqlAnnotationRepo) Find(query *annotations.ItemQuery) ([]*annotations.I
 		query.Limit = 10
 	}
 
-	sql.WriteString(fmt.Sprintf(" ORDER BY epoch DESC LIMIT %v", query.Limit))
+	var sort string = "epoch DESC"
+	switch query.Sort {
+	case "time.asc":
+		sort = "epoch ASC"
+	case "created":
+		sort = "annotation.created DESC"
+	case "created.asc":
+		sort = "annotation.created ASC"
+	}
+
+	sql.WriteString(fmt.Sprintf(" ORDER BY %s LIMIT %v", sort, query.Limit))
 
 	items := make([]*annotations.ItemDTO, 0)
 

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

@@ -90,4 +90,14 @@ func addAnnotationMig(mg *Migrator) {
 		Sqlite(updateTextFieldSql).
 		Postgres(updateTextFieldSql).
 		Mysql(updateTextFieldSql))
+
+	//
+	// Add a 'created' column
+	//
+	mg.AddMigration("Add created time to annotation table", NewAddColumnMigration(table, &Column{
+		Name: "created", Type: DB_BigInt, Nullable: true, Default: "0",
+	}))
+	mg.AddMigration("Add index for created in annotation table", NewAddIndexMigration(table, &Index{
+		Cols: []string{"org_id", "created"}, Type: IndexType,
+	}))
 }