Skip to content
Merged
7 changes: 7 additions & 0 deletions .changeset/saved-search-time-range.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@hyperdx/api': minor
'@hyperdx/app': minor
'@hyperdx/common-utils': minor
---

feat: saved searches remember the selected time range (e.g. past 1 day or a live tail interval) and open with it instead of live tail
1 change: 1 addition & 0 deletions packages/api/src/models/savedSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const savedSearchSchema = new Schema<
},
tags: [String],
filters: [{ type: mongoose.Schema.Types.Mixed }],
defaultTimeRange: { type: mongoose.Schema.Types.Mixed, required: false },
// Immutable: ownership attribution is set once at creation.
createdBy: {
type: mongoose.Schema.Types.ObjectId,
Expand Down
76 changes: 76 additions & 0 deletions packages/api/src/routers/api/__tests__/savedSearch.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,82 @@ describe('savedSearch router', () => {
expect(updatedSavedSearch.body.select).toBe('SELECT 1');
});

describe('defaultTimeRange', () => {
const ONE_DAY = { type: 'relative', value: 86400 };

it('persists a default time range on create and returns it from GET', async () => {
await agent
.post('/saved-search')
.send({ ...MOCK_SAVED_SEARCH, defaultTimeRange: ONE_DAY })
.expect(200);
const savedSearches = await agent.get('/saved-search').expect(200);
expect(savedSearches.body[0].defaultTimeRange).toEqual(ONE_DAY);
});

it('keeps the stored range when an update omits it', async () => {
const created = await agent
.post('/saved-search')
.send({ ...MOCK_SAVED_SEARCH, defaultTimeRange: ONE_DAY })
.expect(200);
const updated = await agent
.patch(`/saved-search/${created.body._id}`)
.send({ name: 'renamed' })
.expect(200);
expect(updated.body.defaultTimeRange).toEqual(ONE_DAY);
});

it('clears the stored range when an update sends null', async () => {
const created = await agent
.post('/saved-search')
.send({ ...MOCK_SAVED_SEARCH, defaultTimeRange: ONE_DAY })
.expect(200);
await agent
.patch(`/saved-search/${created.body._id}`)
.send({ defaultTimeRange: null })
.expect(200);
const dbRecord = await SavedSearch.findById(created.body._id);
expect(dbRecord?.defaultTimeRange ?? null).toBeNull();
});

it('rejects a malformed range', async () => {
await agent
.post('/saved-search')
.send({ ...MOCK_SAVED_SEARCH, defaultTimeRange: '1d' })
.expect(400);
await agent
.post('/saved-search')
.send({
...MOCK_SAVED_SEARCH,
defaultTimeRange: { type: 'relative', value: 0 },
})
.expect(400);
});

it('rejects a fixed window', async () => {
await agent
.post('/saved-search')
.send({
...MOCK_SAVED_SEARCH,
defaultTimeRange: {
type: 'historical',
value: [1700000000000, 1700003600000],
},
})
.expect(400);
});

it('still rejects null for fields that are not nullable', async () => {
const created = await agent
.post('/saved-search')
.send(MOCK_SAVED_SEARCH)
.expect(200);
await agent
.patch(`/saved-search/${created.body._id}`)
.send({ orderBy: null })
.expect(400);
});
});

it('can get saved searches', async () => {
await agent.post('/saved-search').send(MOCK_SAVED_SEARCH).expect(200);
const savedSearches = await agent.get('/saved-search').expect(200);
Expand Down
4 changes: 3 additions & 1 deletion packages/api/src/routers/api/savedSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,11 @@ router.patch(
// Re-parse: validateRequest validates without replacing req.body, so
// server-owned keys like `team`/`createdBy` would otherwise reach the
// update. Parsing drops every key the schema doesn't declare.
// Only undefined is dropped: an explicit null clears a nullable field
// (defaultTimeRange), and the schema rejects null for every other field.
const updates = _.omitBy(
updateSavedSearchBodySchema.parse(req.body),
_.isNil,
_.isUndefined,
);

const updatedSavedSearch = await updateSavedSearch(
Expand Down
117 changes: 106 additions & 11 deletions packages/app/src/DBSearchPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,15 @@ import { recordAction } from '@/utils/browserTelemetry';
import ChartSQLPreview, { SQLPreview } from './components/ChartSQLPreview';
import DBSqlRowTableWithSideBar from './components/DBSqlRowTableWithSidebar';
import PatternTable from './components/PatternTable';
import {
getPresetWindow,
getSavedSearchLanding,
WindowPreset,
} from './components/SavedSearches/savedSearchTimeRange';
import {
SaveTimeRangeField,
useSaveTimeRangeField,
} from './components/SavedSearches/SaveTimeRangeField';
import { DBSearchHeatmapChart } from './components/Search/DBSearchHeatmapChart';
import DirectTraceSidePanel from './components/Search/DirectTraceSidePanel';
import {
Expand Down Expand Up @@ -504,13 +513,23 @@ function SaveSearchModalComponent({
onCreate,
isUpdate,
savedSearchId,
isLive,
liveIntervalMs,
searchedTimeRange,
windowPreset,
openedAtMs,
}: {
searchedConfig: SearchConfig;
opened: boolean;
onClose: () => void;
onCreate: (savedSearchId: string) => Promise<void>;
isUpdate: boolean;
savedSearchId: string | undefined | null;
isLive: boolean;
liveIntervalMs: number;
searchedTimeRange: [Date, Date];
windowPreset: WindowPreset | null;
openedAtMs: number | null;
}) {
const { data: savedSearch } = useSavedSearch(
{ id: savedSearchId ?? '' },
Expand All @@ -519,6 +538,16 @@ function SaveSearchModalComponent({
},
);

const timeRangeOutcome = useSaveTimeRangeField({
openedAtMs,
isLive,
liveIntervalMs,
searchedTimeRange,
windowPreset,
isUpdate,
stored: savedSearch?.defaultTimeRange,
});

const {
control,
handleSubmit,
Expand Down Expand Up @@ -567,6 +596,9 @@ function SaveSearchModalComponent({
e.preventDefault();

handleSubmit(async ({ name }) => {
const timeRange = timeRangeOutcome.value;
const defaultTimeRangeUpdate =
timeRange === undefined ? {} : { defaultTimeRange: timeRange };
if (isUpdate) {
if (savedSearchId == null) {
throw new Error('savedSearchId is required for update');
Expand All @@ -584,6 +616,7 @@ function SaveSearchModalComponent({
orderBy: searchedConfig.orderBy ?? '',
filters: searchedConfig.filters ?? [],
tags: tags,
...defaultTimeRangeUpdate,
},
{
onSuccess: () => {
Expand Down Expand Up @@ -612,6 +645,7 @@ function SaveSearchModalComponent({
orderBy: searchedConfig.orderBy ?? '',
filters: searchedConfig.filters ?? [],
tags: tags,
...defaultTimeRangeUpdate,
});

await onCreate(savedSearch.id);
Expand Down Expand Up @@ -740,6 +774,10 @@ function SaveSearchModalComponent({
</Tags>
</Group>
</Box>
<SaveTimeRangeField
opensWith={timeRangeOutcome.opensWith}
customRangeIgnored={timeRangeOutcome.customRangeIgnored}
/>
<Button
data-testid="save-search-submit-button"
variant="primary"
Expand Down Expand Up @@ -1178,6 +1216,10 @@ function DBSearchPageContent() {
'isLive',
parseAsBoolean.withDefault(true),
);
const [interval, setInterval] = useQueryState(
'liveInterval',
parseAsInteger.withDefault(LIVE_TAIL_DURATION_MS),
);

useEffect(() => {
if (analysisMode === 'delta' || analysisMode === 'pattern') {
Expand Down Expand Up @@ -1330,6 +1372,9 @@ function DBSearchPageContent() {
setAppliedTimeInputValue(value);
}, []);
const isTimeInputEdited = displayedTimeInputValue !== appliedTimeInputValue;
// The preset the window on screen came from. A picked "Last 1 days" stays
// frozen until the next search, so its age alone can't say it's relative.
const [windowPreset, setWindowPreset] = useState<WindowPreset | null>(null);

const { isReady, searchedTimeRange, onSearch, onTimeRangeSelect } =
useNewTimeQuery({
Expand Down Expand Up @@ -1377,6 +1422,33 @@ function DBSearchPageContent() {
savedSearch.id === savedSearchId && // Make sure we've loaded the correct saved search
isSearchConfigEmpty // Only populate if URL doesn't have explicit config
) {
const nowMs = performance.timeOrigin + performance.now();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 minor — performance.timeOrigin + performance.now() is used to get around the Date.now() lint rule

Here and in openSaveSearchModal, both inside an effect/callback, which the rule's message says is fine. Use Date.now() with // eslint-disable-next-line no-restricted-syntax, as parseRelativeTimeQuery in timeQuery.ts already does, so the intent stays obvious.

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

const landing = getSavedSearchLanding(
savedSearch.defaultTimeRange,
window.location.search,
nowMs,
);
// Replace, not push: a pushed entry would leave the bare URL behind,
// and Back would land on it and re-apply the range forever.
if (landing?.kind === 'live') {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 P2 — The live-tail landing branch re-implements onTimePickerRelativeSearch and updates only the displayed input

This branch copies the writes from onTimePickerRelativeSearch (DBSearchPage.tsx:2470), as its own comment says. It then sets the label with the raw setDisplayedTimeInputValue instead of updateRelativeTimeInputValue/setAppliedTimeInput. That leaves appliedTimeInputValue at 'Live Tail', so isTimeInputEdited is true until another effect overwrites it. Give onTimePickerRelativeSearch an optional { history } argument, move it above this effect, and call it here followed by updateRelativeTimeInputValue(landing.intervalMs). That leaves one code path for applying a live interval.

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

// Same writes as picking a live tail range in the time picker.
const [start, end] = parseRelativeTimeQuery(landing.intervalMs, nowMs);
setIsLive(true, { history: 'replace' });
setInterval(landing.intervalMs, { history: 'replace' });
onTimeRangeSelect(start, end, null, { history: 'replace' });
const label = getRelativeTimeOptionLabel(landing.intervalMs);
if (label) setAppliedTimeInput(label);
} else if (landing?.kind === 'window') {
const [start, end] = landing.range;
setWindowPreset({
durationMs: end.getTime() - start.getTime(),
endMs: end.getTime(),
});
setIsLive(false, { history: 'replace' });
onTimeRangeSelect(landing.range[0], landing.range[1], undefined, {
history: 'replace',
});
}
setSearchedConfig({
source: savedSearch.source,
where: savedSearch.where,
Expand Down Expand Up @@ -1413,6 +1485,10 @@ function DBSearchPageContent() {
defaultSourceId,
directTraceId,
sources,
setIsLive,
setInterval,
onTimeRangeSelect,
setAppliedTimeInput,
]);

const [_queryErrors, setQueryErrors] = useState<{
Expand Down Expand Up @@ -1741,9 +1817,19 @@ function DBSearchPageContent() {
}, [setIsLive]);

const [modelFormExpanded, setModelFormExpanded] = useState(false); // Used in local mode
// openedAtMs is read in the click handler: the modal compares it with the
// searched window to decide whether that window reads as "the past N".
const [saveSearchModalState, setSaveSearchModalState] = useState<
'create' | 'update' | undefined
{ mode: 'create' | 'update'; openedAtMs: number } | undefined
>(undefined);
const openSaveSearchModal = useCallback(
(mode: 'create' | 'update') =>
setSaveSearchModalState({
mode,
openedAtMs: performance.timeOrigin + performance.now(),
}),
[],
);
const chartSearchConfig = useMemo(
() => ({
select: searchedConfig.select ?? '',
Expand Down Expand Up @@ -1792,7 +1878,7 @@ function DBSearchPageContent() {
const deleteSavedSearch = useDeleteSavedSearch();
const onSaveSearch = useCallback(() => {
if (savedSearch == null) {
setSaveSearchModalState('create');
openSaveSearchModal('create');
} else {
handleSubmit(s => {
updateSavedSearch.mutate(
Expand All @@ -1809,7 +1895,13 @@ function DBSearchPageContent() {
);
})();
}
}, [savedSearch, updateSavedSearch, onSubmit, handleSubmit]);
}, [
savedSearch,
updateSavedSearch,
onSubmit,
handleSubmit,
openSaveSearchModal,
]);

const handleUpdateTags = useCallback(
(newTags: string[]) => {
Expand Down Expand Up @@ -1879,11 +1971,6 @@ function DBSearchPageContent() {
// State for collapsing all expanded rows when resuming live tail
const [collapseAllRows, setCollapseAllRows] = useState(false);

const [interval, setInterval] = useQueryState(
'liveInterval',
parseAsInteger.withDefault(LIVE_TAIL_DURATION_MS),
);

const [refreshFrequency, setRefreshFrequency] = useQueryState(
'refreshFrequency',
parseAsInteger.withDefault(DEFAULT_REFRESH_FREQUENCY),
Expand Down Expand Up @@ -2407,6 +2494,9 @@ function DBSearchPageContent() {
const onTimePickerSearch = useCallback(
(range: string) => {
setIsLive(false);
setWindowPreset(
getPresetWindow(range, performance.timeOrigin + performance.now()),
);
onSearch(range);
},
[setIsLive, onSearch],
Expand Down Expand Up @@ -2714,7 +2804,7 @@ function DBSearchPageContent() {
});
}}
onClickSaveAsNew={() => {
setSaveSearchModalState('create');
openSaveSearchModal('create');
}}
/>
</Group>
Expand Down Expand Up @@ -2775,7 +2865,7 @@ function DBSearchPageContent() {
variant="secondary"
size="xs"
onClick={() => {
setSaveSearchModalState('update');
openSaveSearchModal('update');
}}
style={{ flexShrink: 0 }}
>
Expand Down Expand Up @@ -2902,8 +2992,13 @@ function DBSearchPageContent() {
onClose={clearSaveSearchModalState}
onCreate={onSavedSearchCreated}
searchedConfig={searchedConfig}
isUpdate={saveSearchModalState === 'update'}
isUpdate={saveSearchModalState?.mode === 'update'}
savedSearchId={savedSearchId}
isLive={isLive}
liveIntervalMs={interval}
searchedTimeRange={searchedTimeRange}
windowPreset={windowPreset}
openedAtMs={saveSearchModalState?.openedAtMs ?? null}
/>
)}
<DirectTraceSidePanel
Expand Down
Loading
Loading