Files
stats/apps/api/src/controllers/import.controller.ts
Carl-Gerhard Lindesvärd 32e91959f6 chore(root): migrate to biome
2024-09-18 23:46:11 +02:00

38 lines
1002 B
TypeScript

import type { FastifyReply, FastifyRequest } from 'fastify';
import { toDots } from '@openpanel/common';
import type { IClickhouseEvent } from '@openpanel/db';
import { TABLE_NAMES, ch, formatClickhouseDate } from '@openpanel/db';
export async function importEvents(
request: FastifyRequest<{
Body: IClickhouseEvent[];
}>,
reply: FastifyReply,
) {
const importedAt = formatClickhouseDate(new Date());
const values: IClickhouseEvent[] = request.body.map((event) => {
return {
...event,
properties: toDots(event.properties),
project_id: request.client?.projectId ?? '',
created_at: formatClickhouseDate(event.created_at),
imported_at: importedAt,
};
});
try {
const res = await ch.insert({
table: TABLE_NAMES.events,
values,
format: 'JSONEachRow',
});
console.log(res.summary?.written_rows, 'events imported');
reply.send('OK');
} catch (e) {
console.error(e);
reply.status(500).send('Error');
}
}