Skip to content

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

fix(buffer): ensure insert correct date format

Carl-Gerhard Lindesvärd500cce2918d2
diff --git a/packages/db/src/buffers/profile-buffer.ts b/packages/db/src/buffers/profile-buffer.ts
index 67b04b778..6aa8d494a 100644
--- a/packages/db/src/buffers/profile-buffer.ts
+++ b/packages/db/src/buffers/profile-buffer.ts
@@ -4,7 +4,12 @@ import { toDots } from '@openpanel/common';
import { getRedisCache } from '@openpanel/redis';
import { escape } from 'sqlstring';
-import { TABLE_NAMES, ch, chQuery } from '../clickhouse-client';
+import {
+ TABLE_NAMES,
+ ch,
+ chQuery,
+ formatClickhouseDate,
+} from '../clickhouse-client';
import { transformProfile } from '../services/profile.service';
import type {
IClickhouseProfile,
@@ -71,7 +76,12 @@ export class ProfileBuffer extends RedisBuffer<BufferType> {
protected async insertIntoDB(items: BufferType[]): Promise<void> {
await ch.insert({
table: TABLE_NAMES.profiles,
- values: items,
+ values: items.map((item) => ({
+ ...item,
+ created_at: item.created_at
+ ? formatClickhouseDate(item.created_at)
+ : '',
+ })),
format: 'JSONEachRow',
});
}
diff --git a/packages/db/src/clickhouse-client.ts b/packages/db/src/clickhouse-client.ts
index d45f5b711..8178ada29 100644
--- a/packages/db/src/clickhouse-client.ts
+++ b/packages/db/src/clickhouse-client.ts
@@ -159,6 +159,7 @@ export function formatClickhouseDate(
}
export function toDate(str: string, interval?: IInterval) {
+ // If it does not match the regex it's a column name eg 'created_at'
if (!interval || interval === 'minute' || interval === 'hour') {
if (str.match(/\d{4}-\d{2}-\d{2}/)) {
return escape(str);
@@ -168,7 +169,7 @@ export function toDate(str: string, interval?: IInterval) {
}
if (str.match(/\d{4}-\d{2}-\d{2}/)) {
- return `toDate(${escape(str)})`;
+ return `toDate(${escape(str.split(' ')[0])})`;
}
return `toDate(${str})`;