mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
* add SQL migrations * dashboard previews from sql: poc * added todos * refactor: use the same enums where possible * use useEffect, always return json * added todo * refactor + delete files after use * refactor + fix manual thumbnail upload * refactor: move all interactions with sqlStore to thumbnail repo * refactor: remove file operations in thumb crawler/service * refactor: fix dashboard_thumbs sql store * refactor: extracted thumbnail fetching/updating to a hook * refactor: store thumbnails in redux store * refactor: store thumbnails in redux store * refactor: private'd repo methods * removed redux storage, saving images as blobs * allow for configurable rendering timeouts * added 1) query for dashboards with stale thumbnails, 2) command for marking thumbnails as stale * use sql-based queue in crawler * ui for marking thumbnails as stale * replaced `stale` boolean prop with `state` enum * introduce rendering session * compilation errors * fix crawler stop button * rename thumbnail state frozen to locked * #44449: fix merge conflicts * #44449: remove thumb methods from `Store` interface * #44449: clean filepath, defer file closing * #44449: fix rendering.Theme cyclic import * #44449: linting * #44449: linting * #44449: mutex'd crawlerStatus access * #44449: added integration tests for `sqlstore.dashboard_thumbs` * #44449: added comments to explain the `ThumbnailState` enum * #44449: use os.ReadFile rather then os.Open * #44449: always enable dashboardPreviews feature during integration tests * #44449: remove sleep time, adjust number of threads * #44449: review fix: add `orgId` to `DashboardThumbnailMeta` * #44449: review fix: automatic parsing of thumbnailState * #44449: lint fixes * #44449: crawler as a background service v0.1 * #44449: use ServerLockService * #44449: use ServerLockService * #44449: review fix: prefer `WithDbSession` over `WithTransactionalDbSession` * #44449: review fix: add a comment explaining source of the filepath * #44449: review fix: added filepath validation * #44449: fix FindDashboardsWithStaleThumbnails to include `theme` and `kind` in search params * #44449: fix FindDashboardsWithStaleThumbnails to include `theme` and `kind` in search params * #44449: create function for crawler on demand * #44449: improve crawler logging * #44449: fix wire * #44449: uncomment dummy thumb service, fix ticker interval * #44449: prevent race condition * #44449: improve logging * #44449: fix theme * #44449: review fixes https://github.com/grafana/grafana/pull/45063/files @fzambia * #44449: add missing unlock * #44449: merge * #44449: review fix - logger @fzambia https://github.com/grafana/grafana/pull/45063/files * #44449: formatting * #44449: merge conflict fix * #44449: merge conflict fix * #44449: merge conflict fix * #44449: naming fix * #44449: update authOpts * #44449: change authOpts.role back to admin * #44449: fix `walk` signature, move ctx to a first argument * #44449: add `dashboardPreviewsScheduler` feature flag Co-authored-by: Ryan McKinley <ryantxu@gmail.com> Co-authored-by: Alexander Emelin <frvzmb@gmail.com>
243 lines
8.2 KiB
Go
243 lines
8.2 KiB
Go
//go:build integration
|
|
// +build integration
|
|
|
|
package sqlstore
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/grafana/grafana/pkg/models"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
var theme = models.ThemeDark
|
|
var kind = models.ThumbnailKindDefault
|
|
|
|
func TestSqlStorage(t *testing.T) {
|
|
|
|
var sqlStore *SQLStore
|
|
var savedFolder *models.Dashboard
|
|
|
|
setup := func() {
|
|
sqlStore = InitTestDB(t)
|
|
savedFolder = insertTestDashboard(t, sqlStore, "1 test dash folder", 1, 0, true, "prod", "webapp")
|
|
}
|
|
|
|
t.Run("Should insert dashboard in default state", func(t *testing.T) {
|
|
setup()
|
|
dash := insertTestDashboard(t, sqlStore, "test dash 23", 1, savedFolder.Id, false, "prod", "webapp")
|
|
upsertTestDashboardThumbnail(t, sqlStore, dash.Uid, dash.OrgId, dash.Version)
|
|
thumb := getThumbnail(t, sqlStore, dash.Uid, dash.OrgId)
|
|
|
|
require.Positive(t, thumb.Id)
|
|
require.Equal(t, models.ThumbnailStateDefault, thumb.State)
|
|
require.Equal(t, dash.Version, thumb.DashboardVersion)
|
|
})
|
|
|
|
t.Run("Should be able to update the thumbnail", func(t *testing.T) {
|
|
setup()
|
|
dash := insertTestDashboard(t, sqlStore, "test dash 23", 1, savedFolder.Id, false, "prod", "webapp")
|
|
upsertTestDashboardThumbnail(t, sqlStore, dash.Uid, dash.OrgId, dash.Version)
|
|
thumb := getThumbnail(t, sqlStore, dash.Uid, dash.OrgId)
|
|
|
|
insertedThumbnailId := thumb.Id
|
|
upsertTestDashboardThumbnail(t, sqlStore, dash.Uid, dash.OrgId, dash.Version+1)
|
|
|
|
updatedThumb := getThumbnail(t, sqlStore, dash.Uid, dash.OrgId)
|
|
require.Equal(t, insertedThumbnailId, updatedThumb.Id)
|
|
require.Equal(t, dash.Version+1, updatedThumb.DashboardVersion)
|
|
})
|
|
|
|
t.Run("Should return empty array if all dashboards have thumbnails", func(t *testing.T) {
|
|
setup()
|
|
dash := insertTestDashboard(t, sqlStore, "test dash 23", 1, savedFolder.Id, false, "prod", "webapp")
|
|
|
|
upsertTestDashboardThumbnail(t, sqlStore, dash.Uid, dash.OrgId, dash.Version)
|
|
|
|
cmd := models.FindDashboardsWithStaleThumbnailsCommand{
|
|
Kind: kind,
|
|
Theme: theme,
|
|
}
|
|
res, err := sqlStore.FindDashboardsWithStaleThumbnails(context.Background(), &cmd)
|
|
require.NoError(t, err)
|
|
require.Len(t, res, 0)
|
|
})
|
|
|
|
t.Run("Should return dashboards with thumbnails marked as stale", func(t *testing.T) {
|
|
setup()
|
|
dash := insertTestDashboard(t, sqlStore, "test dash 23", 1, savedFolder.Id, false, "prod", "webapp")
|
|
upsertTestDashboardThumbnail(t, sqlStore, dash.Uid, dash.OrgId, dash.Version)
|
|
updateThumbnailState(t, sqlStore, dash.Uid, dash.OrgId, models.ThumbnailStateStale)
|
|
|
|
cmd := models.FindDashboardsWithStaleThumbnailsCommand{
|
|
Kind: kind,
|
|
Theme: theme,
|
|
}
|
|
res, err := sqlStore.FindDashboardsWithStaleThumbnails(context.Background(), &cmd)
|
|
require.NoError(t, err)
|
|
require.Len(t, res, 1)
|
|
require.Equal(t, dash.Id, res[0].Id)
|
|
})
|
|
|
|
t.Run("Should not return dashboards with updated thumbnails that had been marked as stale", func(t *testing.T) {
|
|
setup()
|
|
dash := insertTestDashboard(t, sqlStore, "test dash 23", 1, savedFolder.Id, false, "prod", "webapp")
|
|
upsertTestDashboardThumbnail(t, sqlStore, dash.Uid, dash.OrgId, dash.Version)
|
|
updateThumbnailState(t, sqlStore, dash.Uid, dash.OrgId, models.ThumbnailStateStale)
|
|
upsertTestDashboardThumbnail(t, sqlStore, dash.Uid, dash.OrgId, dash.Version)
|
|
|
|
cmd := models.FindDashboardsWithStaleThumbnailsCommand{
|
|
Kind: kind,
|
|
Theme: theme,
|
|
}
|
|
res, err := sqlStore.FindDashboardsWithStaleThumbnails(context.Background(), &cmd)
|
|
require.NoError(t, err)
|
|
require.Len(t, res, 0)
|
|
})
|
|
|
|
t.Run("Should find dashboards without thumbnails", func(t *testing.T) {
|
|
setup()
|
|
dash := insertTestDashboard(t, sqlStore, "test dash 23", 1, savedFolder.Id, false, "prod", "webapp")
|
|
|
|
cmd := models.FindDashboardsWithStaleThumbnailsCommand{
|
|
Kind: kind,
|
|
Theme: theme,
|
|
}
|
|
res, err := sqlStore.FindDashboardsWithStaleThumbnails(context.Background(), &cmd)
|
|
require.NoError(t, err)
|
|
require.Len(t, res, 1)
|
|
require.Equal(t, dash.Id, res[0].Id)
|
|
})
|
|
|
|
t.Run("Should find dashboards with outdated thumbnails", func(t *testing.T) {
|
|
setup()
|
|
dash := insertTestDashboard(t, sqlStore, "test dash 23", 1, savedFolder.Id, false, "prod", "webapp")
|
|
upsertTestDashboardThumbnail(t, sqlStore, dash.Uid, dash.OrgId, dash.Version)
|
|
|
|
updateTestDashboard(t, sqlStore, dash, map[string]interface{}{
|
|
"tags": "different-tag",
|
|
})
|
|
|
|
cmd := models.FindDashboardsWithStaleThumbnailsCommand{
|
|
Kind: kind,
|
|
Theme: theme,
|
|
}
|
|
res, err := sqlStore.FindDashboardsWithStaleThumbnails(context.Background(), &cmd)
|
|
require.NoError(t, err)
|
|
require.Len(t, res, 1)
|
|
require.Equal(t, dash.Id, res[0].Id)
|
|
})
|
|
|
|
t.Run("Should not return dashboards with locked thumbnails even if they are outdated", func(t *testing.T) {
|
|
setup()
|
|
dash := insertTestDashboard(t, sqlStore, "test dash 23", 1, savedFolder.Id, false, "prod", "webapp")
|
|
upsertTestDashboardThumbnail(t, sqlStore, dash.Uid, dash.OrgId, dash.Version)
|
|
updateThumbnailState(t, sqlStore, dash.Uid, dash.OrgId, models.ThumbnailStateLocked)
|
|
|
|
updateTestDashboard(t, sqlStore, dash, map[string]interface{}{
|
|
"tags": "different-tag",
|
|
})
|
|
|
|
cmd := models.FindDashboardsWithStaleThumbnailsCommand{
|
|
Kind: kind,
|
|
Theme: theme,
|
|
}
|
|
res, err := sqlStore.FindDashboardsWithStaleThumbnails(context.Background(), &cmd)
|
|
require.NoError(t, err)
|
|
require.Len(t, res, 0)
|
|
})
|
|
|
|
t.Run("Should not return dashboards with manually uploaded thumbnails by default", func(t *testing.T) {
|
|
setup()
|
|
dash := insertTestDashboard(t, sqlStore, "test dash 23", 1, savedFolder.Id, false, "prod", "webapp")
|
|
upsertTestDashboardThumbnail(t, sqlStore, dash.Uid, dash.OrgId, models.DashboardVersionForManualThumbnailUpload)
|
|
|
|
updateTestDashboard(t, sqlStore, dash, map[string]interface{}{
|
|
"tags": "different-tag",
|
|
})
|
|
|
|
cmd := models.FindDashboardsWithStaleThumbnailsCommand{
|
|
Kind: kind,
|
|
Theme: theme,
|
|
}
|
|
res, err := sqlStore.FindDashboardsWithStaleThumbnails(context.Background(), &cmd)
|
|
require.NoError(t, err)
|
|
require.Len(t, res, 0)
|
|
})
|
|
|
|
t.Run("Should return dashboards with manually uploaded thumbnails if requested", func(t *testing.T) {
|
|
setup()
|
|
dash := insertTestDashboard(t, sqlStore, "test dash 23", 1, savedFolder.Id, false, "prod", "webapp")
|
|
upsertTestDashboardThumbnail(t, sqlStore, dash.Uid, dash.OrgId, models.DashboardVersionForManualThumbnailUpload)
|
|
|
|
updateTestDashboard(t, sqlStore, dash, map[string]interface{}{
|
|
"tags": "different-tag",
|
|
})
|
|
|
|
cmd := models.FindDashboardsWithStaleThumbnailsCommand{
|
|
Kind: kind,
|
|
Theme: theme,
|
|
IncludeManuallyUploadedThumbnails: true,
|
|
}
|
|
res, err := sqlStore.FindDashboardsWithStaleThumbnails(context.Background(), &cmd)
|
|
require.NoError(t, err)
|
|
require.Len(t, res, 1)
|
|
require.Equal(t, dash.Id, res[0].Id)
|
|
})
|
|
}
|
|
|
|
func getThumbnail(t *testing.T, sqlStore *SQLStore, dashboardUID string, orgId int64) *models.DashboardThumbnail {
|
|
t.Helper()
|
|
cmd := models.GetDashboardThumbnailCommand{
|
|
DashboardThumbnailMeta: models.DashboardThumbnailMeta{
|
|
DashboardUID: dashboardUID,
|
|
OrgId: orgId,
|
|
PanelID: 0,
|
|
Kind: kind,
|
|
Theme: theme,
|
|
},
|
|
}
|
|
|
|
thumb, err := sqlStore.GetThumbnail(context.Background(), &cmd)
|
|
require.NoError(t, err)
|
|
return thumb
|
|
}
|
|
|
|
func upsertTestDashboardThumbnail(t *testing.T, sqlStore *SQLStore, dashboardUID string, orgId int64, dashboardVersion int) *models.DashboardThumbnail {
|
|
t.Helper()
|
|
cmd := models.SaveDashboardThumbnailCommand{
|
|
DashboardThumbnailMeta: models.DashboardThumbnailMeta{
|
|
DashboardUID: dashboardUID,
|
|
OrgId: orgId,
|
|
PanelID: 0,
|
|
Kind: kind,
|
|
Theme: theme,
|
|
},
|
|
DashboardVersion: dashboardVersion,
|
|
Image: make([]byte, 0),
|
|
MimeType: "image/png",
|
|
}
|
|
dash, err := sqlStore.SaveThumbnail(context.Background(), &cmd)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, dash)
|
|
|
|
return dash
|
|
}
|
|
|
|
func updateThumbnailState(t *testing.T, sqlStore *SQLStore, dashboardUID string, orgId int64, state models.ThumbnailState) {
|
|
t.Helper()
|
|
cmd := models.UpdateThumbnailStateCommand{
|
|
DashboardThumbnailMeta: models.DashboardThumbnailMeta{
|
|
DashboardUID: dashboardUID,
|
|
OrgId: orgId,
|
|
PanelID: 0,
|
|
Kind: kind,
|
|
Theme: theme,
|
|
},
|
|
State: state,
|
|
}
|
|
err := sqlStore.UpdateThumbnailState(context.Background(), &cmd)
|
|
require.NoError(t, err)
|
|
}
|