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) => {
const params = new URLSearchParams();
params.set("page", pageNum.toString());
params.set("limit", "5");
params.set("limit", "50");
if (source) params.set("source", source);
if (status) params.set("status", status);
if (type) params.set("type", type);

View File

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