forked from bai/curriculum-project-hub
eb0be43eac
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>
93 lines
2.7 KiB
TypeScript
93 lines
2.7 KiB
TypeScript
import { ORG_ROLE_LABELS, PERMISSION_ROLE_LABELS, type OrgRole, type PermissionRole } from './constants';
|
|
|
|
export function fmtDate(iso: string): string {
|
|
if (!iso) return '—';
|
|
const d = new Date(iso);
|
|
if (Number.isNaN(d.getTime())) return iso;
|
|
return d.toLocaleString(undefined, {
|
|
year: 'numeric',
|
|
month: 'short',
|
|
day: '2-digit',
|
|
hour: '2-digit',
|
|
minute: '2-digit',
|
|
});
|
|
}
|
|
|
|
export function fmtDateOnly(iso: string): string {
|
|
if (!iso) return '—';
|
|
const d = new Date(iso);
|
|
if (Number.isNaN(d.getTime())) return iso;
|
|
return d.toLocaleDateString(undefined, { year: 'numeric', month: 'short', day: '2-digit' });
|
|
}
|
|
|
|
export function fmtCost(usd: number | null): string {
|
|
if (usd === null) return '—';
|
|
return `$${Number(usd).toFixed(4)}`;
|
|
}
|
|
|
|
export function fmtNum(n: number): string {
|
|
return n.toLocaleString();
|
|
}
|
|
|
|
const USAGE_KIND_LABELS: Record<string, string> = {
|
|
model_completion: '模型完成',
|
|
external_capability: '外部能力',
|
|
tool_proxy: '工具代理',
|
|
};
|
|
|
|
const METER_UNIT_LABELS: Record<string, string> = {
|
|
pages: '页',
|
|
audio_seconds: '音频秒',
|
|
invocations: '次调用',
|
|
};
|
|
|
|
export function usageKindLabel(kind: string): string {
|
|
return USAGE_KIND_LABELS[kind] ?? kind;
|
|
}
|
|
|
|
export function meterUnitLabel(unit: string | null | undefined): string {
|
|
if (!unit) return '—';
|
|
return METER_UNIT_LABELS[unit] ?? unit;
|
|
}
|
|
|
|
export function fmtQuantity(quantity: number | null | undefined, unit: string | null | undefined): string {
|
|
if (quantity === null || quantity === undefined) return '—';
|
|
const u = meterUnitLabel(unit);
|
|
return u === '—' ? fmtNum(quantity) : `${fmtNum(quantity)} ${u}`;
|
|
}
|
|
|
|
export function fmtTokens(input: number | null | undefined, output: number | null | undefined): string {
|
|
const hasIn = input !== null && input !== undefined;
|
|
const hasOut = output !== null && output !== undefined;
|
|
if (!hasIn && !hasOut) return '—';
|
|
return `${fmtNum(input ?? 0)} / ${fmtNum(output ?? 0)}`;
|
|
}
|
|
|
|
export function runStatusLabel(status: string): string {
|
|
const key = status.toUpperCase();
|
|
if (key === 'COMPLETED') return '完成';
|
|
if (key === 'FAILED') return '失败';
|
|
if (key === 'CANCELED') return '取消';
|
|
if (key === 'TIMED_OUT') return '超时';
|
|
if (key === 'RUNNING') return '运行中';
|
|
if (key === 'QUEUED') return '排队';
|
|
return status;
|
|
}
|
|
|
|
|
|
export function orgRoleLabel(role: string): string {
|
|
const key = role.toUpperCase() as OrgRole;
|
|
return ORG_ROLE_LABELS[key] ?? role;
|
|
}
|
|
|
|
export function permissionRoleLabel(role: string): string {
|
|
const key = role.toUpperCase() as PermissionRole;
|
|
return PERMISSION_ROLE_LABELS[key] ?? role;
|
|
}
|
|
|
|
export function providerModeLabel(mode: string): string {
|
|
if (mode === 'BYOK') return '自带密钥';
|
|
if (mode === 'PLATFORM_MANAGED') return '平台托管';
|
|
return mode;
|
|
}
|