📈 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
storing filters as JSON
Blue Mouseea3e0b220137
diff --git a/apps/production/src/project/dto/create-project-view.dto.ts b/apps/production/src/project/dto/create-project-view.dto.tsindex f6c9ddd5b..557c08490 100644--- a/apps/production/src/project/dto/create-project-view.dto.ts+++ b/apps/production/src/project/dto/create-project-view.dto.ts@@ -3,56 +3,15 @@ import {IsNotEmpty,IsOptional,IsString,- Matches,MaxLength,MinLength,- IsLocale,ValidateNested,- registerDecorator,- ValidationOptions,- ValidatorConstraint,- ValidatorConstraintInterface,- isISO31661Alpha2,- IsUrl,} from 'class-validator'import { ApiProperty } from '@nestjs/swagger'import { ProjectViewType } from '../entity/project-view.entity'import { ProjectViewCustomEventMetaValueType } from '../entity/project-view-custom-event.entity'--// This can be updated by the customer request.-// In case we find out that ``cc`` may have any other values, just extend the ``allowedValues`` defined below-// Current implementation is for ``Cloudflare``--const allowedValues = ['T1', 'XX']--@ValidatorConstraint({ async: false })-export class IsISO31661Alpha2OrCustomConstraint- implements ValidatorConstraintInterface-{- validate(value: any) {- return isISO31661Alpha2(value) || allowedValues.includes(value)- }-- defaultMessage() {- return `cc must be a valid ISO 3166-1 alpha-2 country code or one of the following: ${allowedValues}`- }-}--export function IsISO31661Alpha2OrCustom(- validationOptions?: ValidationOptions,-) {- return function closure(object: object, propertyName: string) {- // changed from Object to object- registerDecorator({- target: object.constructor,- propertyName,- options: validationOptions,- constraints: [],- validator: IsISO31661Alpha2OrCustomConstraint,- })- }-}+import { TRAFFIC_COLUMNS } from '../../common/constants'export class ProjectViewCustomEventDto {@ApiProperty()@@ -82,9 +41,15 @@ export class ProjectViewCustomEventDto {metaValueType: ProjectViewCustomEventMetaValueType}+export interface Filter {+ column: keyof typeof TRAFFIC_COLUMNS+ filter: string+ isExclusive: boolean+}+export class CreateProjectViewDto {@ApiProperty()- @MaxLength(100)+ @MaxLength(20)@MinLength(1)@IsString()@IsNotEmpty()@@ -95,93 +60,21 @@ export class CreateProjectViewDto {@IsNotEmpty()type: ProjectViewType- @ApiProperty({ description: 'Page the user viewed (/hello)', nullable: true })- /* eslint-disable-next-line */- @Matches(/^\/[a-zA-Z0-9-_\/\[\]]*$/, {- message: 'Invalid URL path format for pg',- })- @IsOptional()- pg?: string-- @ApiProperty({ description: 'Name of the custom event (e.g., sign_up)' })- @MaxLength(100)- @MinLength(1)- @IsString()- @IsOptional()- ev?: string-@ApiProperty({- description: 'User device (mobile, desktop, tablet, etc.)',- nullable: true,+ description:+ 'An array of properties to filter [{ column, filter, isExclusive }]',+ required: false,+ isArray: true,})- @IsString()- @IsOptional()- dv?: string-- @ApiProperty({ description: 'Browser', nullable: true })- @IsString()- @IsOptional()- br?: string-- @ApiProperty({ description: 'Operating system', nullable: true })- @IsString()@IsOptional()- os?: string-- @ApiProperty({ description: 'Locale (en-UK, en-US)', nullable: true })- @IsLocale()- @IsOptional()- lc?: string+ filters?: Filter[]@ApiProperty({- description:- 'Referrer (site from which the user came to the site using Swetrix)',- nullable: true,+ type: ProjectViewCustomEventDto,+ required: false,+ isArray: true,})- @IsUrl()- @IsOptional()- ref?: string-- @ApiProperty({ description: 'UTM source', nullable: true })- @MaxLength(100)- @MinLength(1)- @IsString()- @IsOptional()- so?: string-- @ApiProperty({ description: 'UTM medium', nullable: true })- @MaxLength(100)- @MinLength(1)- @IsString()- @IsOptional()- me?: string-- @ApiProperty({ description: 'UTM campaign', nullable: true })- @MaxLength(100)- @MinLength(1)- @IsString()- @IsOptional()- ca?: string-- @ApiProperty({ description: 'Country code', nullable: true })- @IsISO31661Alpha2OrCustom()- @IsNotEmpty()- cc: string-- @ApiProperty({ description: 'Region/state (Alabama, etc.)', nullable: true })- @MaxLength(100)- @MinLength(1)- @IsString()- rg?: string-- @ApiProperty({ description: 'City (Berlin, London, etc.)', nullable: true })- @MaxLength(100)- @MinLength(1)- @IsString()- ct?: string-- @ApiProperty({ type: ProjectViewCustomEventDto, isArray: true })@ValidateNested()- @IsNotEmpty()- customEvents: ProjectViewCustomEventDto[]+ @IsOptional()+ customEvents?: ProjectViewCustomEventDto[]}diff --git a/apps/production/src/project/entity/project-view.entity.ts b/apps/production/src/project/entity/project-view.entity.tsindex 19cfb6f00..6c66743fd 100644--- a/apps/production/src/project/entity/project-view.entity.ts+++ b/apps/production/src/project/entity/project-view.entity.ts@@ -37,55 +37,7 @@ export class ProjectViewEntity {@ApiProperty()@Column('varchar')- pg: string | null-- @ApiProperty()- @Column('varchar')- ev: string-- @ApiProperty()- @Column('varchar', { nullable: true })- dv: string | null-- @ApiProperty()- @Column('varchar', { nullable: true })- br: string | null-- @ApiProperty()- @Column('varchar', { nullable: true })- os: string | null-- @ApiProperty()- @Column('varchar', { nullable: true })- lc: string | null-- @ApiProperty()- @Column('varchar', { nullable: true })- ref: string | null-- @ApiProperty()- @Column('varchar', { nullable: true })- so: string | null-- @ApiProperty()- @Column('varchar', { nullable: true })- me: string | null-- @ApiProperty()- @Column('varchar', { nullable: true })- ca: string | null-- @ApiProperty()- @Column('varchar', { length: 2, nullable: true })- cc: string-- @ApiProperty()- @Column('varchar', { nullable: true })- rg: string | null-- @ApiProperty()- @Column('varchar', { nullable: true })- ct: string | null+ filters: string | null@ApiProperty()@CreateDateColumn()diff --git a/apps/production/src/project/project.controller.ts b/apps/production/src/project/project.controller.tsindex da55cdf1d..ca3629777 100644--- a/apps/production/src/project/project.controller.ts+++ b/apps/production/src/project/project.controller.ts@@ -1948,6 +1948,7 @@ export class ProjectController {async getProjectView(@Param() params: ProjectViewIdsDto,@CurrentUserId() userId: string,+ @Headers() headers: { 'x-password'?: string },) {const project = await this.projectService.findProject(params.projectId, ['admin',@@ -1964,7 +1965,8 @@ export class ProjectController {throw new NotFoundException('User not found.')}- this.projectService.allowedToManage(project, userId, user.roles)+ this.projectService.allowedToView(project, userId, headers['x-password'])+return this.projectsViewsRepository.findProjectView(params.projectId,params.viewId,@@ -2000,10 +2002,12 @@ export class ProjectController {this.projectService.allowedToManage(project, userId, user.roles)const createdProjectView =- await this.projectsViewsRepository.createProjectView(- params.projectId,- body,- )+ await this.projectsViewsRepository.createProjectView(params.projectId, {+ ...body,+ filters: JSON.stringify(+ this.projectService.filterUnsupportedColumns(body.filters),+ ),+ })return _omit(createdProjectView, ['project'])}@@ -2017,6 +2021,7 @@ export class ProjectController {async getProjectViews(@Param() params: ProjectIdDto,@CurrentUserId() userId: string,+ @Headers() headers: { 'x-password'?: string },) {const project = await this.projectService.findProject(params.projectId, ['admin',@@ -2033,7 +2038,7 @@ export class ProjectController {throw new NotFoundException('User not found.')}- this.projectService.allowedToManage(project, userId, user.roles)+ this.projectService.allowedToView(project, userId, headers['x-password'])return this.projectsViewsRepository.findViews(params.projectId)}@@ -2075,7 +2080,12 @@ export class ProjectController {throw new NotFoundException('View not found.')}- await this.projectsViewsRepository.updateProjectView(params.viewId, body)+ await this.projectsViewsRepository.updateProjectView(params.viewId, {+ ...body,+ filters: JSON.stringify(+ this.projectService.filterUnsupportedColumns(body.filters),+ ),+ })return this.projectsViewsRepository.findView(params.viewId)}diff --git a/apps/production/src/project/project.service.ts b/apps/production/src/project/project.service.tsindex e0662c800..da98628f6 100644--- a/apps/production/src/project/project.service.ts+++ b/apps/production/src/project/project.service.ts@@ -20,6 +20,7 @@ import * as _map from 'lodash/map'import * as _pick from 'lodash/pick'import * as _trim from 'lodash/trim'import * as _findIndex from 'lodash/findIndex'+import * as _filter from 'lodash/filter'import * as _includes from 'lodash/includes'import * as _reduce from 'lodash/reduce'import * as dayjs from 'dayjs'@@ -52,6 +53,8 @@ import {redisUserUsageinfoCacheTimeout,TRAFFIC_COLUMNS,EMAIL_ACTION_ENCRYPTION_KEY,+ ALL_COLUMNS,+ TRAFFIC_METAKEY_COLUMNS,} from '../common/constants'import { clickhouse } from '../common/integrations/clickhouse'import { IUsageInfoRedis } from '../user/interfaces'@@ -68,6 +71,7 @@ import {import { ReportFrequency } from './enums'import { nFormatter } from '../common/utils'import { browserArgs } from '../og-image/og-image.service'+import { CreateProjectViewDto } from './dto/create-project-view.dto'dayjs.extend(utc)@@ -1345,4 +1349,19 @@ export class ProjectService {async findProject(id: string, relations: string[]) {return this.projectsRepository.findOne({ relations, where: { id } })}++ filterUnsupportedColumns(+ filters: CreateProjectViewDto['filters'],+ ): CreateProjectViewDto['filters'] {+ if (!filters) {+ return []+ }++ return _filter(+ filters,+ ({ column }) =>+ _includes(ALL_COLUMNS, column) ||+ _includes(TRAFFIC_METAKEY_COLUMNS, column),+ )+ }}diff --git a/apps/production/src/project/repositories/projects-views.repository.ts b/apps/production/src/project/repositories/projects-views.repository.tsindex 7dc7c85e1..bbfb44f64 100644--- a/apps/production/src/project/repositories/projects-views.repository.ts+++ b/apps/production/src/project/repositories/projects-views.repository.ts@@ -1,8 +1,9 @@import { Injectable } from '@nestjs/common'import { InjectRepository } from '@nestjs/typeorm'import { Repository } from 'typeorm'+import * as _map from 'lodash/map'import { ProjectViewEntity } from '../entity/project-view.entity'-import { CreateProjectViewDto } from '../dto/create-project-view.dto'+import { CreateProjectViewDto, Filter } from '../dto/create-project-view.dto'import { UpdateProjectViewDto } from '../dto/update-project-view.dto'import { ProjectViewCustomEventEntity } from '../entity/project-view-custom-event.entity'@@ -15,46 +16,83 @@ export class ProjectsViewsRepository {private projectViewCustomEventsRepository: Repository<ProjectViewCustomEventEntity>,) {}- async findViews(projectId: string) {- return this.viewsRepository.find({+ async findViews(projectId: string): Promise<(typeof this.processView)[]> {+ const views = await this.viewsRepository.find({where: { projectId },relations: ['customEvents'],})++ return _map(views, this.processView)}- async createProjectView(projectId: string, data: CreateProjectViewDto) {+ async createProjectView(+ projectId: string,+ data: Omit<CreateProjectViewDto, 'filters'> & { filters: string },+ ) {const view = await this.viewsRepository.save({project: { id: projectId },...data,- customEvents: data.customEvents,})- const customEventPromises = data.customEvents.map(customEvent =>- this.projectViewCustomEventsRepository.save({- viewId: view.id,- ...customEvent,- }),- )- await Promise.all(customEventPromises)+ if (data.customEvents) {+ const customEventPromises = data.customEvents.map(customEvent =>+ this.projectViewCustomEventsRepository.save({+ viewId: view.id,+ ...customEvent,+ }),+ )+ await Promise.all(customEventPromises)+ }return view}+ processView(+ view: ProjectViewEntity | undefined,+ ): (Omit<ProjectViewEntity, 'filters'> & { filters: Filter[] }) | undefined {+ if (!view) {+ return undefined+ }++ let filters = null++ if (view.filters) {+ try {+ filters = JSON.parse(view.filters)+ } catch (reason) {+ console.error('[ERROR] processView, failed to parse filters:', reason)+ console.error('VIEW:', view)+ }+ }++ return {+ ...view,+ filters,+ }+ }+async findView(id: string) {- return this.viewsRepository.findOne({- where: { id },- relations: ['customEvents'],- })+ return this.processView(+ await this.viewsRepository.findOne({+ where: { id },+ relations: ['customEvents'],+ }),+ )}async findProjectView(projectId: string, viewId: string) {- return this.viewsRepository.findOne({- where: { id: viewId, projectId },- relations: ['customEvents'],- })+ return this.processView(+ await this.viewsRepository.findOne({+ where: { id: viewId, projectId },+ relations: ['customEvents'],+ }),+ )}- async updateProjectView(id: string, data: UpdateProjectViewDto) {+ async updateProjectView(+ id: string,+ data: Omit<UpdateProjectViewDto, 'filters'> & { filters: string },+ ) {await this.viewsRepository.update({ id }, data)}diff --git a/migrations/mysql/2024_08_02.sql b/migrations/mysql/2024_08_02.sqlnew file mode 100644index 000000000..7045f91db--- /dev/null+++ b/migrations/mysql/2024_08_02.sql@@ -0,0 +1,13 @@+DROP TABLE projects_views;++CREATE TABLE `projects_views` (+ `id` varchar(36) NOT NULL,+ `projectId` varchar(255) NOT NULL,+ `name` varchar(255) NOT NULL,+ `type` enum('traffic','performance') NOT NULL,+ `createdAt` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),+ `updatedAt` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),+ `filters` varchar(255) NOT NULL,+ PRIMARY KEY (`id`),+ FOREIGN KEY (`projectId`) REFERENCES `project` (`id`) ON DELETE CASCADE+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;