From fe0cff088f524cf2f895f4b5df04ffb8f7b426c4 Mon Sep 17 00:00:00 2001 From: Maksim Mrug <80621719+kruzhambus@users.noreply.github.com> Date: Tue, 18 Jul 2023 19:01:38 +0300 Subject: [PATCH 01/15] Add 'All time' and 'This hour' timebuckets --- .../src/analytics/analytics.controller.ts | 18 ++++- .../src/analytics/analytics.service.ts | 65 ++++++++++++++++++- 2 files changed, 81 insertions(+), 2 deletions(-) diff --git a/apps/production/src/analytics/analytics.controller.ts b/apps/production/src/analytics/analytics.controller.ts index ba5048eb6..3599b92f9 100644 --- a/apps/production/src/analytics/analytics.controller.ts +++ b/apps/production/src/analytics/analytics.controller.ts @@ -35,6 +35,7 @@ import { getSessionKey, getHeartbeatKey, DataType, + validPeriods, } from './analytics.service' import { TaskManagerService } from '../task-manager/task-manager.service' import { CurrentUserId } from '../auth/decorators/current-user-id.decorator' @@ -289,7 +290,21 @@ export class AnalyticsController { this.analyticsService.validatePeriod(period) } - this.analyticsService.validateTimebucket(timeBucket) + let newTimebucket = timeBucket + let diff + + if (period === validPeriods[validPeriods.length - 1]) { + const res = await this.analyticsService.getTimeBucketForAllTime( + pid, + period, + timeBucket, + ) + + diff = res.diff + newTimebucket = res.timeBucket + } + + this.analyticsService.validateTimebucket(newTimebucket) const [filtersQuery, filtersParams, appliedFilters, customEVFilterApplied] = this.analyticsService.getFiltersQuery( filters, @@ -304,6 +319,7 @@ export class AnalyticsController { timeBucket, period, safeTimezone, + diff, ) await this.analyticsService.checkProjectAccess( pid, diff --git a/apps/production/src/analytics/analytics.service.ts b/apps/production/src/analytics/analytics.service.ts index 155072f6d..b46b5496d 100644 --- a/apps/production/src/analytics/analytics.service.ts +++ b/apps/production/src/analytics/analytics.service.ts @@ -92,7 +92,8 @@ const GMT_0_TIMEZONES = [ // 'Africa/Casablanca', ] -const validPeriods = [ +export const validPeriods = [ + 'thishour', 'today', 'yesterday', '1d', @@ -101,6 +102,7 @@ const validPeriods = [ '3M', '12M', '24M', + 'all', ] const validTimebuckets = [ @@ -122,6 +124,7 @@ const timeBucketToDays = [ }, // 4 weeks { lt: 366, tb: [TimeBucketType.MONTH] }, // 12 months { lt: 732, tb: [TimeBucketType.MONTH] }, // 24 months + { lt: 999999, tb: [TimeBucketType.MONTH] }, // more than 24 months ] // Smaller than 64 characters, must start with an English letter and contain only letters (a-z A-Z), numbers (0-9), underscores (_) and dots (.) @@ -407,6 +410,7 @@ export class AnalyticsService { timeBucket: TimeBucketType | null, period: string, safeTimezone: string, + diff?: number, ): IGetGroupFromTo { let groupFrom: dayjs.Dayjs let groupTo: dayjs.Dayjs @@ -473,9 +477,15 @@ export class AnalyticsService { if (period === 'today') { groupFrom = djsNow.startOf('d') groupTo = djsNow + } else if (period === 'thishour') { + groupFrom = djsNow.startOf('h') + groupTo = djsNow } else if (period === 'yesterday') { groupFrom = djsNow.subtract(1, 'day').startOf('d') groupTo = djsNow.subtract(1, 'day').endOf('d') + } else if (period === 'all' && diff === 0) { + groupFrom = djsNow.subtract(1, 'day').startOf('d') + groupTo = djsNow } else { if (period === '1d') { groupFrom = djsNow.subtract(parseInt(period, 10), _last(period)) @@ -487,6 +497,11 @@ export class AnalyticsService { groupTo = djsNow } + console.log( + 'groupFrom', + groupFrom.format(formatFrom), + groupTo.format(formatTo), + ) if (!_isEmpty(timeBucket)) { checkIfTBAllowed( timeBucket, @@ -630,6 +645,54 @@ export class AnalyticsService { } } + async getTimeBucketForAllTime( + pid: string, + period: string, + timeBucket: TimeBucketType, + ): Promise<{ + timeBucket: TimeBucketType + diff: number + }> { + if (period !== validPeriods[validPeriods.length - 1]) { + return null + } + + const from: any = await clickhouse + .query( + 'SELECT created as from FROM analytics where pid = {pid:FixedString(12)} ORDER BY created ASC LIMIT 1', + { 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() + let newTimeBucket = TimeBucketType.MONTH + let diff = null + + if (from && to) { + diff = dayjs(to[0].to).diff(dayjs(from[0].from), 'days') + + const tbMap = _find(timeBucketToDays, ({ lt }) => diff <= lt) + + if (_isEmpty(tbMap)) { + throw new PreconditionFailedException( + "The difference between 'from' and 'to' is greater than allowed", + ) + } + + // eslint-disable-next-line prefer-destructuring + newTimeBucket = _includes(tbMap.tb, timeBucket) ? timeBucket : tbMap.tb[0] + } + + return { + timeBucket: newTimeBucket, + diff, + } + } + postProcessParsedFilters(parsedFilters: any[]): any[] { return _reduce( parsedFilters, From b6ed5ba9b9ec8d5e5c9d446aa90da8c8c6d81b4a Mon Sep 17 00:00:00 2001 From: Maksim Mrug <80621719+kruzhambus@users.noreply.github.com> Date: Tue, 18 Jul 2023 19:37:29 +0300 Subject: [PATCH 02/15] some update --- apps/production/src/analytics/analytics.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/production/src/analytics/analytics.service.ts b/apps/production/src/analytics/analytics.service.ts index b46b5496d..9d8b17ad4 100644 --- a/apps/production/src/analytics/analytics.service.ts +++ b/apps/production/src/analytics/analytics.service.ts @@ -478,7 +478,7 @@ export class AnalyticsService { groupFrom = djsNow.startOf('d') groupTo = djsNow } else if (period === 'thishour') { - groupFrom = djsNow.startOf('h') + groupFrom = djsNow.subtract(1, 'hour').startOf('h') groupTo = djsNow } else if (period === 'yesterday') { groupFrom = djsNow.subtract(1, 'day').startOf('d') From c2ed3d418bbc73f93abac003497f3ecc2e8ec2db Mon Sep 17 00:00:00 2001 From: Maksim Mrug <80621719+kruzhambus@users.noreply.github.com> Date: Tue, 18 Jul 2023 19:40:02 +0300 Subject: [PATCH 03/15] some fix --- apps/production/src/analytics/analytics.service.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/apps/production/src/analytics/analytics.service.ts b/apps/production/src/analytics/analytics.service.ts index 9d8b17ad4..03ca02819 100644 --- a/apps/production/src/analytics/analytics.service.ts +++ b/apps/production/src/analytics/analytics.service.ts @@ -483,7 +483,7 @@ export class AnalyticsService { } else if (period === 'yesterday') { groupFrom = djsNow.subtract(1, 'day').startOf('d') groupTo = djsNow.subtract(1, 'day').endOf('d') - } else if (period === 'all' && diff === 0) { + } else if (period === 'all' && (diff === 0 || diff === 1)) { groupFrom = djsNow.subtract(1, 'day').startOf('d') groupTo = djsNow } else { @@ -501,6 +501,7 @@ export class AnalyticsService { 'groupFrom', groupFrom.format(formatFrom), groupTo.format(formatTo), + diff, ) if (!_isEmpty(timeBucket)) { checkIfTBAllowed( From 5e780b2c158145660252f232888c051b5f59717c Mon Sep 17 00:00:00 2001 From: Maksim Mrug <80621719+kruzhambus@users.noreply.github.com> Date: Tue, 18 Jul 2023 19:40:38 +0300 Subject: [PATCH 04/15] remove clg --- apps/production/src/analytics/analytics.service.ts | 6 ------ 1 file changed, 6 deletions(-) diff --git a/apps/production/src/analytics/analytics.service.ts b/apps/production/src/analytics/analytics.service.ts index 03ca02819..069fccf65 100644 --- a/apps/production/src/analytics/analytics.service.ts +++ b/apps/production/src/analytics/analytics.service.ts @@ -497,12 +497,6 @@ export class AnalyticsService { groupTo = djsNow } - console.log( - 'groupFrom', - groupFrom.format(formatFrom), - groupTo.format(formatTo), - diff, - ) if (!_isEmpty(timeBucket)) { checkIfTBAllowed( timeBucket, From f590d932af5a8b71b4f149fa9b5a7ccdd3ae3111 Mon Sep 17 00:00:00 2001 From: Maksim Mrug <80621719+kruzhambus@users.noreply.github.com> Date: Tue, 18 Jul 2023 19:43:13 +0300 Subject: [PATCH 05/15] add timebucket year --- apps/production/src/analytics/analytics.service.ts | 4 +++- apps/production/src/analytics/dto/getData.dto.ts | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/apps/production/src/analytics/analytics.service.ts b/apps/production/src/analytics/analytics.service.ts index 069fccf65..4b02e30af 100644 --- a/apps/production/src/analytics/analytics.service.ts +++ b/apps/production/src/analytics/analytics.service.ts @@ -109,6 +109,7 @@ const validTimebuckets = [ TimeBucketType.HOUR, TimeBucketType.DAY, TimeBucketType.MONTH, + TimeBucketType.YEAR, ] // mapping of allowed timebuckets per difference between days @@ -124,7 +125,8 @@ const timeBucketToDays = [ }, // 4 weeks { lt: 366, tb: [TimeBucketType.MONTH] }, // 12 months { lt: 732, tb: [TimeBucketType.MONTH] }, // 24 months - { lt: 999999, tb: [TimeBucketType.MONTH] }, // more than 24 months + { lt: 1464, tb: [TimeBucketType.MONTH, TimeBucketType.YEAR] }, // 48 months + { lt: 99999, tb: [TimeBucketType.YEAR] }, ] // Smaller than 64 characters, must start with an English letter and contain only letters (a-z A-Z), numbers (0-9), underscores (_) and dots (.) diff --git a/apps/production/src/analytics/dto/getData.dto.ts b/apps/production/src/analytics/dto/getData.dto.ts index 53a39d5bc..26e3deabc 100644 --- a/apps/production/src/analytics/dto/getData.dto.ts +++ b/apps/production/src/analytics/dto/getData.dto.ts @@ -6,6 +6,7 @@ export enum TimeBucketType { HOUR = 'hour', DAY = 'day', MONTH = 'month', + YEAR = 'year', } // eslint-disable-next-line @typescript-eslint/naming-convention From 87a6238322fb32482e34161249514b04fbed40da Mon Sep 17 00:00:00 2001 From: Maksim Mrug <80621719+kruzhambus@users.noreply.github.com> Date: Tue, 18 Jul 2023 19:54:10 +0300 Subject: [PATCH 06/15] some fix --- apps/production/src/analytics/analytics.controller.ts | 6 +++--- apps/production/src/analytics/analytics.service.ts | 7 ++++++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/apps/production/src/analytics/analytics.controller.ts b/apps/production/src/analytics/analytics.controller.ts index 3599b92f9..cfcca4e55 100644 --- a/apps/production/src/analytics/analytics.controller.ts +++ b/apps/production/src/analytics/analytics.controller.ts @@ -316,7 +316,7 @@ export class AnalyticsController { this.analyticsService.getGroupFromTo( from, to, - timeBucket, + newTimebucket, period, safeTimezone, diff, @@ -351,7 +351,7 @@ export class AnalyticsController { if (isCaptcha) { result = await this.analyticsService.groupCaptchaByTimeBucket( - timeBucket, + newTimebucket, groupFrom, groupTo, subQuery, @@ -361,7 +361,7 @@ export class AnalyticsController { ) } else { result = await this.analyticsService.groupByTimeBucket( - timeBucket, + newTimebucket, groupFrom, groupTo, subQuery, diff --git a/apps/production/src/analytics/analytics.service.ts b/apps/production/src/analytics/analytics.service.ts index 4b02e30af..534f78230 100644 --- a/apps/production/src/analytics/analytics.service.ts +++ b/apps/production/src/analytics/analytics.service.ts @@ -117,7 +117,7 @@ const validTimebuckets = [ const timeBucketToDays = [ { lt: 7, - tb: [TimeBucketType.HOUR, TimeBucketType.DAY, TimeBucketType.MONTH], + tb: [TimeBucketType.HOUR, TimeBucketType.DAY], }, // 7 days { lt: 28, @@ -499,6 +499,11 @@ export class AnalyticsService { groupTo = djsNow } + console.log( + timeBucket, + groupFrom.format(formatFrom), + groupTo.format(formatTo)) + if (!_isEmpty(timeBucket)) { checkIfTBAllowed( timeBucket, From d79f88b80380cfc6cc7afb53c7f60c6fbe3ccaea Mon Sep 17 00:00:00 2001 From: Maksim Mrug <80621719+kruzhambus@users.noreply.github.com> Date: Tue, 18 Jul 2023 19:54:44 +0300 Subject: [PATCH 07/15] remove clg --- apps/production/src/analytics/analytics.service.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/apps/production/src/analytics/analytics.service.ts b/apps/production/src/analytics/analytics.service.ts index 534f78230..970332006 100644 --- a/apps/production/src/analytics/analytics.service.ts +++ b/apps/production/src/analytics/analytics.service.ts @@ -499,11 +499,6 @@ export class AnalyticsService { groupTo = djsNow } - console.log( - timeBucket, - groupFrom.format(formatFrom), - groupTo.format(formatTo)) - if (!_isEmpty(timeBucket)) { checkIfTBAllowed( timeBucket, From c1a14997138c43913f16a0aef52a1ee996517d20 Mon Sep 17 00:00:00 2001 From: Maksim Mrug <80621719+kruzhambus@users.noreply.github.com> Date: Tue, 18 Jul 2023 20:00:22 +0300 Subject: [PATCH 08/15] add return timebucket --- apps/production/src/analytics/analytics.controller.ts | 8 +++++++- apps/production/src/analytics/analytics.service.ts | 7 +++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/apps/production/src/analytics/analytics.controller.ts b/apps/production/src/analytics/analytics.controller.ts index cfcca4e55..ce2764360 100644 --- a/apps/production/src/analytics/analytics.controller.ts +++ b/apps/production/src/analytics/analytics.controller.ts @@ -291,6 +291,8 @@ export class AnalyticsController { } let newTimebucket = timeBucket + let allowedTumebucketForPeriodAll + let diff if (period === validPeriods[validPeriods.length - 1]) { @@ -301,7 +303,9 @@ export class AnalyticsController { ) diff = res.diff - newTimebucket = res.timeBucket + // eslint-disable-next-line prefer-destructuring + newTimebucket = res.timeBucket[0] + allowedTumebucketForPeriodAll = res.timeBucket } this.analyticsService.validateTimebucket(newTimebucket) @@ -377,6 +381,7 @@ export class AnalyticsController { return { ...result, appliedFilters, + timeBucket: allowedTumebucketForPeriodAll, } } @@ -389,6 +394,7 @@ export class AnalyticsController { ...result, customs, appliedFilters, + timeBucket: allowedTumebucketForPeriodAll, } } diff --git a/apps/production/src/analytics/analytics.service.ts b/apps/production/src/analytics/analytics.service.ts index 970332006..e23af2467 100644 --- a/apps/production/src/analytics/analytics.service.ts +++ b/apps/production/src/analytics/analytics.service.ts @@ -647,7 +647,7 @@ export class AnalyticsService { period: string, timeBucket: TimeBucketType, ): Promise<{ - timeBucket: TimeBucketType + timeBucket: TimeBucketType[] diff: number }> { if (period !== validPeriods[validPeriods.length - 1]) { @@ -666,7 +666,7 @@ export class AnalyticsService { { params: { pid } }, ) .toPromise() - let newTimeBucket = TimeBucketType.MONTH + let newTimeBucket = [TimeBucketType.MONTH] let diff = null if (from && to) { @@ -680,8 +680,7 @@ export class AnalyticsService { ) } - // eslint-disable-next-line prefer-destructuring - newTimeBucket = _includes(tbMap.tb, timeBucket) ? timeBucket : tbMap.tb[0] + newTimeBucket = _includes(tbMap.tb, timeBucket) ? [timeBucket] : tbMap.tb } return { From 23529a612a1bfe7df1b21b5d55a651f35b110117 Mon Sep 17 00:00:00 2001 From: Maksim Mrug <80621719+kruzhambus@users.noreply.github.com> Date: Tue, 18 Jul 2023 22:11:52 +0300 Subject: [PATCH 09/15] some restore --- apps/production/src/analytics/analytics.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/production/src/analytics/analytics.service.ts b/apps/production/src/analytics/analytics.service.ts index e23af2467..7297476f5 100644 --- a/apps/production/src/analytics/analytics.service.ts +++ b/apps/production/src/analytics/analytics.service.ts @@ -117,7 +117,7 @@ const validTimebuckets = [ const timeBucketToDays = [ { lt: 7, - tb: [TimeBucketType.HOUR, TimeBucketType.DAY], + tb: [TimeBucketType.HOUR, TimeBucketType.DAY, TimeBucketType.MONTH], }, // 7 days { lt: 28, From 61441860ea6b3b356e3c126b79c302d072e7b69f Mon Sep 17 00:00:00 2001 From: Maksim Mrug <80621719+kruzhambus@users.noreply.github.com> Date: Tue, 18 Jul 2023 22:19:14 +0300 Subject: [PATCH 10/15] add new tb for custom events --- .../src/analytics/analytics.controller.ts | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/apps/production/src/analytics/analytics.controller.ts b/apps/production/src/analytics/analytics.controller.ts index ce2764360..380348c08 100644 --- a/apps/production/src/analytics/analytics.controller.ts +++ b/apps/production/src/analytics/analytics.controller.ts @@ -1145,7 +1145,24 @@ export class AnalyticsController { this.analyticsService.validatePeriod(period) } - this.analyticsService.validateTimebucket(timeBucket) + let newTimeBucket = timeBucket + let diff + let timeBucketForAllTime + + if (period === validPeriods[validPeriods.length - 1]) { + const res = await this.analyticsService.getTimeBucketForAllTime( + pid, + period, + timeBucket, + ) + + // eslint-disable-next-line prefer-destructuring + newTimeBucket = res.timeBucket[0] + diff = res.diff + timeBucketForAllTime = res.timeBucket + } + + this.analyticsService.validateTimebucket(newTimeBucket) const [filtersQuery, filtersParams, appliedFilters] = this.analyticsService.getFiltersQuery(filters, DataType.ANALYTICS) await this.analyticsService.checkProjectAccess( @@ -1158,9 +1175,10 @@ export class AnalyticsController { const { groupFrom, groupTo } = this.analyticsService.getGroupFromTo( from, to, - timeBucket, + newTimeBucket, period, safeTimezone, + diff, ) const paramsData = { @@ -1173,7 +1191,7 @@ export class AnalyticsController { } const result: any = await this.analyticsService.groupCustomEVByTimeBucket( - timeBucket, + newTimeBucket, groupFrom, groupTo, filtersQuery, @@ -1203,6 +1221,7 @@ export class AnalyticsController { return { ...result, appliedFilters, + timeBucket: timeBucketForAllTime, } } } From 279573d9dc30296a6756648e81cf4793ce9c70e4 Mon Sep 17 00:00:00 2001 From: Maksim Mrug <80621719+kruzhambus@users.noreply.github.com> Date: Wed, 19 Jul 2023 12:55:33 +0300 Subject: [PATCH 11/15] some improvments --- apps/production/src/analytics/analytics.controller.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/production/src/analytics/analytics.controller.ts b/apps/production/src/analytics/analytics.controller.ts index 380348c08..4879acbbc 100644 --- a/apps/production/src/analytics/analytics.controller.ts +++ b/apps/production/src/analytics/analytics.controller.ts @@ -295,7 +295,7 @@ export class AnalyticsController { let diff - if (period === validPeriods[validPeriods.length - 1]) { + if (period === 'all') { const res = await this.analyticsService.getTimeBucketForAllTime( pid, period, From c4fdb3a6d7a21ce5f4f5cf923616eef815b4ec9d Mon Sep 17 00:00:00 2001 From: Maksim Mrug <80621719+kruzhambus@users.noreply.github.com> Date: Wed, 19 Jul 2023 13:40:57 +0300 Subject: [PATCH 12/15] some improvments --- apps/production/src/analytics/analytics.controller.ts | 11 +++++++---- apps/production/src/analytics/analytics.service.ts | 6 +++--- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/apps/production/src/analytics/analytics.controller.ts b/apps/production/src/analytics/analytics.controller.ts index 4879acbbc..d4e4ae5f5 100644 --- a/apps/production/src/analytics/analytics.controller.ts +++ b/apps/production/src/analytics/analytics.controller.ts @@ -2,6 +2,7 @@ import * as _isEmpty from 'lodash/isEmpty' import * as _isArray from 'lodash/isArray' import * as _toNumber from 'lodash/toNumber' import * as _pick from 'lodash/pick' +import * as _includes from 'lodash/includes' import * as _map from 'lodash/map' import * as _uniqBy from 'lodash/uniqBy' import * as _round from 'lodash/round' @@ -299,12 +300,13 @@ export class AnalyticsController { const res = await this.analyticsService.getTimeBucketForAllTime( pid, period, - timeBucket, ) diff = res.diff // eslint-disable-next-line prefer-destructuring - newTimebucket = res.timeBucket[0] + newTimebucket = _includes(res.timeBucket, timeBucket) + ? timeBucket + : res.timeBucket[0] allowedTumebucketForPeriodAll = res.timeBucket } @@ -1153,11 +1155,12 @@ export class AnalyticsController { const res = await this.analyticsService.getTimeBucketForAllTime( pid, period, - timeBucket, ) // eslint-disable-next-line prefer-destructuring - newTimeBucket = res.timeBucket[0] + newTimeBucket = _includes(res.timeBucket, timeBucket) + ? timeBucket + : res.timeBucket[0] diff = res.diff timeBucketForAllTime = res.timeBucket } diff --git a/apps/production/src/analytics/analytics.service.ts b/apps/production/src/analytics/analytics.service.ts index 7297476f5..c8296439b 100644 --- a/apps/production/src/analytics/analytics.service.ts +++ b/apps/production/src/analytics/analytics.service.ts @@ -645,12 +645,11 @@ export class AnalyticsService { async getTimeBucketForAllTime( pid: string, period: string, - timeBucket: TimeBucketType, ): Promise<{ timeBucket: TimeBucketType[] diff: number }> { - if (period !== validPeriods[validPeriods.length - 1]) { + if (period !== 'all') { return null } @@ -666,6 +665,7 @@ export class AnalyticsService { { params: { pid } }, ) .toPromise() + let newTimeBucket = [TimeBucketType.MONTH] let diff = null @@ -680,7 +680,7 @@ export class AnalyticsService { ) } - newTimeBucket = _includes(tbMap.tb, timeBucket) ? [timeBucket] : tbMap.tb + newTimeBucket = tbMap.tb } return { From 01c62612bb7cadc9938eab2570515b71e850fe79 Mon Sep 17 00:00:00 2001 From: Maksim Mrug <80621719+kruzhambus@users.noreply.github.com> Date: Wed, 19 Jul 2023 14:32:03 +0300 Subject: [PATCH 13/15] user-flow for timebucket allperiod --- .../production/src/analytics/analytics.controller.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/apps/production/src/analytics/analytics.controller.ts b/apps/production/src/analytics/analytics.controller.ts index d4e4ae5f5..14f20cd0d 100644 --- a/apps/production/src/analytics/analytics.controller.ts +++ b/apps/production/src/analytics/analytics.controller.ts @@ -645,6 +645,17 @@ export class AnalyticsController { this.analyticsService.validatePeriod(period) } + let diff + + if (period === 'all') { + const res = await this.analyticsService.getTimeBucketForAllTime( + pid, + period, + ) + + diff = res.diff + } + await this.analyticsService.checkProjectAccess( pid, uid, @@ -658,6 +669,7 @@ export class AnalyticsController { null, period, safeTimezone, + diff, ) const [filtersQuery, filtersParams, appliedFilters] = From b9cfb38b1f9a16e7dadac1c14fca59fdcf87fd16 Mon Sep 17 00:00:00 2001 From: Maksim Mrug <80621719+kruzhambus@users.noreply.github.com> Date: Wed, 19 Jul 2023 14:35:02 +0300 Subject: [PATCH 14/15] add new period for perfomance --- .../src/analytics/analytics.controller.ts | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/apps/production/src/analytics/analytics.controller.ts b/apps/production/src/analytics/analytics.controller.ts index 14f20cd0d..6b2f1a8d8 100644 --- a/apps/production/src/analytics/analytics.controller.ts +++ b/apps/production/src/analytics/analytics.controller.ts @@ -513,7 +513,25 @@ export class AnalyticsController { this.analyticsService.validatePeriod(period) } - this.analyticsService.validateTimebucket(timeBucket) + let newTimeBucket = timeBucket + let allowedTumebucketForPeriodAll + let diff + + if (period === 'all') { + const res = await this.analyticsService.getTimeBucketForAllTime( + pid, + period, + ) + + diff = res.diff + // eslint-disable-next-line prefer-destructuring + newTimeBucket = _includes(res.timeBucket, timeBucket) + ? timeBucket + : res.timeBucket[0] + allowedTumebucketForPeriodAll = res.timeBucket + } + + this.analyticsService.validateTimebucket(newTimeBucket) const [filtersQuery, filtersParams, appliedFilters] = this.analyticsService.getFiltersQuery(filters, DataType.PERFORMANCE) @@ -521,9 +539,10 @@ export class AnalyticsController { const { groupFrom, groupTo } = this.analyticsService.getGroupFromTo( from, to, - timeBucket, + newTimeBucket, period, safeTimezone, + diff, ) await this.analyticsService.checkProjectAccess( pid, @@ -543,7 +562,7 @@ export class AnalyticsController { } const result = await this.analyticsService.groupPerfByTimeBucket( - timeBucket, + newTimeBucket, groupFrom, groupTo, subQuery, @@ -555,6 +574,7 @@ export class AnalyticsController { return { ...result, appliedFilters, + timeBucket: allowedTumebucketForPeriodAll, } } From 338e4c07d1c6c135c54dcbc08bc065f78fa0b9a8 Mon Sep 17 00:00:00 2001 From: Maksim Mrug <80621719+kruzhambus@users.noreply.github.com> Date: Wed, 19 Jul 2023 15:21:13 +0300 Subject: [PATCH 15/15] add new timebucket year --- apps/production/src/analytics/analytics.service.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/apps/production/src/analytics/analytics.service.ts b/apps/production/src/analytics/analytics.service.ts index c8296439b..400aee71e 100644 --- a/apps/production/src/analytics/analytics.service.ts +++ b/apps/production/src/analytics/analytics.service.ts @@ -137,6 +137,7 @@ const timeBucketConversion = { hour: 'toStartOfHour', day: 'toStartOfDay', month: 'toStartOfMonth', + year: 'toStartOfYear', } const isValidTimezone = (timezone: string): boolean => { @@ -1061,6 +1062,10 @@ export class AnalyticsService { format = 'YYYY-MM' break + case TimeBucketType.YEAR: + format = 'YYYY' + break + default: throw new BadRequestException( `The provided time bucket (${timeBucket}) is incorrect`, @@ -1188,6 +1193,10 @@ export class AnalyticsService { ] } + if (timeBucket === TimeBucketType.YEAR) { + return [`toYear(tz_created) as year`, 'year'] + } + return [ `toYear(tz_created) as year, toMonth(tz_created) as month,