Chore: StoreSplit tag service (#55453)
* move tag service outside * fix dashboard * fix test * lint * fix linter * remove spew
This commit is contained in:
@@ -8,7 +8,6 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
ac "github.com/grafana/grafana/pkg/services/accesscontrol"
|
||||
@@ -17,6 +16,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore/db"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore/permissions"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore/searchstore"
|
||||
"github.com/grafana/grafana/pkg/services/tag"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
)
|
||||
@@ -41,30 +41,31 @@ func validateTimeRange(item *annotations.Item) error {
|
||||
}
|
||||
|
||||
type SQLAnnotationRepo struct {
|
||||
cfg *setting.Cfg
|
||||
db db.DB
|
||||
log log.Logger
|
||||
cfg *setting.Cfg
|
||||
db db.DB
|
||||
log log.Logger
|
||||
tagService tag.Service
|
||||
}
|
||||
|
||||
func (r *SQLAnnotationRepo) Add(ctx context.Context, item *annotations.Item) error {
|
||||
return r.db.WithTransactionalDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
||||
tags := models.ParseTagPairs(item.Tags)
|
||||
item.Tags = models.JoinTagPairs(tags)
|
||||
item.Created = timeNow().UnixNano() / int64(time.Millisecond)
|
||||
item.Updated = item.Created
|
||||
if item.Epoch == 0 {
|
||||
item.Epoch = item.Created
|
||||
}
|
||||
if err := validateTimeRange(item); err != nil {
|
||||
return err
|
||||
}
|
||||
tags := tag.ParseTagPairs(item.Tags)
|
||||
item.Tags = tag.JoinTagPairs(tags)
|
||||
item.Created = timeNow().UnixNano() / int64(time.Millisecond)
|
||||
item.Updated = item.Created
|
||||
if item.Epoch == 0 {
|
||||
item.Epoch = item.Created
|
||||
}
|
||||
if err := validateTimeRange(item); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return r.db.WithDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
||||
if _, err := sess.Table("annotation").Insert(item); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if item.Tags != nil {
|
||||
tags, err := sqlstore.EnsureTagsExist(sess, tags)
|
||||
tags, err := r.tagService.EnsureTagsExist(ctx, tags)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -74,7 +75,6 @@ func (r *SQLAnnotationRepo) Add(ctx context.Context, item *annotations.Item) err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
@@ -111,7 +111,7 @@ func (r *SQLAnnotationRepo) Update(ctx context.Context, item *annotations.Item)
|
||||
}
|
||||
|
||||
if item.Tags != nil {
|
||||
tags, err := sqlstore.EnsureTagsExist(sess, models.ParseTagPairs(item.Tags))
|
||||
tags, err := r.tagService.EnsureTagsExist(ctx, tag.ParseTagPairs(item.Tags))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -205,7 +205,7 @@ func (r *SQLAnnotationRepo) Get(ctx context.Context, query *annotations.ItemQuer
|
||||
if len(query.Tags) > 0 {
|
||||
keyValueFilters := []string{}
|
||||
|
||||
tags := models.ParseTagPairs(query.Tags)
|
||||
tags := tag.ParseTagPairs(query.Tags)
|
||||
for _, tag := range tags {
|
||||
if tag.Value == "" {
|
||||
keyValueFilters = append(keyValueFilters, "(tag."+r.db.GetDialect().Quote("key")+" = ?)")
|
||||
@@ -249,8 +249,6 @@ func (r *SQLAnnotationRepo) Get(ctx context.Context, query *annotations.ItemQuer
|
||||
|
||||
// order of ORDER BY arguments match the order of a sql index for performance
|
||||
sql.WriteString(" ORDER BY a.org_id, a.epoch_end DESC, a.epoch DESC" + r.db.GetDialect().Limit(query.Limit) + " ) dt on dt.id = annotation.id")
|
||||
|
||||
spew.Dump(">>>> query: ", sql.String())
|
||||
if err := sess.SQL(sql.String(), params...).Find(&items); err != nil {
|
||||
items = nil
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user