fix(buffer): ensure insert correct date format

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-11-30 22:48:07 +01:00
parent afff099c5b
commit 500cce2918
2 changed files with 14 additions and 3 deletions

View File

@@ -4,7 +4,12 @@ import { toDots } from '@openpanel/common';
import { getRedisCache } from '@openpanel/redis'; import { getRedisCache } from '@openpanel/redis';
import { escape } from 'sqlstring'; 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 { transformProfile } from '../services/profile.service';
import type { import type {
IClickhouseProfile, IClickhouseProfile,
@@ -71,7 +76,12 @@ export class ProfileBuffer extends RedisBuffer<BufferType> {
protected async insertIntoDB(items: BufferType[]): Promise<void> { protected async insertIntoDB(items: BufferType[]): Promise<void> {
await ch.insert({ await ch.insert({
table: TABLE_NAMES.profiles, table: TABLE_NAMES.profiles,
values: items, values: items.map((item) => ({
...item,
created_at: item.created_at
? formatClickhouseDate(item.created_at)
: '',
})),
format: 'JSONEachRow', format: 'JSONEachRow',
}); });
} }

View File

@@ -159,6 +159,7 @@ export function formatClickhouseDate(
} }
export function toDate(str: string, interval?: IInterval) { 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 (!interval || interval === 'minute' || interval === 'hour') {
if (str.match(/\d{4}-\d{2}-\d{2}/)) { if (str.match(/\d{4}-\d{2}-\d{2}/)) {
return escape(str); return escape(str);
@@ -168,7 +169,7 @@ export function toDate(str: string, interval?: IInterval) {
} }
if (str.match(/\d{4}-\d{2}-\d{2}/)) { if (str.match(/\d{4}-\d{2}-\d{2}/)) {
return `toDate(${escape(str)})`; return `toDate(${escape(str.split(' ')[0])})`;
} }
return `toDate(${str})`; return `toDate(${str})`;