grafana/pkg/services/dashboardversion/dashverimpl/dashver.go
idafurjes bdf50f3dd2
Dashboards: Split GetDashboardVersions method (#49967)
* Split GetDashboarVersions method

* Add sqlstore dialect and tests

* Fix signature of PAtchPreference

* Add GetDialect to sqlstore and remove GetDashboardVersions

* Add GetDialect to db interface

* Implement List

* add deleted test function

* Remove GetDialect from sqlstore interface

* Remove deleted method from mock

* Refactor test
2022-06-02 15:59:05 +02:00

75 lines
1.7 KiB
Go

package dashverimpl
import (
"context"
dashver "github.com/grafana/grafana/pkg/services/dashboardversion"
"github.com/grafana/grafana/pkg/services/sqlstore/db"
"github.com/grafana/grafana/pkg/setting"
)
const (
maxVersionsToDeletePerBatch = 100
maxVersionDeletionBatches = 50
)
type Service struct {
store store
}
func ProvideService(db db.DB) dashver.Service {
return &Service{
store: &sqlStore{
db: db,
dialect: db.GetDialect(),
},
}
}
func (s *Service) Get(ctx context.Context, query *dashver.GetDashboardVersionQuery) (*dashver.DashboardVersion, error) {
version, err := s.store.Get(ctx, query)
if err != nil {
return nil, err
}
version.Data.Set("id", version.DashboardID)
return version, nil
}
func (s *Service) DeleteExpired(ctx context.Context, cmd *dashver.DeleteExpiredVersionsCommand) error {
versionsToKeep := setting.DashboardVersionsToKeep
if versionsToKeep < 1 {
versionsToKeep = 1
}
for batch := 0; batch < maxVersionDeletionBatches; batch++ {
versionIdsToDelete, batchErr := s.store.GetBatch(ctx, cmd, maxVersionsToDeletePerBatch, versionsToKeep)
if batchErr != nil {
return batchErr
}
if len(versionIdsToDelete) < 1 {
return nil
}
deleted, err := s.store.DeleteBatch(ctx, cmd, versionIdsToDelete)
if err != nil {
return err
}
cmd.DeletedRows += deleted
if deleted < int64(maxVersionsToDeletePerBatch) {
break
}
}
return nil
}
// List all dashboard versions for the given dashboard ID.
func (s *Service) List(ctx context.Context, query *dashver.ListDashboardVersionsQuery) ([]*dashver.DashboardVersionDTO, error) {
if query.Limit == 0 {
query.Limit = 1000
}
return s.store.List(ctx, query)
}