π 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!
TypeScript chartsclickhousedockergdprgoogle-analyticsmetricsmonitoringmysqlnestjsprivacyredisself-hostedselfhostedstatisticstailwindcssweb-analyticswebanalyticswebsite
fix: honour one-hour period in traffic summary
Volodymyr Vovchenkoeb98d0c4d8a4
diff --git a/backend/apps/cloud/src/analytics/analytics.service.ts b/backend/apps/cloud/src/analytics/analytics.service.tsindex 1f0c664ff..e896faf17 100644--- a/backend/apps/cloud/src/analytics/analytics.service.ts+++ b/backend/apps/cloud/src/analytics/analytics.service.ts@@ -577,6 +577,20 @@ export const getLowestPossibleTimeBucket = (return _head(tbMap.tb)}+// 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"+export const getSummaryTimeBucket = (+ timeBucket?: string,+ period?: string,+ from?: string,+ to?: string,+): TimeBucketType =>+ (timeBucket as TimeBucketType) ||+ getLowestPossibleTimeBucket(period, from, to)+const EXCLUDE_NULL_FOR = ['so','me',@@ -4885,12 +4899,12 @@ export class AnalyticsService {): Promise<IOverall> {const safeTimezone = this.getSafeTimezone(timezone)- // Determine the time bucket for chart data- const effectiveTimeBucket = ['today', 'yesterday', 'custom'].includes(+ const effectiveTimeBucket = getSummaryTimeBucket(+ timeBucket,period,+ from,+ to,)- ? TimeBucketType.HOUR- : (timeBucket as TimeBucketType) || TimeBucketType.DAYconst { groupFrom, groupTo, groupFromUTC, groupToUTC } =this.getGroupFromTo(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.tsnew file mode 100644index 000000000..aa2b938d0--- /dev/null+++ b/backend/apps/cloud/src/analytics/v2/__tests__/summary-time-bucket.spec.ts@@ -0,0 +1,68 @@+import { AnalyticsService, getSummaryTimeBucket } from '../../analytics.service'+import { TimeBucketType } from '../../dto/getData.dto'++// getGroupFromTo does not touch `this`, so it can be exercised without+// instantiating the service and its DI graph+const getGroupFromTo = AnalyticsService.prototype.getGroupFromTo.bind(+ null as unknown as AnalyticsService,+)++// Mirrors how getAnalyticsSummary computes the summary window: the resolved+// bucket is fed to getGroupFromTo with no from/to, no diff and no timebucket+// validation+const getSummaryWindow = (period: string, timezone: string) => {+ const bucket = getSummaryTimeBucket(undefined, period, undefined, undefined)++ return {+ bucket,+ ...getGroupFromTo(+ undefined as unknown as string,+ undefined as unknown as string,+ bucket,+ period,+ timezone,+ undefined,+ false,+ ),+ }+}++describe('traffic summary window (no explicit timeBucket)', () => {+ beforeEach(() => {+ jest.useFakeTimers()+ jest.setSystemTime(new Date('2026-08-23T14:37:42Z'))+ })++ afterEach(() => {+ jest.useRealTimers()+ })++ it('period=1h starts one hour ago on a minute boundary, not at midnight', () => {+ const { bucket, groupFrom, groupFromUTC, groupTo } = getSummaryWindow(+ '1h',+ 'Etc/GMT',+ )++ expect(bucket).toBe(TimeBucketType.MINUTE)+ expect(groupFrom).toBe('2026-08-23 13:37:00')+ expect(groupFromUTC).toBe('2026-08-23 13:37:00')+ expect(groupTo).toBe('2026-08-23 14:37:42')+ })++ it('period=today still starts at midnight', () => {+ const { bucket, groupFrom, groupFromUTC } = getSummaryWindow(+ 'today',+ 'Etc/GMT',+ )++ expect(bucket).toBe(TimeBucketType.HOUR)+ expect(groupFrom).toBe('2026-08-23 00:00:00')+ expect(groupFromUTC).toBe('2026-08-23 00:00:00')+ })++ it('an explicitly requested timeBucket takes precedence', () => {+ expect(getSummaryTimeBucket('day', '1h', undefined, undefined)).toBe(+ TimeBucketType.DAY,+ )+ })+})diff --git a/backend/apps/community/src/analytics/analytics.service.ts b/backend/apps/community/src/analytics/analytics.service.tsindex 6de8b073d..766029ce0 100644--- a/backend/apps/community/src/analytics/analytics.service.ts+++ b/backend/apps/community/src/analytics/analytics.service.ts@@ -332,6 +332,20 @@ export const getLowestPossibleTimeBucket = (return _head(tbMap.tb)}+// 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"+export const getSummaryTimeBucket = (+ timeBucket?: string,+ period?: string,+ from?: string,+ to?: string,+): TimeBucketType =>+ (timeBucket as TimeBucketType) ||+ getLowestPossibleTimeBucket(period, from, to)+const checkIfTBAllowed = (timeBucket: TimeBucketType,from: string,@@ -3489,12 +3503,12 @@ export class AnalyticsService {): Promise<IOverall> {const safeTimezone = this.getSafeTimezone(timezone)- // Determine the time bucket for chart data- const effectiveTimeBucket = ['today', 'yesterday', 'custom'].includes(+ const effectiveTimeBucket = getSummaryTimeBucket(+ timeBucket,period,+ from,+ to,)- ? TimeBucketType.HOUR- : (timeBucket as TimeBucketType) || TimeBucketType.DAYconst { groupFrom, groupTo, groupFromUTC, groupToUTC } =this.getGroupFromTo(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.tsnew file mode 100644index 000000000..aa2b938d0--- /dev/null+++ b/backend/apps/community/src/analytics/v2/__tests__/summary-time-bucket.spec.ts@@ -0,0 +1,68 @@+import { AnalyticsService, getSummaryTimeBucket } from '../../analytics.service'+import { TimeBucketType } from '../../dto/getData.dto'++// getGroupFromTo does not touch `this`, so it can be exercised without+// instantiating the service and its DI graph+const getGroupFromTo = AnalyticsService.prototype.getGroupFromTo.bind(+ null as unknown as AnalyticsService,+)++// Mirrors how getAnalyticsSummary computes the summary window: the resolved+// bucket is fed to getGroupFromTo with no from/to, no diff and no timebucket+// validation+const getSummaryWindow = (period: string, timezone: string) => {+ const bucket = getSummaryTimeBucket(undefined, period, undefined, undefined)++ return {+ bucket,+ ...getGroupFromTo(+ undefined as unknown as string,+ undefined as unknown as string,+ bucket,+ period,+ timezone,+ undefined,+ false,+ ),+ }+}++describe('traffic summary window (no explicit timeBucket)', () => {+ beforeEach(() => {+ jest.useFakeTimers()+ jest.setSystemTime(new Date('2026-08-23T14:37:42Z'))+ })++ afterEach(() => {+ jest.useRealTimers()+ })++ it('period=1h starts one hour ago on a minute boundary, not at midnight', () => {+ const { bucket, groupFrom, groupFromUTC, groupTo } = getSummaryWindow(+ '1h',+ 'Etc/GMT',+ )++ expect(bucket).toBe(TimeBucketType.MINUTE)+ expect(groupFrom).toBe('2026-08-23 13:37:00')+ expect(groupFromUTC).toBe('2026-08-23 13:37:00')+ expect(groupTo).toBe('2026-08-23 14:37:42')+ })++ it('period=today still starts at midnight', () => {+ const { bucket, groupFrom, groupFromUTC } = getSummaryWindow(+ 'today',+ 'Etc/GMT',+ )++ expect(bucket).toBe(TimeBucketType.HOUR)+ expect(groupFrom).toBe('2026-08-23 00:00:00')+ expect(groupFromUTC).toBe('2026-08-23 00:00:00')+ })++ it('an explicitly requested timeBucket takes precedence', () => {+ expect(getSummaryTimeBucket('day', '1h', undefined, undefined)).toBe(+ TimeBucketType.DAY,+ )+ })+})