Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/service-map-sampling-storage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hyperdx/app': patch
---

feat: Remember the service map sampling rate in local storage; a `samplingFactor` URL param still takes precedence
30 changes: 27 additions & 3 deletions packages/app/src/DBServiceMapPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { IS_LOCAL_MODE } from '@/config';
import { useGetKeyValues } from '@/hooks/useMetadata';
import { useResolvedSourceParam } from '@/hooks/useResolvedSourceParam';
import { withAppNavForSurface } from '@/layout';
import { useLocalStorage } from '@/utils';
import { parseAsStringEncoded } from '@/utils/queryParsers';

import OnboardingModal from './components/OnboardingModal';
Expand Down Expand Up @@ -76,6 +77,9 @@ const SAMPLING_FACTORS = [
},
];

const DEFAULT_SAMPLING_FACTOR = 10;
const SAMPLING_FACTOR_STORAGE_KEY = 'hdx-service-map-sampling-factor';

const DEFAULT_INTERVAL = 'Past 1h';

const searchQueryStateMap = {
Expand Down Expand Up @@ -210,9 +214,28 @@ function DBServiceMapPage() {
})();
}, [handleSubmit, setSearchedConfig, displayedTimeInputValue, onSearch]);

const [samplingFactor, setSamplingFactor] = useQueryState(
// The URL param wins so shared links reproduce the sender's view, but only
// slider changes are remembered: opening a link must not overwrite the
// viewer's saved preference.
const [samplingFactorParam, setSamplingFactorParam] = useQueryState(
'samplingFactor',
parseAsInteger.withDefault(10),
parseAsInteger,
);
const [storedSamplingFactor, setStoredSamplingFactor] = useLocalStorage(
SAMPLING_FACTOR_STORAGE_KEY,
DEFAULT_SAMPLING_FACTOR,
);
const samplingFactor =
Comment thread
pulpdrew marked this conversation as resolved.
Resolved
samplingFactorParam ??
(SAMPLING_FACTORS.some(factor => factor.value === storedSamplingFactor)
? storedSamplingFactor
: DEFAULT_SAMPLING_FACTOR);
Comment thread
pulpdrew marked this conversation as resolved.
Resolved
const setSamplingFactor = useCallback(
(value: number) => {
setStoredSamplingFactor(value);
setSamplingFactorParam(value);
},
[setStoredSamplingFactor, setSamplingFactorParam],
);
const { label: samplingLabel = '' } =
SAMPLING_FACTORS.find(factor => factor.value === samplingFactor) ?? {};
Expand Down Expand Up @@ -253,7 +276,7 @@ function DBServiceMapPage() {

const headerActions = (
<Group gap="sm" wrap="nowrap">
<Text bg="inherit" size="sm">
<Text bg="inherit" size="sm" data-testid="service-map-sampling-label">
Sampling {samplingLabel}
</Text>
<div style={{ minWidth: '200px' }}>
Expand All @@ -266,6 +289,7 @@ function DBServiceMapPage() {
)}
onChange={v => setSamplingFactor(SAMPLING_FACTORS[v].value)}
showLabelOnHover={false}
data-testid="service-map-sampling-slider"
/>
</div>
<TimePicker
Expand Down
53 changes: 53 additions & 0 deletions packages/app/tests/e2e/features/service-map-sampling.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Page } from '@playwright/test';

import { expect, test } from '../utils/base-test';

const STORAGE_KEY = 'hdx-service-map-sampling-factor';

function getStoredSamplingFactor(page: Page) {
return page.evaluate(key => window.localStorage.getItem(key), STORAGE_KEY);
}

async function gotoServiceMap(page: Page, query = '') {
await page.goto(`/service-map${query}`);
await expect(page.getByTestId('service-map-sampling-slider')).toBeVisible();
}

test.describe('Service map sampling rate', { tag: ['@service-map'] }, () => {
test('persists slider selections to local storage', async ({ page }) => {
await gotoServiceMap(page);
const label = page.getByTestId('service-map-sampling-label');
await expect(label).toHaveText('Sampling 10%');
expect(await getStoredSamplingFactor(page)).toBeNull();

await page
.getByTestId('service-map-sampling-slider')
.getByRole('slider')
.press('ArrowRight');

await expect(label).toHaveText('Sampling 50%');
await expect(page).toHaveURL(/samplingFactor=2/);
expect(await getStoredSamplingFactor(page)).toBe('2');

await gotoServiceMap(page);
await expect(label).toHaveText('Sampling 50%');
});

test('prefers the URL param without overwriting local storage', async ({
page,
}) => {
await gotoServiceMap(page);
await page.evaluate(
key => window.localStorage.setItem(key, JSON.stringify(2)),
STORAGE_KEY,
);

await gotoServiceMap(page, '?samplingFactor=100');
const label = page.getByTestId('service-map-sampling-label');
await expect(label).toHaveText('Sampling 1%');
expect(await getStoredSamplingFactor(page)).toBe('2');

await gotoServiceMap(page);
await expect(label).toHaveText('Sampling 50%');
});
});
Loading