improvements while testing

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-08-07 00:23:04 +02:00
committed by Carl-Gerhard Lindesvärd
parent 03cee826ff
commit 41e46570b7
10 changed files with 81 additions and 40 deletions

View File

@@ -60,6 +60,15 @@ CREATE TABLE IF NOT EXISTS openpanel.profiles (
ORDER BY
(id) SETTINGS index_granularity = 8192;
CREATE TABLE IF NOT EXISTS openpanel.profile_aliases (
`project_id` String,
`profile_id` String,
`alias` String,
`created_at` DateTime
) ENGINE = MergeTree
ORDER BY
(project_id, profile_id, alias) SETTINGS index_granularity = 8192;
--- Materialized views (DAU)
CREATE MATERIALIZED VIEW IF NOT EXISTS dau_mv ENGINE = AggregatingMergeTree() PARTITION BY toYYYYMMDD(date)
ORDER BY

View File

@@ -111,10 +111,13 @@ export async function chQuery<T extends Record<string, any>>(
return (await chQueryWithMeta<T>(query)).data;
}
export function formatClickhouseDate(_date: Date | string, skipTime = false) {
export function formatClickhouseDate(
_date: Date | string,
skipTime = false
): string {
const date = typeof _date === 'string' ? new Date(_date) : _date;
if (skipTime) {
return date.toISOString().split('T')[0];
return date.toISOString().split('T')[0]!;
}
return date.toISOString().replace('T', ' ').replace(/Z+$/, '');
}

View File

@@ -1,6 +1,7 @@
import { escape } from 'sqlstring';
import { toObject } from '@openpanel/common';
import { cacheable } from '@openpanel/redis';
import type { IChartEventFilter } from '@openpanel/validation';
import { profileBuffer } from '../buffers';
@@ -191,3 +192,31 @@ export async function upsertProfile({
is_external: isExternal,
});
}
export async function getProfileId({
profileId,
projectId,
}: {
profileId: string | undefined;
projectId: string;
}) {
if (!profileId) {
return '';
}
const res = await chQuery<{
alias: string;
profile_id: string;
project_id: string;
}>(
`SELECT * FROM ${TABLE_NAMES.alias} WHERE project_id = '${projectId}' AND (alias = '${profileId}' OR profile_id = '${profileId}')`
);
if (res[0]) {
return res[0].profile_id;
}
return profileId;
}
export const getProfileIdCached = cacheable(getProfileId, 60 * 30);

View File

@@ -2,7 +2,7 @@ import { getRedisCache } from './redis';
export function cacheable<T extends (...args: any) => any>(
fn: T,
expire: number
expireInSec: number
) {
return async function (
...args: Parameters<T>
@@ -20,7 +20,7 @@ export function cacheable<T extends (...args: any) => any>(
const result = await fn(...(args as any));
if (result !== undefined || result !== null) {
getRedisCache().setex(key, expire, JSON.stringify(result));
getRedisCache().setex(key, expireInSec, JSON.stringify(result));
}
return result;

View File

@@ -67,7 +67,7 @@ export type OpenPanelOptions = {
sdkVersion?: string;
waitForProfile?: boolean;
filter?: (payload: TrackHandlerPayload) => boolean;
disable?: boolean;
disabled?: boolean;
};
export class OpenPanel {
@@ -106,7 +106,7 @@ export class OpenPanel {
}
async send(payload: TrackHandlerPayload) {
if (this.options.disable) {
if (this.options.disabled) {
return Promise.resolve();
}