fix: memory issues when properties are to big

This commit is contained in:
Carl-Gerhard Lindesvärd
2026-02-12 20:35:21 +00:00
parent f1f932c58b
commit 2389b351e4

View File

@@ -246,18 +246,16 @@ export const chartRouter = createTRPCRouter({
.from(TABLE_NAMES.profiles) .from(TABLE_NAMES.profiles)
.where('project_id', '=', projectId) .where('project_id', '=', projectId)
.where('is_external', '=', true) .where('is_external', '=', true)
.orderBy('created_at', 'DESC') .limit(10_000)
.limit(10000)
.execute(); .execute();
const profileProperties: string[] = []; const profileProperties = [
for (const p of profiles) { ...new Set(
for (const property of Object.keys(p.properties)) { profiles.flatMap((p) =>
if (!profileProperties.includes(`profile.properties.${property}`)) { Object.keys(p.properties).map((k) => `profile.properties.${k}`),
profileProperties.push(`profile.properties.${property}`); ),
} ),
} ];
}
const query = clix(ch) const query = clix(ch)
.select<{ property_key: string; created_at: string }>([ .select<{ property_key: string; created_at: string }>([
@@ -267,7 +265,9 @@ export const chartRouter = createTRPCRouter({
.from(TABLE_NAMES.event_property_values_mv) .from(TABLE_NAMES.event_property_values_mv)
.where('project_id', '=', projectId) .where('project_id', '=', projectId)
.groupBy(['property_key']) .groupBy(['property_key'])
.orderBy('created_at', 'DESC'); .orderBy('length(property_key)', 'ASC')
.orderBy('created_at', 'DESC')
.limit(10_000);
if (event && event !== '*') { if (event && event !== '*') {
query.where('name', '=', event); query.where('name', '=', event);
@@ -275,17 +275,14 @@ export const chartRouter = createTRPCRouter({
const res = await query.execute(); const res = await query.execute();
const properties = res const eventProperties = res.map((item) => {
.map((item) => item.property_key) const key = item.property_key
.map((item) => item.replace(/\.([0-9]+)\./g, '.*.')) .replace(/\.([0-9]+)\./g, '.*.')
.map((item) => item.replace(/\.([0-9]+)/g, '[*]')) .replace(/\.([0-9]+)/g, '[*]');
.map((item) => `properties.${item}`); return `properties.${key}`;
});
if (event === '*' || !event) { const fixedProperties = [
properties.push('name');
}
properties.push(
'duration', 'duration',
'revenue', 'revenue',
'has_profile', 'has_profile',
@@ -308,8 +305,14 @@ export const chartRouter = createTRPCRouter({
'profile.first_name', 'profile.first_name',
'profile.last_name', 'profile.last_name',
'profile.email', 'profile.email',
];
const properties = [
...eventProperties,
...(event === '*' || !event ? ['name'] : []),
...fixedProperties,
...profileProperties, ...profileProperties,
); ];
return pipe( return pipe(
sort<string>((a, b) => a.length - b.length), sort<string>((a, b) => a.length - b.length),