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!

Refactor getTimeBucketForAllTime; allow 'all' period for funnels

Blue Mouse30b3754df150
diff --git a/backend/apps/cloud/src/analytics/analytics.controller.ts b/backend/apps/cloud/src/analytics/analytics.controller.ts
index d81af4530..a3e04af71 100644
--- a/backend/apps/cloud/src/analytics/analytics.controller.ts
+++ b/backend/apps/cloud/src/analytics/analytics.controller.ts
@@ -237,10 +237,9 @@ export class AnalyticsController {
let diff
if (period === 'all') {
- const res = await this.analyticsService.getTimeBucketForAllTime(
+ const res = await this.analyticsService.calculateTimeBucketForAllTime(
pid,
- period,
- timezone,
+ 'analytics',
)
diff = res.diff
@@ -391,6 +390,17 @@ export class AnalyticsController {
pid,
)
+ let diff
+
+ if (period === 'all') {
+ const res = await this.analyticsService.calculateTimeBucketForAllTime(
+ pid,
+ 'analytics',
+ )
+
+ diff = res.diff
+ }
+
const safeTimezone = this.analyticsService.getSafeTimezone(timezone)
const { groupFrom, groupTo } = this.analyticsService.getGroupFromTo(
from,
@@ -398,6 +408,7 @@ export class AnalyticsController {
null,
period,
safeTimezone,
+ diff,
)
const params = { pid, groupFrom, groupTo }
@@ -589,10 +600,9 @@ export class AnalyticsController {
let diff
if (period === 'all') {
- const res = await this.analyticsService.getTimeBucketForAllTime(
+ const res = await this.analyticsService.calculateTimeBucketForAllTime(
pid,
- period,
- timezone,
+ 'performance',
)
diff = res.diff
@@ -721,10 +731,9 @@ export class AnalyticsController {
let diff
if (period === 'all') {
- const res = await this.analyticsService.getTimeBucketForAllTime(
+ const res = await this.analyticsService.calculateTimeBucketForAllTime(
pid,
- period,
- timezone,
+ 'analytics',
)
diff = res.diff
@@ -1490,10 +1499,9 @@ export class AnalyticsController {
let diff
if (period === 'all') {
- const res = await this.analyticsService.getTimeBucketForAllTime(
+ const res = await this.analyticsService.calculateTimeBucketForAllTime(
pid,
- period,
- timezone,
+ 'analytics',
)
timeBucket = res.timeBucket[0]
@@ -1574,10 +1582,9 @@ export class AnalyticsController {
let diff
if (period === 'all') {
- const res = await this.analyticsService.getTimeBucketForAllTime(
+ const res = await this.analyticsService.calculateTimeBucketForAllTime(
pid,
- period,
- timezone,
+ 'errors',
)
timeBucket = res.timeBucket[0]
@@ -1649,10 +1656,9 @@ export class AnalyticsController {
let diff
if (period === 'all') {
- const res = await this.analyticsService.getTimeBucketForAllTime(
+ const res = await this.analyticsService.calculateTimeBucketForAllTime(
pid,
- period,
- timezone,
+ 'errors',
)
timeBucket = res.timeBucket[0]
@@ -1747,10 +1753,9 @@ export class AnalyticsController {
let timeBucketForAllTime
if (period === VALID_PERIODS[VALID_PERIODS.length - 1]) {
- const res = await this.analyticsService.getTimeBucketForAllTime(
+ const res = await this.analyticsService.calculateTimeBucketForAllTime(
pid,
- period,
- timezone,
+ 'customEV',
)
newTimeBucket = _includes(res.timeBucket, timeBucket)
diff --git a/backend/apps/cloud/src/analytics/analytics.service.ts b/backend/apps/cloud/src/analytics/analytics.service.ts
index e43ab7307..3f27d743a 100644
--- a/backend/apps/cloud/src/analytics/analytics.service.ts
+++ b/backend/apps/cloud/src/analytics/analytics.service.ts
@@ -811,49 +811,47 @@ export class AnalyticsService {
}
}
- async getTimeBucketForAllTime(
+ async calculateTimeBucketForAllTime(
pid: string,
- period: string,
- safeTimezone: string,
+ table: 'analytics' | 'customEV' | 'performance' | 'errors',
): Promise<{
timeBucket: TimeBucketType[]
diff: number
}> {
- if (period !== 'all') {
- return null
- }
-
- const { data: from } = await clickhouse
+ const { data: fromData } = await clickhouse
.query({
- query:
- 'SELECT created FROM analytics WHERE pid = {pid:FixedString(12)} ORDER BY created ASC LIMIT 1',
+ query: `SELECT min(created) AS firstCreated FROM ${table} WHERE pid = {pid:FixedString(12)}`,
query_params: { pid },
})
- .then(res => res.json<{ created?: string }>())
+ .then(res => res.json<{ firstCreated?: string }>())
- const to = _includes(GMT_0_TIMEZONES, safeTimezone)
- ? dayjs.utc()
- : dayjs().tz(safeTimezone)
+ const firstCreated = fromData?.[0]?.firstCreated
- let newTimeBucket = [TimeBucketType.MONTH]
- let diff = null
+ if (!firstCreated) {
+ return {
+ timeBucket: [TimeBucketType.DAY],
+ diff: 0,
+ }
+ }
- if (from && to) {
- diff = dayjs(to).diff(dayjs(from?.[0]?.created || to), 'days')
+ const nowUtc = dayjs.utc()
+ const firstCreatedUtc = dayjs.utc(firstCreated)
+ const diff = nowUtc.diff(firstCreatedUtc, 'days')
- const tbMap = _find(timeBucketToDays, ({ lt }) => diff <= lt)
+ const tbMap = _find(timeBucketToDays, ({ lt }) => diff <= lt)
- if (_isEmpty(tbMap)) {
- throw new PreconditionFailedException(
- "The difference between 'from' and 'to' is greater than allowed",
- )
+ if (_isEmpty(tbMap)) {
+ console.error(
+ `[ERROR] calculateTimeBucketForAllTime: Difference ${diff} exceeds allowed limits for pid ${pid}`,
+ )
+ return {
+ timeBucket: [TimeBucketType.YEAR],
+ diff,
}
-
- newTimeBucket = tbMap?.tb
}
return {
- timeBucket: newTimeBucket,
+ timeBucket: tbMap.tb,
diff,
}
}
@@ -3197,7 +3195,7 @@ export class AnalyticsService {
)
if (period === 'all') {
- const res = await this.getTimeBucketForAllTime(pid, period, timezone)
+ const res = await this.calculateTimeBucketForAllTime(pid, 'customEV')
diff = res.diff
@@ -3298,7 +3296,7 @@ export class AnalyticsService {
}
if (period === 'all') {
- const res = await this.getTimeBucketForAllTime(pid, period, timezone)
+ const res = await this.calculateTimeBucketForAllTime(pid, 'analytics')
diff = res.diff
diff --git a/backend/apps/community/src/analytics/analytics.controller.ts b/backend/apps/community/src/analytics/analytics.controller.ts
index 28b32567e..5e06fb618 100644
--- a/backend/apps/community/src/analytics/analytics.controller.ts
+++ b/backend/apps/community/src/analytics/analytics.controller.ts
@@ -216,10 +216,9 @@ export class AnalyticsController {
let diff
if (period === 'all') {
- const res = await this.analyticsService.getTimeBucketForAllTime(
+ const res = await this.analyticsService.calculateTimeBucketForAllTime(
pid,
- period,
- timezone,
+ 'analytics',
)
diff = res.diff
@@ -340,6 +339,17 @@ export class AnalyticsController {
headers['x-password'],
)
+ let diff
+
+ if (period === 'all') {
+ const res = await this.analyticsService.calculateTimeBucketForAllTime(
+ pid,
+ 'analytics',
+ )
+
+ diff = res.diff
+ }
+
const safeTimezone = this.analyticsService.getSafeTimezone(timezone)
const { groupFrom, groupTo } = this.analyticsService.getGroupFromTo(
from,
@@ -347,6 +357,7 @@ export class AnalyticsController {
null,
period,
safeTimezone,
+ diff,
)
const params = { pid, groupFrom, groupTo }
@@ -508,10 +519,9 @@ export class AnalyticsController {
let diff
if (period === 'all') {
- const res = await this.analyticsService.getTimeBucketForAllTime(
+ const res = await this.analyticsService.calculateTimeBucketForAllTime(
pid,
- period,
- timezone,
+ 'performance',
)
diff = res.diff
@@ -627,10 +637,9 @@ export class AnalyticsController {
let diff
if (period === 'all') {
- const res = await this.analyticsService.getTimeBucketForAllTime(
+ const res = await this.analyticsService.calculateTimeBucketForAllTime(
pid,
- period,
- timezone,
+ 'analytics',
)
diff = res.diff
@@ -1200,10 +1209,9 @@ export class AnalyticsController {
let diff
if (period === 'all') {
- const res = await this.analyticsService.getTimeBucketForAllTime(
+ const res = await this.analyticsService.calculateTimeBucketForAllTime(
pid,
- period,
- timezone,
+ 'analytics',
)
timeBucket = res.timeBucket[0]
@@ -1305,10 +1313,9 @@ export class AnalyticsController {
let timeBucketForAllTime
if (period === VALID_PERIODS[VALID_PERIODS.length - 1]) {
- const res = await this.analyticsService.getTimeBucketForAllTime(
+ const res = await this.analyticsService.calculateTimeBucketForAllTime(
pid,
- period,
- timezone,
+ 'customEV',
)
newTimeBucket = _includes(res.timeBucket, timeBucket)
@@ -1510,10 +1517,9 @@ export class AnalyticsController {
let diff
if (period === 'all') {
- const res = await this.analyticsService.getTimeBucketForAllTime(
+ const res = await this.analyticsService.calculateTimeBucketForAllTime(
pid,
- period,
- timezone,
+ 'errors',
)
timeBucket = res.timeBucket[0]
@@ -1583,10 +1589,9 @@ export class AnalyticsController {
let diff
if (period === 'all') {
- const res = await this.analyticsService.getTimeBucketForAllTime(
+ const res = await this.analyticsService.calculateTimeBucketForAllTime(
pid,
- period,
- timezone,
+ 'errors',
)
timeBucket = res.timeBucket[0]
diff --git a/backend/apps/community/src/analytics/analytics.service.ts b/backend/apps/community/src/analytics/analytics.service.ts
index 4a19545c7..99d56ebd8 100644
--- a/backend/apps/community/src/analytics/analytics.service.ts
+++ b/backend/apps/community/src/analytics/analytics.service.ts
@@ -792,49 +792,47 @@ export class AnalyticsService {
}
}
- async getTimeBucketForAllTime(
+ async calculateTimeBucketForAllTime(
pid: string,
- period: string,
- safeTimezone: string,
+ table: 'analytics' | 'customEV' | 'performance' | 'errors',
): Promise<{
timeBucket: TimeBucketType[]
diff: number
}> {
- if (period !== 'all') {
- return null
- }
-
- const { data: from } = await clickhouse
+ const { data: fromData } = await clickhouse
.query({
- query:
- 'SELECT created FROM analytics WHERE pid = {pid:FixedString(12)} ORDER BY created ASC LIMIT 1',
+ query: `SELECT min(created) AS firstCreated FROM ${table} WHERE pid = {pid:FixedString(12)}`,
query_params: { pid },
})
- .then(res => res.json<{ created?: string }>())
+ .then(res => res.json<{ firstCreated?: string }>())
- const to = _includes(GMT_0_TIMEZONES, safeTimezone)
- ? dayjs.utc()
- : dayjs().tz(safeTimezone)
+ const firstCreated = fromData?.[0]?.firstCreated
- let newTimeBucket = [TimeBucketType.MONTH]
- let diff = null
+ if (!firstCreated) {
+ return {
+ timeBucket: [TimeBucketType.DAY],
+ diff: 0,
+ }
+ }
- if (from && to) {
- diff = dayjs(to).diff(dayjs(from?.[0]?.created || to), 'days')
+ const nowUtc = dayjs.utc()
+ const firstCreatedUtc = dayjs.utc(firstCreated)
+ const diff = nowUtc.diff(firstCreatedUtc, 'days')
- const tbMap = _find(timeBucketToDays, ({ lt }) => diff <= lt)
+ const tbMap = _find(timeBucketToDays, ({ lt }) => diff <= lt)
- if (_isEmpty(tbMap)) {
- throw new PreconditionFailedException(
- "The difference between 'from' and 'to' is greater than allowed",
- )
+ if (_isEmpty(tbMap)) {
+ console.error(
+ `[ERROR] calculateTimeBucketForAllTime: Difference ${diff} exceeds allowed limits for pid ${pid}`,
+ )
+ return {
+ timeBucket: [TimeBucketType.YEAR],
+ diff,
}
-
- newTimeBucket = tbMap?.tb
}
return {
- timeBucket: newTimeBucket,
+ timeBucket: tbMap.tb,
diff,
}
}
@@ -2884,7 +2882,7 @@ export class AnalyticsService {
)
if (period === 'all') {
- const res = await this.getTimeBucketForAllTime(pid, period, timezone)
+ const res = await this.calculateTimeBucketForAllTime(pid, 'customEV')
diff = res.diff
@@ -2985,7 +2983,7 @@ export class AnalyticsService {
}
if (period === 'all') {
- const res = await this.getTimeBucketForAllTime(pid, period, timezone)
+ const res = await this.calculateTimeBucketForAllTime(pid, 'analytics')
diff = res.diff