Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ export function useKeysOverviewLogsQuery({ apiId, limit = 50 }: UseLogsQueryPara
isLoading: isLoadingInitial,
} = trpc.api.keys.query.useInfiniteQuery(queryParams, {
getNextPageParam: (lastPage) => lastPage.nextCursor,
initialCursor: { requestId: null, time: null },
staleTime: Number.POSITIVE_INFINITY,
refetchOnMount: false,
refetchOnWindowFocus: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@ export const keysQueryOverviewLogsPayload = z.object({
endTime: z.number().int(),
apiId: z.string(),
since: z.string(),
cursor: z
.object({
requestId: z.string().nullable(),
time: z.number().nullable(),
})
.optional()
.nullable(),
cursor: z.number().nullable().optional().nullable(),
outcomes: z
.array(
z.object({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ export function useLogsQuery({
isLoading: isLoadingInitial,
} = trpc.logs.queryLogs.useInfiniteQuery(queryParams, {
getNextPageParam: (lastPage) => lastPage.nextCursor,
initialCursor: { requestId: null, time: null },
staleTime: Number.POSITIVE_INFINITY,
refetchOnMount: false,
refetchOnWindowFocus: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,5 @@ export const queryLogsPayload = z.object({
),
})
.nullable(),
cursor: z
.object({
requestId: z.string().nullable(),
time: z.number().nullable(),
})
.optional()
.nullable(),
cursor: z.number().nullable().optional().nullable(),
});
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ export function useRatelimitOverviewLogsQuery({ namespaceId, limit = 50 }: UseLo
isLoading: isLoadingInitial,
} = trpc.ratelimit.overview.logs.query.useInfiniteQuery(queryParams, {
getNextPageParam: (lastPage) => lastPage.nextCursor,
initialCursor: { requestId: null, time: null },
staleTime: Number.POSITIVE_INFINITY,
refetchOnMount: false,
refetchOnWindowFocus: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,7 @@ export const ratelimitQueryOverviewLogsPayload = z.object({
),
})
.nullable(),
cursor: z
.object({
requestId: z.string().nullable(),
time: z.number().nullable(),
})
.optional()
.nullable(),
cursor: z.number().nullable().optional().nullable(),
sorts: z
.array(
z.object({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ export function useRatelimitLogsQuery({
isLoading: isLoadingInitial,
} = trpc.ratelimit.logs.query.useInfiniteQuery(queryParams, {
getNextPageParam: (lastPage) => lastPage.nextCursor,
initialCursor: { requestId: null, time: null },
staleTime: Number.POSITIVE_INFINITY,
refetchOnMount: false,
refetchOnWindowFocus: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,7 @@ export const ratelimitQueryLogsPayload = z.object({
),
})
.nullable(),
cursor: z
.object({
requestId: z.string().nullable(),
time: z.number().nullable(),
})
.optional()
.nullable(),
cursor: z.number().nullable().optional().nullable(),
});

export type RatelimitQueryLogsPayload = z.infer<typeof ratelimitQueryLogsPayload>;
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@ import { transformKeysFilters } from "./utils";
const KeysOverviewLogsResponse = z.object({
keysOverviewLogs: z.array(keysLogs),
hasMore: z.boolean(),
nextCursor: z
.object({
time: z.number().int(),
requestId: z.string(),
})
.optional(),
nextCursor: z.number().int().optional(),
});

type KeysOverviewLogsResponse = z.infer<typeof KeysOverviewLogsResponse>;
Expand Down Expand Up @@ -46,8 +41,7 @@ export const queryKeysOverviewLogs = t.procedure

const clickhouseResult = await clickhouse.api.keys.logs({
...transformedInputs,
cursorRequestId: input.cursor?.requestId ?? null,
cursorTime: input.cursor?.time ?? null,
cursorTime: input.cursor ?? null,
workspaceId: ctx.workspace.id,
keyspaceId: keyspaceId,
// Only include keyIds filters if explicitly provided in the input
Expand Down Expand Up @@ -99,13 +93,7 @@ export const queryKeysOverviewLogs = t.procedure
const response: KeysOverviewLogsResponse = {
keysOverviewLogs,
hasMore: logs.length === input.limit && keysOverviewLogs.length > 0,
nextCursor:
logs.length === input.limit
? {
time: logs[logs.length - 1].time,
requestId: logs[logs.length - 1].request_id,
}
: undefined,
nextCursor: logs.length === input.limit ? logs[logs.length - 1].time : undefined,
};

return response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ export function transformKeysFilters(
names,
identities,
outcomes,
cursorTime: params.cursor?.time ?? null,
cursorRequestId: params.cursor?.requestId ?? null,
cursorTime: params.cursor ?? null,
sorts,
};
}
18 changes: 3 additions & 15 deletions apps/dashboard/lib/trpc/routers/logs/query-logs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@ const LogsResponse = z.object({
logs: z.array(log),
hasMore: z.boolean(),
total: z.number(),
nextCursor: z
.object({
time: z.number().int(),
requestId: z.string(),
})
.optional(),
nextCursor: z.number().int().optional(),
});

type LogsResponse = z.infer<typeof LogsResponse>;
Expand Down Expand Up @@ -52,8 +47,7 @@ export const queryLogs = t.procedure
const transformedInputs = transformFilters(input);
const { logsQuery, totalQuery } = await clickhouse.api.logs({
...transformedInputs,
cursorRequestId: input.cursor?.requestId ?? null,
cursorTime: input.cursor?.time ?? null,
cursorTime: input.cursor ?? null,
workspaceId: workspace.id,
});

Expand All @@ -73,13 +67,7 @@ export const queryLogs = t.procedure
logs,
hasMore: logs.length === input.limit,
total: countResult.val[0].total_count,
nextCursor:
logs.length > 0
? {
time: logs[logs.length - 1].time,
requestId: logs[logs.length - 1].request_id,
}
: undefined,
nextCursor: logs.length > 0 ? logs[logs.length - 1].time : undefined,
};

return response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ describe("transformFilters", () => {
statusCodes: [],
requestIds: [],
cursorTime: null,
cursorRequestId: null,
});
});

Expand Down Expand Up @@ -96,6 +95,5 @@ describe("transformFilters", () => {
const result = transformFilters(payload);

expect(result.cursorTime).toBe(1706024400000);
expect(result.cursorRequestId).toBe("req123");
});
});
3 changes: 1 addition & 2 deletions apps/dashboard/lib/trpc/routers/logs/query-logs/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export function transformFilters(
methods,
paths,
statusCodes,
cursorTime: params.cursor?.time ?? null,
cursorRequestId: params.cursor?.requestId ?? null,
cursorTime: params.cursor ?? null,
};
}
18 changes: 3 additions & 15 deletions apps/dashboard/lib/trpc/routers/ratelimit/query-logs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@ const RatelimitLogsResponse = z.object({
ratelimitLogs: z.array(ratelimitLogs),
hasMore: z.boolean(),
total: z.number(),
nextCursor: z
.object({
time: z.number().int(),
requestId: z.string(),
})
.optional(),
nextCursor: z.number().int().optional(),
});

type RatelimitLogsResponse = z.infer<typeof RatelimitLogsResponse>;
Expand Down Expand Up @@ -61,8 +56,7 @@ export const queryRatelimitLogs = t.procedure
const transformedInputs = transformFilters(input);
const { countQuery, logsQuery } = await clickhouse.ratelimits.logs({
...transformedInputs,
cursorRequestId: input.cursor?.requestId ?? null,
cursorTime: input.cursor?.time ?? null,
cursorTime: input.cursor ?? null,
workspaceId: ctx.workspace.id,
namespaceId: ratelimitNamespaces[0].id,
});
Expand All @@ -81,13 +75,7 @@ export const queryRatelimitLogs = t.procedure
ratelimitLogs: logs,
total: countResult.val[0].total_count,
hasMore: logs.length === input.limit,
nextCursor:
logs.length > 0
? {
time: logs[logs.length - 1].time,
requestId: logs[logs.length - 1].request_id,
}
: undefined,
nextCursor: logs.length > 0 ? logs[logs.length - 1].time : undefined,
};

return response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export function transformFilters(
identifiers,
requestIds: params.requestIds?.filters.map((f) => f.value) || [],
status,
cursorTime: params.cursor?.time ?? null,
cursorRequestId: params.cursor?.requestId ?? null,
cursorTime: params.cursor ?? null,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@ const RatelimitOverviewLogsResponse = z.object({
ratelimitOverviewLogs: z.array(ratelimitOverviewLogs),
hasMore: z.boolean(),
total: z.number(),
nextCursor: z
.object({
time: z.number().int(),
requestId: z.string(),
})
.optional(),
nextCursor: z.number().int().optional(),
});

type RatelimitOverviewLogsResponse = z.infer<typeof RatelimitOverviewLogsResponse>;
Expand Down Expand Up @@ -61,8 +56,7 @@ export const queryRatelimitOverviewLogs = t.procedure
const transformedInputs = transformFilters(input);
const { countQuery, logsQuery } = await clickhouse.ratelimits.overview.logs({
...transformedInputs,
cursorRequestId: input.cursor?.requestId ?? null,
cursorTime: input.cursor?.time ?? null,
cursorTime: input.cursor ?? null,
workspaceId: ctx.workspace.id,
namespaceId: ratelimitNamespaces[0].id,
});
Expand All @@ -84,10 +78,7 @@ export const queryRatelimitOverviewLogs = t.procedure
hasMore: logsWithOverrides.length === input.limit,
nextCursor:
logsWithOverrides.length === input.limit
? {
time: logsWithOverrides[logsWithOverrides.length - 1].time,
requestId: logsWithOverrides[logsWithOverrides.length - 1].request_id,
}
? logsWithOverrides[logsWithOverrides.length - 1].time
: undefined,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ export function transformFilters(
startTime,
endTime,
identifiers,
cursorTime: params.cursor?.time ?? null,
cursorTime: params.cursor ?? null,
status,
cursorRequestId: params.cursor?.requestId ?? null,
sorts, // Add sorts to the returned params
};
}
22 changes: 7 additions & 15 deletions internal/clickhouse/src/keys/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export const keysOverviewLogsParams = z.object({
)
.nullable(),
cursorTime: z.number().int().nullable(),
cursorRequestId: z.string().nullable(),
sorts: z
.array(
z.object({
Expand Down Expand Up @@ -207,34 +206,27 @@ export function getKeysOverviewLogs(ch: Querier) {
// Remove any existing time sort from the orderBy array
const orderByWithoutTime = orderBy.filter((clause) => !clause.startsWith("time"));

// Construct final ORDER BY clause with time and request_id always at the end
// Construct final ORDER BY clause with only time at the end
const orderByClause =
[...orderByWithoutTime, `time ${timeDirection}`, `request_id ${timeDirection}`].join(", ") ||
"time DESC, request_id DESC"; // Fallback if empty
[...orderByWithoutTime, `time ${timeDirection}`].join(", ") || "time DESC"; // Fallback if empty

// Create cursor condition based on time direction
let cursorCondition: string;

// For first page or no cursor provided
if (!args.cursorTime || !args.cursorRequestId) {
if (!args.cursorTime) {
cursorCondition = `
AND ({cursorTime: Nullable(UInt64)} IS NULL AND {cursorRequestId: Nullable(String)} IS NULL)
AND ({cursorTime: Nullable(UInt64)} IS NULL)
`;
} else {
// For subsequent pages, use cursor based on time direction
if (timeDirection === "ASC") {
cursorCondition = `
AND (
(time = {cursorTime: Nullable(UInt64)} AND request_id > {cursorRequestId: Nullable(String)})
OR time > {cursorTime: Nullable(UInt64)}
)
AND (time > {cursorTime: Nullable(UInt64)})
`;
} else {
cursorCondition = `
AND (
(time = {cursorTime: Nullable(UInt64)} AND request_id < {cursorRequestId: Nullable(String)})
OR time < {cursorTime: Nullable(UInt64)}
)
AND (time < {cursorTime: Nullable(UInt64)})
`;
}
}
Expand All @@ -258,7 +250,7 @@ WITH
AND (${keyIdConditions})
-- Apply dynamic outcome filtering
AND (${outcomeCondition})
-- Handle pagination using time and request_id as cursor
-- Handle pagination using only time as cursor
${cursorCondition}
),
-- Second CTE: Calculate per-key aggregated metrics
Expand Down
Loading
Loading