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!

refactored the query

Blue Mousedfdae7135b11
diff --git a/apps/production/src/project/project.controller.ts b/apps/production/src/project/project.controller.ts
index 0d5772485..99d3c2897 100644
--- a/apps/production/src/project/project.controller.ts
+++ b/apps/production/src/project/project.controller.ts
@@ -167,16 +167,16 @@ export class ProjectController {
this.userService.findOne(userId),
])
- // const pidsWithData =
- // await this.projectService.getPIDsWhereAnalyticsDataExists(
- // _map(paginated.results, ({ id }) => id),
- // )
+ const pidsWithData =
+ await this.projectService.getPIDsWhereAnalyticsDataExists(
+ _map(paginated.results, ({ id }) => id),
+ )
paginated.results = _map(paginated.results, p => ({
...p,
isOwner: true,
isLocked: !!user?.dashboardBlockReason,
- isDataExists: true, // _includes(pidsWithData, p?.id),
+ isDataExists: _includes(pidsWithData, p?.id),
}))
return {
@@ -1758,15 +1758,15 @@ export class ProjectController {
this.projectService.allowedToView(project, uid, headers['x-password'])
- // const isDataExists = !_isEmpty(
- // await this.projectService.getPIDsWhereAnalyticsDataExists([id]),
- // )
+ const isDataExists = !_isEmpty(
+ await this.projectService.getPIDsWhereAnalyticsDataExists([id]),
+ )
return {
..._omit(project, ['admin', 'passwordHash', 'share']),
isOwner: uid === project.admin?.id,
isLocked: !!project.admin?.dashboardBlockReason,
- isDataExists: true,
+ isDataExists,
}
}
}
diff --git a/apps/production/src/project/project.service.ts b/apps/production/src/project/project.service.ts
index 827ee2d6f..ba4bbed81 100644
--- a/apps/production/src/project/project.service.ts
+++ b/apps/production/src/project/project.service.ts
@@ -824,6 +824,10 @@ export class ProjectService {
}
async getPIDsWhereAnalyticsDataExists(projectIds: string[]) {
+ if (_isEmpty(projectIds)) {
+ return {}
+ }
+
const params = _reduce(
projectIds,
(acc, curr, index) => ({
@@ -833,17 +837,42 @@ export class ProjectService {
{},
)
+ const pids = _join(
+ _map(params, (val, key) => `{${key}:FixedString(12)}`),
+ ',',
+ )
+
const query = `
SELECT
- COALESCE(a.pid, c.pid) AS pid,
- COUNT(COALESCE(a.pid, c.pid)) AS rowCount
- FROM analytics a
- FULL OUTER JOIN customEV c ON a.pid = c.pid
- WHERE COALESCE(a.pid, c.pid) IN (${_join(
- _map(params, (val, key) => `{${key}:FixedString(12)}`),
- ',',
- )})
- GROUP BY COALESCE(a.pid, c.pid);
+ pid,
+ CASE
+ WHEN EXISTS (
+ SELECT 1
+ FROM analytics
+ WHERE pid IN (${pids})
+ )
+ OR EXISTS (
+ SELECT 1
+ FROM customEV
+ WHERE pid IN (${pids})
+ )
+ THEN 1
+ ELSE 0
+ END AS exists
+ FROM
+ (
+ SELECT DISTINCT pid
+ FROM
+ (
+ SELECT pid
+ FROM analytics
+ WHERE pid IN (${pids})
+ UNION ALL
+ SELECT pid
+ FROM customEV
+ WHERE pid IN (${pids})
+ ) AS t
+ );
`
const result: any = await clickhouse.query(query, { params }).toPromise()
diff --git a/apps/selfhosted/src/project/project.service.ts b/apps/selfhosted/src/project/project.service.ts
index c815faf6a..3f078f9c6 100644
--- a/apps/selfhosted/src/project/project.service.ts
+++ b/apps/selfhosted/src/project/project.service.ts
@@ -100,6 +100,10 @@ export class ProjectService {
}
async getPIDsWhereAnalyticsDataExists(projectIds: string[]) {
+ if (_isEmpty(projectIds)) {
+ return {}
+ }
+
const params = _reduce(
projectIds,
(acc, curr, index) => ({
@@ -109,17 +113,42 @@ export class ProjectService {
{},
)
+ const pids = _join(
+ _map(params, (val, key) => `{${key}:FixedString(12)}`),
+ ',',
+ )
+
const query = `
SELECT
- COALESCE(a.pid, c.pid) AS pid,
- COUNT(COALESCE(a.pid, c.pid)) AS rowCount
- FROM analytics a
- FULL OUTER JOIN customEV c ON a.pid = c.pid
- WHERE COALESCE(a.pid, c.pid) IN (${_join(
- _map(params, (val, key) => `{${key}:FixedString(12)}`),
- ',',
- )})
- GROUP BY COALESCE(a.pid, c.pid);
+ pid,
+ CASE
+ WHEN EXISTS (
+ SELECT 1
+ FROM analytics
+ WHERE pid IN (${pids})
+ )
+ OR EXISTS (
+ SELECT 1
+ FROM customEV
+ WHERE pid IN (${pids})
+ )
+ THEN 1
+ ELSE 0
+ END AS exists
+ FROM
+ (
+ SELECT DISTINCT pid
+ FROM
+ (
+ SELECT pid
+ FROM analytics
+ WHERE pid IN (${pids})
+ UNION ALL
+ SELECT pid
+ FROM customEV
+ WHERE pid IN (${pids})
+ ) AS t
+ );
`
const result: any = await clickhouse.query(query, { params }).toPromise()