mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Chore: Add context dashboard version (#41672)
* Add context dashboard version * Fix codeql
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
|
||||
"github.com/grafana/grafana/pkg/api/dtos"
|
||||
"github.com/grafana/grafana/pkg/api/response"
|
||||
@@ -20,6 +21,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/services/guardian"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
"github.com/grafana/grafana/pkg/web"
|
||||
"gopkg.in/macaron.v1"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -519,7 +521,7 @@ func GetDashboardVersions(c *models.ReqContext) response.Response {
|
||||
Start: c.QueryInt("start"),
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(&query); err != nil {
|
||||
if err := bus.DispatchCtx(c.Req.Context(), &query); err != nil {
|
||||
return response.Error(404, fmt.Sprintf("No versions found for dashboardId %d", dashID), err)
|
||||
}
|
||||
|
||||
@@ -551,13 +553,14 @@ func GetDashboardVersion(c *models.ReqContext) response.Response {
|
||||
return dashboardGuardianResponse(err)
|
||||
}
|
||||
|
||||
version, _ := strconv.ParseInt(macaron.Params(c.Req)[":id"], 10, 32)
|
||||
query := models.GetDashboardVersionQuery{
|
||||
OrgId: c.OrgId,
|
||||
DashboardId: dashID,
|
||||
Version: int(c.ParamsInt64(":id")),
|
||||
Version: int(version),
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(&query); err != nil {
|
||||
if err := bus.DispatchCtx(c.Req.Context(), &query); err != nil {
|
||||
return response.Error(500, fmt.Sprintf("Dashboard version %d not found for dashboardId %d", query.Version, dashID), err)
|
||||
}
|
||||
|
||||
@@ -610,7 +613,7 @@ func CalculateDashboardDiff(c *models.ReqContext, apiOptions dtos.CalculateDiffO
|
||||
},
|
||||
}
|
||||
|
||||
result, err := dashdiffs.CalculateDiff(&options)
|
||||
result, err := dashdiffs.CalculateDiff(c.Req.Context(), &options)
|
||||
if err != nil {
|
||||
if errors.Is(err, models.ErrDashboardVersionNotFound) {
|
||||
return response.Error(404, "Dashboard version not found", err)
|
||||
@@ -638,7 +641,7 @@ func (hs *HTTPServer) RestoreDashboardVersion(c *models.ReqContext, apiCmd dtos.
|
||||
}
|
||||
|
||||
versionQuery := models.GetDashboardVersionQuery{DashboardId: dash.Id, Version: apiCmd.Version, OrgId: c.OrgId}
|
||||
if err := bus.Dispatch(&versionQuery); err != nil {
|
||||
if err := bus.DispatchCtx(c.Req.Context(), &versionQuery); err != nil {
|
||||
return response.Error(404, "Dashboard version not found", nil)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package dashdiffs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
@@ -57,14 +58,14 @@ func ParseDiffType(diff string) DiffType {
|
||||
|
||||
// CompareDashboardVersionsCommand computes the JSON diff of two versions,
|
||||
// assigning the delta of the diff to the `Delta` field.
|
||||
func CalculateDiff(options *Options) (*Result, error) {
|
||||
func CalculateDiff(ctx context.Context, options *Options) (*Result, error) {
|
||||
baseVersionQuery := models.GetDashboardVersionQuery{
|
||||
DashboardId: options.Base.DashboardId,
|
||||
Version: options.Base.Version,
|
||||
OrgId: options.OrgId,
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(&baseVersionQuery); err != nil {
|
||||
if err := bus.DispatchCtx(ctx, &baseVersionQuery); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -74,7 +75,7 @@ func CalculateDiff(options *Options) (*Result, error) {
|
||||
OrgId: options.OrgId,
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(&newVersionQuery); err != nil {
|
||||
if err := bus.DispatchCtx(ctx, &newVersionQuery); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
@@ -48,7 +48,7 @@ func (srv *CleanUpService) Run(ctx context.Context) error {
|
||||
|
||||
srv.cleanUpTmpFiles()
|
||||
srv.deleteExpiredSnapshots()
|
||||
srv.deleteExpiredDashboardVersions()
|
||||
srv.deleteExpiredDashboardVersions(ctx)
|
||||
srv.cleanUpOldAnnotations(ctxWithTimeout)
|
||||
srv.expireOldUserInvites(ctx)
|
||||
srv.deleteStaleShortURLs(ctx)
|
||||
@@ -134,9 +134,9 @@ func (srv *CleanUpService) deleteExpiredSnapshots() {
|
||||
}
|
||||
}
|
||||
|
||||
func (srv *CleanUpService) deleteExpiredDashboardVersions() {
|
||||
func (srv *CleanUpService) deleteExpiredDashboardVersions(ctx context.Context) {
|
||||
cmd := models.DeleteExpiredVersionsCommand{}
|
||||
if err := bus.Dispatch(&cmd); err != nil {
|
||||
if err := bus.DispatchCtx(ctx, &cmd); err != nil {
|
||||
srv.log.Error("Failed to delete expired dashboard versions", "error", err.Error())
|
||||
} else {
|
||||
srv.log.Debug("Deleted old/expired dashboard versions", "rows affected", cmd.DeletedRows)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
@@ -8,40 +9,43 @@ import (
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
)
|
||||
|
||||
func init() {
|
||||
bus.AddHandler("sql", GetDashboardVersion)
|
||||
bus.AddHandler("sql", GetDashboardVersions)
|
||||
bus.AddHandler("sql", DeleteExpiredVersions)
|
||||
func (ss *SQLStore) addDashboardVersionQueryAndCommandHandlers() {
|
||||
bus.AddHandlerCtx("sql", ss.GetDashboardVersion)
|
||||
bus.AddHandlerCtx("sql", ss.GetDashboardVersions)
|
||||
bus.AddHandlerCtx("sql", ss.DeleteExpiredVersions)
|
||||
}
|
||||
|
||||
// GetDashboardVersion gets the dashboard version for the given dashboard ID and version number.
|
||||
func GetDashboardVersion(query *models.GetDashboardVersionQuery) error {
|
||||
version := models.DashboardVersion{}
|
||||
has, err := x.Where("dashboard_version.dashboard_id=? AND dashboard_version.version=? AND dashboard.org_id=?", query.DashboardId, query.Version, query.OrgId).
|
||||
Join("LEFT", "dashboard", `dashboard.id = dashboard_version.dashboard_id`).
|
||||
Get(&version)
|
||||
func (ss *SQLStore) GetDashboardVersion(ctx context.Context, query *models.GetDashboardVersionQuery) error {
|
||||
return ss.WithDbSession(ctx, func(sess *DBSession) error {
|
||||
version := models.DashboardVersion{}
|
||||
has, err := sess.Where("dashboard_version.dashboard_id=? AND dashboard_version.version=? AND dashboard.org_id=?", query.DashboardId, query.Version, query.OrgId).
|
||||
Join("LEFT", "dashboard", `dashboard.id = dashboard_version.dashboard_id`).
|
||||
Get(&version)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !has {
|
||||
return models.ErrDashboardVersionNotFound
|
||||
}
|
||||
if !has {
|
||||
return models.ErrDashboardVersionNotFound
|
||||
}
|
||||
|
||||
version.Data.Set("id", version.DashboardId)
|
||||
query.Result = &version
|
||||
return nil
|
||||
version.Data.Set("id", version.DashboardId)
|
||||
query.Result = &version
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// GetDashboardVersions gets all dashboard versions for the given dashboard ID.
|
||||
func GetDashboardVersions(query *models.GetDashboardVersionsQuery) error {
|
||||
if query.Limit == 0 {
|
||||
query.Limit = 1000
|
||||
}
|
||||
func (ss *SQLStore) GetDashboardVersions(ctx context.Context, query *models.GetDashboardVersionsQuery) error {
|
||||
return ss.WithDbSession(ctx, func(sess *DBSession) error {
|
||||
if query.Limit == 0 {
|
||||
query.Limit = 1000
|
||||
}
|
||||
|
||||
err := x.Table("dashboard_version").
|
||||
Select(`dashboard_version.id,
|
||||
err := sess.Table("dashboard_version").
|
||||
Select(`dashboard_version.id,
|
||||
dashboard_version.dashboard_id,
|
||||
dashboard_version.parent_version,
|
||||
dashboard_version.restored_from,
|
||||
@@ -50,31 +54,32 @@ func GetDashboardVersions(query *models.GetDashboardVersionsQuery) error {
|
||||
dashboard_version.created_by as created_by_id,
|
||||
dashboard_version.message,
|
||||
dashboard_version.data,`+
|
||||
dialect.Quote("user")+`.login as created_by`).
|
||||
Join("LEFT", dialect.Quote("user"), `dashboard_version.created_by = `+dialect.Quote("user")+`.id`).
|
||||
Join("LEFT", "dashboard", `dashboard.id = dashboard_version.dashboard_id`).
|
||||
Where("dashboard_version.dashboard_id=? AND dashboard.org_id=?", query.DashboardId, query.OrgId).
|
||||
OrderBy("dashboard_version.version DESC").
|
||||
Limit(query.Limit, query.Start).
|
||||
Find(&query.Result)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
dialect.Quote("user")+`.login as created_by`).
|
||||
Join("LEFT", dialect.Quote("user"), `dashboard_version.created_by = `+dialect.Quote("user")+`.id`).
|
||||
Join("LEFT", "dashboard", `dashboard.id = dashboard_version.dashboard_id`).
|
||||
Where("dashboard_version.dashboard_id=? AND dashboard.org_id=?", query.DashboardId, query.OrgId).
|
||||
OrderBy("dashboard_version.version DESC").
|
||||
Limit(query.Limit, query.Start).
|
||||
Find(&query.Result)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(query.Result) < 1 {
|
||||
return models.ErrNoVersionsForDashboardId
|
||||
}
|
||||
return nil
|
||||
if len(query.Result) < 1 {
|
||||
return models.ErrNoVersionsForDashboardId
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
const MAX_VERSIONS_TO_DELETE_PER_BATCH = 100
|
||||
const MAX_VERSION_DELETION_BATCHES = 50
|
||||
|
||||
func DeleteExpiredVersions(cmd *models.DeleteExpiredVersionsCommand) error {
|
||||
return deleteExpiredVersions(cmd, MAX_VERSIONS_TO_DELETE_PER_BATCH, MAX_VERSION_DELETION_BATCHES)
|
||||
func (ss *SQLStore) DeleteExpiredVersions(ctx context.Context, cmd *models.DeleteExpiredVersionsCommand) error {
|
||||
return ss.deleteExpiredVersions(ctx, cmd, MAX_VERSIONS_TO_DELETE_PER_BATCH, MAX_VERSION_DELETION_BATCHES)
|
||||
}
|
||||
|
||||
func deleteExpiredVersions(cmd *models.DeleteExpiredVersionsCommand, perBatch int, maxBatches int) error {
|
||||
func (ss *SQLStore) deleteExpiredVersions(ctx context.Context, cmd *models.DeleteExpiredVersionsCommand, perBatch int, maxBatches int) error {
|
||||
versionsToKeep := setting.DashboardVersionsToKeep
|
||||
if versionsToKeep < 1 {
|
||||
versionsToKeep = 1
|
||||
@@ -83,7 +88,7 @@ func deleteExpiredVersions(cmd *models.DeleteExpiredVersionsCommand, perBatch in
|
||||
for batch := 0; batch < maxBatches; batch++ {
|
||||
deleted := int64(0)
|
||||
|
||||
batchErr := inTransaction(func(sess *DBSession) error {
|
||||
batchErr := ss.WithTransactionalDbSession(ctx, func(sess *DBSession) error {
|
||||
// Idea of this query is finding version IDs to delete based on formula:
|
||||
// min_version_to_keep = min_version + (versions_count - versions_to_keep)
|
||||
// where version stats is processed for each dashboard. This guarantees that we keep at least versions_to_keep
|
||||
|
||||
@@ -40,7 +40,7 @@ func TestGetDashboardVersion(t *testing.T) {
|
||||
OrgId: 1,
|
||||
}
|
||||
|
||||
err := GetDashboardVersion(&query)
|
||||
err := sqlStore.GetDashboardVersion(context.Background(), &query)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, query.DashboardId, savedDash.Id)
|
||||
require.Equal(t, query.Version, savedDash.Version)
|
||||
@@ -63,7 +63,7 @@ func TestGetDashboardVersion(t *testing.T) {
|
||||
OrgId: 1,
|
||||
}
|
||||
|
||||
err := GetDashboardVersion(&query)
|
||||
err := sqlStore.GetDashboardVersion(context.Background(), &query)
|
||||
require.Error(t, err)
|
||||
require.Equal(t, models.ErrDashboardVersionNotFound, err)
|
||||
})
|
||||
@@ -76,7 +76,7 @@ func TestGetDashboardVersions(t *testing.T) {
|
||||
t.Run("Get all versions for a given Dashboard ID", func(t *testing.T) {
|
||||
query := models.GetDashboardVersionsQuery{DashboardId: savedDash.Id, OrgId: 1}
|
||||
|
||||
err := GetDashboardVersions(&query)
|
||||
err := sqlStore.GetDashboardVersions(context.Background(), &query)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, 1, len(query.Result))
|
||||
})
|
||||
@@ -84,7 +84,7 @@ func TestGetDashboardVersions(t *testing.T) {
|
||||
t.Run("Attempt to get the versions for a non-existent Dashboard ID", func(t *testing.T) {
|
||||
query := models.GetDashboardVersionsQuery{DashboardId: int64(999), OrgId: 1}
|
||||
|
||||
err := GetDashboardVersions(&query)
|
||||
err := sqlStore.GetDashboardVersions(context.Background(), &query)
|
||||
require.Error(t, err)
|
||||
require.Equal(t, models.ErrNoVersionsForDashboardId, err)
|
||||
require.Equal(t, 0, len(query.Result))
|
||||
@@ -96,7 +96,7 @@ func TestGetDashboardVersions(t *testing.T) {
|
||||
})
|
||||
|
||||
query := models.GetDashboardVersionsQuery{DashboardId: savedDash.Id, OrgId: 1}
|
||||
err := GetDashboardVersions(&query)
|
||||
err := sqlStore.GetDashboardVersions(context.Background(), &query)
|
||||
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, 2, len(query.Result))
|
||||
@@ -122,11 +122,11 @@ func TestDeleteExpiredVersions(t *testing.T) {
|
||||
|
||||
t.Run("Clean up old dashboard versions", func(t *testing.T) {
|
||||
setup(t)
|
||||
err := DeleteExpiredVersions(&models.DeleteExpiredVersionsCommand{})
|
||||
err := sqlStore.DeleteExpiredVersions(context.Background(), &models.DeleteExpiredVersionsCommand{})
|
||||
require.Nil(t, err)
|
||||
|
||||
query := models.GetDashboardVersionsQuery{DashboardId: savedDash.Id, OrgId: 1}
|
||||
err = GetDashboardVersions(&query)
|
||||
err = sqlStore.GetDashboardVersions(context.Background(), &query)
|
||||
require.Nil(t, err)
|
||||
|
||||
require.Equal(t, versionsToKeep, len(query.Result))
|
||||
@@ -139,11 +139,11 @@ func TestDeleteExpiredVersions(t *testing.T) {
|
||||
setup(t)
|
||||
setting.DashboardVersionsToKeep = versionsToWrite
|
||||
|
||||
err := DeleteExpiredVersions(&models.DeleteExpiredVersionsCommand{})
|
||||
err := sqlStore.DeleteExpiredVersions(context.Background(), &models.DeleteExpiredVersionsCommand{})
|
||||
require.Nil(t, err)
|
||||
|
||||
query := models.GetDashboardVersionsQuery{DashboardId: savedDash.Id, OrgId: 1, Limit: versionsToWrite}
|
||||
err = GetDashboardVersions(&query)
|
||||
err = sqlStore.GetDashboardVersions(context.Background(), &query)
|
||||
require.Nil(t, err)
|
||||
|
||||
require.Equal(t, versionsToWrite, len(query.Result))
|
||||
@@ -161,11 +161,11 @@ func TestDeleteExpiredVersions(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
err := deleteExpiredVersions(&models.DeleteExpiredVersionsCommand{}, perBatch, maxBatches)
|
||||
err := sqlStore.deleteExpiredVersions(context.Background(), &models.DeleteExpiredVersionsCommand{}, perBatch, maxBatches)
|
||||
require.Nil(t, err)
|
||||
|
||||
query := models.GetDashboardVersionsQuery{DashboardId: savedDash.Id, OrgId: 1, Limit: versionsToWriteBigNumber}
|
||||
err = GetDashboardVersions(&query)
|
||||
err = sqlStore.GetDashboardVersions(context.Background(), &query)
|
||||
require.Nil(t, err)
|
||||
|
||||
// Ensure we have at least versionsToKeep versions
|
||||
|
||||
@@ -119,6 +119,7 @@ func newSQLStore(cfg *setting.Cfg, cacheService *localcache.CacheService, bus bu
|
||||
ss.addStarQueryAndCommandHandlers()
|
||||
ss.addAlertQueryAndCommandHandlers()
|
||||
ss.addTempUserQueryAndCommandHandlers()
|
||||
ss.addDashboardVersionQueryAndCommandHandlers()
|
||||
|
||||
// if err := ss.Reset(); err != nil {
|
||||
// return nil, err
|
||||
|
||||
Reference in New Issue
Block a user