Repository navigation
feat(app): save a temporary dashboard with its content intact #3729
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@hyperdx/app': patch | ||
| --- | ||
|
|
||
| feat: save a temporary dashboard with its content intact |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 P2 — Saving a temporary dashboard always stores a default time range, even an odd one from a zoomed or absolute view
Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond |
||
| 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, | ||
|
pulpdrew marked this conversation as resolved.
Resolved
|
||
| containers: dashboard.containers, | ||
| ...viewDefaults, | ||
| }, | ||
| { | ||
|
pulpdrew marked this conversation as resolved.
Resolved
|
||
| 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={<IconDeviceFloppy size={16} />} | ||
| onClick={handleSaveQuery} | ||
| > | ||
| {hasSavedQueryAndFilterDefaults | ||
| {hasSavedDefaults | ||
| ? 'Update Default Query & Filters' | ||
| : 'Save Query & Filters as Default'} | ||
| </Menu.Item> | ||
| {hasSavedQueryAndFilterDefaults && ( | ||
| {hasSavedDefaults && ( | ||
| <Menu.Item | ||
| data-testid="remove-default-query-filters-menu-item" | ||
| leftSection={<IconX size={16} />} | ||
|
|
@@ -3359,15 +3404,16 @@ function DBDashboardPage({ | |
| <Paper mt="xs" mb="md" p="md" data-testid="temporary-dashboard-banner"> | ||
| <Flex justify="space-between" align="center"> | ||
| <Text size="sm"> | ||
| This is a temporary dashboard and can not be saved. | ||
| This is currently an unsaved, temporary dashboard. | ||
| </Text> | ||
| <Button | ||
| variant="primary" | ||
| fw={400} | ||
| onClick={onCreateDashboard} | ||
| loading={createDashboard.isPending || createDashboard.isSuccess} | ||
| data-testid="create-dashboard-button" | ||
| > | ||
| Create New Saved Dashboard | ||
| Save dashboard | ||
| </Button> | ||
| </Flex> | ||
| </Paper> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -261,7 +261,14 @@ | |
| * 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); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -279,7 +286,7 @@ | |
| // The document title comes from the saved dashboard, so it only changes | ||
| // once the rename has persisted. | ||
| const escaped = newName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); | ||
| await expect(this.page).toHaveTitle(new RegExp(`^${escaped} - `), { | ||
|
Check failure on line 289 in packages/app/tests/e2e/page-objects/DashboardPage.ts
|
||
| timeout: 10000, | ||
| }); | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.