Skip to content

πŸ“ˆ Open source, privacy-first web analytics β€” a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

fix: preserve summary range validation

Volodymyr Vovchenkoe80b9258ce51
diff --git a/backend/apps/cloud/src/analytics/analytics.service.ts b/backend/apps/cloud/src/analytics/analytics.service.ts
index e896faf17..1325d6bd5 100644
--- a/backend/apps/cloud/src/analytics/analytics.service.ts
+++ b/backend/apps/cloud/src/analytics/analytics.service.ts
@@ -579,17 +579,30 @@ export const getLowestPossibleTimeBucket = (
// getAnalyticsSummary may be called without an explicit timeBucket (the v2
// traffic summary endpoint never sends one). getGroupFromTo rounds the
-// window's lower boundary down to the bucket, so the fallback must be the
-// lowest bucket the period allows β€” a fixed DAY fallback would turn
-// `period=1h` into "since midnight"
+// window's lower boundary down to the bucket, so the period-only fallback
+// must be the lowest bucket the period allows β€” a fixed DAY fallback would
+// turn `period=1h` into "since midnight"
export const getSummaryTimeBucket = (
timeBucket?: string,
period?: string,
from?: string,
to?: string,
-): TimeBucketType =>
- (timeBucket as TimeBucketType) ||
- getLowestPossibleTimeBucket(period, from, to)
+): TimeBucketType => {
+ if (timeBucket) {
+ return timeBucket as TimeBucketType
+ }
+
+ // Preserve the legacy fallback for custom or partial ranges: passing them
+ // to getLowestPossibleTimeBucket could throw a misleading range-length
+ // error before getGroupFromTo runs its own validation
+ if (from || to || !period) {
+ return ['today', 'yesterday', 'custom'].includes(period ?? '')
+ ? TimeBucketType.HOUR
+ : TimeBucketType.DAY
+ }
+
+ return getLowestPossibleTimeBucket(period)
+}
const EXCLUDE_NULL_FOR = [
'so',
diff --git a/backend/apps/cloud/src/analytics/v2/__tests__/summary-time-bucket.spec.ts b/backend/apps/cloud/src/analytics/v2/__tests__/summary-time-bucket.spec.ts
index aa2b938d0..a0bff6c5b 100644
--- a/backend/apps/cloud/src/analytics/v2/__tests__/summary-time-bucket.spec.ts
+++ b/backend/apps/cloud/src/analytics/v2/__tests__/summary-time-bucket.spec.ts
@@ -49,6 +49,28 @@ describe('traffic summary window (no explicit timeBucket)', () => {
expect(groupTo).toBe('2026-08-23 14:37:42')
})
+ it('period=1d starts one day ago on an hour boundary, not at midnight', () => {
+ const { bucket, groupFrom, groupFromUTC } = getSummaryWindow(
+ '1d',
+ 'Etc/GMT',
+ )
+
+ expect(bucket).toBe(TimeBucketType.HOUR)
+ expect(groupFrom).toBe('2026-08-22 14:00:00')
+ expect(groupFromUTC).toBe('2026-08-22 14:00:00')
+ })
+
+ it('period=7d starts six days ago on an hour boundary', () => {
+ const { bucket, groupFrom, groupFromUTC } = getSummaryWindow(
+ '7d',
+ 'Etc/GMT',
+ )
+
+ expect(bucket).toBe(TimeBucketType.HOUR)
+ expect(groupFrom).toBe('2026-08-17 14:00:00')
+ expect(groupFromUTC).toBe('2026-08-17 14:00:00')
+ })
+
it('period=today still starts at midnight', () => {
const { bucket, groupFrom, groupFromUTC } = getSummaryWindow(
'today',
@@ -65,4 +87,28 @@ describe('traffic summary window (no explicit timeBucket)', () => {
TimeBucketType.DAY,
)
})
+
+ it('partial and invalid custom ranges keep the legacy fallback and are left to getGroupFromTo to validate', () => {
+ // Ranges must not be interpreted by getLowestPossibleTimeBucket: its NaN
+ // diff would throw a misleading range-length error before getGroupFromTo
+ // returns the correct validation error
+ expect(
+ getSummaryTimeBucket(undefined, undefined, '2026-08-20', undefined),
+ ).toBe(TimeBucketType.DAY)
+ expect(
+ getSummaryTimeBucket(undefined, 'custom', 'not-a-date', '2026-08-23'),
+ ).toBe(TimeBucketType.HOUR)
+
+ expect(() =>
+ getGroupFromTo(
+ 'not-a-date',
+ '2026-08-23',
+ getSummaryTimeBucket(undefined, 'custom', 'not-a-date', '2026-08-23'),
+ 'custom',
+ 'Etc/GMT',
+ undefined,
+ false,
+ ),
+ ).toThrow("The timeframe 'from' parameter is invalid")
+ })
})
diff --git a/backend/apps/community/src/analytics/analytics.service.ts b/backend/apps/community/src/analytics/analytics.service.ts
index 766029ce0..2bdda0d70 100644
--- a/backend/apps/community/src/analytics/analytics.service.ts
+++ b/backend/apps/community/src/analytics/analytics.service.ts
@@ -334,17 +334,30 @@ export const getLowestPossibleTimeBucket = (
// getAnalyticsSummary may be called without an explicit timeBucket (the v2
// traffic summary endpoint never sends one). getGroupFromTo rounds the
-// window's lower boundary down to the bucket, so the fallback must be the
-// lowest bucket the period allows β€” a fixed DAY fallback would turn
-// `period=1h` into "since midnight"
+// window's lower boundary down to the bucket, so the period-only fallback
+// must be the lowest bucket the period allows β€” a fixed DAY fallback would
+// turn `period=1h` into "since midnight"
export const getSummaryTimeBucket = (
timeBucket?: string,
period?: string,
from?: string,
to?: string,
-): TimeBucketType =>
- (timeBucket as TimeBucketType) ||
- getLowestPossibleTimeBucket(period, from, to)
+): TimeBucketType => {
+ if (timeBucket) {
+ return timeBucket as TimeBucketType
+ }
+
+ // Preserve the legacy fallback for custom or partial ranges: passing them
+ // to getLowestPossibleTimeBucket could throw a misleading range-length
+ // error before getGroupFromTo runs its own validation
+ if (from || to || !period) {
+ return ['today', 'yesterday', 'custom'].includes(period ?? '')
+ ? TimeBucketType.HOUR
+ : TimeBucketType.DAY
+ }
+
+ return getLowestPossibleTimeBucket(period)
+}
const checkIfTBAllowed = (
timeBucket: TimeBucketType,
diff --git a/backend/apps/community/src/analytics/v2/__tests__/summary-time-bucket.spec.ts b/backend/apps/community/src/analytics/v2/__tests__/summary-time-bucket.spec.ts
index aa2b938d0..a0bff6c5b 100644
--- a/backend/apps/community/src/analytics/v2/__tests__/summary-time-bucket.spec.ts
+++ b/backend/apps/community/src/analytics/v2/__tests__/summary-time-bucket.spec.ts
@@ -49,6 +49,28 @@ describe('traffic summary window (no explicit timeBucket)', () => {
expect(groupTo).toBe('2026-08-23 14:37:42')
})
+ it('period=1d starts one day ago on an hour boundary, not at midnight', () => {
+ const { bucket, groupFrom, groupFromUTC } = getSummaryWindow(
+ '1d',
+ 'Etc/GMT',
+ )
+
+ expect(bucket).toBe(TimeBucketType.HOUR)
+ expect(groupFrom).toBe('2026-08-22 14:00:00')
+ expect(groupFromUTC).toBe('2026-08-22 14:00:00')
+ })
+
+ it('period=7d starts six days ago on an hour boundary', () => {
+ const { bucket, groupFrom, groupFromUTC } = getSummaryWindow(
+ '7d',
+ 'Etc/GMT',
+ )
+
+ expect(bucket).toBe(TimeBucketType.HOUR)
+ expect(groupFrom).toBe('2026-08-17 14:00:00')
+ expect(groupFromUTC).toBe('2026-08-17 14:00:00')
+ })
+
it('period=today still starts at midnight', () => {
const { bucket, groupFrom, groupFromUTC } = getSummaryWindow(
'today',
@@ -65,4 +87,28 @@ describe('traffic summary window (no explicit timeBucket)', () => {
TimeBucketType.DAY,
)
})
+
+ it('partial and invalid custom ranges keep the legacy fallback and are left to getGroupFromTo to validate', () => {
+ // Ranges must not be interpreted by getLowestPossibleTimeBucket: its NaN
+ // diff would throw a misleading range-length error before getGroupFromTo
+ // returns the correct validation error
+ expect(
+ getSummaryTimeBucket(undefined, undefined, '2026-08-20', undefined),
+ ).toBe(TimeBucketType.DAY)
+ expect(
+ getSummaryTimeBucket(undefined, 'custom', 'not-a-date', '2026-08-23'),
+ ).toBe(TimeBucketType.HOUR)
+
+ expect(() =>
+ getGroupFromTo(
+ 'not-a-date',
+ '2026-08-23',
+ getSummaryTimeBucket(undefined, 'custom', 'not-a-date', '2026-08-23'),
+ 'custom',
+ 'Etc/GMT',
+ undefined,
+ false,
+ ),
+ ).toThrow("The timeframe 'from' parameter is invalid")
+ })
})