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!

Fixed 'all' period (#176)

Maksim Mrug4ef29c6e911f
diff --git a/apps/production/src/analytics/analytics.controller.ts b/apps/production/src/analytics/analytics.controller.ts
index 6b2f1a8d8..0a1ecd236 100644
--- a/apps/production/src/analytics/analytics.controller.ts
+++ b/apps/production/src/analytics/analytics.controller.ts
@@ -300,6 +300,7 @@ export class AnalyticsController {
const res = await this.analyticsService.getTimeBucketForAllTime(
pid,
period,
+ timezone,
)
diff = res.diff
@@ -521,6 +522,7 @@ export class AnalyticsController {
const res = await this.analyticsService.getTimeBucketForAllTime(
pid,
period,
+ timezone,
)
diff = res.diff
@@ -671,6 +673,7 @@ export class AnalyticsController {
const res = await this.analyticsService.getTimeBucketForAllTime(
pid,
period,
+ timezone,
)
diff = res.diff
@@ -1187,6 +1190,7 @@ export class AnalyticsController {
const res = await this.analyticsService.getTimeBucketForAllTime(
pid,
period,
+ timezone,
)
// eslint-disable-next-line prefer-destructuring
diff --git a/apps/production/src/analytics/analytics.service.ts b/apps/production/src/analytics/analytics.service.ts
index 400aee71e..806cd7ab7 100644
--- a/apps/production/src/analytics/analytics.service.ts
+++ b/apps/production/src/analytics/analytics.service.ts
@@ -489,6 +489,9 @@ export class AnalyticsService {
} else if (period === 'all' && (diff === 0 || diff === 1)) {
groupFrom = djsNow.subtract(1, 'day').startOf('d')
groupTo = djsNow
+ } else if (period === 'all') {
+ groupFrom = djsNow.subtract(diff - 1, 'day').startOf(timeBucket)
+ groupTo = djsNow
} else {
if (period === '1d') {
groupFrom = djsNow.subtract(parseInt(period, 10), _last(period))
@@ -646,6 +649,7 @@ export class AnalyticsService {
async getTimeBucketForAllTime(
pid: string,
period: string,
+ safeTimezone: string,
): Promise<{
timeBucket: TimeBucketType[]
diff: number
@@ -660,18 +664,15 @@ export class AnalyticsService {
{ params: { pid } },
)
.toPromise()
- const to: any = await clickhouse
- .query(
- 'SELECT created as to FROM analytics where pid = {pid:FixedString(12)} ORDER BY created DESC LIMIT 1',
- { params: { pid } },
- )
- .toPromise()
+ const to = _includes(GMT_0_TIMEZONES, safeTimezone)
+ ? dayjs.utc()
+ : dayjs().tz(safeTimezone)
let newTimeBucket = [TimeBucketType.MONTH]
let diff = null
if (from && to) {
- diff = dayjs(to[0].to).diff(dayjs(from[0].from), 'days')
+ diff = dayjs(to).diff(dayjs(from[0].from), 'days')
const tbMap = _find(timeBucketToDays, ({ lt }) => diff <= lt)