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!

fixed cyclic dependencies in user flow

Blue Mouse67a92ca2b205
diff --git a/src/analytics/analytics.service.ts b/src/analytics/analytics.service.ts
index d4422816c..fc25b8eaf 100644
--- a/src/analytics/analytics.service.ts
+++ b/src/analytics/analytics.service.ts
@@ -58,6 +58,7 @@ import {
IUserFlowNode,
IUserFlowLink,
IUserFlow,
+ IBuildUserFlow,
IGenerateXAxis,
IExtractChartData,
} from './interfaces'
@@ -438,6 +439,31 @@ export class AnalyticsService {
}
}
+ removeCyclicDependencies(links: IUserFlowLink[]): IUserFlowLink[] {
+ const visited = new Set<string>()
+
+ return links.filter((link) => {
+ const key = `${link.source}_${link.target}`
+ if (visited.has(key)) {
+ return false
+ }
+ visited.add(key)
+ return true
+ })
+ }
+
+ buildUserFlow(links: IUserFlowLink[]): IBuildUserFlow {
+ const nodes: IUserFlowNode[] = Array.from(
+ new Set(
+ links
+ .map((link: IUserFlowLink) => link.source)
+ .concat(links.map((link: IUserFlowLink) => link.target)),
+ ),
+ ).map((node: any) => ({ id: node }))
+
+ return { nodes, links }
+ }
+
async getUserFlow(params: unknown): Promise<IUserFlow> {
const query = `
SELECT
@@ -454,26 +480,36 @@ export class AnalyticsService {
prev
`
- const results = await clickhouse.query(query, { params }).toPromise()
+ const results = <IUserFlowLink[]>(await clickhouse.query(query, { params }).toPromise())
if (_isEmpty(results)) {
- return { nodes: [], links: [] }
+ const empty = { nodes: [], links: [] }
+ return {
+ ascending: empty,
+ descending: empty,
+ }
}
- const nodes: IUserFlowNode[] = Array.from(
- new Set(
- results
- .map((row: any) => row.source)
- .concat(results.map((row: any) => row.target)),
- ),
- ).map((node: any) => ({ id: node }))
- const links: IUserFlowLink[] = results.map((row: any) => ({
- source: row.source,
- target: row.target,
- value: row.value,
- }))
+ const ascendingLinks: IUserFlowLink[] = []
+ const descendingLinks: IUserFlowLink[] = []
- return { nodes, links }
+ this.removeCyclicDependencies(results).forEach((row: any) => {
+ const link: IUserFlowLink = {
+ source: row.source,
+ target: row.target,
+ value: row.value,
+ }
+ if (link.source < link.target) {
+ ascendingLinks.push(link)
+ } else {
+ descendingLinks.push(link)
+ }
+ })
+
+ return {
+ ascending: this.buildUserFlow(ascendingLinks),
+ descending: this.buildUserFlow(descendingLinks),
+ }
}
async getSessionHash(
diff --git a/src/analytics/interfaces/index.ts b/src/analytics/interfaces/index.ts
index 0ed9efc3c..4ce435bdf 100644
--- a/src/analytics/interfaces/index.ts
+++ b/src/analytics/interfaces/index.ts
@@ -34,11 +34,16 @@ export interface IUserFlowLink {
value: number
}
-export interface IUserFlow {
+export interface IBuildUserFlow {
nodes: IUserFlowNode[]
links: IUserFlowLink[]
}
+export interface IUserFlow {
+ ascending: IBuildUserFlow
+ descending: IBuildUserFlow
+}
+
export interface IGenerateXAxis {
x: string[]
xM: string[]