diff --git a/server/routers/auditLogs/queryRequestAnalytics.ts b/server/routers/auditLogs/queryRequestAnalytics.ts index b5b27b40..75bb6901 100644 --- a/server/routers/auditLogs/queryRequestAnalytics.ts +++ b/server/routers/auditLogs/queryRequestAnalytics.ts @@ -95,70 +95,6 @@ async function query(query: Q) { return { requestsPerCountry, totalBlocked, totalRequests }; } -// function getWhere(data: Q) { -// return and( -// gt(requestAuditLog.timestamp, data.timeStart), -// lt(requestAuditLog.timestamp, data.timeEnd), -// eq(requestAuditLog.orgId, data.orgId), -// data.resourceId -// ? eq(requestAuditLog.resourceId, data.resourceId) -// : undefined, -// data.actor ? eq(requestAuditLog.actor, data.actor) : undefined, -// data.method ? eq(requestAuditLog.method, data.method) : undefined, -// data.reason ? eq(requestAuditLog.reason, data.reason) : undefined, -// data.host ? eq(requestAuditLog.host, data.host) : undefined, -// data.location ? eq(requestAuditLog.location, data.location) : undefined, -// data.path ? eq(requestAuditLog.path, data.path) : undefined, -// data.action !== undefined -// ? eq(requestAuditLog.action, data.action) -// : undefined -// ); -// } - -// function queryRequest(data: Q) { -// return db -// .select({ -// id: requestAuditLog.id, -// timestamp: requestAuditLog.timestamp, -// orgId: requestAuditLog.orgId, -// action: requestAuditLog.action, -// reason: requestAuditLog.reason, -// actorType: requestAuditLog.actorType, -// actor: requestAuditLog.actor, -// actorId: requestAuditLog.actorId, -// resourceId: requestAuditLog.resourceId, -// ip: requestAuditLog.ip, -// location: requestAuditLog.location, -// userAgent: requestAuditLog.userAgent, -// metadata: requestAuditLog.metadata, -// headers: requestAuditLog.headers, -// query: requestAuditLog.query, -// originalRequestURL: requestAuditLog.originalRequestURL, -// scheme: requestAuditLog.scheme, -// host: requestAuditLog.host, -// path: requestAuditLog.path, -// method: requestAuditLog.method, -// tls: requestAuditLog.tls, -// resourceName: resources.name, -// resourceNiceId: resources.niceId -// }) -// .from(requestAuditLog) -// .leftJoin( -// resources, -// eq(requestAuditLog.resourceId, resources.resourceId) -// ) // TODO: Is this efficient? -// .where(getWhere(data)) -// .orderBy(desc(requestAuditLog.timestamp), desc(requestAuditLog.id)); -// } - -// function countRequestQuery(data: Q) { -// const countQuery = db -// .select({ count: count() }) -// .from(requestAuditLog) -// .where(getWhere(data)); -// return countQuery; -// } - registry.registerPath({ method: "get", path: "/org/{orgId}/logs/analytics", @@ -171,81 +107,6 @@ registry.registerPath({ responses: {} }); -// async function queryUniqueFilterAttributes( -// timeStart: number, -// timeEnd: number, -// orgId: string -// ) { -// const baseConditions = and( -// gt(requestAuditLog.timestamp, timeStart), -// lt(requestAuditLog.timestamp, timeEnd), -// eq(requestAuditLog.orgId, orgId) -// ); - -// // Get unique actors -// const uniqueActors = await db -// .selectDistinct({ -// actor: requestAuditLog.actor -// }) -// .from(requestAuditLog) -// .where(baseConditions); - -// // Get unique locations -// const uniqueLocations = await db -// .selectDistinct({ -// locations: requestAuditLog.location -// }) -// .from(requestAuditLog) -// .where(baseConditions); - -// // Get unique actors -// const uniqueHosts = await db -// .selectDistinct({ -// hosts: requestAuditLog.host -// }) -// .from(requestAuditLog) -// .where(baseConditions); - -// // Get unique actors -// const uniquePaths = await db -// .selectDistinct({ -// paths: requestAuditLog.path -// }) -// .from(requestAuditLog) -// .where(baseConditions); - -// // Get unique resources with names -// const uniqueResources = await db -// .selectDistinct({ -// id: requestAuditLog.resourceId, -// name: resources.name -// }) -// .from(requestAuditLog) -// .leftJoin( -// resources, -// eq(requestAuditLog.resourceId, resources.resourceId) -// ) -// .where(baseConditions); - -// return { -// actors: uniqueActors -// .map((row) => row.actor) -// .filter((actor): actor is string => actor !== null), -// resources: uniqueResources.filter( -// (row): row is { id: number; name: string | null } => row.id !== null -// ), -// locations: uniqueLocations -// .map((row) => row.locations) -// .filter((location): location is string => location !== null), -// hosts: uniqueHosts -// .map((row) => row.hosts) -// .filter((host): host is string => host !== null), -// paths: uniquePaths -// .map((row) => row.paths) -// .filter((path): path is string => path !== null) -// }; -// } - export type QueryRequestAnalyticsResponse = Awaited>; export async function queryRequestAnalytics( @@ -282,7 +143,7 @@ export async function queryRequestAnalytics( data, success: true, error: false, - message: "Action audit logs retrieved successfully", + message: "Request audit analytics retrieved successfully", status: HttpCode.OK }); } catch (error) { diff --git a/server/routers/auditLogs/queryRequestAuditLog.ts b/server/routers/auditLogs/queryRequestAuditLog.ts index 8c9aa902..6c56c186 100644 --- a/server/routers/auditLogs/queryRequestAuditLog.ts +++ b/server/routers/auditLogs/queryRequestAuditLog.ts @@ -31,7 +31,8 @@ export const queryAccessAuditLogsQuery = z.object({ .openapi({ type: "string", format: "date-time", - description: "End time as ISO date string (defaults to current time)" + description: + "End time as ISO date string (defaults to current time)" }), action: z .union([z.boolean(), z.string()]) @@ -72,8 +73,9 @@ export const queryRequestAuditLogsParams = z.object({ orgId: z.string() }); -export const queryRequestAuditLogsCombined = - queryAccessAuditLogsQuery.merge(queryRequestAuditLogsParams); +export const queryRequestAuditLogsCombined = queryAccessAuditLogsQuery.merge( + queryRequestAuditLogsParams +); type Q = z.infer; function getWhere(data: Q) { @@ -209,11 +211,21 @@ async function queryUniqueFilterAttributes( .where(baseConditions); return { - actors: uniqueActors.map(row => row.actor).filter((actor): actor is string => actor !== null), - resources: uniqueResources.filter((row): row is { id: number; name: string | null } => row.id !== null), - locations: uniqueLocations.map(row => row.locations).filter((location): location is string => location !== null), - hosts: uniqueHosts.map(row => row.hosts).filter((host): host is string => host !== null), - paths: uniquePaths.map(row => row.paths).filter((path): path is string => path !== null) + actors: uniqueActors + .map((row) => row.actor) + .filter((actor): actor is string => actor !== null), + resources: uniqueResources.filter( + (row): row is { id: number; name: string | null } => row.id !== null + ), + locations: uniqueLocations + .map((row) => row.locations) + .filter((location): location is string => location !== null), + hosts: uniqueHosts + .map((row) => row.hosts) + .filter((host): host is string => host !== null), + paths: uniquePaths + .map((row) => row.paths) + .filter((path): path is string => path !== null) }; } @@ -270,7 +282,7 @@ export async function queryRequestAuditLogs( }, success: true, error: false, - message: "Action audit logs retrieved successfully", + message: "Request audit logs retrieved successfully", status: HttpCode.OK }); } catch (error) {