Chore: Remove result fields from thumbs (#65096)

remove result fields from thumbs
This commit is contained in:
Serge Zaitsev 2023-03-22 21:25:07 +01:00 committed by GitHub
parent d561920147
commit e1d99365c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 28 deletions

View File

@ -15,17 +15,17 @@ type xormStore struct {
db db.DB
}
func (ss *xormStore) Get(ctx context.Context, query *thumbs.GetDashboardThumbnailCommand) (*thumbs.DashboardThumbnail, error) {
err := ss.db.WithDbSession(ctx, func(sess *db.Session) error {
result, err := findThumbnailByMeta(sess, query.DashboardThumbnailMeta)
func (ss *xormStore) Get(ctx context.Context, query *thumbs.GetDashboardThumbnailCommand) (result *thumbs.DashboardThumbnail, err error) {
err = ss.db.WithDbSession(ctx, func(sess *db.Session) error {
thumb, err := findThumbnailByMeta(sess, query.DashboardThumbnailMeta)
if err != nil {
return err
}
query.Result = result
result = thumb
return nil
})
return query.Result, err
return result, err
}
func marshalDatasourceUids(dsUids []string) (string, error) {
@ -40,8 +40,8 @@ func marshalDatasourceUids(dsUids []string) (string, error) {
return string(b), nil
}
func (ss *xormStore) Save(ctx context.Context, cmd *thumbs.SaveDashboardThumbnailCommand) (*thumbs.DashboardThumbnail, error) {
err := ss.db.WithTransactionalDbSession(ctx, func(sess *db.Session) error {
func (ss *xormStore) Save(ctx context.Context, cmd *thumbs.SaveDashboardThumbnailCommand) (result *thumbs.DashboardThumbnail, err error) {
err = ss.db.WithTransactionalDbSession(ctx, func(sess *db.Session) error {
existing, err := findThumbnailByMeta(sess, cmd.DashboardThumbnailMeta)
if err != nil && !errors.Is(err, dashboards.ErrDashboardThumbnailNotFound) {
@ -61,7 +61,7 @@ func (ss *xormStore) Save(ctx context.Context, cmd *thumbs.SaveDashboardThumbnai
existing.DsUIDs = dsUids
existing.State = thumbs.ThumbnailStateDefault
_, err = sess.ID(existing.Id).Update(existing)
cmd.Result = existing
result = existing
return err
}
@ -84,11 +84,11 @@ func (ss *xormStore) Save(ctx context.Context, cmd *thumbs.SaveDashboardThumbnai
thumb.State = thumbs.ThumbnailStateDefault
thumb.PanelId = cmd.PanelID
_, err = sess.Insert(thumb)
cmd.Result = thumb
result = thumb
return err
})
return cmd.Result, err
return result, err
}
func (ss *xormStore) UpdateState(ctx context.Context, cmd *thumbs.UpdateThumbnailStateCommand) error {
@ -107,22 +107,22 @@ func (ss *xormStore) UpdateState(ctx context.Context, cmd *thumbs.UpdateThumbnai
return err
}
func (ss *xormStore) Count(ctx context.Context, cmd *thumbs.FindDashboardThumbnailCountCommand) (int64, error) {
err := ss.db.WithDbSession(ctx, func(sess *db.Session) error {
func (ss *xormStore) Count(ctx context.Context, cmd *thumbs.FindDashboardThumbnailCountCommand) (n int64, err error) {
err = ss.db.WithDbSession(ctx, func(sess *db.Session) error {
count, err := sess.Count(&thumbs.DashboardThumbnail{})
if err != nil {
return err
}
cmd.Result = count
n = count
return nil
})
return cmd.Result, err
return n, err
}
func (ss *xormStore) FindDashboardsWithStaleThumbnails(ctx context.Context, cmd *thumbs.FindDashboardsWithStaleThumbnailsCommand) ([]*thumbs.DashboardWithStaleThumbnail, error) {
err := ss.db.WithDbSession(ctx, func(sess *db.Session) error {
func (ss *xormStore) FindDashboardsWithStaleThumbnails(ctx context.Context, cmd *thumbs.FindDashboardsWithStaleThumbnailsCommand) (result []*thumbs.DashboardWithStaleThumbnail, err error) {
err = ss.db.WithDbSession(ctx, func(sess *db.Session) error {
sess.Table("dashboard")
sess.Join("LEFT", "dashboard_thumbnail", "dashboard.id = dashboard_thumbnail.dashboard_id AND dashboard_thumbnail.theme = ? AND dashboard_thumbnail.kind = ?", cmd.Theme, cmd.Kind)
sess.Where("dashboard.is_folder = ?", ss.db.GetDialect().BooleanStr(false))
@ -152,17 +152,17 @@ func (ss *xormStore) FindDashboardsWithStaleThumbnails(ctx context.Context, cmd
"dashboard.version",
"dashboard.slug")
var result = make([]*thumbs.DashboardWithStaleThumbnail, 0)
err := sess.Find(&result)
var list = make([]*thumbs.DashboardWithStaleThumbnail, 0)
err := sess.Find(&list)
if err != nil {
return err
}
cmd.Result = result
result = list
return err
})
return cmd.Result, err
return result, err
}
func findThumbnailByMeta(sess *db.Session, meta thumbs.DashboardThumbnailMeta) (*thumbs.DashboardThumbnail, error) {

View File

@ -151,8 +151,6 @@ type DashboardThumbnailMeta struct {
type GetDashboardThumbnailCommand struct {
DashboardThumbnailMeta
Result *DashboardThumbnail
}
const DashboardVersionForManualThumbnailUpload = -1
@ -166,7 +164,6 @@ type DashboardWithStaleThumbnail struct {
}
type FindDashboardThumbnailCountCommand struct {
Result int64
}
type FindDashboardsWithStaleThumbnailsCommand struct {
@ -174,7 +171,6 @@ type FindDashboardsWithStaleThumbnailsCommand struct {
IncludeThumbnailsWithEmptyDsUIDs bool
Theme models.Theme
Kind ThumbnailKind
Result []*DashboardWithStaleThumbnail
}
type SaveDashboardThumbnailCommand struct {
@ -183,8 +179,6 @@ type SaveDashboardThumbnailCommand struct {
Image []byte
MimeType string
DatasourceUIDs []string
Result *DashboardThumbnail
}
type UpdateThumbnailStateCommand struct {

View File

@ -64,13 +64,13 @@ func (r *sqlThumbnailRepository) saveFromBytes(ctx context.Context, content []by
DatasourceUIDs: dsUids,
}
_, err := r.store.SaveThumbnail(ctx, cmd)
result, err := r.store.SaveThumbnail(ctx, cmd)
if err != nil {
r.log.Error("Error saving to the db", "dashboardUID", meta.DashboardUID, "err", err)
return 0, err
}
return cmd.Result.Id, nil
return result.Id, nil
}
func (r *sqlThumbnailRepository) updateThumbnailState(ctx context.Context, state ThumbnailState, meta DashboardThumbnailMeta) error {