Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions apps/api/src/routes/activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
and,
gte,
lte,
eq,
projectHourlyStats,
projectHourlyModelStats,
apiKeyHourlyStats,
Expand Down Expand Up @@ -191,8 +190,17 @@ activity.openapi(getActivity, async (c) => {
});
}

// If filtering by apiKeyId, use the apiKeyHourlyStats aggregation table
if (apiKeyId) {
// If filtering by apiKeyId, use the apiKeyHourlyStats aggregation table.
// apiKeyId may be a comma-separated list of IDs (e.g. for filtering across
// rotated key history) — split into an array of trimmed IDs.
const apiKeyIdList = apiKeyId
? apiKeyId
.split(",")
.map((s) => s.trim())
.filter((s) => s.length > 0)
: [];

if (apiKeyIdList.length > 0) {
// Query aggregated data from apiKeyHourlyStats table
const hourlyAggregates = await db
.select({
Expand Down Expand Up @@ -304,7 +312,7 @@ activity.openapi(getActivity, async (c) => {
.from(apiKeyHourlyStats)
.where(
and(
eq(apiKeyHourlyStats.apiKeyId, apiKeyId),
inArray(apiKeyHourlyStats.apiKeyId, apiKeyIdList),
inArray(apiKeyHourlyStats.projectId, projectIds),
gte(apiKeyHourlyStats.hourTimestamp, startDate),
lte(apiKeyHourlyStats.hourTimestamp, endDate),
Expand Down Expand Up @@ -356,7 +364,7 @@ activity.openapi(getActivity, async (c) => {
.from(apiKeyHourlyModelStats)
.where(
and(
eq(apiKeyHourlyModelStats.apiKeyId, apiKeyId),
inArray(apiKeyHourlyModelStats.apiKeyId, apiKeyIdList),
inArray(apiKeyHourlyModelStats.projectId, projectIds),
gte(apiKeyHourlyModelStats.hourTimestamp, startDate),
lte(apiKeyHourlyModelStats.hourTimestamp, endDate),
Expand Down
17 changes: 17 additions & 0 deletions apps/api/src/routes/dev-plans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,7 @@ const getStatus = createRoute({
organizationId: z.string().nullable(),
projectId: z.string().nullable(),
apiKey: z.string().nullable(),
apiKeyIds: z.array(z.string()),
devPlanAllowAllModels: z.boolean(),
cachingEnabled: z.boolean(),
cacheDurationSeconds: z.number(),
Expand Down Expand Up @@ -772,6 +773,7 @@ devPlans.openapi(getStatus, async (c) => {
organizationId: null,
projectId: null,
apiKey: null,
apiKeyIds: [],
devPlanAllowAllModels: false,
cachingEnabled: false,
cacheDurationSeconds: 60,
Expand All @@ -788,6 +790,7 @@ devPlans.openapi(getStatus, async (c) => {
let projectId: string | null = null;
let cachingEnabled = false;
let cacheDurationSeconds = 60;
let apiKeyIds: string[] = [];
if (personalOrg.devPlan !== "none") {
// Find the default project for this org. Order by createdAt asc so we
// always return the original "Default Project" rather than whichever
Expand All @@ -812,6 +815,19 @@ devPlans.openapi(getStatus, async (c) => {
project.id,
user.id,
);

// Include both active and previously rotated (status="deleted")
// dev-plan API keys so the dashboard can scope usage charts to the
// devpass key across its rotation history while excluding keys the
// user created via the llmgateway dashboard for the same project.
const devPlanKeys = await db.query.apiKey.findMany({
where: {
projectId: { eq: project.id },
description: { eq: "Dev Plan API Key" },
},
columns: { id: true },
});
apiKeyIds = devPlanKeys.map((k) => k.id);
}
}

Expand All @@ -830,6 +846,7 @@ devPlans.openapi(getStatus, async (c) => {
organizationId: personalOrg.id,
projectId,
apiKey,
apiKeyIds,
devPlanAllowAllModels: personalOrg.devPlanAllowAllModels,
cachingEnabled,
cacheDurationSeconds,
Expand Down
42 changes: 28 additions & 14 deletions apps/api/src/routes/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,8 @@ const getById = createRoute({

const querySchema = z.object({
apiKeyId: z.string().optional().openapi({
description: "Filter logs by API key ID",
description:
"Filter logs by API key ID (supports comma-separated list of IDs)",
}),
providerKeyId: z.string().optional().openapi({
description: "Filter logs by provider key ID",
Expand Down Expand Up @@ -444,25 +445,36 @@ logs.openapi(get, async (c) => {
});
}

// Check apiKeyId authorization if provided
if (apiKeyId) {
const apiKey = await db.query.apiKey.findFirst({
// Check apiKeyId authorization if provided. apiKeyId may be a single ID or
// a comma-separated list (e.g. when filtering across a key's rotation
// history). Every requested key must belong to one of the user's projects.
const apiKeyIdList = apiKeyId
? apiKeyId
.split(",")
.map((s) => s.trim())
.filter((s) => s.length > 0)
: [];

if (apiKeyIdList.length > 0) {
const apiKeys = await db.query.apiKey.findMany({
where: {
id: apiKeyId,
id: { in: apiKeyIdList },
},
columns: { id: true, projectId: true },
});

if (!apiKey) {
if (apiKeys.length !== apiKeyIdList.length) {
throw new HTTPException(404, {
message: "API key not found",
});
}

// Check if the API key belongs to one of the user's projects
if (!projectIds.includes(apiKey.projectId)) {
throw new HTTPException(403, {
message: "You don't have access to this API key",
});
for (const key of apiKeys) {
if (!projectIds.includes(key.projectId)) {
throw new HTTPException(403, {
message: "You don't have access to this API key",
});
}
}
}

Expand Down Expand Up @@ -534,9 +546,11 @@ logs.openapi(get, async (c) => {
);
}

// Add apiKeyId filter
if (apiKeyId) {
whereConditions.push(eq(tables.log.apiKeyId, apiKeyId));
// Add apiKeyId filter (supports comma-separated list)
if (apiKeyIdList.length === 1) {
whereConditions.push(eq(tables.log.apiKeyId, apiKeyIdList[0]));
} else if (apiKeyIdList.length > 1) {
whereConditions.push(inArray(tables.log.apiKeyId, apiKeyIdList));
}

// Add providerKeyId filter
Expand Down
6 changes: 5 additions & 1 deletion apps/code/src/app/dashboard/DashboardClient.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ export default function DashboardClient() {
{/* Usage — full-width with metrics + chart */}
<UsageOverview
projectId={devPlanStatus?.projectId ?? null}
apiKeyIds={devPlanStatus?.apiKeyIds ?? []}
creditsUsed={creditsUsed}
creditsLimit={creditsLimit}
planName={currentPlanName}
Expand Down Expand Up @@ -439,7 +440,10 @@ export default function DashboardClient() {

{/* Coding Agents */}
{devPlanStatus?.organizationId && (
<CodingAgents orgId={devPlanStatus.organizationId} />
<CodingAgents
orgId={devPlanStatus.organizationId}
apiKeyIds={devPlanStatus.apiKeyIds ?? []}
/>
)}

{/* Integrations */}
Expand Down
Loading
Loading