| gio | 40c0c99 | 2025-07-02 13:18:05 +0000 | [diff] [blame^] | 1 | import { Prisma, PrismaClient } from "@prisma/client"; |
| 2 | import { LogItem } from "./project_monitor"; |
| 3 | |
| 4 | type LogRecord = LogItem & { |
| 5 | id: number; |
| 6 | }; |
| 7 | |
| 8 | class LogStore { |
| 9 | constructor(private prisma: PrismaClient) {} |
| 10 | |
| 11 | async store(projectId: number, serviceName: string, workerId: string, logs: LogItem[]) { |
| 12 | await this.prisma.log.createMany({ |
| 13 | data: logs.map((log) => ({ |
| 14 | projectId, |
| 15 | serviceName, |
| 16 | workerId, |
| 17 | runId: log.runId, |
| 18 | commit: log.commit ?? undefined, |
| 19 | contents: log.contents, |
| 20 | timestampMilli: log.timestampMilli, |
| 21 | })), |
| 22 | }); |
| 23 | } |
| 24 | |
| 25 | async get( |
| 26 | projectId: number, |
| 27 | serviceName: string, |
| 28 | workerId: string, |
| 29 | afterId?: number, |
| 30 | numRecords?: number, |
| 31 | ): Promise<LogRecord[]> { |
| 32 | const where: Prisma.LogWhereInput = { projectId, serviceName, workerId }; |
| 33 | if (afterId) { |
| 34 | where.id = { gt: afterId }; |
| 35 | } |
| 36 | const logs = await this.prisma.log.findMany({ |
| 37 | where, |
| 38 | orderBy: { timestampMilli: "asc" }, |
| 39 | take: numRecords ?? 100, |
| 40 | }); |
| 41 | return logs.map((log) => ({ |
| 42 | id: log.id, |
| 43 | timestampMilli: Number(log.timestampMilli), |
| 44 | contents: log.contents, |
| 45 | runId: log.runId, |
| 46 | commit: log.commit ?? undefined, |
| 47 | })); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | export default LogStore; |