feat(root): added migrations and optimized profile table

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-09-10 10:08:26 +02:00
committed by Carl-Gerhard Lindesvärd
parent 2258fed24a
commit b44f1958a2
22 changed files with 280 additions and 169 deletions

View File

@@ -9,13 +9,12 @@ export const TABLE_NAMES = {
profiles: 'profiles',
alias: 'profile_aliases',
self_hosting: 'self_hosting',
events_bots: 'events_bots',
dau_mv: 'dau_mv',
};
export const originalCh = createClient({
url: process.env.CLICKHOUSE_URL,
username: process.env.CLICKHOUSE_USER,
password: process.env.CLICKHOUSE_PASSWORD,
database: process.env.CLICKHOUSE_DB,
max_open_connections: 30,
request_timeout: 30000,
keep_alive: {

View File

@@ -225,23 +225,24 @@ export async function getEvents(
options: GetEventsOptions = {}
): Promise<IServiceEvent[]> {
const events = await chQuery<IClickhouseEvent>(sql);
if (options.profile) {
const projectId = events[0]?.project_id;
if (options.profile && projectId) {
const ids = events.map((e) => e.profile_id);
const profiles = await getProfiles(ids);
const profiles = await getProfiles(ids, projectId);
for (const event of events) {
event.profile = profiles.find((p) => p.id === event.profile_id);
}
}
if (options.meta) {
if (options.meta && projectId) {
const names = uniq(events.map((e) => e.name));
const metas = await db.eventMeta.findMany({
where: {
name: {
in: names,
},
projectId: events[0]?.project_id,
projectId,
},
select: options.meta === true ? undefined : options.meta,
});

View File

@@ -69,7 +69,7 @@ interface GetProfileListOptions {
search?: string;
}
export async function getProfiles(ids: string[]) {
export async function getProfiles(ids: string[], projectId: string) {
const filteredIds = uniq(ids.filter((id) => id !== ''));
if (filteredIds.length === 0) {
@@ -78,8 +78,10 @@ export async function getProfiles(ids: string[]) {
const data = await chQuery<IClickhouseProfile>(
`SELECT id, first_name, last_name, email, avatar, is_external
FROM profiles FINAL
WHERE id IN (${filteredIds.map((id) => escape(id)).join(',')})
FROM ${TABLE_NAMES.profiles} FINAL
WHERE
project_id = ${escape(projectId)} AND
id IN (${filteredIds.map((id) => escape(id)).join(',')})
`
);
@@ -94,18 +96,14 @@ export async function getProfileList({
search,
}: GetProfileListOptions) {
const { sb, getSql } = createSqlBuilder();
sb.from = 'profiles FINAL';
sb.from = `${TABLE_NAMES.profiles} FINAL`;
sb.select.all = '*';
sb.where.project_id = `project_id = ${escape(projectId)}`;
sb.limit = take;
sb.offset = Math.max(0, (cursor ?? 0) * take);
sb.orderBy.created_at = 'created_at DESC';
if (search) {
if (search.includes('@')) {
sb.where.email = `email ILIKE '%${search}%'`;
} else {
sb.where.first_name = `first_name ILIKE '%${search}%' OR last_name ILIKE '%${search}%'`;
}
sb.where.search = `(email ILIKE '%${search}%' OR first_name ILIKE '%${search}%' OR last_name ILIKE '%${search}%')`;
}
const data = await chQuery<IClickhouseProfile>(getSql());
return data.map(transformProfile);

View File

@@ -121,7 +121,7 @@ export function getRollingActiveUsers({
FROM
(
SELECT *
FROM dau_mv
FROM ${TABLE_NAMES.dau_mv}
WHERE project_id = ${escape(projectId)}
)
ARRAY JOIN range(${days}) AS n