From e7f17dbe4d7064a034c205c931ff91395f21c80e Mon Sep 17 00:00:00 2001 From: Maxime Cannoodt Date: Mon, 8 Aug 2022 22:46:50 +0200 Subject: [PATCH] feat: :card_file_box: Add event table to schema, add EventLogger class to server --- .../20220808203936_event_table/migration.sql | 12 +++ server/prisma/schema.prisma | 12 +++ server/src/EventLogger.integration.test.ts | 79 +++++++++++++++++++ server/src/EventLogger.ts | 51 ++++++++++++ 4 files changed, 154 insertions(+) create mode 100644 server/prisma/migrations/20220808203936_event_table/migration.sql create mode 100644 server/src/EventLogger.integration.test.ts create mode 100644 server/src/EventLogger.ts diff --git a/server/prisma/migrations/20220808203936_event_table/migration.sql b/server/prisma/migrations/20220808203936_event_table/migration.sql new file mode 100644 index 0000000..dc3393b --- /dev/null +++ b/server/prisma/migrations/20220808203936_event_table/migration.sql @@ -0,0 +1,12 @@ +-- CreateTable +CREATE TABLE "event" ( + "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + "time" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + "type" TEXT NOT NULL, + "success" BOOLEAN NOT NULL, + "size_bytes" INTEGER, + "purge_count" INTEGER, + "host" TEXT, + "error" TEXT, + "expire_window_days" INTEGER +); diff --git a/server/prisma/schema.prisma b/server/prisma/schema.prisma index 6b26a4c..b3d73f2 100644 --- a/server/prisma/schema.prisma +++ b/server/prisma/schema.prisma @@ -17,3 +17,15 @@ model EncryptedNote { ciphertext String hmac String } + +model event { + id Int @id @default(autoincrement()) + time DateTime @default(now()) + type String + success Boolean + size_bytes Int? + purge_count Int? + host String? + error String? + expire_window_days Int? +} diff --git a/server/src/EventLogger.integration.test.ts b/server/src/EventLogger.integration.test.ts new file mode 100644 index 0000000..207bb8d --- /dev/null +++ b/server/src/EventLogger.integration.test.ts @@ -0,0 +1,79 @@ +import { describe, it, expect } from "vitest"; +import EventLogger, { EventType } from "./EventLogger"; +import prisma from "./client"; + +describe("Logging write events", () => { + it("Should write a write event to database", async () => { + const testWriteEvent = { + host: "localhost", + size_bytes: 100, + success: true, + expire_window_days: 30, + }; + + // Is event written successfully? + const logged = await EventLogger.writeEvent(testWriteEvent); + expect(logged).not.toBeNull(); + expect(logged).toMatchObject(testWriteEvent); + + // Is event in database? + const results = await prisma.event.findMany({ + where: { type: EventType.WRITE }, + }); + expect(results.length).toBe(1); + + // Are default fields populated? + expect(logged.time).not.toBeNull(); + expect(logged.id).not.toBeNull(); + }); + + it("Should log a read event to database", async () => { + const testReadEvent = { + host: "localhost", + size_bytes: 100, + success: true, + }; + + // Is event written successfully? + const logged = await EventLogger.readEvent({ + host: "localhost", + size_bytes: 100, + success: true, + }); + expect(logged).not.toBeNull(); + expect(logged).toMatchObject(testReadEvent); + + // Is event in database? + const results = await prisma.event.findMany({ + where: { type: EventType.READ }, + }); + expect(results.length).toBe(1); + + // Are default fields populated? + expect(logged.time).not.toBeNull(); + expect(logged.id).not.toBeNull(); + }); + + it("Should log a purge event to database", async () => { + const testPurgeEvent = { + success: true, + purge_count: 1, + size_bytes: 100, + }; + + // Is event written successfully? + const logged = await EventLogger.purgeEvent(testPurgeEvent); + expect(logged).not.toBeNull(); + expect(logged).toMatchObject(testPurgeEvent); + + // Is event in database? + const results = await prisma.event.findMany({ + where: { type: EventType.PURGE }, + }); + expect(results.length).toBe(1); + + // Are default fields populated? + expect(logged.time).not.toBeNull(); + expect(logged.id).not.toBeNull(); + }); +}); diff --git a/server/src/EventLogger.ts b/server/src/EventLogger.ts new file mode 100644 index 0000000..7a3b00d --- /dev/null +++ b/server/src/EventLogger.ts @@ -0,0 +1,51 @@ +import prisma from "./client"; +import { event } from "@prisma/client"; + +export enum EventType { + WRITE = "WRITE", + READ = "READ", + PURGE = "PURGE", +} + +interface Event { + success: boolean; + error?: string; +} + +interface ClientEvent extends Event { + host: string; + size_bytes: number; + success: boolean; +} + +interface WriteEvent extends ClientEvent { + expire_window_days: number; +} + +interface ReadEvent extends ClientEvent {} + +interface PurgeEvent extends Event { + success: boolean; + purge_count: number; + size_bytes: number; +} + +export default class EventLogger { + public static writeEvent(event: WriteEvent): Promise { + return prisma.event.create({ + data: { type: EventType.WRITE, ...event }, + }); + } + + public static readEvent(event: ReadEvent): Promise { + return prisma.event.create({ + data: { type: EventType.READ, ...event }, + }); + } + + public static purgeEvent(event: PurgeEvent): Promise { + return prisma.event.create({ + data: { type: EventType.PURGE, ...event }, + }); + } +}