📈 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
added support for from/to pair & timeBucket validation
Blue Mouse0278abe3f615
diff --git a/src/analytics/analytics.controller.ts b/src/analytics/analytics.controller.tsindex e0065a7af..f04034998 100644--- a/src/analytics/analytics.controller.ts+++ b/src/analytics/analytics.controller.ts@@ -12,13 +12,15 @@ import { hash } from 'blake3'import ct from 'countries-and-timezones'import {Controller, Body, Query, UseGuards, Get, Post, Headers, BadRequestException, InternalServerErrorException,- NotImplementedException, UnprocessableEntityException, PreconditionFailedException, Ip, ForbiddenException, Response,+ UnprocessableEntityException, PreconditionFailedException, Ip, ForbiddenException, Response,} from '@nestjs/common'import { ApiTags } from '@nestjs/swagger'import * as UAParser from 'ua-parser-js'import * as isbot from 'isbot'-import { AnalyticsService, getSessionKey, isValidTimezone } from './analytics.service'+import {+ AnalyticsService, getSessionKey, isValidTimezone, isValidDate, checkIfTBAllowed,+} from './analytics.service'import { TaskManagerService } from '../task-manager/task-manager.service'import { CurrentUserId } from '../common/decorators/current-user-id.decorator'import { DEFAULT_TIMEZONE } from '../user/entities/user.entity'@@ -97,15 +99,19 @@ export class AnalyticsController {async getData(@Query() data: AnalyticsGET_DTO, @CurrentUserId() uid: string): Promise<any> {const { pid, period, timeBucket, from, to, filters, timezone = DEFAULT_TIMEZONE } = datathis.analyticsService.validatePID(pid)- this.analyticsService.validatePeriod(period)++ if (!_isEmpty(period)) {+ this.analyticsService.validatePeriod(period)+ }+this.analyticsService.validateTimebucket(timeBucket)const [filtersQuery, filtersParams] = this.analyticsService.getFiltersQuery(filters)await this.analyticsService.checkProjectAccess(pid, uid)let groupFrom = from, groupTo = to- let queryCustoms = 'SELECT ev, count() FROM customEV WHERE pid = {pid:FixedString(12)}'- let subQuery = `FROM analytics WHERE pid = {pid:FixedString(12)} ${filtersQuery}`+ const queryCustoms = 'SELECT ev, count() FROM customEV WHERE pid = {pid:FixedString(12)} AND created BETWEEN {groupFrom:String} AND {groupTo:String} GROUP BY ev'+ const subQuery = `FROM analytics WHERE pid = {pid:FixedString(12)} ${filtersQuery} AND created BETWEEN {groupFrom:String} AND {groupTo:String}`const paramsData = {params: {@@ -117,16 +123,25 @@ export class AnalyticsController {}if (!_isEmpty(from) && !_isEmpty(to)) {- throw new NotImplementedException('Filtering by from/to params is currently not available')+ 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\'')}- if (dayjs.utc(to).isAfter(dayjs.utc(), 'second')) {- groupTo = dayjs.utc().format('YYYY-MM-DD HH:mm:ss')- queryCustoms += ` AND created BETWEEN ${from} AND ${groupTo} GROUP BY ev`+ checkIfTBAllowed(timeBucket, from, to)++ groupFrom = from+ if (from === to) {+ groupTo = dayjs(to).format('YYYY-MM-DD 23:59:59')} else {- queryCustoms += ` AND created BETWEEN ${from} AND ${to} GROUP BY ev`+ groupTo = to}} else if (!_isEmpty(period)) {if (period === 'today') {@@ -148,19 +163,18 @@ export class AnalyticsController {} else {groupFrom = dayjs.utc().subtract(parseInt(period), _last(period)).format('YYYY-MM-DD')groupTo = dayjs.utc().format('YYYY-MM-DD 23:59:59')- }- paramsData.params = {- ...paramsData.params,- groupFrom, groupTo,+ checkIfTBAllowed(timeBucket, groupFrom, groupTo)}-- queryCustoms += ' AND created BETWEEN {groupFrom:String} AND {groupTo:String} GROUP BY ev'- subQuery += ' AND created BETWEEN {groupFrom:String} AND {groupTo:String}'} else {throw new BadRequestException('The timeframe (either from/to pair or period) has to be provided')}+ paramsData.params = {+ ...paramsData.params,+ groupFrom, groupTo,+ }+const result = await this.analyticsService.groupByTimeBucket(timeBucket, groupFrom, groupTo, subQuery, filtersQuery, paramsData, timezone)const customs = await this.analyticsService.processCustomEV(queryCustoms, paramsData)diff --git a/src/analytics/analytics.service.ts b/src/analytics/analytics.service.tsindex 84a0c0bdc..2717cf48a 100644--- a/src/analytics/analytics.service.ts+++ b/src/analytics/analytics.service.ts@@ -19,6 +19,7 @@ import { isLocale } from 'validator'import { hash } from 'blake3'import {Injectable, BadRequestException, InternalServerErrorException, ForbiddenException, UnprocessableEntityException,+ PreconditionFailedException,} from '@nestjs/common'import { ACCOUNT_PLANS, DEFAULT_TIMEZONE } from '../user/entities/user.entity'@@ -39,7 +40,7 @@ dayjs.extend(timezone)export const getSessionKey = (ip: string, ua: string, pid: string, salt: string = '') => `ses_${hash(`${ua}${ip}${pid}${salt}`).toString('hex')}`export const cols = [- 'cc', 'pg', 'lc', 'br', 'os', 'dv', 'ref','so', 'me', 'ca',+ 'cc', 'pg', 'lc', 'br', 'os', 'dv', 'ref', 'so', 'me', 'ca',]interface chartCHResponse {@@ -54,14 +55,23 @@ interface customsCHResponse {}const validPeriods = ['today', 'yesterday', '1d', '7d', '4w', '3M', '12M', '24M']-const validTimebuckets = ['hour', 'day', 'week', 'month']+const validTimebuckets = [TimeBucketType.HOUR, TimeBucketType.DAY, TimeBucketType.WEEK, TimeBucketType.MONTH]++// mapping of allowed timebuckets per difference between days+// (e.g. if difference is lower than (lt) (including) -> then the specified timebuckets are allowed to be applied)+const timeBucketToDays = [+ { lt: 7, tb: [TimeBucketType.HOUR, TimeBucketType.DAY] }, // 7 days+ { lt: 28, tb: [TimeBucketType.DAY, TimeBucketType.WEEK] }, // 4 weeks+ { lt: 366, tb: [TimeBucketType.WEEK, TimeBucketType.MONTH] }, // 12 months+ { lt: 732, tb: [TimeBucketType.MONTH] }, // 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 (.)const customEVvalidate = /^[a-zA-Z](?:[\w\.]){0,62}$/-interface GetFiltersQuery extends Array<string | object>{0:string; 1:object}+interface GetFiltersQuery extends Array<string | object> { 0: string; 1: object }-export const isValidTimezone = (timezone) => {+export const isValidTimezone = (timezone: string): boolean => {if (_isEmpty(timezone)) {return false}@@ -74,6 +84,30 @@ export const isValidTimezone = (timezone) => {}}+export const isValidDate = (date: string, format: string = 'YYYY-MM-DD'): boolean => {+ if (_isEmpty(date)) {+ return false+ }++ return dayjs(date, format).format(format) === date+}++export const checkIfTBAllowed = (timeBucket: TimeBucketType, from: string, to: string): void => {+ const diff = dayjs(to).diff(dayjs(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')+ }++ const { tb } = tbMap++ if (!_includes(tb, timeBucket)) {+ throw new PreconditionFailedException('The specified \'timeBucket\' parameter cannot be applied to the date range')+ }+}+@Injectable()export class AnalyticsService {constructor(@@ -220,6 +254,10 @@ export class AnalyticsService {let query = ''let params = {}+ if (_isEmpty(filters)) {+ return [query, params]+ }+try {parsed = JSON.parse(filters)} catch (e) {@@ -250,7 +288,7 @@ export class AnalyticsService {return [query, params]}- validateTimebucket(tb: string): void {+ validateTimebucket(tb: TimeBucketType): void {if (!_includes(validTimebuckets, tb)) {throw new UnprocessableEntityException('The provided timebucket is incorrect')}@@ -343,10 +381,6 @@ export class AnalyticsService {const iterateTo = djsTo > now ? now : djsToswitch (timeBucket) {- case TimeBucketType.MINUTE:- groupDateIterator = dayjs.utc(from).startOf('minute')- break-case TimeBucketType.HOUR:groupDateIterator = dayjs.utc(from).startOf('hour')break@@ -354,7 +388,6 @@ export class AnalyticsService {case TimeBucketType.DAY:case TimeBucketType.WEEK:case TimeBucketType.MONTH:- case TimeBucketType.YEAR:groupDateIterator = dayjs.utc(from).startOf('day')breakdiff --git a/src/analytics/dto/getData.dto.ts b/src/analytics/dto/getData.dto.tsindex 36b6b049d..76cb64b56 100644--- a/src/analytics/dto/getData.dto.ts+++ b/src/analytics/dto/getData.dto.ts@@ -3,12 +3,10 @@ import { IsNotEmpty } from 'class-validator'import { DEFAULT_TIMEZONE } from '../../user/entities/user.entity'export enum TimeBucketType {- MINUTE = 'minute',HOUR = 'hour',DAY = 'day',WEEK = 'week',MONTH = 'month',- YEAR = 'year',}export class AnalyticsGET_DTO {