Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

fix(api): sessions without a known country no longer break the page

Carl-Gerhard Lindesvärd7f9d3623e4ca
diff --git a/packages/core/src/modules/chart/src/engine/fetch.ts b/packages/core/src/modules/chart/src/engine/fetch.ts
index 7693a3148..d841ba1a9 100644
--- a/packages/core/src/modules/chart/src/engine/fetch.ts
+++ b/packages/core/src/modules/chart/src/engine/fetch.ts
@@ -1,4 +1,5 @@
import type { ServiceDeps } from '../../../../services';
+import { stripFixedStringPadding } from '../../../../shared/ch-fixed-string';
import type { ISerieDataItem } from '../../../../shared/group-by-labels';
import { groupByLabels } from '../../../../shared/group-by-labels';
import type {
@@ -68,6 +69,35 @@ function breakdownValues(
return values;
}
+const BREAKDOWN_LABEL_KEYS = ['label_1', 'label_2', 'label_3'] as const;
+
+/**
+ * A `WITH FILL` row carries each label column's type default: `\0\0` for the
+ * `FixedString(2)` country, the epoch for a date. Grouped as-is those became a
+ * zero series named after the default. A real row always has `label_0` (the
+ * event name or `*`), so a fill row keeps only its date and count, which still
+ * pads every series to the full range. A real row without geo also comes back
+ * NUL-padded; it reads as an empty value, like any other unset breakdown.
+ */
+function readableLabels(row: ISerieDataItem): ISerieDataItem {
+ if (!row.label_0) {
+ return {
+ label_0: '',
+ date: row.date,
+ count: row.count,
+ total_count: row.total_count,
+ };
+ }
+ const labels: Partial<ISerieDataItem> = {};
+ for (const key of BREAKDOWN_LABEL_KEYS) {
+ const value = row[key];
+ if (typeof value === 'string') {
+ labels[key] = stripFixedStringPadding(value);
+ }
+ }
+ return { ...row, ...labels };
+}
+
/**
* One ConcreteSeries per label group. `name[0]` is the event name, `name[1+]`
* the breakdown values the row was grouped on.
@@ -87,7 +117,7 @@ function expandGroupedRows({
definitionIndex: number;
seriesId: (nameParts: string[]) => string;
}): ConcreteSeries[] {
- return groupByLabels(rows).map((grouped) => {
+ return groupByLabels(rows.map(readableLabels)).map((grouped) => {
const hasBreakdownParts = breakdowns.length > 0 && grouped.name.length > 1;
const breakdownValue = hasBreakdownParts
? grouped.name.slice(1).join(' - ')
diff --git a/packages/core/src/modules/session/session.service.ts b/packages/core/src/modules/session/session.service.ts
index 27647ce11..8234fc99a 100644
--- a/packages/core/src/modules/session/session.service.ts
+++ b/packages/core/src/modules/session/session.service.ts
@@ -12,6 +12,7 @@ import { cacheablePerDeps } from '../../cacheable-per-deps';
import { chQuery } from '../../ch-query';
import type { ServiceDeps, Services } from '../../services';
import { toRangeBoundaryLiteral } from '../../shared/ch-dates';
+import { stripFixedStringPadding } from '../../shared/ch-fixed-string';
import { buildFilterWhere } from '../chart/src/table-filter-where';
import type { IServiceProfile } from '../profile/profile.service';
import type { IChartEventFilter } from '../report/report.constants';
@@ -154,6 +155,12 @@ const QUERY_SESSIONS_DEFAULT_LIMIT = 20;
// clix keep sending it so their result sets stay identical.
const CLIX_SESSION_TIMEZONE = { session_timezone: 'UTC' } as const;
+export function withReadableCountry<Row extends { country: string }>(
+ row: Row
+): Row {
+ return { ...row, country: stripFixedStringPadding(row.country) };
+}
+
export function transformSession(session: IClickhouseSession): IServiceSession {
return {
id: session.id,
@@ -176,7 +183,7 @@ export function transformSession(session: IClickhouseSession): IServiceSession {
device: session.device,
brand: session.brand,
model: session.model,
- country: session.country,
+ country: stripFixedStringPadding(session.country),
region: session.region,
city: session.city,
longitude: session.longitude,
@@ -455,7 +462,7 @@ export async function querySessionsCore(
const from = toRangeBoundaryLiteral(startDate, 'start');
const to = toRangeBoundaryLiteral(endDate, 'end');
- return chQuery<IClickhouseSession>(
+ const rows = await chQuery<IClickhouseSession>(
deps,
querySessionsQuery({
...input,
@@ -473,6 +480,7 @@ export async function querySessionsCore(
}),
CLIX_SESSION_TIMEZONE
);
+ return rows.map(withReadableCountry);
}
export function createSessionService(