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!

added an ability to resetFilters by multiple filters

Blue Mouse5066b03c9ebd
diff --git a/apps/production/src/analytics/analytics.service.ts b/apps/production/src/analytics/analytics.service.ts
index 7b0a67985..49f28f817 100644
--- a/apps/production/src/analytics/analytics.service.ts
+++ b/apps/production/src/analytics/analytics.service.ts
@@ -39,6 +39,7 @@ import {
UNIQUE_SESSION_LIFE_TIME,
clickhouse,
REDIS_SESSION_SALT_KEY,
+ TRAFFIC_COLUMNS,
} from '../common/constants'
import {
calculateRelativePercentage,
@@ -84,8 +85,6 @@ const GMT_0_TIMEZONES = [
// 'Africa/Casablanca',
]
-const cols = ['cc', 'pg', 'lc', 'br', 'os', 'dv', 'ref', 'so', 'me', 'ca']
-
const captchaColumns = ['cc', 'br', 'os', 'dv']
const perfColumns = ['cc', 'pg', 'dv', 'br']
@@ -372,7 +371,7 @@ export class AnalyticsService {
getDataTypeColumns(dataType: DataType): string[] {
if (dataType === DataType.ANALYTICS) {
- return cols
+ return TRAFFIC_COLUMNS
}
if (dataType === DataType.PERFORMANCE) {
@@ -789,7 +788,7 @@ export class AnalyticsService {
}
async getFilters(pid: string, type: string): Promise<Array<string>> {
- if (!_includes(cols, type)) {
+ if (!_includes(TRAFFIC_COLUMNS, type)) {
throw new UnprocessableEntityException(
`The provided type (${type}) is incorrect`,
)
@@ -824,7 +823,7 @@ export class AnalyticsService {
),
)
- let columns = cols
+ let columns = TRAFFIC_COLUMNS
if (isCaptcha) {
columns = captchaColumns
diff --git a/apps/production/src/common/constants.ts b/apps/production/src/common/constants.ts
index 8ac44fb89..015fbc49a 100644
--- a/apps/production/src/common/constants.ts
+++ b/apps/production/src/common/constants.ts
@@ -103,6 +103,19 @@ const CAPTCHA_COOKIE_KEY = 'swetrix-captcha-token'
const CAPTCHA_TOKEN_LIFETIME = 300 // seconds (5 minutes).
const CAPTCHA_SECRET_KEY_LENGTH = 50
+const TRAFFIC_COLUMNS = [
+ 'cc',
+ 'pg',
+ 'lc',
+ 'br',
+ 'os',
+ 'dv',
+ 'ref',
+ 'so',
+ 'me',
+ 'ca',
+]
+
export {
clickhouse,
redis,
@@ -140,4 +153,5 @@ export {
JWT_ACCESS_TOKEN_SECRET,
getRedisUserUsageInfoKey,
redisUserUsageinfoCacheTimeout,
+ TRAFFIC_COLUMNS,
}
diff --git a/apps/production/src/project/project.controller.ts b/apps/production/src/project/project.controller.ts
index 58a99ca18..3312e45bd 100644
--- a/apps/production/src/project/project.controller.ts
+++ b/apps/production/src/project/project.controller.ts
@@ -634,11 +634,7 @@ export class ProjectController {
const filters = JSON.parse(rawFilters)
- await Promise.all([
- ..._map(filters, async (filter: string) => {
- this.projectService.deleteByFilter(pid, type, filter)
- }),
- ])
+ await this.projectService.deleteByFilters(pid, type, filters)
}
@Post('/:pid/share')
diff --git a/apps/production/src/project/project.service.ts b/apps/production/src/project/project.service.ts
index c812a5031..adddbcbc2 100644
--- a/apps/production/src/project/project.service.ts
+++ b/apps/production/src/project/project.service.ts
@@ -49,6 +49,7 @@ import {
PRODUCTION_ORIGIN,
getRedisUserUsageInfoKey,
redisUserUsageinfoCacheTimeout,
+ TRAFFIC_COLUMNS,
} from '../common/constants'
import { IUsageInfoRedis } from '../user/interfaces'
import { ProjectSubscriber } from './entity'
@@ -517,18 +518,39 @@ export class ProjectService {
}
}
- async deleteByFilter(
+ async deleteByFilters(
pid: string,
type: string,
- filter: string,
+ filters: string,
): Promise<void> {
- const query = `DELETE FROM analytics WHERE pid={pid:FixedString(12)} AND {type:String}={filter:String};`
+ if (!type || _isEmpty(filters)) {
+ return
+ }
+
+ if (!_includes(TRAFFIC_COLUMNS, type)) {
+ throw new UnprocessableEntityException(
+ `The provided type (${type}) is incorrect`,
+ )
+ }
+
+ let query = `ALTER TABLE analytics DELETE WHERE pid={pid:FixedString(12)} AND (`
+
const params = {
pid,
- type,
- filter,
}
+ for (let i = 0; i < _size(filters); ++i) {
+ if (i > 0) {
+ query += ' OR '
+ }
+
+ const key = `filter_${i}`
+ query += `${type}={${key}:String}`
+ params[key] = filters[i]
+ }
+
+ query += ')'
+
await clickhouse.query(query, { params }).toPromise()
}