feat: added cron job to remove healthcheck logs older than 12h

This commit is contained in:
charles-gauthereau
2026-03-25 21:11:09 +01:00
parent a9d6481193
commit 7a4af4e1e0
6 changed files with 74 additions and 20 deletions
+3 -17
View File
@@ -2,10 +2,11 @@
import {ActionError, userAction} from "@/lib/safe-actions/actions";
import {AgentSchema} from "@/features/agents/agents.schema";
import {z} from "zod";
import {eq, and, ne, count, gte} from "drizzle-orm";
import {eq, and, ne, count, gte, lt} from "drizzle-orm";
import {db} from "@/db";
import * as drizzleDb from "@/db";
import {slugify} from "@/utils/slugify";
import {getHealthLast12hLogs} from "@/db/services/healthcheck";
const verifySlugUniqueness = async (slug: string, agentId?: string) => {
const conditions = agentId ? and(eq(drizzleDb.schemas.agent.slug, slug), ne(drizzleDb.schemas.agent.id, agentId)) : eq(drizzleDb.schemas.agent.slug, slug);
@@ -57,22 +58,7 @@ export const getAgentAction = userAction.schema(z.string()).action(async ({parse
return {
data: agent,
health: agent ? await getLast12hLogs({ id: agent.id }) : []
health: agent ? await getHealthLast12hLogs({ id: agent.id }) : []
};
});
export async function getLast12hLogs({id}: { id: string }) {
const now = new Date()
const since = new Date(now.getTime() - 12 * 60 * 60 * 1000)
return db
.select()
.from(drizzleDb.schemas.healthcheckLog)
.where(
and(
eq(drizzleDb.schemas.healthcheckLog.objectId, id),
gte(drizzleDb.schemas.healthcheckLog.date, since)
)
)
}