mirror of
https://github.com/fosrl/pangolin.git
synced 2026-03-02 08:46:38 +00:00
✨load logs per day
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { db, requestAuditLog, resources } from "@server/db";
|
import { db, requestAuditLog, driver } from "@server/db";
|
||||||
import { registry } from "@server/openApi";
|
import { registry } from "@server/openApi";
|
||||||
import { NextFunction } from "express";
|
import { NextFunction } from "express";
|
||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
@@ -95,11 +95,41 @@ async function query(query: Q) {
|
|||||||
.groupBy(requestAuditLog.location)
|
.groupBy(requestAuditLog.location)
|
||||||
.orderBy(desc(totalQ));
|
.orderBy(desc(totalQ));
|
||||||
|
|
||||||
|
const groupByDayFunction =
|
||||||
|
driver === "pg"
|
||||||
|
? sql<string>`DATE_TRUNC('day', TO_TIMESTAMP(${requestAuditLog.timestamp}))`.as(
|
||||||
|
"day"
|
||||||
|
)
|
||||||
|
: sql<string>`DATE(${requestAuditLog.timestamp}, 'unixepoch')`.as(
|
||||||
|
"day"
|
||||||
|
);
|
||||||
|
|
||||||
|
const booleanTrue = driver === "pg" ? sql`true` : sql`1`;
|
||||||
|
const booleanFalse = driver === "pg" ? sql`false` : sql`0`;
|
||||||
|
|
||||||
|
const requestsPerDay = await db
|
||||||
|
.select({
|
||||||
|
day: groupByDayFunction,
|
||||||
|
allowedCount:
|
||||||
|
sql<number>`SUM(CASE WHEN ${requestAuditLog.action} = ${booleanTrue} THEN 1 ELSE 0 END)`.as(
|
||||||
|
"allowed_count"
|
||||||
|
),
|
||||||
|
blockedCount:
|
||||||
|
sql<number>`SUM(CASE WHEN ${requestAuditLog.action} = ${booleanFalse} THEN 1 ELSE 0 END)`.as(
|
||||||
|
"blocked_count"
|
||||||
|
),
|
||||||
|
totalCount: sql<number>`COUNT(*)`.as("total_count")
|
||||||
|
})
|
||||||
|
.from(requestAuditLog)
|
||||||
|
.groupBy(groupByDayFunction)
|
||||||
|
.orderBy(groupByDayFunction);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
requestsPerCountry: requestsPerCountry as Array<{
|
requestsPerCountry: requestsPerCountry as Array<{
|
||||||
code: string;
|
code: string;
|
||||||
count: number;
|
count: number;
|
||||||
}>,
|
}>,
|
||||||
|
requestsPerDay,
|
||||||
totalBlocked: blocked.total,
|
totalBlocked: blocked.total,
|
||||||
totalRequests: all.total
|
totalRequests: all.total
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user