From 500cce2918d2a8895c9052a55afd3607262cf674 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carl-Gerhard=20Lindesva=CC=88rd?= Date: Sat, 30 Nov 2024 22:48:07 +0100 Subject: [PATCH] fix(buffer): ensure insert correct date format --- packages/db/src/buffers/profile-buffer.ts | 14 ++++++++++++-- packages/db/src/clickhouse-client.ts | 3 ++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/packages/db/src/buffers/profile-buffer.ts b/packages/db/src/buffers/profile-buffer.ts index 67b04b77..6aa8d494 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 { protected async insertIntoDB(items: BufferType[]): Promise { 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 d45f5b71..8178ada2 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})`;