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!

Selfhosted: Validate periods in DTO

Blue Mouse7ca978cde9f8
diff --git a/backend/apps/selfhosted/src/analytics/analytics.controller.ts b/backend/apps/selfhosted/src/analytics/analytics.controller.ts
index b166c029e..a53783d1f 100644
--- a/backend/apps/selfhosted/src/analytics/analytics.controller.ts
+++ b/backend/apps/selfhosted/src/analytics/analytics.controller.ts
@@ -30,10 +30,10 @@ import { Auth, Public } from '../auth/decorators'
import {
AnalyticsService,
getHeartbeatKey,
- validPeriods,
DataType,
getLowestPossibleTimeBucket,
} from './analytics.service'
+import { VALID_PERIODS } from './decorators/validate-period.decorator'
import { CurrentUserId } from '../auth/decorators/current-user-id.decorator'
import { DEFAULT_TIMEZONE } from '../user/entities/user.entity'
import { RolesGuard } from '../auth/guards/roles.guard'
@@ -42,9 +42,9 @@ import { EventsDTO } from './dto/events.dto'
import { GetDataDto, ChartRenderMode } from './dto/getData.dto'
import { GetFiltersDto } from './dto/get-filters.dto'
import { GetCustomEventMetadata } from './dto/get-custom-event-meta.dto'
-import { GetPagePropertyMetaDTO } from './dto/get-page-property-meta.dto'
-import { GetUserFlowDTO } from './dto/getUserFlow.dto'
-import { GetFunnelsDTO } from './dto/getFunnels.dto'
+import { GetPagePropertyMetaDto } from './dto/get-page-property-meta.dto'
+import { GetUserFlowDto } from './dto/getUserFlow.dto'
+import { GetFunnelsDto } from './dto/getFunnels.dto'
import { AppLoggerService } from '../logger/logger.service'
import { redis, HEARTBEAT_SID_LIFE_TIME } from '../common/constants'
import { clickhouse } from '../common/integrations/clickhouse'
@@ -75,6 +75,7 @@ import {
} from './utils/transformers'
import { MAX_METRICS_IN_VIEW } from '../project/dto/create-project-view.dto'
import { extensions } from './utils/ua-parser'
+import { GetOverallStatsDto } from './dto/get-overall-stats.dto'
dayjs.extend(utc)
dayjs.extend(dayjsTimezone)
@@ -178,7 +179,7 @@ export class AnalyticsController {
@Query() data: GetDataDto,
@CurrentUserId() uid: string,
@Headers() headers: { 'x-password'?: string },
- ): Promise<any> {
+ ) {
const {
pid,
period,
@@ -192,10 +193,6 @@ export class AnalyticsController {
} = data
this.analyticsService.validatePID(pid)
- if (!_isEmpty(period)) {
- this.analyticsService.validatePeriod(period)
- }
-
await this.analyticsService.checkProjectAccess(
pid,
uid,
@@ -314,7 +311,7 @@ export class AnalyticsController {
@Get('funnel')
@Auth([], true, true)
async getFunnel(
- @Query() data: GetFunnelsDTO,
+ @Query() data: GetFunnelsDto,
@CurrentUserId() uid: string,
@Headers() headers: { 'x-password'?: string },
): Promise<IGetFunnel> {
@@ -329,10 +326,6 @@ export class AnalyticsController {
} = data
this.analyticsService.validatePID(pid)
- if (!_isEmpty(period)) {
- this.analyticsService.validatePeriod(period)
- }
-
const pagesArr = await this.analyticsService.getPagesArray(
pages,
funnelId,
@@ -391,13 +384,9 @@ export class AnalyticsController {
@CurrentUserId() uid: string,
@Headers() headers: { 'x-password'?: string },
): Promise<ReturnType<typeof this.analyticsService.getCustomEventMetadata>> {
- const { pid, period } = data
+ const { pid } = data
this.analyticsService.validatePID(pid)
- if (!_isEmpty(period)) {
- this.analyticsService.validatePeriod(period)
- }
-
await this.analyticsService.checkProjectAccess(
pid,
uid,
@@ -410,17 +399,13 @@ export class AnalyticsController {
@Get('property')
@Auth([], true, true)
async getPagePropertyMetadata(
- @Query() data: GetPagePropertyMetaDTO,
+ @Query() data: GetPagePropertyMetaDto,
@CurrentUserId() uid: string,
@Headers() headers: { 'x-password'?: string },
): Promise<ReturnType<typeof this.analyticsService.getPagePropertyMeta>> {
- const { pid, period } = data
+ const { pid } = data
this.analyticsService.validatePID(pid)
- if (!_isEmpty(period)) {
- this.analyticsService.validatePeriod(period)
- }
-
await this.analyticsService.checkProjectAccess(
pid,
uid,
@@ -436,7 +421,7 @@ export class AnalyticsController {
@Query() data: GetFiltersDto,
@CurrentUserId() uid: string,
@Headers() headers: { 'x-password'?: string },
- ): Promise<any> {
+ ) {
const { pid, type } = data
this.analyticsService.validatePID(pid)
@@ -455,7 +440,7 @@ export class AnalyticsController {
@Query() data: GetDataDto,
@CurrentUserId() uid: string,
@Headers() headers: { 'x-password'?: string },
- ): Promise<any> {
+ ) {
const {
pid,
period,
@@ -468,10 +453,6 @@ export class AnalyticsController {
} = data
this.analyticsService.validatePID(pid)
- if (!_isEmpty(period)) {
- this.analyticsService.validatePeriod(period)
- }
-
const [filtersQuery, filtersParams, parsedFilters, customEVFilterApplied] =
this.analyticsService.getFiltersQuery(filters, DataType.ANALYTICS)
@@ -521,7 +502,7 @@ export class AnalyticsController {
@Query() data: GetDataDto & { measure: PerfMeasure },
@CurrentUserId() uid: string,
@Headers() headers: { 'x-password'?: string },
- ): Promise<any> {
+ ) {
const {
pid,
period,
@@ -536,10 +517,6 @@ export class AnalyticsController {
this.analyticsService.checkIfPerfMeasureIsValid(measure)
- if (!_isEmpty(period)) {
- this.analyticsService.validatePeriod(period)
- }
-
await this.analyticsService.checkProjectAccess(
pid,
uid,
@@ -613,7 +590,7 @@ export class AnalyticsController {
@Query() data: GetDataDto & { measure: PerfMeasure },
@CurrentUserId() uid: string,
@Headers() headers: { 'x-password'?: string },
- ): Promise<any> {
+ ) {
const {
pid,
period,
@@ -628,10 +605,6 @@ export class AnalyticsController {
this.analyticsService.checkIfPerfMeasureIsValid(measure)
- if (!_isEmpty(period)) {
- this.analyticsService.validatePeriod(period)
- }
-
const [filtersQuery, filtersParams, parsedFilters] =
this.analyticsService.getFiltersQuery(filters, DataType.PERFORMANCE)
@@ -677,17 +650,13 @@ export class AnalyticsController {
@Get('user-flow')
@Auth([], true, true)
async getUserFlow(
- @Query() data: GetUserFlowDTO,
+ @Query() data: GetUserFlowDto,
@CurrentUserId() uid: string,
@Headers() headers: { 'x-password'?: string },
): Promise<IUserFlow | { appliedFilters: any[] }> {
const { pid, period, from, to, timezone = DEFAULT_TIMEZONE, filters } = data
this.analyticsService.validatePID(pid)
- if (!_isEmpty(period)) {
- this.analyticsService.validatePeriod(period)
- }
-
await this.analyticsService.checkProjectAccess(
pid,
uid,
@@ -738,10 +707,10 @@ export class AnalyticsController {
@Auth([], true, true)
// returns overall short statistics per project
async getOverallStats(
- @Query() data,
+ @Query() data: GetOverallStatsDto,
@CurrentUserId() uid: string,
@Headers() headers: { 'x-password'?: string },
- ): Promise<any> {
+ ) {
const {
pids,
pid,
@@ -777,10 +746,10 @@ export class AnalyticsController {
@Get('performance/birdseye')
@Auth([], true, true)
async getPerformanceOverallStats(
- @Query() data,
+ @Query() data: GetOverallStatsDto & { measure: PerfMeasure },
@CurrentUserId() uid: string,
@Headers() headers: { 'x-password'?: string },
- ): Promise<any> {
+ ) {
const {
pids,
pid,
@@ -919,7 +888,7 @@ export class AnalyticsController {
@Body() eventsDTO: EventsDTO,
@Headers() headers,
@Ip() reqIP,
- ): Promise<any> {
+ ) {
const { 'user-agent': userAgent, origin } = headers
const ip = getIPFromHeaders(headers) || reqIP || ''
@@ -1000,7 +969,7 @@ export class AnalyticsController {
@Body() logDTO: PageviewsDTO,
@Headers() headers,
@Ip() reqIP,
- ): Promise<any> {
+ ) {
const { 'user-agent': userAgent, origin } = headers
const { pid } = logDTO
const ip = getIPFromHeaders(headers) || reqIP || ''
@@ -1022,11 +991,7 @@ export class AnalyticsController {
@Post()
@Public()
- async log(
- @Body() logDTO: PageviewsDTO,
- @Headers() headers,
- @Ip() reqIP,
- ): Promise<any> {
+ async log(@Body() logDTO: PageviewsDTO, @Headers() headers, @Ip() reqIP) {
const { 'user-agent': userAgent, origin } = headers
const ip = getIPFromHeaders(headers) || reqIP || ''
@@ -1149,7 +1114,7 @@ export class AnalyticsController {
@Headers() headers,
@Ip() reqIP,
@Response() res,
- ): Promise<any> {
+ ) {
const { 'user-agent': userAgent, origin } = headers
const { pid } = data
@@ -1228,14 +1193,10 @@ export class AnalyticsController {
@Query() data: GetSessionsDto,
@CurrentUserId() uid: string,
@Headers() headers: { 'x-password'?: string },
- ): Promise<any> {
+ ) {
const { pid, period, from, to, filters, timezone = DEFAULT_TIMEZONE } = data
this.analyticsService.validatePID(pid)
- if (!_isEmpty(period)) {
- this.analyticsService.validatePeriod(period)
- }
-
await this.analyticsService.checkProjectAccess(
pid,
uid,
@@ -1313,7 +1274,7 @@ export class AnalyticsController {
@Query() data: GetSessionDto,
@CurrentUserId() uid: string,
@Headers() headers: { 'x-password'?: string },
- ): Promise<any> {
+ ) {
const {
pid,
psid,
@@ -1344,7 +1305,7 @@ export class AnalyticsController {
@Query() data: GetCustomEventsDto,
@CurrentUserId() uid: string,
@Headers() headers: { 'x-password'?: string },
- ): Promise<any> {
+ ) {
const {
pid,
period,
@@ -1357,10 +1318,6 @@ export class AnalyticsController {
} = data
this.analyticsService.validatePID(pid)
- if (!_isEmpty(period)) {
- this.analyticsService.validatePeriod(period)
- }
-
await this.analyticsService.checkProjectAccess(
pid,
uid,
@@ -1371,7 +1328,7 @@ export class AnalyticsController {
let diff
let timeBucketForAllTime
- if (period === validPeriods[validPeriods.length - 1]) {
+ if (period === VALID_PERIODS[VALID_PERIODS.length - 1]) {
const res = await this.analyticsService.getTimeBucketForAllTime(
pid,
period,
@@ -1449,7 +1406,7 @@ export class AnalyticsController {
@Query() data: GetFiltersDto,
@CurrentUserId() uid: string,
@Headers() headers: { 'x-password'?: string },
- ): Promise<any> {
+ ) {
const { pid, type } = data
this.analyticsService.validatePID(pid)
@@ -1464,11 +1421,7 @@ export class AnalyticsController {
@Post('error')
@Public()
- async logError(
- @Body() errorDTO: ErrorDTO,
- @Headers() headers,
- @Ip() reqIP,
- ): Promise<any> {
+ async logError(@Body() errorDTO: ErrorDTO, @Headers() headers, @Ip() reqIP) {
const { 'user-agent': userAgent, origin } = headers
const ip = getIPFromHeaders(headers) || reqIP || ''
@@ -1529,7 +1482,7 @@ export class AnalyticsController {
@CurrentUserId() uid: string,
@Headers() headers,
@Ip() reqIP,
- ): Promise<any> {
+ ) {
const { pid, eid, eids: unprocessedEids, status } = statusDTO
const ip = getIPFromHeaders(headers) || reqIP || ''
@@ -1555,7 +1508,7 @@ export class AnalyticsController {
@Query() data: GetErrorsDto,
@CurrentUserId() uid: string,
@Headers() headers: { 'x-password'?: string },
- ): Promise<any> {
+ ) {
const {
pid,
period,
@@ -1567,10 +1520,6 @@ export class AnalyticsController {
} = data
this.analyticsService.validatePID(pid)
- if (!_isEmpty(period)) {
- this.analyticsService.validatePeriod(period)
- }
-
await this.analyticsService.checkProjectAccess(
pid,
uid,
@@ -1648,7 +1597,7 @@ export class AnalyticsController {
@Query() data: GetErrorDTO,
@CurrentUserId() uid: string,
@Headers() headers: { 'x-password'?: string },
- ): Promise<any> {
+ ) {
const {
pid,
timezone = DEFAULT_TIMEZONE,
@@ -1660,10 +1609,6 @@ export class AnalyticsController {
} = data
this.analyticsService.validatePID(pid)
- if (!_isEmpty(period)) {
- this.analyticsService.validatePeriod(period)
- }
-
await this.analyticsService.checkProjectAccess(
pid,
uid,
diff --git a/backend/apps/selfhosted/src/analytics/analytics.service.ts b/backend/apps/selfhosted/src/analytics/analytics.service.ts
index 9d5b0cfa1..dcbe56dac 100644
--- a/backend/apps/selfhosted/src/analytics/analytics.service.ts
+++ b/backend/apps/selfhosted/src/analytics/analytics.service.ts
@@ -91,7 +91,7 @@ import {
ICustomEvent,
} from './interfaces'
import { ErrorDTO } from './dto/error.dto'
-import { GetPagePropertyMetaDTO } from './dto/get-page-property-meta.dto'
+import { GetPagePropertyMetaDto } from './dto/get-page-property-meta.dto'
import { ProjectViewCustomEventMetaValueType } from '../project/entity/project-view-custom-event.entity'
import { ProjectViewCustomEventDto } from '../project/dto/create-project-view.dto'
@@ -110,19 +110,6 @@ const GMT_0_TIMEZONES = [
// 'Africa/Casablanca',
]
-export const validPeriods = [
- '1h',
- 'today',
- 'yesterday',
- '1d',
- '7d',
- '4w',
- '3M',
- '12M',
- '24M',
- 'all',
-]
-
const MEASURES_MAP = {
average: 'avg',
median: 'median',
@@ -189,10 +176,6 @@ export const getLowestPossibleTimeBucket = (
to?: string,
): TimeBucketType => {
if (!from && !to) {
- if (!_includes(validPeriods, period)) {
- throw new UnprocessableEntityException('The provided period is incorrect')
- }
-
if (period === '1h') {
return TimeBucketType.MINUTE
}
@@ -761,12 +744,6 @@ export class AnalyticsService {
}
}
- validatePeriod(period: string): void {
- if (!_includes(validPeriods, period)) {
- throw new UnprocessableEntityException('The provided period is incorrect')
- }
- }
-
async getPagesArray(
rawPages?: string,
funnelId?: string,
@@ -1295,8 +1272,6 @@ export class AnalyticsService {
_from = groupFrom
_to = groupTo
- } else {
- this.validatePeriod(period)
}
const result = {}
@@ -1489,8 +1464,6 @@ export class AnalyticsService {
_from = groupFrom
_to = groupTo
- } else {
- this.validatePeriod(period)
}
const result = {}
@@ -2807,7 +2780,7 @@ export class AnalyticsService {
}
}
- async getPagePropertyMeta(data: GetPagePropertyMetaDTO): Promise<{
+ async getPagePropertyMeta(data: GetPagePropertyMetaDto): Promise<{
result: IAggregatedMetadata[]
appliedFilters: GetFiltersQuery[2]
}> {
diff --git a/backend/apps/selfhosted/src/analytics/decorators/validate-period.decorator.ts b/backend/apps/selfhosted/src/analytics/decorators/validate-period.decorator.ts
new file mode 100644
index 000000000..917104e61
--- /dev/null
+++ b/backend/apps/selfhosted/src/analytics/decorators/validate-period.decorator.ts
@@ -0,0 +1,40 @@
+import { registerDecorator, ValidationOptions } from 'class-validator'
+import { UnprocessableEntityException } from '@nestjs/common'
+import _includes from 'lodash/includes'
+
+export const VALID_PERIODS = [
+ '1h',
+ 'today',
+ 'yesterday',
+ '1d',
+ '7d',
+ '4w',
+ '3M',
+ '12M',
+ '24M',
+ 'all',
+]
+
+export function ValidatePeriod(validationOptions?: ValidationOptions) {
+ return (object: object, propertyName: string) => {
+ registerDecorator({
+ name: 'validatePeriod',
+ target: object.constructor,
+ propertyName,
+ options: validationOptions,
+ validator: {
+ validate(value: any) {
+ if (value === undefined || value === '') {
+ return true // Allow empty or undefined values
+ }
+ if (!_includes(VALID_PERIODS, value)) {
+ throw new UnprocessableEntityException(
+ `The provided period is incorrect. It should be one of: ${VALID_PERIODS.join(', ')}`,
+ )
+ }
+ return true
+ },
+ },
+ })
+ }
+}
diff --git a/backend/apps/selfhosted/src/analytics/dto/get-overall-stats.dto.ts b/backend/apps/selfhosted/src/analytics/dto/get-overall-stats.dto.ts
new file mode 100644
index 000000000..1b644086c
--- /dev/null
+++ b/backend/apps/selfhosted/src/analytics/dto/get-overall-stats.dto.ts
@@ -0,0 +1,49 @@
+import { ApiProperty } from '@nestjs/swagger'
+import { IsOptional, IsString, ValidateIf } from 'class-validator'
+import { DEFAULT_TIMEZONE } from '../../user/entities/user.entity'
+import { ValidatePeriod } from '../decorators/validate-period.decorator'
+
+export class GetOverallStatsDto {
+ @ApiProperty({ description: 'Array of project IDs', required: false })
+ @ValidateIf(o => o.pids || !o.pid)
+ @IsOptional()
+ @IsString({ each: true })
+ pids?: string[]
+
+ @ApiProperty({ description: 'Single project ID', required: false })
+ @ValidateIf(o => !o.pids || o.pid)
+ @IsOptional()
+ @IsString()
+ pid?: string
+
+ @ApiProperty({ required: false })
+ @IsOptional()
+ @ValidatePeriod()
+ period?: string
+
+ @ApiProperty({ required: false })
+ @IsOptional()
+ @IsString()
+ from?: string
+
+ @ApiProperty({ required: false })
+ @IsOptional()
+ @IsString()
+ to?: string
+
+ @ApiProperty({
+ description: 'Timezone to display data in',
+ default: DEFAULT_TIMEZONE,
+ })
+ @IsOptional()
+ @IsString()
+ timezone?: string = DEFAULT_TIMEZONE
+
+ @ApiProperty({
+ description: 'A stringified array of properties to filter',
+ required: false,
+ })
+ @IsOptional()
+ @IsString()
+ filters?: string
+}
diff --git a/backend/apps/selfhosted/src/analytics/dto/get-page-property-meta.dto.ts b/backend/apps/selfhosted/src/analytics/dto/get-page-property-meta.dto.ts
index 768af843b..5f9d7cb8e 100644
--- a/backend/apps/selfhosted/src/analytics/dto/get-page-property-meta.dto.ts
+++ b/backend/apps/selfhosted/src/analytics/dto/get-page-property-meta.dto.ts
@@ -2,7 +2,7 @@ import { ApiProperty, PickType } from '@nestjs/swagger'
import { IsNotEmpty, IsString } from 'class-validator'
import { GetDataDto } from './getData.dto'
-export class GetPagePropertyMetaDTO extends PickType(GetDataDto, [
+export class GetPagePropertyMetaDto extends PickType(GetDataDto, [
'pid',
'period',
'timeBucket',
diff --git a/backend/apps/selfhosted/src/analytics/dto/getData.dto.ts b/backend/apps/selfhosted/src/analytics/dto/getData.dto.ts
index 05d6a180a..d648005db 100644
--- a/backend/apps/selfhosted/src/analytics/dto/getData.dto.ts
+++ b/backend/apps/selfhosted/src/analytics/dto/getData.dto.ts
@@ -1,6 +1,7 @@
import { ApiProperty } from '@nestjs/swagger'
import { IsEnum, IsNotEmpty, IsOptional } from 'class-validator'
import { DEFAULT_TIMEZONE } from '../../user/entities/user.entity'
+import { ValidatePeriod } from '../decorators/validate-period.decorator'
export enum TimeBucketType {
MINUTE = 'minute',
@@ -21,8 +22,10 @@ export class GetDataDto {
@IsNotEmpty()
pid: string
- @ApiProperty()
- period: string
+ @ApiProperty({ required: false })
+ @IsOptional()
+ @ValidatePeriod()
+ period?: string
@ApiProperty({ enum: TimeBucketType })
@IsNotEmpty()
diff --git a/backend/apps/selfhosted/src/analytics/dto/getFunnels.dto.ts b/backend/apps/selfhosted/src/analytics/dto/getFunnels.dto.ts
index 197895faa..e88365717 100644
--- a/backend/apps/selfhosted/src/analytics/dto/getFunnels.dto.ts
+++ b/backend/apps/selfhosted/src/analytics/dto/getFunnels.dto.ts
@@ -1,15 +1,18 @@
import { ApiProperty } from '@nestjs/swagger'
-import { IsNotEmpty } from 'class-validator'
+import { IsNotEmpty, IsOptional } from 'class-validator'
import { DEFAULT_TIMEZONE } from '../../user/entities/user.entity'
+import { ValidatePeriod } from '../decorators/validate-period.decorator'
// eslint-disable-next-line @typescript-eslint/naming-convention
-export class GetFunnelsDTO {
+export class GetFunnelsDto {
@ApiProperty()
@IsNotEmpty()
pid: string
- @ApiProperty()
- period: string
+ @ApiProperty({ required: false })
+ @IsOptional()
+ @ValidatePeriod()
+ period?: string
@ApiProperty()
from: string
diff --git a/backend/apps/selfhosted/src/analytics/dto/getUserFlow.dto.ts b/backend/apps/selfhosted/src/analytics/dto/getUserFlow.dto.ts
index 63fd88641..774962487 100644
--- a/backend/apps/selfhosted/src/analytics/dto/getUserFlow.dto.ts
+++ b/backend/apps/selfhosted/src/analytics/dto/getUserFlow.dto.ts
@@ -1,15 +1,17 @@
import { ApiProperty } from '@nestjs/swagger'
-import { IsNotEmpty } from 'class-validator'
+import { IsNotEmpty, IsOptional } from 'class-validator'
import { DEFAULT_TIMEZONE } from '../../user/entities/user.entity'
+import { ValidatePeriod } from '../decorators/validate-period.decorator'
-// eslint-disable-next-line @typescript-eslint/naming-convention
-export class GetUserFlowDTO {
+export class GetUserFlowDto {
@ApiProperty()
@IsNotEmpty()
pid: string
- @ApiProperty()
- period: string
+ @ApiProperty({ required: false })
+ @IsOptional()
+ @ValidatePeriod()
+ period?: string
@ApiProperty()
from: string