fix: logs api should with both API and access keys

This commit is contained in:
Harshith Mullapudi 2025-09-22 19:57:11 +05:30
parent 169a2713d2
commit b912080815
2 changed files with 134 additions and 127 deletions

View File

@ -47,7 +47,7 @@ export function useLogs({ endpoint, source, status, type }: UseLogsOptions) {
(pageNum: number) => { (pageNum: number) => {
const params = new URLSearchParams(); const params = new URLSearchParams();
params.set("page", pageNum.toString()); params.set("page", pageNum.toString());
params.set("limit", "5"); params.set("limit", "50");
if (source) params.set("source", source); if (source) params.set("source", source);
if (status) params.set("status", status); if (status) params.set("status", status);
if (type) params.set("type", type); if (type) params.set("type", type);

View File

@ -1,29 +1,35 @@
import { type LoaderFunctionArgs, json } from "@remix-run/node"; import { type LoaderFunctionArgs, json } from "@remix-run/node";
import { z } from "zod";
import { prisma } from "~/db.server"; import { prisma } from "~/db.server";
import { requireUserId } from "~/services/session.server"; import { createHybridLoaderApiRoute } from "~/services/routeBuilders/apiBuilder.server";
/** // Schema for logs search parameters
* Optimizations: const LogsSearchParams = z.object({
* - Use `findMany` with `select` instead of `include` to fetch only required fields. page: z.string().optional(),
* - Use `count` with the same where clause, but only after fetching logs (to avoid unnecessary count if no logs). limit: z.string().optional(),
* - Use a single query for availableSources with minimal fields. source: z.string().optional(),
* - Avoid unnecessary object spreading and type casting. status: z.string().optional(),
* - Minimize nested object traversal in mapping. type: z.string().optional(),
*/ });
export async function loader({ request }: LoaderFunctionArgs) {
const userId = await requireUserId(request);
const url = new URL(request.url);
const page = parseInt(url.searchParams.get("page") || "1"); export const loader = createHybridLoaderApiRoute(
const limit = parseInt(url.searchParams.get("limit") || "100"); {
const source = url.searchParams.get("source"); allowJWT: true,
const status = url.searchParams.get("status"); searchParams: LogsSearchParams,
const type = url.searchParams.get("type"); corsStrategy: "all",
findResource: async () => 1,
},
async ({ authentication, searchParams }) => {
const page = parseInt(searchParams.page || "1");
const limit = parseInt(searchParams.limit || "100");
const source = searchParams.source;
const status = searchParams.status;
const type = searchParams.type;
const skip = (page - 1) * limit; const skip = (page - 1) * limit;
// Get user and workspace in one query // Get user and workspace in one query
const user = await prisma.user.findUnique({ const user = await prisma.user.findUnique({
where: { id: userId }, where: { id: authentication.userId },
select: { Workspace: { select: { id: true } } }, select: { Workspace: { select: { id: true } } },
}); });
@ -147,4 +153,5 @@ export async function loader({ request }: LoaderFunctionArgs) {
hasMore: skip + logs.length < totalCount, hasMore: skip + logs.length < totalCount,
availableSources, availableSources,
}); });
} },
);