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
1 change: 1 addition & 0 deletions static/app/utils/useChartInterval.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe('useChartInterval', () => {
{value: '1h', label: '1 hour'},
{value: '3h', label: '3 hours'},
{value: '6h', label: '6 hours'},
{value: '1d', label: '1 day'},
]);
expect(chartInterval).toBe('1h'); // default

Expand Down
35 changes: 22 additions & 13 deletions static/app/utils/useChartInterval.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,32 @@ function useChartIntervalImpl({
intervalOptions: Array<{label: string; value: string}>,
] {
const {datetime} = pagefilters.selection;
const intervalOptions = useMemo(
() => getIntervalOptionsForPageFilter(datetime),
[datetime]
);

const interval = useMemo(() => {
const decodedInterval = decodeScalar(location.query.interval);
const {intervalOptions, defaultInterval} = useMemo(() => {
const diffInMinutes = getDiffInMinutes(datetime);
const options = getIntervalOptionsForPageFilter(datetime);

// Default to the second largest option or largest option
const fallbackInterval =
// Compute the default from the ladder-derived options, before appending extras
const fallback =
unspecifiedStrategy === ChartIntervalUnspecifiedStrategy.USE_SMALLEST
? intervalOptions[0]!.value
: (intervalOptions[intervalOptions.length - 2]?.value ??
intervalOptions[intervalOptions.length - 1]!.value);
? options[0]!.value
: (options[options.length - 2]?.value ?? options[options.length - 1]!.value);

if (diffInMinutes >= MINIMUM_DURATION_FOR_ONE_DAY_INTERVAL) {
options.push(ONE_DAY_OPTION);
}

return {intervalOptions: options, defaultInterval: fallback};
}, [datetime, unspecifiedStrategy]);

const interval = useMemo(() => {
const decodedInterval = decodeScalar(location.query.interval);

return decodedInterval &&
intervalOptions.some(option => option.value === decodedInterval)
? decodedInterval
: fallbackInterval;
}, [location.query.interval, unspecifiedStrategy, intervalOptions]);
: defaultInterval;
}, [location.query.interval, intervalOptions, defaultInterval]);

const setInterval = useCallback(
(newInterval: string) => {
Expand Down Expand Up @@ -139,6 +145,9 @@ const MAXIMUM_INTERVAL = new GranularityLadder([
[0, '1m'],
]);

const ONE_DAY_OPTION = {value: '1d', label: t('1 day')};
const MINIMUM_DURATION_FOR_ONE_DAY_INTERVAL = TWO_WEEKS;

export function getIntervalOptionsForPageFilter(datetime: PageFilters['datetime']) {
const diffInMinutes = getDiffInMinutes(datetime);

Expand Down
Loading