- Default range changed from 30d to 7d - Presets (today/7d/30d) now directly set customStart/customEnd dates, eliminating duplicate getTimeRange() calculation - "All" preset fetches actual data boundaries from /api/date-range and backfills the custom date picker - Clicking "custom" opens popover without triggering data refresh; only confirm applies changes - SQL trend dates cast to ::text to avoid pg driver Date timezone offset - Fix created_at filter from < to <= for end timestamp
39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
import dayjs from "dayjs";
|
|
|
|
export function formatNumber(n: number): string {
|
|
return n.toLocaleString("en-US");
|
|
}
|
|
|
|
export function formatTokens(n: number): string {
|
|
if (n >= 1_000_000_000) return `${(n / 1_000_000_000).toFixed(1)}B`;
|
|
if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(1)}M`;
|
|
if (n >= 1_000) return `${(n / 1_000).toFixed(1)}K`;
|
|
return String(n);
|
|
}
|
|
|
|
export function formatUSD(n: number): string {
|
|
if (n >= 1000) return `$${n.toLocaleString("en-US", { maximumFractionDigits: 0 })}`;
|
|
if (n >= 1) return `$${n.toFixed(2)}`;
|
|
if (n >= 0.01) return `$${n.toFixed(3)}`;
|
|
return `$${n.toFixed(4)}`;
|
|
}
|
|
|
|
export function formatDate(iso: string): string {
|
|
return dayjs(iso).format("YYYY-MM-DD HH:mm:ss");
|
|
}
|
|
|
|
// 预设时间范围
|
|
export type TimeRange = "today" | "7d" | "30d" | "all" | "custom";
|
|
|
|
export function buildQuery(
|
|
base: string,
|
|
params: Record<string, string | number | undefined>
|
|
): string {
|
|
const sp = new URLSearchParams();
|
|
for (const [k, v] of Object.entries(params)) {
|
|
if (v !== undefined && v !== "") sp.set(k, String(v));
|
|
}
|
|
const qs = sp.toString();
|
|
return qs ? `${base}?${qs}` : base;
|
|
}
|