Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

fix(studio): bucket multi-hour report ranges hourly instead of by minute (#48409)

Ali Waseem66658ab3398b
diff --git a/apps/studio/hooks/misc/__tests__/useReportDateRange.test.ts b/apps/studio/hooks/misc/__tests__/useReportDateRange.test.ts
new file mode 100644
index 00000000000..400c0c7094d
--- /dev/null
+++ b/apps/studio/hooks/misc/__tests__/useReportDateRange.test.ts
@@ -0,0 +1,35 @@
+import dayjs from 'dayjs'
+import { describe, expect, test } from 'vitest'
+
+import { analyticsIntervalToGranularity } from '@/data/reports/report.utils'
+import { getIntervalGranularity } from '@/hooks/misc/useReportDateRange'
+
+const rangeOf = (value: number, unit: dayjs.ManipulateType) => {
+ const to = dayjs()
+ return { from: to.subtract(value, unit).toISOString(), to: to.toISOString() }
+}
+
+describe('getIntervalGranularity', () => {
+ test.each([
+ [24, 'hour' as dayjs.ManipulateType, 24],
+ [7, 'day' as dayjs.ManipulateType, 168],
+ ])('a %s-%s range buckets hourly (FE-4023)', (value, unit, expectedBuckets) => {
+ const { from, to } = rangeOf(value, unit)
+ const interval = getIntervalGranularity(from, to)
+
+ expect(interval).toBe('1h')
+ expect(analyticsIntervalToGranularity(interval)).toBe('hour')
+ expect(dayjs(to).diff(from, 'hour')).toBe(expectedBuckets)
+ })
+
+ test.each([
+ [10, 'minute' as dayjs.ManipulateType, '1m'],
+ [1, 'hour' as dayjs.ManipulateType, '1m'],
+ [3, 'hour' as dayjs.ManipulateType, '2m'],
+ [14, 'day' as dayjs.ManipulateType, '1d'],
+ [28, 'day' as dayjs.ManipulateType, '1d'],
+ ])('leaves a %s-%s range on the %s interval', (value, unit, expected) => {
+ const { from, to } = rangeOf(value, unit)
+ expect(getIntervalGranularity(from, to)).toBe(expected)
+ })
+})
diff --git a/apps/studio/hooks/misc/useReportDateRange.ts b/apps/studio/hooks/misc/useReportDateRange.ts
index 73b4690f029..24417c3990b 100644
--- a/apps/studio/hooks/misc/useReportDateRange.ts
+++ b/apps/studio/hooks/misc/useReportDateRange.ts
@@ -19,8 +19,7 @@ export const getIntervalGranularity = (from: string, to: string): AnalyticsInter
if (diffInHours <= 1) return '1m'
if (diffInHours <= 12) return '2m'
- if (diffInHours <= 24) return '10m'
- if (diffInDays <= 7) return '30m'
+ if (diffInDays <= 7) return '1h'
return '1d'
}