Files
archie/ui/src/org/OrgUsagePage.vue
T

335 lines
12 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
import { computed, onMounted, ref, watch } from "vue";
import PageHeader from "@/base/PageHeader.vue";
import { DataTable, type DataTableColumn } from "@/components/ui/data-table";
import { Empty, EmptyHeader, EmptyTitle } from "@/components/ui/empty";
import { MetadataList } from "@/components/ui/metadata-list";
import { ReadError } from "@/components/ui/read-error";
import { Section } from "@/components/ui/section";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import { Spinner } from "@/components/ui/spinner";
import { api, describeError } from "@/lib/api";
import { errors } from "@/copy/errors";
import { compact, usd } from "@/lib/format";
/**
* What the org's model calls used and cost over a range: the totals, a row per
* day, and the model, source and identity breakdowns that explain them. The
* server derives the org from the caller, so the page names no org.
*/
interface Bucket {
calls: number;
input_tokens: number;
output_tokens: number;
cached_tokens: number;
cost_usd: number | null;
unpriced_calls: number;
}
type DayRow = { day: string } & Bucket;
type ModelRow = { provider: string; model: string } & Bucket;
type SourceRow = { source: string } & Bucket;
type IdentityRow = { identity_id: string; identity_name: string } & Bucket;
interface UsageReport {
days: number;
totals: Bucket;
by_day: DayRow[];
by_model: ModelRow[];
by_source: SourceRow[];
by_identity: IdentityRow[];
}
const RANGES = [
{ value: "7", label: "Last 7 days" },
{ value: "30", label: "Last 30 days" },
{ value: "90", label: "Last 90 days" },
];
const range = ref("30");
const report = ref<UsageReport | null>(null);
const loading = ref(true);
const error = ref("");
// Identifies the current read, so a reply for a range the operator has left is
// dropped instead of painted over the newer one.
let revision = 0;
async function load(): Promise<void> {
const attempt = ++revision;
loading.value = true;
error.value = "";
// The numbers below the control are the numbers for its range, so the
// previous range's report goes before the new read settles.
report.value = null;
try {
const next = await api.usage<UsageReport>(Number(range.value));
if (attempt !== revision) return;
report.value = next;
} catch (cause) {
if (attempt !== revision) return;
error.value = describeError(cause, errors.common.unknown);
} finally {
if (attempt === revision) loading.value = false;
}
}
onMounted(() => void load());
watch(range, () => void load());
/** Prompt and completion tokens; cached tokens are a subset of the input. */
const tokens = (bucket: Bucket): number =>
bucket.input_tokens + bucket.output_tokens;
/** The exact counts behind a compact cell. */
function tokenTitle(bucket: Bucket): string {
const cached = bucket.cached_tokens
? `, ${bucket.cached_tokens.toLocaleString()} cached`
: "";
return `${tokens(bucket).toLocaleString()} tokens (${bucket.input_tokens.toLocaleString()} in, ${bucket.output_tokens.toLocaleString()} out${cached})`;
}
interface TotalRow {
label: string;
value?: string;
mono?: boolean;
title?: string;
}
const totalRows = computed<TotalRow[]>(() => {
const totals = report.value?.totals;
if (!totals) return [];
const rows: TotalRow[] = [
{ label: "Model calls", value: totals.calls.toLocaleString() },
{
label: "Total tokens",
value: compact(tokens(totals)),
mono: true,
title: `${tokens(totals).toLocaleString()} tokens`,
},
{
label: "Input tokens",
value: compact(totals.input_tokens),
mono: true,
title: `${totals.input_tokens.toLocaleString()} tokens`,
},
{
label: "Output tokens",
value: compact(totals.output_tokens),
mono: true,
title: `${totals.output_tokens.toLocaleString()} tokens`,
},
{
label: "Cached tokens",
value: compact(totals.cached_tokens),
mono: true,
title: `${totals.cached_tokens.toLocaleString()} cached tokens, included in input tokens`,
},
{
label: "Cost",
value: usd(totals.cost_usd),
mono: true,
title:
totals.cost_usd == null
? "No call in this range could be priced"
: undefined,
},
];
if (totals.unpriced_calls > 0) {
rows.push({
label: "Unpriced calls",
value: totals.unpriced_calls.toLocaleString(),
title:
"Calls to models without a catalog price count tokens but have no cost.",
});
}
return rows;
});
const dayColumns: DataTableColumn<DayRow>[] = [
{ id: "day", label: "Day", fixed: true, class: "whitespace-nowrap" },
{ id: "calls", label: "Calls", class: "tabular-nums", sort: (row) => row.calls },
{ id: "tokens", label: "Tokens", class: "tabular-nums", sort: tokens },
{ id: "cost", label: "Cost", class: "tabular-nums", sort: (row) => row.cost_usd ?? -1 },
];
const modelColumns: DataTableColumn<ModelRow>[] = [
{ id: "provider", label: "Provider", fixed: true, class: "text-fg-muted whitespace-nowrap" },
{ id: "model", label: "Model", fixed: true, class: "w-full whitespace-normal" },
{ id: "calls", label: "Calls", class: "tabular-nums", sort: (row) => row.calls },
{ id: "tokens", label: "Tokens", class: "tabular-nums", sort: tokens },
{ id: "cost", label: "Cost", class: "tabular-nums", sort: (row) => row.cost_usd ?? -1 },
];
const sourceColumns: DataTableColumn<SourceRow>[] = [
{ id: "source", label: "Source", fixed: true },
{ id: "calls", label: "Calls", class: "tabular-nums", sort: (row) => row.calls },
{ id: "tokens", label: "Tokens", class: "tabular-nums", sort: tokens },
{ id: "cost", label: "Cost", class: "tabular-nums", sort: (row) => row.cost_usd ?? -1 },
];
const identityColumns: DataTableColumn<IdentityRow>[] = [
{ id: "identity", label: "Identity", fixed: true, class: "w-full whitespace-normal" },
{ id: "calls", label: "Calls", class: "tabular-nums", sort: (row) => row.calls },
{ id: "tokens", label: "Tokens", class: "tabular-nums", sort: tokens },
{ id: "cost", label: "Cost", class: "tabular-nums", sort: (row) => row.cost_usd ?? -1 },
];
</script>
<template>
<div>
<PageHeader title="Usage">
<Select v-model="range">
<SelectTrigger class="w-40" size="sm" aria-label="Date range">
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem v-for="option in RANGES" :key="option.value" :value="option.value">
{{ option.label }}
</SelectItem>
</SelectContent>
</Select>
</PageHeader>
<ReadError v-if="error" :message="error" retry-label="Retry" class="mb-4" @retry="load" />
<Section v-else-if="loading">
<p role="status" class="flex items-center gap-2 text-label text-fg-muted">
<Spinner />Loading usage…
</p>
</Section>
<Section v-else-if="!report || report.totals.calls === 0">
<Empty>
<EmptyHeader>
<EmptyTitle>No model calls in this range.</EmptyTitle>
</EmptyHeader>
</Empty>
</Section>
<template v-else>
<Section title="Totals">
<MetadataList :rows="totalRows">
<template #value="{ row }">
<span
class="tabular-nums text-title font-medium"
:class="row.mono && 'font-mono'"
:title="row.title"
>{{ row.value }}</span
>
</template>
</MetadataList>
</Section>
<Section title="Daily">
<DataTable
:columns="dayColumns"
:rows="report?.by_day ?? []"
:row-key="(row) => row.day"
storage-key="usage-daily"
>
<template #empty>
<Empty><EmptyHeader><EmptyTitle>No model calls in this range.</EmptyTitle></EmptyHeader></Empty>
</template>
<template #cell-day="{ row }">
<span class="font-mono text-caption">{{ row.day }}</span>
</template>
<template #cell-calls="{ row }">
<span class="tabular-nums">{{ row.calls.toLocaleString() }}</span>
</template>
<template #cell-tokens="{ row }">
<span class="tabular-nums" :title="tokenTitle(row)">{{ compact(tokens(row)) }}</span>
</template>
<template #cell-cost="{ row }">
<span class="tabular-nums">{{ usd(row.cost_usd) }}</span>
</template>
</DataTable>
</Section>
<Section title="By model">
<DataTable
:columns="modelColumns"
:rows="report?.by_model ?? []"
:row-key="(row) => `${row.provider}/${row.model}`"
storage-key="usage-by-model"
>
<template #empty>
<Empty><EmptyHeader><EmptyTitle>No model calls in this range.</EmptyTitle></EmptyHeader></Empty>
</template>
<template #cell-provider="{ row }">
<span class="font-mono text-caption">{{ row.provider || "–" }}</span>
</template>
<template #cell-model="{ row }">
<span class="font-mono text-caption">{{ row.model || "–" }}</span>
</template>
<template #cell-calls="{ row }">
<span class="tabular-nums">{{ row.calls.toLocaleString() }}</span>
</template>
<template #cell-tokens="{ row }">
<span class="tabular-nums" :title="tokenTitle(row)">{{ compact(tokens(row)) }}</span>
</template>
<template #cell-cost="{ row }">
<span class="tabular-nums">{{ usd(row.cost_usd) }}</span>
</template>
</DataTable>
</Section>
<Section title="By source">
<DataTable
:columns="sourceColumns"
:rows="report?.by_source ?? []"
:row-key="(row) => row.source"
storage-key="usage-by-source"
>
<template #empty>
<Empty><EmptyHeader><EmptyTitle>No model calls in this range.</EmptyTitle></EmptyHeader></Empty>
</template>
<template #cell-source="{ row }">
<span class="font-mono text-caption">{{ row.source || "–" }}</span>
</template>
<template #cell-calls="{ row }">
<span class="tabular-nums">{{ row.calls.toLocaleString() }}</span>
</template>
<template #cell-tokens="{ row }">
<span class="tabular-nums" :title="tokenTitle(row)">{{ compact(tokens(row)) }}</span>
</template>
<template #cell-cost="{ row }">
<span class="tabular-nums">{{ usd(row.cost_usd) }}</span>
</template>
</DataTable>
</Section>
<Section title="By identity">
<DataTable
:columns="identityColumns"
:rows="report?.by_identity ?? []"
:row-key="(row) => row.identity_id || 'unknown'"
storage-key="usage-by-identity"
>
<template #empty>
<Empty><EmptyHeader><EmptyTitle>No model calls in this range.</EmptyTitle></EmptyHeader></Empty>
</template>
<template #cell-identity="{ row }">
<span :title="row.identity_id || undefined">{{ row.identity_name || "Unknown" }}</span>
</template>
<template #cell-calls="{ row }">
<span class="tabular-nums">{{ row.calls.toLocaleString() }}</span>
</template>
<template #cell-tokens="{ row }">
<span class="tabular-nums" :title="tokenTitle(row)">{{ compact(tokens(row)) }}</span>
</template>
<template #cell-cost="{ row }">
<span class="tabular-nums">{{ usd(row.cost_usd) }}</span>
</template>
</DataTable>
</Section>
</template>
</div>
</template>