feat(usage): add resource-usage API client, meta hook, and shared utils

The data layer the resource tabs build on:
- apis/resource.ts: adapter over the unified metered_usage read API
  (resource/gpu-instances/storage/summary/events breakdowns), flattening
  the server's generic shape into the per-tab item shape.
- hooks/use-resource-meta.ts: loads creators/instances/volumes filter
  options for the current scope.
- utils/time-buckets.ts: day/week/month/hour bucket keys + range fill.
- utils/export-breakdown.ts: derive Excel columns from antd table specs.
This commit is contained in:
michelia
2026-06-03 17:10:51 +08:00
committed by michela feng
parent 234e42ccfa
commit 2407416e33
4 changed files with 635 additions and 0 deletions
+50
View File
@@ -0,0 +1,50 @@
/**
* Export a resource-breakdown table to Excel — the GPU Instances / Storage
* tabs' counterpart to the Tokens tab export.
*
* Columns are derived from the same antd column specs the table renders, so the
* export always matches what's on screen (whichever group_by tab is active).
* Raw values are written (not the table's formatted render output) so numbers
* stay sortable / calculable in the spreadsheet.
*/
import { exportJsonToExcel } from '@gpustack/core-ui/excel';
export interface ExportColumn {
title: string;
dataIndex: string;
}
// Keep only real data columns (drop index / render-only columns), and only
// those whose title is a plain string so the header is meaningful.
export const toExportColumns = (columns: any[]): ExportColumn[] =>
(columns || [])
.filter(
(c) => typeof c?.dataIndex === 'string' && typeof c?.title === 'string'
)
.map((c) => ({
title: c.title as string,
dataIndex: c.dataIndex as string
}));
export const exportBreakdownRows = (
rows: any[],
columns: ExportColumn[],
fileName: string,
sheetName = 'usage'
): void => {
const fields = columns.map((c) => c.dataIndex);
const fieldLabels = Object.fromEntries(
columns.map((c) => [c.dataIndex, c.title])
);
const jsonData = (rows || []).map((r) => {
const o: Record<string, any> = {};
columns.forEach((c) => {
o[c.dataIndex] = r?.[c.dataIndex] ?? '';
});
return o;
});
exportJsonToExcel({
fileName,
sheets: [{ jsonData, sheetName, fields, fieldLabels, formatMap: {} }]
});
};