From b57f1a234a5eaacd8ec115c396fc0fb84b841c66 Mon Sep 17 00:00:00 2001 From: Grant Date: Wed, 10 Jul 2024 22:10:54 -0600 Subject: [PATCH 1/4] initial version of the pixels.log implementation --- Dockerfile | 4 ++ packages/server/src/api/admin.ts | 25 +++++++++ packages/server/src/index.ts | 4 ++ packages/server/src/lib/Canvas.ts | 19 ++++++- packages/server/src/lib/LogMan.ts | 85 +++++++++++++++++++++++++++++++ packages/server/src/lib/Logger.ts | 17 +++++++ packages/server/src/types.ts | 2 + 7 files changed, 154 insertions(+), 2 deletions(-) create mode 100644 packages/server/src/lib/LogMan.ts diff --git a/Dockerfile b/Dockerfile index b470299..c814cb2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -107,9 +107,13 @@ ENV PORT 3000 ENV NODE_ENV production ENV SERVE_CLIENT /home/node/app/packages/client ENV SERVE_ADMIN /home/node/app/packages/admin +ENV PIXEL_LOG_PATH /home/node/app/pixel.log + +VOLUME /home/node/app/pixel.log EXPOSE 3000 # profiler port, only used if profiler is explicity running EXPOSE 9229 + ENTRYPOINT [ "/bin/sh" ] CMD [ "./docker-start.sh" ] \ No newline at end of file diff --git a/packages/server/src/api/admin.ts b/packages/server/src/api/admin.ts index 58503b8..a4030e3 100644 --- a/packages/server/src/api/admin.ts +++ b/packages/server/src/api/admin.ts @@ -11,6 +11,7 @@ import { InstanceNotFound, } from "../models/Instance"; import { AuditLog } from "../models/AuditLog"; +import { LogMan } from "../lib/LogMan"; const app = Router(); const Logger = getLogger("HTTP/ADMIN"); @@ -50,6 +51,25 @@ app.get("/check", (req, res) => { res.send({ success: true }); }); +// TODO: Delete before merge +app.get("/log", (req, res) => { + const user = "grant@grants.cafe"; + + for (let i = 0; i < 100; i++) { + LogMan.log("pixel_place", user, { x: 0, y: 0, hex: "ABC123" }); + LogMan.log("pixel_undo", user, { x: 0, y: 0, hex: "FFFFFF" }); + LogMan.log("mod_fill", user, { from: [0, 0], to: [1, 1], hex: "000000" }); + LogMan.log("mod_override", user, { x: 0, y: 0, hex: "111111" }); + LogMan.log("mod_rollback", user, { x: 0, y: 0, hex: "222222" }); + LogMan.log("mod_rollback_undo", user, { x: 0, y: 0, hex: "333333" }); + LogMan.log("canvas_size", { width: 100, height: 100 }); + LogMan.log("canvas_freeze", {}); + LogMan.log("canvas_unfreeze", {}); + } + + res.send("ok"); +}); + app.get("/canvas/size", async (req, res) => { const config = Canvas.getCanvasConfig(); @@ -86,6 +106,11 @@ app.post("/canvas/size", async (req, res) => { } await Canvas.setSize(width, height); + + // we log this here because Canvas#setSize is ran at launch + // this is currently the only way the size is changed is via the API + LogMan.log("canvas_size", { width, height }); + const user = (await User.fromAuthSession(req.session.user!))!; const auditLog = AuditLog.Factory(user.sub) .doing("CANVAS_SIZE") diff --git a/packages/server/src/index.ts b/packages/server/src/index.ts index 0a354d3..7a9fcdd 100644 --- a/packages/server/src/index.ts +++ b/packages/server/src/index.ts @@ -81,6 +81,10 @@ if (!process.env.INHIBIT_LOGIN) { } } +if (!process.env.PIXEL_LOG_PATH) { + Logger.warn("PIXEL_LOG_PATH is not defined, defaulting to packages/server"); +} + // run startup tasks, all of these need to be completed to serve Promise.all([ Redis.getClient(), diff --git a/packages/server/src/lib/Canvas.ts b/packages/server/src/lib/Canvas.ts index 6f7246b..132a8b2 100644 --- a/packages/server/src/lib/Canvas.ts +++ b/packages/server/src/lib/Canvas.ts @@ -5,6 +5,7 @@ import { SocketServer } from "./SocketServer"; import { getLogger } from "./Logger"; import { Pixel } from "@prisma/client"; import { CanvasWorker } from "../workers/worker"; +import { LogMan } from "./LogMan"; const Logger = getLogger("CANVAS"); @@ -144,9 +145,14 @@ class Canvas { }, }); - const coveringPixel = ( + const coveringPixel: Pixel | undefined = ( await prisma.pixel.findMany({ - where: { x: pixel.x, y: pixel.y, createdAt: { lt: pixel.createdAt } }, + where: { + x: pixel.x, + y: pixel.y, + createdAt: { lt: pixel.createdAt }, + deletedAt: null, // undone pixels will have this set + }, orderBy: { createdAt: "desc" }, take: 1, }) @@ -160,6 +166,12 @@ class Canvas { }, }); } + + LogMan.log("pixel_undo", pixel.userId, { + x: pixel.x, + y: pixel.y, + hex: coveringPixel?.color, + }); } /** @@ -312,6 +324,8 @@ class Canvas { hex, })) ); + + LogMan.log("mod_fill", user.sub, { from: start, to: end, hex }); } async setPixel( @@ -350,6 +364,7 @@ class Canvas { await this.updateCanvasRedisAtPos(x, y); Logger.info(`${user.sub} placed pixel at (${x}, ${y})`); + LogMan.log("pixel_place", user.sub, { x, y, hex }); } /** diff --git a/packages/server/src/lib/LogMan.ts b/packages/server/src/lib/LogMan.ts new file mode 100644 index 0000000..3bfec0f --- /dev/null +++ b/packages/server/src/lib/LogMan.ts @@ -0,0 +1,85 @@ +import { PixelLogger } from "./Logger"; + +interface UserEvents { + pixel_place: { x: number; y: number; hex: string }; + pixel_undo: { x: number; y: number; hex?: string }; + mod_fill: { + from: [x: number, y: number]; + to: [x: number, y: number]; + hex: string; + }; + mod_override: { x: number; y: number; hex: string }; + mod_rollback: { x: number; y: number; hex?: string }; + mod_rollback_undo: { x: number; y: number; hex?: string }; +} + +interface SystemEvents { + canvas_size: { width: number; height: number }; + canvas_freeze: {}; + canvas_unfreeze: {}; +} + +/** + * Handle logs that should be written to a text file + * + * This could be used as an EventEmitter in the future, but as of right now + * it just adds typing to logging of these events + * + * TODO: better name, this one is not it + * + * @see #57 + */ +class LogMan_ { + log( + event: EventName, + data: SystemEvents[EventName] + ): void; + log( + event: EventName, + user: string, + data: UserEvents[EventName] + ): void; + log( + event: EventName, + ...params: EventName extends keyof UserEvents + ? [user: string, data: UserEvents[EventName]] + : EventName extends keyof SystemEvents + ? [data: SystemEvents[EventName]] + : never + ): void { + let parts: string[] = []; + + if (params.length === 2) { + // user event + let user = params[0] as string; + parts.push(user, event); + + if (event === "mod_fill") { + // this event format has a different line format + let data: UserEvents["mod_fill"] = params[1] as any; + + parts.push(data.from.join(","), data.to.join(","), data.hex); + } else { + let data: UserEvents[Exclude] = + params[1] as any; + parts.push(...[data.x, data.y, data.hex || "unset"].map((a) => a + "")); + } + } else { + // system event + + parts.push("system", event); + + switch (event) { + case "canvas_size": + let data: SystemEvents["canvas_size"] = params[0] as any; + let { width, height } = data; + parts.push(width + "", height + ""); + break; + } + } + + PixelLogger.info(parts.join("\t")); + } +} + +export const LogMan = new LogMan_(); diff --git a/packages/server/src/lib/Logger.ts b/packages/server/src/lib/Logger.ts index f7e5651..0399b21 100644 --- a/packages/server/src/lib/Logger.ts +++ b/packages/server/src/lib/Logger.ts @@ -1,6 +1,11 @@ import winston, { format } from "winston"; +import path from "node:path"; import { createEnum } from "./utils"; +// if PIXEL_LOG_PATH is defined, use that, otherwise default to packages/server root +const PIXEL_LOG_PATH = + process.env.PIXEL_LOG_PATH || path.join(__dirname, "..", "..", "pixels.log"); + const formatter = format.printf((options) => { let maxModuleWidth = 0; for (const module of Object.values(LoggerType)) { @@ -26,6 +31,18 @@ const Winston = winston.createLogger({ transports: [new winston.transports.Console()], }); +// Used by LogMan for writing to pixels.log +export const PixelLogger = winston.createLogger({ + format: format.printf((options) => { + return [new Date().toISOString(), options.message].join("\t"); + }), + transports: [ + new winston.transports.File({ + filename: PIXEL_LOG_PATH, + }), + ], +}); + export const LoggerType = createEnum([ "MAIN", "SETTINGS", diff --git a/packages/server/src/types.ts b/packages/server/src/types.ts index 4347b15..5e03170 100644 --- a/packages/server/src/types.ts +++ b/packages/server/src/types.ts @@ -60,6 +60,8 @@ declare global { MATRIX_HOMESERVER: string; ELEMENT_HOST: string; MATRIX_GENERAL_ALIAS: string; + + PIXEL_LOG_PATH?: string; } } } -- GitLab From 833da3181b7edc865bc044f59a27b0000befdee7 Mon Sep 17 00:00:00 2001 From: Grant Date: Thu, 11 Jul 2024 12:13:45 -0600 Subject: [PATCH 2/4] add canvas freeze triggers --- packages/server/src/api/admin.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/server/src/api/admin.ts b/packages/server/src/api/admin.ts index c3e2b7a..6abe848 100644 --- a/packages/server/src/api/admin.ts +++ b/packages/server/src/api/admin.ts @@ -136,6 +136,9 @@ app.get("/canvas/freeze", async (req, res) => { app.post("/canvas/freeze", async (req, res) => { await Canvas.setFrozen(true); + // same reason as canvas size changes, we log this here because #setFrozen is ran at startup + LogMan.log("canvas_freeze", {}); + const user = (await User.fromAuthSession(req.session.user!))!; const auditLog = AuditLog.Factory(user.sub) .doing("CANVAS_FREEZE") @@ -154,6 +157,9 @@ app.post("/canvas/freeze", async (req, res) => { app.delete("/canvas/freeze", async (req, res) => { await Canvas.setFrozen(false); + // same reason as canvas size changes, we log this here because #setFrozen is ran at startup + LogMan.log("canvas_unfreeze", {}); + const user = (await User.fromAuthSession(req.session.user!))!; const auditLog = AuditLog.Factory(user.sub) .doing("CANVAS_UNFREEZE") -- GitLab From 45b1ccf43957d28a02d2a73b0c78421f05b00c3b Mon Sep 17 00:00:00 2001 From: Grant Date: Thu, 11 Jul 2024 16:05:22 -0600 Subject: [PATCH 3/4] add mod override --- packages/server/src/lib/Canvas.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/server/src/lib/Canvas.ts b/packages/server/src/lib/Canvas.ts index 391e982..e09a116 100644 --- a/packages/server/src/lib/Canvas.ts +++ b/packages/server/src/lib/Canvas.ts @@ -395,7 +395,11 @@ class Canvas { await this.updateCanvasRedisAtPos(x, y); Logger.info(`${user.sub} placed pixel at (${x}, ${y})`); - LogMan.log("pixel_place", user.sub, { x, y, hex }); + LogMan.log(isModAction ? "mod_override" : "pixel_place", user.sub, { + x, + y, + hex, + }); } /** -- GitLab From a89dcf5e819ac2115a50814b04ec8e66fd88bf15 Mon Sep 17 00:00:00 2001 From: Grant Date: Thu, 11 Jul 2024 21:25:25 -0600 Subject: [PATCH 4/4] add area undo as a logged action --- packages/server/src/api/admin.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/server/src/api/admin.ts b/packages/server/src/api/admin.ts index ccf4889..eb101a4 100644 --- a/packages/server/src/api/admin.ts +++ b/packages/server/src/api/admin.ts @@ -303,6 +303,13 @@ app.put("/canvas/undo", async (req, res) => { ? paletteColors.find((p) => p.hex === coveredPixel.color)?.id || -1 : -1, }); + + // TODO: this spams the log, it would be nicer if it combined + LogMan.log("mod_rollback", user_sub, { + x: pixel.pixel.x, + y: pixel.pixel.y, + hex: coveredPixel?.color, + }); break; } case "rejected": -- GitLab