forked from EduCraft/curriculum-project-hub
feat(hub): usage fact breakdown API + admin usage/session UI + release v0.0.34 (#10)
Expose UsageFact kind/capability rollups on org and project usage reports, and add admin pages that separate model tokens from external-capability meters. Co-authored-by: Hong Jiarong <me@jrhim.com> Co-committed-by: Hong Jiarong <me@jrhim.com>
This commit is contained in:
+285
-124
@@ -9,13 +9,14 @@
|
||||
* external-capability consumption (PDF→MD, ASR, …) is attributed correctly,
|
||||
* not just the main model loop. A run with no cost-bearing fact is
|
||||
* `runsWithoutCost` — ADR-0022: missing cost ≠ zero.
|
||||
*
|
||||
* Breakdown buckets keep kind / provider / model / capability / unit, so the
|
||||
* admin UI can separate model tokens from non-token external meters instead of
|
||||
* collapsing everything into a single input/output token total.
|
||||
*/
|
||||
import type { Prisma, PrismaClient } from "@prisma/client";
|
||||
|
||||
export interface ProjectUsageRow {
|
||||
readonly projectId: string;
|
||||
readonly projectName: string;
|
||||
readonly folderId: string | null;
|
||||
export interface UsageTotals {
|
||||
readonly runCount: number;
|
||||
readonly runsWithCost: number;
|
||||
readonly runsWithoutCost: number;
|
||||
@@ -24,21 +25,50 @@ export interface ProjectUsageRow {
|
||||
readonly costUsd: number | null;
|
||||
}
|
||||
|
||||
export interface ProjectUsageRow extends UsageTotals {
|
||||
readonly projectId: string;
|
||||
readonly projectName: string;
|
||||
readonly folderId: string | null;
|
||||
}
|
||||
|
||||
/** One ledger slice: kind + provider + model + capability + unit. */
|
||||
export interface UsageBreakdownRow {
|
||||
readonly kind: string;
|
||||
readonly provider: string;
|
||||
readonly model: string | null;
|
||||
readonly capabilityId: string | null;
|
||||
readonly unit: string | null;
|
||||
readonly factCount: number;
|
||||
readonly factsWithCost: number;
|
||||
readonly factsWithoutCost: number;
|
||||
readonly inputTokens: number;
|
||||
readonly outputTokens: number;
|
||||
/** Sum of quantity when unit is non-null; null when this bucket is token-only. */
|
||||
readonly quantity: number | null;
|
||||
readonly costUsd: number | null;
|
||||
}
|
||||
|
||||
export interface UsageReport {
|
||||
readonly from: string | null;
|
||||
readonly to: string | null;
|
||||
readonly projects: readonly ProjectUsageRow[];
|
||||
readonly totals: {
|
||||
readonly runCount: number;
|
||||
readonly runsWithCost: number;
|
||||
readonly runsWithoutCost: number;
|
||||
readonly inputTokens: number;
|
||||
readonly outputTokens: number;
|
||||
readonly costUsd: number | null;
|
||||
};
|
||||
readonly totals: UsageTotals;
|
||||
readonly breakdown: readonly UsageBreakdownRow[];
|
||||
}
|
||||
|
||||
export interface ProjectUsageReport extends ProjectUsageRow {
|
||||
readonly from: string | null;
|
||||
readonly to: string | null;
|
||||
readonly breakdown: readonly UsageBreakdownRow[];
|
||||
}
|
||||
|
||||
type FactRow = {
|
||||
readonly kind: string;
|
||||
readonly provider: string;
|
||||
readonly model: string | null;
|
||||
readonly capabilityId: string | null;
|
||||
readonly unit: string | null;
|
||||
readonly quantity: unknown;
|
||||
readonly inputTokens: number | null;
|
||||
readonly outputTokens: number | null;
|
||||
readonly costUsd: unknown;
|
||||
@@ -49,26 +79,91 @@ type RunWithFacts = {
|
||||
readonly usageFacts: readonly FactRow[];
|
||||
};
|
||||
|
||||
/** A run is "recorded with cost" if any of its facts carries a known costUsd. */
|
||||
function runHasRecordedCost(facts: readonly FactRow[]): boolean {
|
||||
return facts.some((f) => f.costUsd !== null && f.costUsd !== undefined);
|
||||
}
|
||||
type MutableTotals = {
|
||||
runCount: number;
|
||||
runsWithCost: number;
|
||||
runsWithoutCost: number;
|
||||
inputTokens: number;
|
||||
outputTokens: number;
|
||||
costUsd: number | null;
|
||||
};
|
||||
|
||||
type MutableBreakdown = {
|
||||
kind: string;
|
||||
provider: string;
|
||||
model: string | null;
|
||||
capabilityId: string | null;
|
||||
unit: string | null;
|
||||
factCount: number;
|
||||
factsWithCost: number;
|
||||
factsWithoutCost: number;
|
||||
inputTokens: number;
|
||||
outputTokens: number;
|
||||
quantity: number | null;
|
||||
costUsd: number | null;
|
||||
};
|
||||
|
||||
type MutableProjectRow = MutableTotals & {
|
||||
readonly projectId: string;
|
||||
readonly projectName: string;
|
||||
readonly folderId: string | null;
|
||||
};
|
||||
|
||||
const FACT_SELECT = {
|
||||
kind: true,
|
||||
provider: true,
|
||||
model: true,
|
||||
capabilityId: true,
|
||||
unit: true,
|
||||
quantity: true,
|
||||
inputTokens: true,
|
||||
outputTokens: true,
|
||||
costUsd: true,
|
||||
} as const;
|
||||
|
||||
/** Decimal→number for Prisma Decimal; null/undefined → null. */
|
||||
function factCostUsdToNumber(value: unknown): number | null {
|
||||
if (value === null || value === undefined) return null;
|
||||
const n = typeof value === "number" ? value : Number(value);
|
||||
return Number.isFinite(n) ? n : null;
|
||||
}
|
||||
|
||||
interface RunRollup {
|
||||
function factQuantityToNumber(value: unknown): number | null {
|
||||
if (value === null || value === undefined) return null;
|
||||
const n = typeof value === "number" ? value : Number(value);
|
||||
return Number.isFinite(n) ? n : null;
|
||||
}
|
||||
|
||||
function breakdownKey(f: FactRow): string {
|
||||
return [
|
||||
f.kind,
|
||||
f.provider,
|
||||
f.model ?? "",
|
||||
f.capabilityId ?? "",
|
||||
f.unit ?? "",
|
||||
].join("\u0000");
|
||||
}
|
||||
|
||||
function emptyMutableTotals(): MutableTotals {
|
||||
return {
|
||||
runCount: 0,
|
||||
runsWithCost: 0,
|
||||
runsWithoutCost: 0,
|
||||
inputTokens: 0,
|
||||
outputTokens: 0,
|
||||
costUsd: null,
|
||||
};
|
||||
}
|
||||
|
||||
function addCost(existing: number | null, add: number): number {
|
||||
return (existing ?? 0) + add;
|
||||
}
|
||||
|
||||
function rollupRunTokensAndCost(facts: readonly FactRow[]): {
|
||||
readonly hasCost: boolean;
|
||||
readonly inputTokens: number;
|
||||
readonly outputTokens: number;
|
||||
readonly costUsd: number | null;
|
||||
}
|
||||
|
||||
function rollupRun(facts: readonly FactRow[]): RunRollup {
|
||||
} {
|
||||
let inputTokens = 0;
|
||||
let outputTokens = 0;
|
||||
let costUsd: number | null = null;
|
||||
@@ -79,12 +174,118 @@ function rollupRun(facts: readonly FactRow[]): RunRollup {
|
||||
const c = factCostUsdToNumber(f.costUsd);
|
||||
if (c !== null) {
|
||||
hasCost = true;
|
||||
costUsd = (costUsd ?? 0) + c;
|
||||
costUsd = addCost(costUsd, c);
|
||||
}
|
||||
}
|
||||
return { hasCost, inputTokens, outputTokens, costUsd };
|
||||
}
|
||||
|
||||
function accumulateBreakdown(
|
||||
buckets: Map<string, MutableBreakdown>,
|
||||
facts: readonly FactRow[],
|
||||
): void {
|
||||
for (const f of facts) {
|
||||
const key = breakdownKey(f);
|
||||
let bucket = buckets.get(key);
|
||||
if (bucket === undefined) {
|
||||
bucket = {
|
||||
kind: f.kind,
|
||||
provider: f.provider,
|
||||
model: f.model,
|
||||
capabilityId: f.capabilityId,
|
||||
unit: f.unit,
|
||||
factCount: 0,
|
||||
factsWithCost: 0,
|
||||
factsWithoutCost: 0,
|
||||
inputTokens: 0,
|
||||
outputTokens: 0,
|
||||
quantity: null,
|
||||
costUsd: null,
|
||||
};
|
||||
buckets.set(key, bucket);
|
||||
}
|
||||
bucket.factCount += 1;
|
||||
bucket.inputTokens += f.inputTokens ?? 0;
|
||||
bucket.outputTokens += f.outputTokens ?? 0;
|
||||
const qty = factQuantityToNumber(f.quantity);
|
||||
if (qty !== null) {
|
||||
bucket.quantity = (bucket.quantity ?? 0) + qty;
|
||||
}
|
||||
const c = factCostUsdToNumber(f.costUsd);
|
||||
if (c !== null) {
|
||||
bucket.factsWithCost += 1;
|
||||
bucket.costUsd = addCost(bucket.costUsd, c);
|
||||
} else {
|
||||
bucket.factsWithoutCost += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function freezeBreakdown(buckets: Map<string, MutableBreakdown>): UsageBreakdownRow[] {
|
||||
return [...buckets.values()]
|
||||
.map((b) => ({
|
||||
kind: b.kind,
|
||||
provider: b.provider,
|
||||
model: b.model,
|
||||
capabilityId: b.capabilityId,
|
||||
unit: b.unit,
|
||||
factCount: b.factCount,
|
||||
factsWithCost: b.factsWithCost,
|
||||
factsWithoutCost: b.factsWithoutCost,
|
||||
inputTokens: b.inputTokens,
|
||||
outputTokens: b.outputTokens,
|
||||
quantity: b.quantity,
|
||||
costUsd: b.costUsd,
|
||||
}))
|
||||
.sort((a, b) => {
|
||||
const kindCmp = a.kind.localeCompare(b.kind);
|
||||
if (kindCmp !== 0) return kindCmp;
|
||||
const provCmp = a.provider.localeCompare(b.provider);
|
||||
if (provCmp !== 0) return provCmp;
|
||||
const capA = a.capabilityId ?? "";
|
||||
const capB = b.capabilityId ?? "";
|
||||
const capCmp = capA.localeCompare(capB);
|
||||
if (capCmp !== 0) return capCmp;
|
||||
const modelA = a.model ?? "";
|
||||
const modelB = b.model ?? "";
|
||||
return modelA.localeCompare(modelB);
|
||||
});
|
||||
}
|
||||
|
||||
function freezeTotals(t: MutableTotals): UsageTotals {
|
||||
return {
|
||||
runCount: t.runCount,
|
||||
runsWithCost: t.runsWithCost,
|
||||
runsWithoutCost: t.runsWithoutCost,
|
||||
inputTokens: t.inputTokens,
|
||||
outputTokens: t.outputTokens,
|
||||
costUsd: t.costUsd,
|
||||
};
|
||||
}
|
||||
|
||||
function applyRunToTotals(totals: MutableTotals, facts: readonly FactRow[]): void {
|
||||
const roll = rollupRunTokensAndCost(facts);
|
||||
totals.runCount += 1;
|
||||
if (roll.hasCost) {
|
||||
totals.runsWithCost += 1;
|
||||
totals.costUsd = addCost(totals.costUsd, roll.costUsd ?? 0);
|
||||
} else {
|
||||
totals.runsWithoutCost += 1;
|
||||
}
|
||||
totals.inputTokens += roll.inputTokens;
|
||||
totals.outputTokens += roll.outputTokens;
|
||||
}
|
||||
|
||||
function emptyReport(from?: Date, to?: Date): UsageReport {
|
||||
return {
|
||||
from: from?.toISOString() ?? null,
|
||||
to: to?.toISOString() ?? null,
|
||||
projects: [],
|
||||
totals: freezeTotals(emptyMutableTotals()),
|
||||
breakdown: [],
|
||||
};
|
||||
}
|
||||
|
||||
export async function getOrgUsage(
|
||||
prisma: PrismaClient,
|
||||
input: {
|
||||
@@ -108,101 +309,65 @@ export async function getOrgUsage(
|
||||
}
|
||||
|
||||
const projectIds = projects.map((p) => p.id);
|
||||
const runWhere: Prisma.AgentRunWhereInput = {
|
||||
projectId: { in: projectIds },
|
||||
...(input.from !== undefined || input.to !== undefined
|
||||
? {
|
||||
startedAt: {
|
||||
...(input.from !== undefined ? { gte: input.from } : {}),
|
||||
...(input.to !== undefined ? { lte: input.to } : {}),
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
};
|
||||
|
||||
// Date range filters by run.startedAt, matching the pre-ADR-0026 semantics:
|
||||
// a run is in or out of the window based on when it started, and all of its
|
||||
// facts come along. Filtering facts by occurredAt independently would let a
|
||||
// run contribute partial cost to a window it doesn't belong to.
|
||||
const runs = await prisma.agentRun.findMany({
|
||||
where: runWhere,
|
||||
where: {
|
||||
projectId: { in: projectIds },
|
||||
...(input.from !== undefined || input.to !== undefined
|
||||
? {
|
||||
startedAt: {
|
||||
...(input.from !== undefined ? { gte: input.from } : {}),
|
||||
...(input.to !== undefined ? { lte: input.to } : {}),
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
},
|
||||
select: {
|
||||
projectId: true,
|
||||
usageFacts: {
|
||||
select: { inputTokens: true, outputTokens: true, costUsd: true },
|
||||
select: FACT_SELECT,
|
||||
orderBy: { occurredAt: "asc" },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
type MutableRow = {
|
||||
readonly projectId: string;
|
||||
readonly projectName: string;
|
||||
readonly folderId: string | null;
|
||||
runCount: number;
|
||||
runsWithCost: number;
|
||||
runsWithoutCost: number;
|
||||
inputTokens: number;
|
||||
outputTokens: number;
|
||||
costUsd: number | null;
|
||||
};
|
||||
|
||||
const byProject = new Map<string, MutableRow>();
|
||||
const byProject = new Map<string, MutableProjectRow>();
|
||||
for (const p of projects) {
|
||||
byProject.set(p.id, {
|
||||
projectId: p.id,
|
||||
projectName: p.name,
|
||||
folderId: p.folderId,
|
||||
runCount: 0,
|
||||
runsWithCost: 0,
|
||||
runsWithoutCost: 0,
|
||||
inputTokens: 0,
|
||||
outputTokens: 0,
|
||||
costUsd: null,
|
||||
...emptyMutableTotals(),
|
||||
});
|
||||
}
|
||||
|
||||
const breakdownBuckets = new Map<string, MutableBreakdown>();
|
||||
for (const run of runs as readonly RunWithFacts[]) {
|
||||
const row = byProject.get(run.projectId);
|
||||
if (row === undefined) continue;
|
||||
const roll = rollupRun(run.usageFacts);
|
||||
row.runCount += 1;
|
||||
if (roll.hasCost) {
|
||||
row.runsWithCost += 1;
|
||||
row.costUsd = (row.costUsd ?? 0) + (roll.costUsd ?? 0);
|
||||
} else {
|
||||
row.runsWithoutCost += 1;
|
||||
}
|
||||
row.inputTokens += roll.inputTokens;
|
||||
row.outputTokens += roll.outputTokens;
|
||||
applyRunToTotals(row, run.usageFacts);
|
||||
accumulateBreakdown(breakdownBuckets, run.usageFacts);
|
||||
}
|
||||
|
||||
const projectsOut: ProjectUsageRow[] = [...byProject.values()].map((r) => ({
|
||||
projectId: r.projectId,
|
||||
projectName: r.projectName,
|
||||
folderId: r.folderId,
|
||||
runCount: r.runCount,
|
||||
runsWithCost: r.runsWithCost,
|
||||
runsWithoutCost: r.runsWithoutCost,
|
||||
inputTokens: r.inputTokens,
|
||||
outputTokens: r.outputTokens,
|
||||
costUsd: r.costUsd,
|
||||
...freezeTotals(r),
|
||||
}));
|
||||
|
||||
let runCount = 0;
|
||||
let runsWithCost = 0;
|
||||
let runsWithoutCost = 0;
|
||||
let inputTokens = 0;
|
||||
let outputTokens = 0;
|
||||
let costUsd: number | null = null;
|
||||
const totals = emptyMutableTotals();
|
||||
for (const row of projectsOut) {
|
||||
runCount += row.runCount;
|
||||
runsWithCost += row.runsWithCost;
|
||||
runsWithoutCost += row.runsWithoutCost;
|
||||
inputTokens += row.inputTokens;
|
||||
outputTokens += row.outputTokens;
|
||||
totals.runCount += row.runCount;
|
||||
totals.runsWithCost += row.runsWithCost;
|
||||
totals.runsWithoutCost += row.runsWithoutCost;
|
||||
totals.inputTokens += row.inputTokens;
|
||||
totals.outputTokens += row.outputTokens;
|
||||
if (row.costUsd !== null) {
|
||||
costUsd = (costUsd ?? 0) + row.costUsd;
|
||||
totals.costUsd = addCost(totals.costUsd, row.costUsd);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -210,14 +375,8 @@ export async function getOrgUsage(
|
||||
from: input.from?.toISOString() ?? null,
|
||||
to: input.to?.toISOString() ?? null,
|
||||
projects: projectsOut,
|
||||
totals: {
|
||||
runCount,
|
||||
runsWithCost,
|
||||
runsWithoutCost,
|
||||
inputTokens,
|
||||
outputTokens,
|
||||
costUsd,
|
||||
},
|
||||
totals: freezeTotals(totals),
|
||||
breakdown: freezeBreakdown(breakdownBuckets),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -229,7 +388,7 @@ export async function getProjectUsage(
|
||||
readonly from?: Date | undefined;
|
||||
readonly to?: Date | undefined;
|
||||
},
|
||||
): Promise<ProjectUsageRow> {
|
||||
): Promise<ProjectUsageReport> {
|
||||
const project = await prisma.project.findFirst({
|
||||
where: { id: input.projectId, organizationId: input.organizationId },
|
||||
select: { id: true, name: true, folderId: true },
|
||||
@@ -237,39 +396,41 @@ export async function getProjectUsage(
|
||||
if (project === null) {
|
||||
throw new Error(`project not found: ${input.projectId}`);
|
||||
}
|
||||
const report = await getOrgUsage(prisma, {
|
||||
organizationId: input.organizationId,
|
||||
from: input.from,
|
||||
to: input.to,
|
||||
});
|
||||
const row = report.projects.find((p) => p.projectId === project.id);
|
||||
return (
|
||||
row ?? {
|
||||
projectId: project.id,
|
||||
projectName: project.name,
|
||||
folderId: project.folderId,
|
||||
runCount: 0,
|
||||
runsWithCost: 0,
|
||||
runsWithoutCost: 0,
|
||||
inputTokens: 0,
|
||||
outputTokens: 0,
|
||||
costUsd: null,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
function emptyReport(from?: Date, to?: Date): UsageReport {
|
||||
return {
|
||||
from: from?.toISOString() ?? null,
|
||||
to: to?.toISOString() ?? null,
|
||||
projects: [],
|
||||
totals: {
|
||||
runCount: 0,
|
||||
runsWithCost: 0,
|
||||
runsWithoutCost: 0,
|
||||
inputTokens: 0,
|
||||
outputTokens: 0,
|
||||
costUsd: null,
|
||||
const runs = await prisma.agentRun.findMany({
|
||||
where: {
|
||||
projectId: project.id,
|
||||
...(input.from !== undefined || input.to !== undefined
|
||||
? {
|
||||
startedAt: {
|
||||
...(input.from !== undefined ? { gte: input.from } : {}),
|
||||
...(input.to !== undefined ? { lte: input.to } : {}),
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
},
|
||||
select: {
|
||||
usageFacts: {
|
||||
select: FACT_SELECT,
|
||||
orderBy: { occurredAt: "asc" },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const totals = emptyMutableTotals();
|
||||
const breakdownBuckets = new Map<string, MutableBreakdown>();
|
||||
for (const run of runs) {
|
||||
applyRunToTotals(totals, run.usageFacts as readonly FactRow[]);
|
||||
accumulateBreakdown(breakdownBuckets, run.usageFacts as readonly FactRow[]);
|
||||
}
|
||||
|
||||
return {
|
||||
projectId: project.id,
|
||||
projectName: project.name,
|
||||
folderId: project.folderId,
|
||||
...freezeTotals(totals),
|
||||
from: input.from?.toISOString() ?? null,
|
||||
to: input.to?.toISOString() ?? null,
|
||||
breakdown: freezeBreakdown(breakdownBuckets),
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user