@@ -29,6 +29,7 @@ const DEBUG_RENDER_MUL = parseInt(process.env["DEBUG_RENDER_MUL"] ?? "0") || 0;
2929export async function actionList ( args : Args ) : Promise < boolean > {
3030 const weightSql = args [ "weight-sql" ] || undefined ;
3131 const verbose = ! ! args [ "verbose" ] ;
32+ const json = ! ! args [ "json" ] ;
3233
3334 const dsns = await normalizeDsns ( args [ "dsns" ] || args [ "dsn" ] ) ;
3435 if ( dsns . length === 0 ) {
@@ -105,86 +106,108 @@ export async function actionList(args: Args): Promise<boolean> {
105106 ( ( stdout . columns || 80 ) - 10 ) / tableWidth ,
106107 ) ;
107108
108- for ( const [ islandNo , shards ] of islands ) {
109- const dsn = islandNosToDsn . get ( islandNo ) ! ;
110- print . section (
111- `${ dsnShort ( dsn ) } — ` + `${ pluralize ( shards . length , "microshard" ) } ` ,
112- ) ;
113- if ( shards . length === 0 ) {
114- print ( indent ( table ( [ [ "No microshards" ] ] ) ) ) ;
115- continue ;
116- }
117-
118- const totalWeight : number = sumBy ( shards , ( shard ) => shard . weight ) ;
119- const totalUnit : string | undefined = shards [ 0 ] . unit ;
120- const totalOther : Record < string , string | undefined > = mapValues (
121- shards [ 0 ] . other ,
122- ( _ , k ) => {
109+ const toPrint = {
110+ islands : [ ...islands ] . map ( ( [ islandNo , shards ] ) => {
111+ const dsn = islandNosToDsn . get ( islandNo ) ! ;
112+ const totalWeight = sumBy ( shards , ( shard ) => shard . weight ) ;
113+ const totalUnit = first ( shards ) ?. unit ;
114+ const totalOther = mapValues ( first ( shards ) ?. other , ( _ , k ) => {
123115 const values = shards
124116 . map ( ( { other } ) => parseFloat ( other [ k ] ) )
125117 . filter ( ( v ) => ! isNaN ( v ) ) ;
126118 return values . length > 0 ? sum ( values ) . toString ( ) : undefined ;
127- } ,
128- ) ;
119+ } ) ;
120+ return {
121+ no : islandNo ,
122+ caption :
123+ `${ dsnShort ( dsn ) } — ` + `${ pluralize ( shards . length , "microshard" ) } ` ,
124+ dsn : dsnShort ( dsn ) ,
125+ header,
126+ shards : shards . map (
127+ ( { no, weight, appliedFactor, unit, otherNormalized } ) => [
128+ no . toString ( ) ,
129+ round ( weight / appliedFactor , 5 ) . toString ( ) +
130+ ( unit ? ` ${ unit } ` : "" ) +
131+ ( appliedFactor !== 1 ? ` ✖${ appliedFactor } ` : "" ) ,
132+ totalWeight !== 0
133+ ? `${ ( ( weight / totalWeight ) * 100 ) . toFixed ( 1 ) } %`
134+ : "-" ,
135+ ...Object . values ( otherNormalized ) ,
136+ ] ,
137+ ) ,
138+ total :
139+ shards . length > 0
140+ ? [
141+ "total" ,
142+ totalWeight + ( totalUnit ? ` ${ totalUnit } ` : "" ) ,
143+ "100%" ,
144+ ...Object . values ( totalOther ) . map ( ( v ) => v ?? "" ) ,
145+ ]
146+ : undefined ,
147+ } ;
148+ } ) ,
149+ total :
150+ `TOTAL ${ grandTotalWeightColName . toUpperCase ( ) } : ${ grandTotalWeight } ` +
151+ ( grandTotalUnit ? ` ${ grandTotalUnit } ` : "" ) +
152+ `, ${ pluralize ( islands . size , "island" ) } , ${ pluralize ( grandTotalShards , "microshard" ) } ` ,
153+ } ;
154+
155+ if ( json ) {
156+ print ( JSON . stringify ( toPrint , null , 2 ) + "\n" ) ;
157+ return true ;
158+ }
159+
160+ for ( const island of toPrint . islands ) {
161+ print . section ( island . caption ) ;
162+
163+ if ( island . shards . length === 0 ) {
164+ print ( indent ( table ( [ [ "No microshards" ] ] ) ) ) ;
165+ continue ;
166+ }
129167
130- const shardTables =
168+ const tables =
169+ json ||
131170 islands . size === 1 ||
132171 maxTablesSideBySide === 0 ||
133172 grandTotalShards + islands . size * 8 < ( stdout . rows || 25 ) ||
134- shards . length <= 4
135- ? [ shards ]
136- : shards . length / maxTablesSideBySide < 2
137- ? chunk ( shards , 2 )
138- : chunkIntoN ( shards , maxTablesSideBySide ) ;
139- const tables = shardTables . map ( ( shards , tableNo ) =>
140- indent (
141- table (
142- compact ( [
143- header ,
144- ...shards . map (
145- ( { no, weight, appliedFactor, unit, otherNormalized } ) =>
146- [
147- no . toString ( ) ,
148- round ( weight / appliedFactor , 5 ) . toString ( ) +
149- ( unit ? ` ${ unit } ` : "" ) +
150- ( appliedFactor !== 1 ? ` ✖${ appliedFactor } ` : "" ) ,
151- totalWeight !== 0
152- ? `${ ( ( weight / totalWeight ) * 100 ) . toFixed ( 1 ) } %`
153- : "-" ,
154- ...Object . values ( otherNormalized ) ,
155- ] . map ( ( cell ) => ( no === 0 ? chalk . yellow ( cell ) : cell ) ) ,
173+ island . shards . length <= 4
174+ ? [ island . shards ]
175+ : island . shards . length / maxTablesSideBySide < 2
176+ ? chunk ( island . shards , 2 )
177+ : chunkIntoN ( island . shards , maxTablesSideBySide ) ;
178+ print (
179+ glueHorizontally (
180+ tables . map ( ( tbl , tblNo ) =>
181+ indent (
182+ table (
183+ compact ( [
184+ island . header . map ( ( cell ) => chalk . bold ( cell ) ) ,
185+ ...tbl . map ( ( row ) =>
186+ row [ 0 ] . trim ( ) === "0"
187+ ? row . map ( ( cell ) => chalk . yellow ( cell ) )
188+ : row ,
189+ ) ,
190+ tblNo === 0 &&
191+ island . total &&
192+ island . total . map ( ( cell ) => chalk . bold ( cell ) ) ,
193+ ] ) ,
194+ {
195+ drawHorizontalLine : ( i , rowCount ) =>
196+ i === 0 ||
197+ i === 1 ||
198+ ( tblNo === 0 && i === rowCount - 1 ) ||
199+ i === rowCount ,
200+ } ,
156201 ) ,
157- tableNo === 0 &&
158- [
159- "total" ,
160- totalWeight + ( totalUnit ? ` ${ totalUnit } ` : "" ) ,
161- "100%" ,
162- ...Object . values ( totalOther ) . map ( ( v ) => v ?? "" ) ,
163- ] . map ( ( cell ) => chalk . bold ( cell ) ) ,
164- ] ) ,
165- {
166- drawHorizontalLine : ( i , rowCount ) =>
167- i === 0 ||
168- i === 1 ||
169- ( tableNo === 0 && i === rowCount - 1 ) ||
170- i === rowCount ,
171- } ,
202+ ) ,
172203 ) ,
173204 ) ,
174205 ) ;
175- print ( glueHorizontally ( tables ) ) ;
176206 }
177207
178208 print (
179- chalk . bgBlue (
180- chalk . whiteBright (
181- " " +
182- `TOTAL ${ grandTotalWeightColName . toUpperCase ( ) } : ${ grandTotalWeight } ` +
183- ( grandTotalUnit ? ` ${ grandTotalUnit } ` : "" ) +
184- `, ${ pluralize ( islands . size , "island" ) } , ${ pluralize ( grandTotalShards , "microshard" ) } ` +
185- " " ,
186- ) ,
187- ) + ( verbose && ! weightSql ? "" : " (try --verbose)" ) ,
209+ chalk . bgBlue ( " " + chalk . whiteBright ( toPrint . total ) + " " ) +
210+ ( verbose && ! weightSql ? "" : " (try --verbose)" ) ,
188211 ) ;
189212
190213 return true ;
0 commit comments