diff --git a/.changeset/save-temporary-dashboard.md b/.changeset/save-temporary-dashboard.md new file mode 100644 index 000000000..ba2db8664 --- /dev/null +++ b/.changeset/save-temporary-dashboard.md @@ -0,0 +1,5 @@ +--- +'@hyperdx/app': patch +--- + +feat: save a temporary dashboard with its content intact diff --git a/packages/app/src/DBDashboardPage.tsx b/packages/app/src/DBDashboardPage.tsx index 6c712a8b1..429612191 100644 --- a/packages/app/src/DBDashboardPage.tsx +++ b/packages/app/src/DBDashboardPage.tsx @@ -178,6 +178,7 @@ import { useCreateDashboard, useDashboards, useDeleteDashboard, + withoutAlert, } from '@/dashboard'; import { useAlertAnnotations } from '@/hooks/useAlertAnnotations'; import { getMinGranularitySeconds } from '@/hooks/useChartConfig'; @@ -416,6 +417,41 @@ const whereLanguageParser = parseAsString.withDefault( typeof window !== 'undefined' ? (getStoredLanguage() ?? 'lucene') : 'lucene', ); +function toWhereLanguage(value: string | null): SearchConditionLanguage { + return value === 'sql' || value === 'lucene' + ? value + : (getStoredLanguage() ?? 'lucene'); +} + +// Shared by "Save query" and saving a temporary dashboard so both persist a +// view the same way. +function toViewDefaults({ + where, + whereLanguage, + rangeSeconds, + filterValues, +}: { + where: string; + whereLanguage: SearchConditionLanguage; + rangeSeconds: number | null; + filterValues: Dashboard['savedFilterValues'] | null; +}): Pick< + Dashboard, + 'savedQuery' | 'savedQueryLanguage' | 'savedFilterValues' | 'savedDateRange' +> { + const savedQuery = where || null; + return { + savedQuery, + savedQueryLanguage: savedQuery ? whereLanguage || 'lucene' : null, + savedFilterValues: filterValues ?? [], + // The time input shows absolute text even for relative picks, so a saved + // range can only be its duration. + savedDateRange: rangeSeconds + ? { type: 'relative', value: rangeSeconds } + : undefined, + }; +} + /** * A tile that cannot draw its chart yet, explaining why in its place. Keeps the * chrome identical to a rendered tile so the toolbar stays usable. @@ -2219,46 +2255,27 @@ function DBDashboardPage({ // Sync changes to the URL params into the form useEffect(() => { setValue('where', where); - setValue( - 'whereLanguage', - whereLanguage === 'sql' || whereLanguage === 'lucene' - ? whereLanguage - : (getStoredLanguage() ?? 'lucene'), - ); + setValue('whereLanguage', toWhereLanguage(whereLanguage)); }, [setValue, where, whereLanguage]); const handleSaveQuery = useCallback(() => { if (!dashboard || isLocalDashboard) return; - // Execute the query first (updates URL) + // Apply the draft query/time input so what's saved is what's shown. onSubmit(); - - // Then save to database (reads from form values which were just submitted to URL) const formValues = getValues(); - const currentWhere = formValues.where || null; - const currentWhereLanguage = currentWhere - ? formValues.whereLanguage || 'lucene' - : null; - const currentFilterValues = filterValueEntries?.length - ? filterValueEntries - : []; - - const currentRelativeDateRange = timeRangeInputToSeconds( - displayedTimeInputValue, - timeZone, - ); + const { savedDateRange, ...defaults } = toViewDefaults({ + where: formValues.where, + whereLanguage: formValues.whereLanguage, + rangeSeconds: timeRangeInputToSeconds(displayedTimeInputValue, timeZone), + filterValues: filterValueEntries, + }); setDashboard( produce(dashboard, draft => { - draft.savedQuery = currentWhere; - draft.savedQueryLanguage = currentWhereLanguage; - draft.savedFilterValues = currentFilterValues; - // Only supporting relative date range saving ATM - if (currentRelativeDateRange) { - draft.savedDateRange = { - type: 'relative', - value: currentRelativeDateRange, - }; + Object.assign(draft, defaults); + if (savedDateRange) { + draft.savedDateRange = savedDateRange; } }), () => { @@ -2275,11 +2292,11 @@ function DBDashboardPage({ dashboard, isLocalDashboard, setDashboard, - getValues, - filterValueEntries, onSubmit, - timeZone, + getValues, displayedTimeInputValue, + timeZone, + filterValueEntries, ]); const handleRemoveSavedQuery = useCallback(() => { if (!dashboard || isLocalDashboard) return; @@ -2565,14 +2582,7 @@ function DBDashboardPage({ ...dashboard, tiles: [ ...dashboard.tiles, - { - ...chart, - id: makeId(), - config: { - ...chart.config, - alert: undefined, - }, - }, + { ...withoutAlert(chart), id: makeId() }, ], }); } @@ -2929,25 +2939,60 @@ function DBDashboardPage({ const createDashboard = useCreateDashboard(); const onCreateDashboard = useCallback(() => { + if (!dashboard) return; + + // The new /dashboards/{id} page drops this page's URL state, so the view + // only survives as saved defaults. + // Reads the applied URL state rather than calling onSubmit(): its queued + // URL update would cancel the navigation to the new dashboard. + const [start, end] = searchedTimeRange; + const viewDefaults = toViewDefaults({ + where, + whereLanguage: toWhereLanguage(whereLanguage), + rangeSeconds: (end.getTime() - start.getTime()) / 1000, + filterValues: filterValueEntries, + }); + createDashboard.mutate( { - name: 'My Dashboard', - tiles: [], - tags: [], + name: dashboard.name?.trim() ? dashboard.name : 'My New Dashboard', + tiles: dashboard.tiles.map(withoutAlert), + tags: dashboard.tags ?? [], + filters: dashboard.filters, + containers: dashboard.containers, + ...viewDefaults, }, { onSuccess: data => { router.push(`/dashboards/${data.id}`); }, + onError: e => { + notifications.show({ + color: 'red', + title: 'Unable to save dashboard', + message: e.message.slice(0, 100), + autoClose: 5000, + }); + }, }, ); - }, [createDashboard, router]); + }, [ + createDashboard, + router, + dashboard, + searchedTimeRange, + where, + whereLanguage, + filterValueEntries, + ]); const [isSaving, setIsSaving] = useState(false); const hasTiles = dashboard && dashboard.tiles.length > 0; - const hasSavedQueryAndFilterDefaults = Boolean( - dashboard?.savedQuery || dashboard?.savedFilterValues?.length, + const hasSavedDefaults = Boolean( + dashboard?.savedQuery || + dashboard?.savedFilterValues?.length || + dashboard?.savedDateRange, ); const dashboardMeta = @@ -3179,11 +3224,11 @@ function DBDashboardPage({ leftSection={} onClick={handleSaveQuery} > - {hasSavedQueryAndFilterDefaults + {hasSavedDefaults ? 'Update Default Query & Filters' : 'Save Query & Filters as Default'} - {hasSavedQueryAndFilterDefaults && ( + {hasSavedDefaults && ( } @@ -3359,15 +3404,16 @@ function DBDashboardPage({ - This is a temporary dashboard and can not be saved. + This is currently an unsaved, temporary dashboard. diff --git a/packages/app/src/dashboard.ts b/packages/app/src/dashboard.ts index b727c532d..3e7c3af72 100644 --- a/packages/app/src/dashboard.ts +++ b/packages/app/src/dashboard.ts @@ -38,6 +38,12 @@ export type Tile = { tabId?: string; }; +// Copied tiles must not carry alerts: saving a dashboard creates every tile's +// alert server-side, without the user seeing an alert editor. +export function withoutAlert(tile: Tile): Tile { + return { ...tile, config: { ...tile.config, alert: undefined } }; +} + export type Dashboard = { id: string; name: string; diff --git a/packages/app/tests/e2e/features/temporary-dashboard.spec.ts b/packages/app/tests/e2e/features/temporary-dashboard.spec.ts index b72536be8..dc625cd49 100644 --- a/packages/app/tests/e2e/features/temporary-dashboard.spec.ts +++ b/packages/app/tests/e2e/features/temporary-dashboard.spec.ts @@ -1,6 +1,7 @@ import { DashboardPage } from '../page-objects/DashboardPage'; import { DashboardsListPage } from '../page-objects/DashboardsListPage'; import { expect, test } from '../utils/base-test'; +import { DEFAULT_LOGS_SOURCE_NAME } from '../utils/constants'; test.describe('Temporary Dashboard', { tag: ['@dashboard'] }, () => { let dashboardPage: DashboardPage; @@ -24,8 +25,9 @@ test.describe('Temporary Dashboard', { tag: ['@dashboard'] }, () => { await test.step('Verify the temporary dashboard banner is visible', async () => { await expect(dashboardPage.temporaryDashboardBanner).toBeVisible(); await expect(dashboardPage.temporaryDashboardBanner).toContainText( - 'This is a temporary dashboard and can not be saved.', + 'This is currently an unsaved, temporary dashboard.', ); + await expect(dashboardPage.createButton).toHaveText('Save dashboard'); }); }, ); @@ -79,20 +81,57 @@ test.describe('Temporary Dashboard', { tag: ['@dashboard'] }, () => { ); test( - 'should convert temporary dashboard to saved dashboard', + 'should carry tiles, title, filters, and saved filters into the saved dashboard', { tag: '@full-stack' }, async ({ page }) => { + const uniqueName = `Saved From Temp ${Date.now()}`; + const savedQuery = 'SeverityText:error'; + await dashboardPage.goto(); await test.step('Verify the temporary dashboard banner is visible', async () => { await expect(dashboardPage.temporaryDashboardBanner).toBeVisible(); }); - await test.step('Click Create New Saved Dashboard', async () => { - await dashboardPage.createButton.click(); + await test.step('Rename the temporary dashboard', async () => { + await dashboardPage.renameDashboard('My New Dashboard', uniqueName); + }); + + await test.step('Add a tile', async () => { + await dashboardPage.addTile(); + await dashboardPage.chartEditor.createBasicChart('Temp tile'); + await expect(dashboardPage.getTiles()).toHaveCount(1, { + timeout: 10000, + }); + }); + + await test.step('Add a dashboard filter', async () => { + await dashboardPage.openEditFiltersModal(); + await expect(dashboardPage.emptyFiltersList).toBeVisible(); + await dashboardPage.addFilterToDashboard( + 'Service', + DEFAULT_LOGS_SOURCE_NAME, + 'ServiceName', + ); + await expect( + dashboardPage.getFilterItemByName('Service'), + ).toBeVisible(); + await dashboardPage.closeFiltersModal(); }); - await test.step('Verify navigation to a saved dashboard', async () => { + await test.step('Select a filter value and enter a search query', async () => { + await dashboardPage.clickFilterOption('Service', 'accounting'); + await expect( + dashboardPage + .getFilterSelectByName('Service') + .locator('..') + .getByText('accounting'), + ).toBeVisible(); + await dashboardPage.setGlobalFilter(savedQuery); + }); + + await test.step('Save the temporary dashboard', async () => { + await dashboardPage.createButton.click(); await expect(page).toHaveURL(/\/dashboards\/.+/, { timeout: 10000 }); }); @@ -104,6 +143,36 @@ test.describe('Temporary Dashboard', { tag: ['@dashboard'] }, () => { .getByRole('link', { name: 'Dashboards' }), ).toBeVisible(); }); + + await test.step('Verify the title carried over', async () => { + await expect(dashboardPage.dashboardName).toHaveValue(uniqueName, { + timeout: 10000, + }); + }); + + await test.step('Verify the tile carried over', async () => { + await expect(dashboardPage.getTiles()).toHaveCount(1, { + timeout: 10000, + }); + }); + + await test.step('Verify the filter carried over', async () => { + await expect( + dashboardPage.getFilterSelectByName('Service'), + ).toBeVisible({ timeout: 10000 }); + }); + + await test.step('Verify the saved query and filter value carried over', async () => { + await expect(dashboardPage.searchInput).toHaveValue(savedQuery, { + timeout: 10000, + }); + await expect( + dashboardPage + .getFilterSelectByName('Service') + .locator('..') + .getByText('accounting'), + ).toBeVisible({ timeout: 10000 }); + }); }, ); }); diff --git a/packages/app/tests/e2e/page-objects/DashboardPage.ts b/packages/app/tests/e2e/page-objects/DashboardPage.ts index d2e972ae3..f1e25b106 100644 --- a/packages/app/tests/e2e/page-objects/DashboardPage.ts +++ b/packages/app/tests/e2e/page-objects/DashboardPage.ts @@ -261,7 +261,14 @@ export class DashboardPage { * Edit dashboard name */ async editDashboardName(newName: string) { - await this.renameDashboard('My Dashboard', newName); + // The default name depends on how the dashboard was created: the list-page + // "Saved Dashboard" action names it "My Dashboard", while saving a + // temporary dashboard carries its name over ("My New Dashboard" by + // default). Read whatever is there rather than assuming one. + const nameInput = this.page.getByTestId('dashboard-name-input'); + await expect(nameInput).not.toHaveValue('', { timeout: 10000 }); + const currentName = await nameInput.inputValue(); + await this.renameDashboard(currentName, newName); } /**