OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
improve queries with limit
Carl-Gerhard Lindesvärd8025859b6d51
diff --git a/packages/db/src/clickhouse-client.ts b/packages/db/src/clickhouse-client.tsindex b01656dce..edae6867d 100644--- a/packages/db/src/clickhouse-client.ts+++ b/packages/db/src/clickhouse-client.ts@@ -56,6 +56,11 @@ export const ch = new Proxy(originalCh, {throw retryError; // Rethrow or handle as needed}} else {+ if (args[0].query) {+ console.log('FAILED QUERY:');+ console.log(args[0].query);+ }+// Handle other errors or rethrow themthrow error;}diff --git a/packages/db/src/services/chart.service.ts b/packages/db/src/services/chart.service.tsindex a2a9ebf44..630ada492 100644--- a/packages/db/src/services/chart.service.ts+++ b/packages/db/src/services/chart.service.ts@@ -16,6 +16,8 @@ export function getChartSql({startDate,endDate,projectId,+ chartType,+ limit,}: IGetChartDataInput) {const { sb, join, getWhere, getFrom, getSelect, getOrderBy, getGroupBy } =createSqlBuilder();@@ -59,6 +61,17 @@ export function getChartSql({sb.where.endDate = `created_at <= '${formatClickhouseDate(endDate)}'`;}+ if (breakdowns.length > 0 && limit) {+ sb.where.bar = `(${breakdowns.map((b) => b.name).join(',')}) IN (+ SELECT ${breakdowns.map((b) => b.name).join(',')}+ FROM ${TABLE_NAMES.events}+ ${getWhere()}+ GROUP BY ${breakdowns.map((b) => b.name).join(',')}+ ORDER BY count(*) DESC+ LIMIT ${limit}+ )`;+ }+breakdowns.forEach((breakdown, index) => {const key = `label_${index}`;const value = breakdown.name.startsWith('properties.')