Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

usageChartData.ts

33 lines1.0 KB

1import type { UsageProviderKind } from "@t3tools/contracts";
2import type { DailyTotals } from "@t3tools/shared/usageMerge";
3​
4import { PROVIDER_ORDER } from "./usageProviders";
5​
6export type UsageChartMetric = "cost" | "tokens";
7​
8export interface UsageChartDay {
9 readonly day: string;
10 readonly values: readonly { readonly provider: UsageProviderKind; readonly value: number }[];
11 readonly total: number;
13​
14export function buildChartDays(
15 days: readonly string[],
16 daily: readonly DailyTotals[],
17 metric: UsageChartMetric,
18): readonly UsageChartDay[] {
19 const byDay = new Map(daily.map((totals) => [totals.day, totals]));
20 return days.map((day) => {
21 const totals = byDay.get(day);
22 const values = PROVIDER_ORDER.map((provider) => {
23 const entry = totals?.byProvider.get(provider);
24 const value = entry === undefined ? 0 : metric === "cost" ? entry.costUsd : entry.totalTokens;
25 return { provider, value };
26 });
27 return {
28 day,
29 values,
30 total: values.reduce((sum, entry) => sum + entry.value, 0),
31 };
32 });