@@ -109,93 +109,83 @@ export function getCurrentDirectory() {
109109 */
110110export async function getUsageInfo ( ) {
111111 console . log ( chalk . green ( 'Fetching usage information...\n' ) ) ;
112+ const puter = getPuter ( ) ;
112113 try {
113- const response = await fetch ( `${ API_BASE } /drivers/usage` , {
114- method : 'GET' ,
115- headers : getHeaders ( )
116- } ) ;
117-
118- const data = await response . json ( ) ;
114+ const data = await puter . auth . getMonthlyUsage ( ) ;
119115 if ( data ) {
120- console . log ( chalk . cyan ( 'Usage Information:' ) ) ;
121- console . log ( chalk . dim ( '========================================' ) ) ;
116+ // Display allowance information
117+ if ( data . allowanceInfo ) {
118+ console . log ( chalk . cyan ( 'Allowance Information:' ) ) ;
119+ console . log ( chalk . dim ( '=' . repeat ( 100 ) ) ) ;
120+ console . log ( chalk . cyan ( `Month Usage Allowance: ` ) + chalk . white ( data . allowanceInfo . monthUsageAllowance . toLocaleString ( ) ) ) ;
121+ console . log ( chalk . cyan ( `Remaining: ` ) + chalk . white ( data . allowanceInfo . remaining . toLocaleString ( ) ) ) ;
122+ const usedPercentage = ( ( data . allowanceInfo . monthUsageAllowance - data . allowanceInfo . remaining ) / data . allowanceInfo . monthUsageAllowance * 100 ) . toFixed ( 2 ) ;
123+ console . log ( chalk . cyan ( `Used: ` ) + chalk . white ( `${ usedPercentage } %` ) ) ;
124+ console . log ( chalk . dim ( '=' . repeat ( 100 ) ) ) ;
125+ }
122126
123- // Display user usage in a table
124- if ( data . user && data . user . length > 0 ) {
125- console . log ( chalk . cyan ( 'User Usage:' ) ) ;
126- console . log ( chalk . dim ( '----------------------------------------' ) ) ;
127+ // Display usage information per API
128+ if ( data . usage ) {
129+ console . log ( chalk . cyan ( '\nAPI Usage:' ) ) ;
130+ console . log ( chalk . dim ( '=' . repeat ( 100 ) ) ) ;
127131 console . log (
128- chalk . bold ( 'Service' . padEnd ( 30 ) ) +
129- chalk . bold ( 'Implementation' . padEnd ( 20 ) ) +
130- chalk . bold ( 'Month' . padEnd ( 10 ) ) +
131- chalk . bold ( 'Usage' . padEnd ( 10 ) ) +
132- chalk . bold ( 'Limit' . padEnd ( 10 ) ) +
133- chalk . bold ( 'Rate Limit' )
132+ chalk . bold ( 'API' . padEnd ( 50 ) ) +
133+ chalk . bold ( 'Count' . padEnd ( 15 ) ) +
134+ chalk . bold ( 'Cost' . padEnd ( 20 ) ) +
135+ chalk . bold ( 'Units' )
134136 ) ;
135- console . log ( chalk . dim ( '----------------------------------------' ) ) ;
136- data . user . forEach ( usage => {
137- const service = `${ usage . service [ 'driver.interface' ] } .${ usage . service [ 'driver.method' ] } ` ;
138- const implementation = usage . service [ 'driver.implementation' ] ;
139- const month = `${ usage . month } /${ usage . year } ` ;
140- const monthlyUsage = usage . monthly_usage ?. toString ( ) ;
141- const monthlyLimit = usage . monthly_limit ? usage . monthly_limit . toString ( ) : 'No Limit' ;
142- const rateLimit = usage . policy ? `${ usage . policy [ 'rate-limit' ] . max } req/${ usage . policy [ 'rate-limit' ] . period / 1000 } s` : 'N/A' ;
137+ console . log ( chalk . dim ( '=' . repeat ( 100 ) ) ) ;
143138
139+ // Filter out 'total' and sort entries by cost (descending)
140+ const usageEntries = Object . entries ( data . usage )
141+ . filter ( ( [ key ] ) => key !== 'total' )
142+ . sort ( ( [ , a ] , [ , b ] ) => b . cost - a . cost ) ;
143+
144+ usageEntries . forEach ( ( [ api , details ] ) => {
144145 console . log (
145- service . padEnd ( 30 ) +
146- implementation . padEnd ( 20 ) +
147- month . padEnd ( 10 ) +
148- monthlyUsage . padEnd ( 10 ) +
149- monthlyLimit . padEnd ( 10 ) +
150- rateLimit
146+ api . padEnd ( 50 ) +
147+ details . count . toString ( ) . padEnd ( 15 ) +
148+ details . cost . toLocaleString ( ) . padEnd ( 20 ) +
149+ details . units . toLocaleString ( )
151150 ) ;
152151 } ) ;
153- console . log ( chalk . dim ( '----------------------------------------' ) ) ;
154- }
155152
156- // Display app usage in a table (if available)
157- if ( data . apps && Object . keys ( data . apps ) . length > 0 ) {
158- console . log ( chalk . cyan ( '\nApp Usage:' ) ) ;
159- console . log ( chalk . dim ( '----------------------------------------' ) ) ;
160- console . log (
161- chalk . bold ( 'App' . padEnd ( 30 ) ) +
162- chalk . bold ( 'Usage' . padEnd ( 10 ) ) +
163- chalk . bold ( 'Limit' . padEnd ( 10 ) )
164- ) ;
165- console . log ( chalk . dim ( '----------------------------------------' ) ) ;
166- for ( const [ app , usage ] of Object . entries ( data . apps ) ) {
153+ // Display total if available
154+ if ( data . usage . total !== undefined ) {
155+ console . log ( chalk . dim ( '=' . repeat ( 100 ) ) ) ;
167156 console . log (
168- app . padEnd ( 30 ) +
169- usage . used . toString ( ) . padEnd ( 10 ) +
170- usage . available . toString ( ) . padEnd ( 10 )
157+ chalk . bold ( 'TOTAL' . padEnd ( 50 ) ) +
158+ '' . padEnd ( 15 ) +
159+ chalk . bold ( data . usage . total . toLocaleString ( ) )
171160 ) ;
172161 }
173- console . log ( chalk . dim ( '----------------------------------------' ) ) ;
162+ console . log ( chalk . dim ( '=' . repeat ( 100 ) ) ) ;
174163 }
175164
176- // Display general usages in a table (if available)
177- if ( data . usages && data . usages . length > 0 ) {
178- console . log ( chalk . cyan ( '\nGeneral Usages :' ) ) ;
179- console . log ( chalk . dim ( '----------------------------------------' ) ) ;
165+ // Display app totals
166+ if ( data . appTotals && Object . keys ( data . appTotals ) . length > 0 ) {
167+ console . log ( chalk . cyan ( '\nApp Totals :' ) ) ;
168+ console . log ( chalk . dim ( '=' . repeat ( 100 ) ) ) ;
180169 console . log (
181- chalk . bold ( 'Name' . padEnd ( 30 ) ) +
182- chalk . bold ( 'Used' . padEnd ( 10 ) ) +
183- chalk . bold ( 'Available' . padEnd ( 10 ) ) +
184- chalk . bold ( 'Refill' )
170+ chalk . bold ( 'App' . padEnd ( 50 ) ) +
171+ chalk . bold ( 'Count' . padEnd ( 15 ) ) +
172+ chalk . bold ( 'Total' )
185173 ) ;
186- console . log ( chalk . dim ( '----------------------------------------' ) ) ;
187- data . usages . forEach ( usage => {
174+ console . log ( chalk . dim ( '=' . repeat ( 100 ) ) ) ;
175+
176+ // Sort by total (descending)
177+ const appEntries = Object . entries ( data . appTotals )
178+ . sort ( ( [ , a ] , [ , b ] ) => b . total - a . total ) ;
179+
180+ appEntries . forEach ( ( [ app , details ] ) => {
188181 console . log (
189- usage . name . padEnd ( 30 ) +
190- usage . used . toString ( ) . padEnd ( 10 ) +
191- usage . available . toString ( ) . padEnd ( 10 ) +
192- usage . refill
182+ app . padEnd ( 50 ) +
183+ details . count . toString ( ) . padEnd ( 15 ) +
184+ details . total . toLocaleString ( )
193185 ) ;
194186 } ) ;
195- console . log ( chalk . dim ( '----------------------------------------' ) ) ;
187+ console . log ( chalk . dim ( '=' . repeat ( 100 ) ) ) ;
196188 }
197-
198- console . log ( chalk . dim ( '========================================' ) ) ;
199189 console . log ( chalk . green ( 'Done.' ) ) ;
200190 } else {
201191 console . error ( chalk . red ( 'Unable to fetch usage information.' ) ) ;
0 commit comments