@deathbeam/pi-usage 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.ts +37 -42
- package/package.json +1 -1
- package/usage.ts +3 -4
package/index.ts
CHANGED
|
@@ -17,6 +17,9 @@ import {
|
|
|
17
17
|
type UsageRecord,
|
|
18
18
|
} from "./usage.ts";
|
|
19
19
|
|
|
20
|
+
type Column = { label: string; width: number; cell: (stats: Stats, requests: number, totalCost: number) => string };
|
|
21
|
+
type BodyRow = { name: string; stats: Stats; requests: number; dim: boolean };
|
|
22
|
+
|
|
20
23
|
const PERIODS = [
|
|
21
24
|
{ label: "Today", days: 1 },
|
|
22
25
|
{ label: "7d", days: 7 },
|
|
@@ -32,6 +35,25 @@ const NAME_HEADERS: Record<GroupMode, string> = {
|
|
|
32
35
|
project: "Project",
|
|
33
36
|
};
|
|
34
37
|
|
|
38
|
+
const COLUMNS: Column[] = [
|
|
39
|
+
{ label: "Cost", width: 10, cell: (s) => formatCost(s.cost) },
|
|
40
|
+
{
|
|
41
|
+
label: "Share",
|
|
42
|
+
width: 13,
|
|
43
|
+
cell: (s, _r, totalCost) => {
|
|
44
|
+
const share = totalCost > 0 ? s.cost / totalCost : 0;
|
|
45
|
+
const filled = Math.round(8 * share);
|
|
46
|
+
return "█".repeat(filled) + "·".repeat(8 - filled) + " " + `${Math.round(100 * share)}%`.padStart(4);
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
{ label: "Reqs", width: 6, cell: (_s, requests) => formatTokens(requests) },
|
|
50
|
+
{ label: "In", width: 7, cell: (s) => formatTokens(s.input) },
|
|
51
|
+
{ label: "Out", width: 7, cell: (s) => formatTokens(s.output) },
|
|
52
|
+
// Cost already prices read/write differently; one combined token column is enough.
|
|
53
|
+
{ label: "Cache", width: 7, cell: (s) => formatTokens(s.cacheRead + s.cacheWrite) },
|
|
54
|
+
{ label: "Hit%", width: 5, cell: (s) => hitPct(s) },
|
|
55
|
+
];
|
|
56
|
+
|
|
35
57
|
const SPARK_BARS = "▁▂▃▄▅▆▇█";
|
|
36
58
|
const MIN_NAME_WIDTH = 12;
|
|
37
59
|
|
|
@@ -64,27 +86,6 @@ function hitPct(stats: Stats): string {
|
|
|
64
86
|
return total > 0 ? `${Math.round((100 * stats.cacheRead) / total)}%` : "-";
|
|
65
87
|
}
|
|
66
88
|
|
|
67
|
-
type Column = { label: string; width: number; cell: (stats: Stats, requests: number, totalCost: number) => string };
|
|
68
|
-
|
|
69
|
-
const COLUMNS: Column[] = [
|
|
70
|
-
{ label: "Cost", width: 10, cell: (s) => formatCost(s.cost) },
|
|
71
|
-
// Cost already prices read/write differently; one combined token column is enough.
|
|
72
|
-
{
|
|
73
|
-
label: "Share",
|
|
74
|
-
width: 13,
|
|
75
|
-
cell: (s, _r, totalCost) => {
|
|
76
|
-
const share = totalCost > 0 ? s.cost / totalCost : 0;
|
|
77
|
-
const filled = Math.round(8 * share);
|
|
78
|
-
return "█".repeat(filled) + "·".repeat(8 - filled) + " " + `${Math.round(100 * share)}%`.padStart(4);
|
|
79
|
-
},
|
|
80
|
-
},
|
|
81
|
-
{ label: "Reqs", width: 6, cell: (_s, requests) => formatTokens(requests) },
|
|
82
|
-
{ label: "In", width: 7, cell: (s) => formatTokens(s.input) },
|
|
83
|
-
{ label: "Out", width: 7, cell: (s) => formatTokens(s.output) },
|
|
84
|
-
{ label: "Cache", width: 7, cell: (s) => formatTokens(s.cacheRead + s.cacheWrite) },
|
|
85
|
-
{ label: "Hit%", width: 5, cell: (s) => hitPct(s) },
|
|
86
|
-
];
|
|
87
|
-
|
|
88
89
|
/** Widths fit the worst case ($999,999 / 999.9B); the name column shrinks first, then Share, then Hit%. */
|
|
89
90
|
function layout(width: number): { nameWidth: number; columns: Column[] } {
|
|
90
91
|
const used = (cols: Column[]) => cols.reduce((sum, c) => sum + c.width + 1, 0);
|
|
@@ -155,12 +156,10 @@ function sparkline(
|
|
|
155
156
|
};
|
|
156
157
|
}
|
|
157
158
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
function flatten(rows: GroupRow[]): BodyRow[] {
|
|
159
|
+
function flatten(rows: GroupRow[], mode: GroupMode): BodyRow[] {
|
|
161
160
|
const body: BodyRow[] = [];
|
|
162
161
|
for (const row of rows) {
|
|
163
|
-
body.push({ name: row.name, stats: row.stats, requests: row.requests, dim: false });
|
|
162
|
+
body.push({ name: displayName(mode, row.name), stats: row.stats, requests: row.requests, dim: false });
|
|
164
163
|
for (const child of row.children ?? [])
|
|
165
164
|
body.push({ name: ` ${child.name}`, stats: child.stats, requests: child.requests, dim: true });
|
|
166
165
|
}
|
|
@@ -191,7 +190,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
191
190
|
} as const;
|
|
192
191
|
await ctx.ui.custom((tui, theme, keys, done) => {
|
|
193
192
|
let period = 0;
|
|
194
|
-
let group
|
|
193
|
+
let group = 0;
|
|
195
194
|
let cursor = 0;
|
|
196
195
|
let top = 0;
|
|
197
196
|
let rowCount = 0;
|
|
@@ -199,19 +198,18 @@ export default function (pi: ExtensionAPI) {
|
|
|
199
198
|
render = () => tui.requestRender();
|
|
200
199
|
void loaded.then(render);
|
|
201
200
|
const title = (width: number): string => {
|
|
202
|
-
const
|
|
203
|
-
|
|
204
|
-
).join(" ");
|
|
205
|
-
const groupTabs = GROUPS.map((g, i) =>
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
let right = `${groupTabs}${theme.fg("dim", " (v)")}`;
|
|
201
|
+
const tab = (label: string, active: boolean) =>
|
|
202
|
+
active ? theme.fg("accent", `[${label}]`) : theme.fg("muted", ` ${label} `);
|
|
203
|
+
const periodTabs = PERIODS.map(({ label }, i) => tab(label, i === period)).join(" ");
|
|
204
|
+
const groupTabs = GROUPS.map((g, i) => tab(GROUP_LABELS[g], i === group)).join(" ");
|
|
205
|
+
const periodHint = theme.fg("dim", " (tab/←→)");
|
|
206
|
+
const groupHint = theme.fg("dim", " (v)");
|
|
207
|
+
let left = `${theme.fg("accent", theme.bold("Usage"))} ${periodTabs}${periodHint}`;
|
|
208
|
+
let right = `${groupTabs}${groupHint}`;
|
|
209
|
+
// Drop the key hints before the tabs, so the right-hand tabs survive truncation.
|
|
212
210
|
if (visibleWidth(left) + 1 + visibleWidth(right) > width) {
|
|
213
|
-
left = left.replace(
|
|
214
|
-
right = right.replace(
|
|
211
|
+
left = left.replace(periodHint, "");
|
|
212
|
+
right = right.replace(groupHint, "");
|
|
215
213
|
}
|
|
216
214
|
const pad = Math.max(1, width - visibleWidth(left) - visibleWidth(right));
|
|
217
215
|
return truncateToWidth(left + " ".repeat(pad) + right, width);
|
|
@@ -263,10 +261,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
263
261
|
),
|
|
264
262
|
);
|
|
265
263
|
// Fixed top: title, rule, summary, sparkline, column header. Fixed bottom: totals, rule, footer.
|
|
266
|
-
const body = flatten(buildRows(state.records, since, mode))
|
|
267
|
-
...row,
|
|
268
|
-
name: displayName(mode, row.name),
|
|
269
|
-
}));
|
|
264
|
+
const body = flatten(buildRows(state.records, since, mode), mode);
|
|
270
265
|
rowCount = body.length;
|
|
271
266
|
viewHeight = Math.max(0, rows - lines.length - 3);
|
|
272
267
|
cursor = Math.min(cursor, Math.max(0, rowCount - 1));
|
package/package.json
CHANGED
package/usage.ts
CHANGED
|
@@ -217,18 +217,17 @@ export type DailyCost = { label: string; cost: number };
|
|
|
217
217
|
|
|
218
218
|
/** Cost per local calendar day, oldest first, covering the `days` days ending today. */
|
|
219
219
|
export function dailyCosts(records: UsageRecord[], days: number, now = Date.now()): DailyCost[] {
|
|
220
|
-
const start = new Date(now);
|
|
221
|
-
start.setHours(0, 0, 0, 0);
|
|
222
|
-
// setDate walks calendar days, so DST shifts cannot misplace a bucket.
|
|
223
220
|
const buckets: DailyCost[] = [];
|
|
224
221
|
const index = new Map<number, number>();
|
|
225
|
-
const cursor = new Date(
|
|
222
|
+
const cursor = new Date(now);
|
|
223
|
+
cursor.setHours(0, 0, 0, 0);
|
|
226
224
|
for (let i = 0; i < days; i++) {
|
|
227
225
|
index.set(cursor.getTime(), days - 1 - i);
|
|
228
226
|
buckets.unshift({
|
|
229
227
|
label: `${String(cursor.getMonth() + 1).padStart(2, "0")}-${String(cursor.getDate()).padStart(2, "0")}`,
|
|
230
228
|
cost: 0,
|
|
231
229
|
});
|
|
230
|
+
// setDate walks calendar days, so DST shifts cannot misplace a bucket.
|
|
232
231
|
cursor.setDate(cursor.getDate() - 1);
|
|
233
232
|
}
|
|
234
233
|
for (const record of records) {
|