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!

(improvement) Return a flag to indicate if there is data for a specific project (#216)

Andrii Romasiun8edbd5d2a6f8
diff --git a/apps/production/src/project/project.controller.ts b/apps/production/src/project/project.controller.ts
index d0d490030..1f0ad52d2 100644
--- a/apps/production/src/project/project.controller.ts
+++ b/apps/production/src/project/project.controller.ts
@@ -1752,10 +1752,15 @@ export class ProjectController {
this.projectService.allowedToView(project, uid, headers['x-password'])
+ const isDataExists = !_isEmpty(
+ await this.projectService.getPIDsWhereAnalyticsDataExists([id]),
+ )
+
return {
..._omit(project, ['admin', 'passwordHash', 'share']),
isOwner: uid === project.admin?.id,
isLocked: !!project.admin?.dashboardBlockReason,
+ isDataExists,
}
}
}
diff --git a/apps/production/src/project/project.service.ts b/apps/production/src/project/project.service.ts
index bef945821..827ee2d6f 100644
--- a/apps/production/src/project/project.service.ts
+++ b/apps/production/src/project/project.service.ts
@@ -20,6 +20,7 @@ import * as _pick from 'lodash/pick'
import * as _trim from 'lodash/trim'
import * as _findIndex from 'lodash/findIndex'
import * as _includes from 'lodash/includes'
+import * as _reduce from 'lodash/reduce'
import * as dayjs from 'dayjs'
import * as utc from 'dayjs/plugin/utc'
import { compareSync } from 'bcrypt'
@@ -822,6 +823,34 @@ export class ProjectService {
})
}
+ async getPIDsWhereAnalyticsDataExists(projectIds: string[]) {
+ const params = _reduce(
+ projectIds,
+ (acc, curr, index) => ({
+ ...acc,
+ [`pid_${index}`]: curr,
+ }),
+ {},
+ )
+
+ 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);
+ `
+
+ const result: any = await clickhouse.query(query, { params }).toPromise()
+
+ return _map(result, ({ pid }) => pid)
+ }
+
async getSubscriberByEmail(projectId: string, email: string) {
return this.projectSubscriberRepository.findOne({
where: { projectId, email },
diff --git a/apps/selfhosted/src/project/project.controller.ts b/apps/selfhosted/src/project/project.controller.ts
index ae449dee0..26b418b2b 100644
--- a/apps/selfhosted/src/project/project.controller.ts
+++ b/apps/selfhosted/src/project/project.controller.ts
@@ -288,6 +288,13 @@ export class ProjectController {
this.projectService.allowedToView(project, uid)
- return this.projectService.formatFromClickhouse(project)
+ const isDataExists = !_isEmpty(
+ await this.projectService.getPIDsWhereAnalyticsDataExists([id]),
+ )
+
+ return this.projectService.formatFromClickhouse({
+ ...project,
+ isDataExists,
+ })
}
}
diff --git a/apps/selfhosted/src/project/project.service.ts b/apps/selfhosted/src/project/project.service.ts
index 2530d564a..c815faf6a 100644
--- a/apps/selfhosted/src/project/project.service.ts
+++ b/apps/selfhosted/src/project/project.service.ts
@@ -17,6 +17,7 @@ import * as _map from 'lodash/map'
import * as _isNull from 'lodash/isNull'
import * as _split from 'lodash/split'
import * as _trim from 'lodash/trim'
+import * as _reduce from 'lodash/reduce'
import { Project } from './entity/project.entity'
import { ProjectDTO } from './dto/project.dto'
@@ -98,6 +99,34 @@ export class ProjectService {
}
}
+ async getPIDsWhereAnalyticsDataExists(projectIds: string[]) {
+ const params = _reduce(
+ projectIds,
+ (acc, curr, index) => ({
+ ...acc,
+ [`pid_${index}`]: curr,
+ }),
+ {},
+ )
+
+ 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);
+ `
+
+ const result: any = await clickhouse.query(query, { params }).toPromise()
+
+ return _map(result, ({ pid }) => pid)
+ }
+
async removeDataFromClickhouse(
pid: string,
from: string,