fix(dashboard version service): add DashboardUID to query and respons… (#63821)
fix(dashboard version service): add DashboardUID to query and responses (#60800)
* fix(dashboard version service): add DashboardUID to query and responses
The DashboardUID was not populated in the response from Get and ListDashboardVersions. This adds the DashboardUID to the Get query (it was already in List) and populated the DashboardUID in the returned DashboardVersionDTOs.
(cherry picked from commit 42be0e106f)
Co-authored-by: Kristin Laemmert <mildwonkey@users.noreply.github.com>
This commit is contained in:
co-authored by
Kristin Laemmert
parent
194dbead83
commit
183f9317c5
@@ -2,8 +2,11 @@ package dashverimpl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/db"
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/services/dashboards"
|
||||
dashver "github.com/grafana/grafana/pkg/services/dashboardversion"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
)
|
||||
@@ -14,27 +17,49 @@ const (
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
store store
|
||||
store store
|
||||
dashSvc dashboards.DashboardService
|
||||
log log.Logger
|
||||
}
|
||||
|
||||
func ProvideService(db db.DB) dashver.Service {
|
||||
func ProvideService(db db.DB, dashboardService dashboards.DashboardService) dashver.Service {
|
||||
return &Service{
|
||||
store: &sqlStore{
|
||||
db: db,
|
||||
dialect: db.GetDialect(),
|
||||
},
|
||||
dashSvc: dashboardService,
|
||||
log: log.New("dashboard-version"),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) Get(ctx context.Context, query *dashver.GetDashboardVersionQuery) (*dashver.DashboardVersionDTO, error) {
|
||||
// Get the DashboardUID if not populated
|
||||
if query.DashboardUID == "" {
|
||||
u, err := s.getDashUIDMaybeEmpty(ctx, query.DashboardID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
query.DashboardUID = u
|
||||
}
|
||||
|
||||
// The store methods require the dashboard ID (uid is not in the dashboard
|
||||
// versions table, at time of this writing), so get the DashboardID if it
|
||||
// was not populated.
|
||||
if query.DashboardID == 0 {
|
||||
id, err := s.getDashIDMaybeEmpty(ctx, query.DashboardUID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
query.DashboardID = id
|
||||
}
|
||||
|
||||
version, err := s.store.Get(ctx, query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
version.Data.Set("id", version.DashboardID)
|
||||
|
||||
// FIXME: the next PR will add the dashboardService so we can grab the DashboardUID
|
||||
return version.ToDTO(""), nil
|
||||
return version.ToDTO(query.DashboardUID), nil
|
||||
}
|
||||
|
||||
func (s *Service) DeleteExpired(ctx context.Context, cmd *dashver.DeleteExpiredVersionsCommand) error {
|
||||
@@ -69,6 +94,25 @@ func (s *Service) DeleteExpired(ctx context.Context, cmd *dashver.DeleteExpiredV
|
||||
|
||||
// List all dashboard versions for the given dashboard ID.
|
||||
func (s *Service) List(ctx context.Context, query *dashver.ListDashboardVersionsQuery) ([]*dashver.DashboardVersionDTO, error) {
|
||||
// Get the DashboardUID if not populated
|
||||
if query.DashboardUID == "" {
|
||||
u, err := s.getDashUIDMaybeEmpty(ctx, query.DashboardID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
query.DashboardUID = u
|
||||
}
|
||||
|
||||
// The store methods require the dashboard ID (uid is not in the dashboard
|
||||
// versions table, at time of this writing), so get the DashboardID if it
|
||||
// was not populated.
|
||||
if query.DashboardID == 0 {
|
||||
id, err := s.getDashIDMaybeEmpty(ctx, query.DashboardUID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
query.DashboardID = id
|
||||
}
|
||||
if query.Limit == 0 {
|
||||
query.Limit = 1000
|
||||
}
|
||||
@@ -78,8 +122,42 @@ func (s *Service) List(ctx context.Context, query *dashver.ListDashboardVersions
|
||||
}
|
||||
dtos := make([]*dashver.DashboardVersionDTO, len(dvs))
|
||||
for i, v := range dvs {
|
||||
// FIXME: the next PR will add the dashboardService so we can grab the DashboardUID
|
||||
dtos[i] = v.ToDTO("")
|
||||
dtos[i] = v.ToDTO(query.DashboardUID)
|
||||
}
|
||||
return dtos, nil
|
||||
}
|
||||
|
||||
// getDashUIDMaybeEmpty is a helper function which takes a dashboardID and
|
||||
// returns the UID. If the dashboard is not found, it will return an empty
|
||||
// string.
|
||||
func (s *Service) getDashUIDMaybeEmpty(ctx context.Context, id int64) (string, error) {
|
||||
q := dashboards.GetDashboardRefByIDQuery{ID: id}
|
||||
result, err := s.dashSvc.GetDashboardUIDByID(ctx, &q)
|
||||
if err != nil {
|
||||
if errors.Is(err, dashboards.ErrDashboardNotFound) {
|
||||
s.log.Debug("dashboard not found")
|
||||
return "", nil
|
||||
} else {
|
||||
s.log.Error("error getting dashboard", err)
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
return result.UID, nil
|
||||
}
|
||||
|
||||
// getDashIDMaybeEmpty is a helper function which takes a dashboardUID and
|
||||
// returns the ID. If the dashboard is not found, it will return -1.
|
||||
func (s *Service) getDashIDMaybeEmpty(ctx context.Context, uid string) (int64, error) {
|
||||
q := dashboards.GetDashboardQuery{UID: uid}
|
||||
result, err := s.dashSvc.GetDashboard(ctx, &q)
|
||||
if err != nil {
|
||||
if errors.Is(err, dashboards.ErrDashboardNotFound) {
|
||||
s.log.Debug("dashboard not found")
|
||||
return -1, nil
|
||||
} else {
|
||||
s.log.Error("error getting dashboard", err)
|
||||
return -1, err
|
||||
}
|
||||
}
|
||||
return result.ID, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user