@@ -18,8 +18,9 @@ import { ModelTable } from "../src/schema/model.sql.js"
1818
1919// get input from command line
2020const identifier = process . argv [ 2 ]
21+ const verbose = process . argv [ process . argv . length - 1 ] === "-v"
2122if ( ! identifier ) {
22- console . error ( "Usage: bun lookup-user.ts <email|workspaceID|apiKey>" )
23+ console . error ( "Usage: bun lookup-user.ts <email|workspaceID|apiKey> [-v] " )
2324 process . exit ( 1 )
2425}
2526
@@ -223,93 +224,68 @@ async function printWorkspace(workspaceID: string) {
223224 ) ,
224225 )
225226
226- await printTable ( "28-Day Usage" , ( tx ) =>
227- tx
228- . select ( {
229- date : sql < string > `DATE(${ UsageTable . timeCreated } )` . as ( "date" ) ,
230- requests : sql < number > `COUNT(*)` . as ( "requests" ) ,
231- inputTokens : sql < number > `SUM(${ UsageTable . inputTokens } )` . as ( "input_tokens" ) ,
232- outputTokens : sql < number > `SUM(${ UsageTable . outputTokens } )` . as ( "output_tokens" ) ,
233- reasoningTokens : sql < number > `SUM(${ UsageTable . reasoningTokens } )` . as ( "reasoning_tokens" ) ,
234- cacheReadTokens : sql < number > `SUM(${ UsageTable . cacheReadTokens } )` . as ( "cache_read_tokens" ) ,
235- cacheWrite5mTokens : sql < number > `SUM(${ UsageTable . cacheWrite5mTokens } )` . as ( "cache_write_5m_tokens" ) ,
236- cacheWrite1hTokens : sql < number > `SUM(${ UsageTable . cacheWrite1hTokens } )` . as ( "cache_write_1h_tokens" ) ,
237- cost : sql < number > `SUM(${ UsageTable . cost } )` . as ( "cost" ) ,
238- } )
239- . from ( UsageTable )
240- . where (
241- and (
242- eq ( UsageTable . workspaceID , workspace . id ) ,
243- sql `${ UsageTable . timeCreated } >= DATE_SUB(NOW(), INTERVAL 28 DAY)` ,
227+ if ( verbose ) {
228+ await printTable ( "28-Day Usage" , ( tx ) =>
229+ tx
230+ . select ( {
231+ date : sql < string > `DATE(${ UsageTable . timeCreated } )` . as ( "date" ) ,
232+ requests : sql < number > `COUNT(*)` . as ( "requests" ) ,
233+ inputTokens : sql < number > `SUM(${ UsageTable . inputTokens } )` . as ( "input_tokens" ) ,
234+ outputTokens : sql < number > `SUM(${ UsageTable . outputTokens } )` . as ( "output_tokens" ) ,
235+ reasoningTokens : sql < number > `SUM(${ UsageTable . reasoningTokens } )` . as ( "reasoning_tokens" ) ,
236+ cacheReadTokens : sql < number > `SUM(${ UsageTable . cacheReadTokens } )` . as ( "cache_read_tokens" ) ,
237+ cacheWrite5mTokens : sql < number > `SUM(${ UsageTable . cacheWrite5mTokens } )` . as ( "cache_write_5m_tokens" ) ,
238+ cacheWrite1hTokens : sql < number > `SUM(${ UsageTable . cacheWrite1hTokens } )` . as ( "cache_write_1h_tokens" ) ,
239+ cost : sql < number > `SUM(${ UsageTable . cost } )` . as ( "cost" ) ,
240+ } )
241+ . from ( UsageTable )
242+ . where (
243+ and (
244+ eq ( UsageTable . workspaceID , workspace . id ) ,
245+ sql `${ UsageTable . timeCreated } >= DATE_SUB(NOW(), INTERVAL 28 DAY)` ,
246+ ) ,
247+ )
248+ . groupBy ( sql `DATE(${ UsageTable . timeCreated } )` )
249+ . orderBy ( sql `DATE(${ UsageTable . timeCreated } ) DESC` )
250+ . then ( ( rows ) => {
251+ const totalCost = rows . reduce ( ( sum , r ) => sum + Number ( r . cost ) , 0 )
252+ const mapped = rows . map ( ( row ) => ( {
253+ ...row ,
254+ cost : `$${ ( Number ( row . cost ) / 100000000 ) . toFixed ( 2 ) } ` ,
255+ } ) )
256+ if ( mapped . length > 0 ) {
257+ mapped . push ( {
258+ date : "TOTAL" ,
259+ requests : null as any ,
260+ inputTokens : null as any ,
261+ outputTokens : null as any ,
262+ reasoningTokens : null as any ,
263+ cacheReadTokens : null as any ,
264+ cacheWrite5mTokens : null as any ,
265+ cacheWrite1hTokens : null as any ,
266+ cost : `$${ ( totalCost / 100000000 ) . toFixed ( 2 ) } ` ,
267+ } )
268+ }
269+ return mapped
270+ } ) ,
271+ )
272+ await printTable ( "Disabled Models" , ( tx ) =>
273+ tx
274+ . select ( {
275+ model : ModelTable . model ,
276+ timeCreated : ModelTable . timeCreated ,
277+ } )
278+ . from ( ModelTable )
279+ . where ( eq ( ModelTable . workspaceID , workspace . id ) )
280+ . orderBy ( sql `${ ModelTable . timeCreated } DESC` )
281+ . then ( ( rows ) =>
282+ rows . map ( ( row ) => ( {
283+ model : row . model ,
284+ timeCreated : formatDate ( row . timeCreated ) ,
285+ } ) ) ,
244286 ) ,
245- )
246- . groupBy ( sql `DATE(${ UsageTable . timeCreated } )` )
247- . orderBy ( sql `DATE(${ UsageTable . timeCreated } ) DESC` )
248- . then ( ( rows ) => {
249- const totalCost = rows . reduce ( ( sum , r ) => sum + Number ( r . cost ) , 0 )
250- const mapped = rows . map ( ( row ) => ( {
251- ...row ,
252- cost : `$${ ( Number ( row . cost ) / 100000000 ) . toFixed ( 2 ) } ` ,
253- } ) )
254- if ( mapped . length > 0 ) {
255- mapped . push ( {
256- date : "TOTAL" ,
257- requests : null as any ,
258- inputTokens : null as any ,
259- outputTokens : null as any ,
260- reasoningTokens : null as any ,
261- cacheReadTokens : null as any ,
262- cacheWrite5mTokens : null as any ,
263- cacheWrite1hTokens : null as any ,
264- cost : `$${ ( totalCost / 100000000 ) . toFixed ( 2 ) } ` ,
265- } )
266- }
267- return mapped
268- } ) ,
269- )
270- /*
271- await printTable("Usage", (tx) =>
272- tx
273- .select({
274- model: UsageTable.model,
275- provider: UsageTable.provider,
276- inputTokens: UsageTable.inputTokens,
277- outputTokens: UsageTable.outputTokens,
278- reasoningTokens: UsageTable.reasoningTokens,
279- cacheReadTokens: UsageTable.cacheReadTokens,
280- cacheWrite5mTokens: UsageTable.cacheWrite5mTokens,
281- cacheWrite1hTokens: UsageTable.cacheWrite1hTokens,
282- cost: UsageTable.cost,
283- timeCreated: UsageTable.timeCreated,
284- })
285- .from(UsageTable)
286- .where(eq(UsageTable.workspaceID, workspace.id))
287- .orderBy(sql`${UsageTable.timeCreated} DESC`)
288- .limit(10)
289- .then((rows) =>
290- rows.map((row) => ({
291- ...row,
292- cost: `$${(row.cost / 100000000).toFixed(2)}`,
293- })),
294- ),
295- )
296- await printTable("Disabled Models", (tx) =>
297- tx
298- .select({
299- model: ModelTable.model,
300- timeCreated: ModelTable.timeCreated,
301- })
302- .from(ModelTable)
303- .where(eq(ModelTable.workspaceID, workspace.id))
304- .orderBy(sql`${ModelTable.timeCreated} DESC`)
305- .then((rows) =>
306- rows.map((row) => ({
307- model: row.model,
308- timeCreated: formatDate(row.timeCreated),
309- })),
310- ),
311- )
312- */
287+ )
288+ }
313289}
314290
315291function formatMicroCents ( value : number | null | undefined ) {
0 commit comments