Chore: StoreSplit tag service (#55453)

* move tag service outside

* fix dashboard

* fix test

* lint

* fix linter

* remove spew
This commit is contained in:
ying-jeanne
2022-09-21 08:04:01 -04:00
committed by GitHub
parent e3e954cbba
commit 7b4cea8151
46 changed files with 355 additions and 293 deletions
@@ -6,6 +6,7 @@ import (
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/services/annotations"
"github.com/grafana/grafana/pkg/services/sqlstore/db"
"github.com/grafana/grafana/pkg/services/tag"
"github.com/grafana/grafana/pkg/setting"
)
@@ -13,12 +14,13 @@ type RepositoryImpl struct {
store store
}
func ProvideService(db db.DB, cfg *setting.Cfg) *RepositoryImpl {
func ProvideService(db db.DB, cfg *setting.Cfg, tagService tag.Service) *RepositoryImpl {
return &RepositoryImpl{
store: &SQLAnnotationRepo{
cfg: cfg,
db: db,
log: log.New("annotations"),
cfg: cfg,
db: db,
log: log.New("annotations"),
tagService: tagService,
},
}
}
@@ -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
@@ -7,6 +7,7 @@ import (
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/services/tag/tagimpl"
"github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/setting"
@@ -27,7 +28,7 @@ func TestIntegrationAnnotations(t *testing.T) {
t.Skip("skipping integration test")
}
sql := sqlstore.InitTestDB(t)
repo := SQLAnnotationRepo{db: sql, cfg: setting.NewCfg(), log: log.New("annotation.test")}
repo := SQLAnnotationRepo{db: sql, cfg: setting.NewCfg(), log: log.New("annotation.test"), tagService: tagimpl.ProvideService(sql)}
testUser := &user.SignedInUser{
OrgID: 1,
@@ -52,7 +53,7 @@ func TestIntegrationAnnotations(t *testing.T) {
assert.NoError(t, err)
})
dashboardStore := dashboardstore.ProvideDashboardStore(sql, featuremgmt.WithFeatures())
dashboardStore := dashboardstore.ProvideDashboardStore(sql, featuremgmt.WithFeatures(), tagimpl.ProvideService(sql))
testDashboard1 := models.SaveDashboardCommand{
UserId: 1,
@@ -61,7 +62,8 @@ func TestIntegrationAnnotations(t *testing.T) {
"title": "Dashboard 1",
}),
}
dashboard, err := dashboardStore.SaveDashboard(testDashboard1)
dashboard, err := dashboardStore.SaveDashboard(context.Background(), testDashboard1)
require.NoError(t, err)
testDashboard2 := models.SaveDashboardCommand{
@@ -71,7 +73,7 @@ func TestIntegrationAnnotations(t *testing.T) {
"title": "Dashboard 2",
}),
}
dashboard2, err := dashboardStore.SaveDashboard(testDashboard2)
dashboard2, err := dashboardStore.SaveDashboard(context.Background(), testDashboard2)
require.NoError(t, err)
annotation := &annotations.Item{
@@ -96,7 +98,7 @@ func TestIntegrationAnnotations(t *testing.T) {
Type: "alert",
Epoch: 21, // Should swap epoch & epochEnd
EpochEnd: 20,
Tags: []string{"outage", "error", "type:outage", "server:server-1"},
Tags: []string{"outage", "type:outage", "server:server-1", "error"},
}
err = repo.Add(context.Background(), annotation2)
require.NoError(t, err)
@@ -392,8 +394,8 @@ func TestIntegrationAnnotationListingWithRBAC(t *testing.T) {
t.Skip("skipping integration test")
}
sql := sqlstore.InitTestDB(t, sqlstore.InitTestDBOpt{})
repo := SQLAnnotationRepo{db: sql, cfg: setting.NewCfg(), log: log.New("annotation.test")}
dashboardStore := dashboardstore.ProvideDashboardStore(sql, featuremgmt.WithFeatures())
repo := SQLAnnotationRepo{db: sql, cfg: setting.NewCfg(), log: log.New("annotation.test"), tagService: tagimpl.ProvideService(sql)}
dashboardStore := dashboardstore.ProvideDashboardStore(sql, featuremgmt.WithFeatures(), tagimpl.ProvideService(sql))
testDashboard1 := models.SaveDashboardCommand{
UserId: 1,
@@ -402,7 +404,7 @@ func TestIntegrationAnnotationListingWithRBAC(t *testing.T) {
"title": "Dashboard 1",
}),
}
dashboard, err := dashboardStore.SaveDashboard(testDashboard1)
dashboard, err := dashboardStore.SaveDashboard(context.Background(), testDashboard1)
require.NoError(t, err)
dash1UID := dashboard.Uid
@@ -413,7 +415,7 @@ func TestIntegrationAnnotationListingWithRBAC(t *testing.T) {
"title": "Dashboard 2",
}),
}
_, err = dashboardStore.SaveDashboard(testDashboard2)
_, err = dashboardStore.SaveDashboard(context.Background(), testDashboard2)
require.NoError(t, err)
dash1Annotation := &annotations.Item{