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!

de-duplicated grouping logic

Blue Mouseb1f4e0cf6f9b
diff --git a/src/analytics/analytics.controller.ts b/src/analytics/analytics.controller.ts
index 3393bc6bb..1b9800549 100644
--- a/src/analytics/analytics.controller.ts
+++ b/src/analytics/analytics.controller.ts
@@ -929,104 +929,23 @@ export class AnalyticsController {
this.analyticsService.getFiltersQuery(filters)
await this.analyticsService.checkProjectAccess(pid, uid)
- let groupFrom = from
- let groupTo = to
+ const { groupFrom, groupTo } = this.analyticsService.getGroupFromTo(
+ from,
+ to,
+ timeBucket,
+ period,
+ timezone,
+ )
const paramsData = {
params: {
pid,
- groupFrom: null,
- groupTo: null,
+ groupFrom,
+ groupTo,
...filtersParams,
},
}
- if (!_isEmpty(from) && !_isEmpty(to)) {
- if (!isValidDate(from)) {
- throw new PreconditionFailedException(
- "The timeframe 'from' parameter is invalid",
- )
- }
-
- if (!isValidDate(to)) {
- throw new PreconditionFailedException(
- "The timeframe 'to' parameter is invalid",
- )
- }
-
- if (dayjs.utc(from).isAfter(dayjs.utc(to), 'second')) {
- throw new PreconditionFailedException(
- "The timeframe 'from' parameter cannot be greater than 'to'",
- )
- }
-
- checkIfTBAllowed(timeBucket, from, to)
-
- groupFrom = dayjs.tz(from, timezone).utc().format('YYYY-MM-DD HH:mm:ss')
-
- if (from === to) {
- groupTo = dayjs
- .tz(to, timezone)
- .add(1, 'day')
- .format('YYYY-MM-DD HH:mm:ss')
- } else {
- groupTo = dayjs.tz(to, timezone).format('YYYY-MM-DD HH:mm:ss')
- }
- } else if (!_isEmpty(period)) {
- if (period === 'today') {
- if (timezone !== DEFAULT_TIMEZONE && isValidTimezone(timezone)) {
- groupFrom = dayjs()
- .tz(timezone)
- .startOf('d')
- .utc()
- .format('YYYY-MM-DD HH:mm:ss')
- groupTo = dayjs().tz(timezone).utc().format('YYYY-MM-DD HH:mm:ss')
- } else {
- groupFrom = dayjs.utc().startOf('d').format('YYYY-MM-DD')
- groupTo = dayjs.utc().format('YYYY-MM-DD HH:mm:ss')
- }
- } else if (period === 'yesterday') {
- if (timezone !== DEFAULT_TIMEZONE && isValidTimezone(timezone)) {
- groupFrom = dayjs()
- .tz(timezone)
- .startOf('d')
- .subtract(1, 'day')
- .utc()
- .format('YYYY-MM-DD HH:mm:ss')
- groupTo = dayjs()
- .tz(timezone)
- .startOf('d')
- .utc()
- .format('YYYY-MM-DD HH:mm:ss')
- } else {
- groupFrom = dayjs
- .utc()
- .startOf('d')
- .subtract(1, 'day')
- .format('YYYY-MM-DD')
- groupTo = dayjs.utc().startOf('d').format('YYYY-MM-DD HH:mm:ss')
- }
- } else {
- groupFrom = dayjs
- .utc()
- .subtract(parseInt(period, 10), _last(period))
- .format('YYYY-MM-DD')
- groupTo = dayjs.utc().format('YYYY-MM-DD 23:59:59')
-
- checkIfTBAllowed(timeBucket, groupFrom, groupTo)
- }
- } else {
- throw new BadRequestException(
- 'The timeframe (either from/to pair or period) has to be provided',
- )
- }
-
- paramsData.params = {
- ...paramsData.params,
- groupFrom,
- groupTo,
- }
-
// let result: object | void
let appliedFilters = filters