Skip to content

Commit

Permalink
feat: deleting logs
Browse files Browse the repository at this point in the history
  • Loading branch information
SeanCassiere committed Jun 23, 2024
1 parent d755e7e commit 3c0388e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/types/hono.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import type { HttpBindings } from "@hono/node-server";
import type { services as servicesTable } from "@/config/db/schema";

type Service = typeof servicesTable.$inferSelect;

type Bindings = HttpBindings & {};

type Variables = {
service: Service | null;
};

export type ServerContext = {
Bindings: {};
Bindings: Bindings;
Variables: Variables;
};
52 changes: 48 additions & 4 deletions src/v2/logging/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { Hono } from "hono";
import { and, eq, lt } from "drizzle-orm";

import { parseSearchParams, serviceValidation } from "@/utils/server-helpers";
import { db } from "@/config/db";
import { env } from "@/config/env";
import { logs as logsTable } from "@/config/db/schema";
import { parseSearchParams, serviceValidation } from "@/utils/server-helpers";
import { ENDPOINT_MESSAGES } from "@/utils/messages";
import { createDbId } from "@/utils/db";
import type { ServerContext } from "@/types/hono";

import { createLogOutputSchema, createLogSchema, getLogsFiltersSchema, getLogsOutputSchema } from "./schemas";
import { env } from "@/config/env";
import { logs as logsTable } from "@/config/db/schema";
import { createDbId } from "@/utils/db";

const app = new Hono<ServerContext>();

Expand Down Expand Up @@ -101,4 +102,47 @@ app.post("/", serviceValidation, async (c) => {
return c.json(createLogOutputSchema.parse(log[0]));
});

/**
* @public
* Cleans log for a service for a specific number of months
*/
app.delete("/", serviceValidation, async (c) => {
if (env.FREEZE_DB_WRITES) {
c.status(503);
return c.json({ success: false, message: ENDPOINT_MESSAGES.DBWritesFrozen });
}

const service = c.var.service!;

const countMonths = Number(env.DEFAULT_NUM_OF_MONTHS_TO_DELETE);

const ip = c.req.header("x-forwarded-for") ?? null;

const date = new Date();
date.setMonth(date.getMonth() - countMonths);

await db
.delete(logsTable)
.where(and(eq(logsTable.isPersisted, false), lt(logsTable.createdAt, date.toISOString())))
.execute();

const logId = createDbId("log");
await db
.insert(logsTable)
.values({
id: logId,
serviceId: service.id,
isPersisted: true,
action: "app-admin-clean-service-logs",
ip,
environment: "production",
lookupFilterValue: "app-admin-action",
data: { client: service.name, ip },
level: "info",
})
.execute();

return c.json({ success: true, message: `Successfully cleaned logs for the last ${countMonths} months` });
});

export default app;

0 comments on commit 3c0388e

Please sign in to comment.