api: add first version of export api

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-04-11 21:30:36 +02:00
committed by Carl-Gerhard Lindesvärd
parent 7ea95afe16
commit 7f8d857508
10 changed files with 232 additions and 7 deletions

View File

@@ -0,0 +1,12 @@
-- CreateEnum
CREATE TYPE "ClientType" AS ENUM ('read', 'write', 'root');
-- DropForeignKey
ALTER TABLE "clients" DROP CONSTRAINT "clients_projectId_fkey";
-- AlterTable
ALTER TABLE "clients" ADD COLUMN "type" "ClientType" NOT NULL DEFAULT 'read',
ALTER COLUMN "projectId" DROP NOT NULL;
-- AddForeignKey
ALTER TABLE "clients" ADD CONSTRAINT "clients_projectId_fkey" FOREIGN KEY ("projectId") REFERENCES "projects"("id") ON DELETE SET NULL ON UPDATE CASCADE;

View File

@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "clients" ALTER COLUMN "type" SET DEFAULT 'write';

View File

@@ -90,14 +90,21 @@ model Profile {
@@map("profiles")
}
enum ClientType {
read
write
root
}
model Client {
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
name String
secret String?
projectId String
project Project @relation(fields: [projectId], references: [id])
type ClientType @default(write)
projectId String?
project Project? @relation(fields: [projectId], references: [id])
organizationSlug String
cors String @default("*")
cors String @default("*")
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt

View File

@@ -248,6 +248,10 @@ export interface GetEventListOptions {
cursor?: number;
events?: string[] | null;
filters?: IChartEventFilter[];
startDate?: Date;
endDate?: Date;
meta?: boolean;
profile?: boolean;
}
export async function getEventList({
@@ -257,6 +261,10 @@ export async function getEventList({
profileId,
events,
filters,
startDate,
endDate,
meta = true,
profile = true,
}: GetEventListOptions) {
const { sb, getSql, join } = createSqlBuilder();
@@ -268,6 +276,10 @@ export async function getEventList({
sb.where.deviceId = `device_id IN (SELECT device_id as did FROM events WHERE profile_id = ${escape(profileId)} group by did)`;
}
if (startDate && endDate) {
sb.where.created_at = `created_at BETWEEN '${formatClickhouseDate(startDate)}' AND '${formatClickhouseDate(endDate)}'`;
}
if (events && events.length > 0) {
sb.where.events = `name IN (${join(
events.map((event) => escape(event)),
@@ -288,7 +300,7 @@ export async function getEventList({
sb.orderBy.created_at = 'created_at DESC';
return getEvents(getSql(), { profile: true, meta: true });
return getEvents(getSql(), { profile, meta });
}
export async function getEventsCount({
@@ -296,6 +308,8 @@ export async function getEventsCount({
profileId,
events,
filters,
startDate,
endDate,
}: Omit<GetEventListOptions, 'cursor' | 'take'>) {
const { sb, getSql, join } = createSqlBuilder();
sb.where.projectId = `project_id = ${escape(projectId)}`;
@@ -303,6 +317,10 @@ export async function getEventsCount({
sb.where.profileId = `profile_id = ${escape(profileId)}`;
}
if (startDate && endDate) {
sb.where.created_at = `created_at BETWEEN '${formatClickhouseDate(startDate)}' AND '${formatClickhouseDate(endDate)}'`;
}
if (events && events.length > 0) {
sb.where.events = `name IN (${join(
events.map((event) => escape(event)),