11package org .ebean .monitor .cli ;
22
33import java .util .List ;
4+ import java .util .Locale ;
45import java .util .concurrent .Callable ;
56
67import org .ebean .monitor .v1 .model .TopGroup ;
@@ -115,7 +116,6 @@ public Integer call() {
115116 if (env == null ) {
116117 env = ConfigDefaults .envOrNull ();
117118 }
118- final boolean byHash = "hash" .equalsIgnoreCase (by );
119119 final boolean gauge = sort == Sort .value ;
120120 try (Insight insight = Insight .open (conn )) {
121121 java .util .function .Function <String , List <TopGroup >> fetch = ob -> (app == null )
@@ -140,57 +140,88 @@ public Integer call() {
140140 Charts .printPareto (rows , sort );
141141 return 0 ;
142142 }
143- printTable (rows , byHash , gauge );
143+ printTable (rows , gauge );
144144 return 0 ;
145145 }
146146 }
147147
148- private void printTable (List <TopGroup > rows , boolean byHash , boolean gauge ) {
148+ private void printTable (List <TopGroup > rows , boolean gauge ) {
149+ final boolean byHash = "hash" .equalsIgnoreCase (by );
150+ final boolean byName = "name" .equalsIgnoreCase (by );
151+ // A "tag" dimension is any grouping other than name/label/hash (e.g. type, kind):
152+ // NAME and LABEL aren't single-valued, so the tag value gets its own column.
153+ final boolean tagDim = !byHash && !byName && !"label" .equalsIgnoreCase (by );
154+ final String tagHeader = by .toUpperCase (Locale .ROOT );
155+
149156 int appWidth = "APP" .length ();
150- int groupWidth = (byHash ? "LABEL" : "GROUP" ).length ();
157+ int nameWidth = "NAME" .length ();
158+ int labelWidth = "LABEL" .length ();
159+ int tagWidth = tagHeader .length ();
151160 for (TopGroup r : rows ) {
152161 if (r .app () != null ) {
153162 appWidth = Math .max (appWidth , r .app ().length ());
154163 }
155- String groupValue = byHash ? (r .label () == null ? r .group () : r .label ()) : r .group ();
156- if (groupValue != null ) {
157- groupWidth = Math .max (groupWidth , groupValue .length ());
164+ nameWidth = Math .max (nameWidth , nv (r .name ()).length ());
165+ labelWidth = Math .max (labelWidth , nv (r .label ()).length ());
166+ if (tagDim ) {
167+ tagWidth = Math .max (tagWidth , nv (r .group ()).length ());
158168 }
159169 }
170+
171+ String idFmt = "%-" + appWidth + "s %-" + nameWidth + "s %-" + labelWidth + "s"
172+ + (tagDim ? " %-" + tagWidth + "s" : "" );
173+
160174 if (gauge ) {
161- String headFmt = "%-" + appWidth + "s %-" + groupWidth + "s %16s %8s %6s%n" ;
162- String rowFmt = "%-" + appWidth + "s %-" + groupWidth + "s %16s %8d %6d%n" ;
163- System .out .printf (headFmt , "APP" , "GROUP" , "VALUE" , "WINDOW" , "HASHES" );
175+ System .out .printf (idFmt + " %16s %8s %6s%n" , idHead (tagDim , tagHeader , "VALUE" , "WINDOW" , "HASHES" ));
164176 for (TopGroup r : rows ) {
165- System .out .printf (rowFmt , nv (r .app ()), nv (r .group ()),
166- r .value () == null ? "" : String .format ("%,.0f" , r .value ()),
167- r .windowMinutes (), r .hashCount ());
177+ System .out .printf (idFmt + " %16s %8d %6d%n" , idRow (r , tagDim ,
178+ r .value () == null ? "" : String .format ("%,.0f" , r .value ()), r .windowMinutes (), r .hashCount ()));
168179 }
169180 return ;
170181 }
171182 if (byHash ) {
172- String headFmt = "%-" + appWidth + "s %-" + groupWidth + "s %10s %16s %14s %14s %8s %5s %s%n" ;
173- String rowFmt = "%-" + appWidth + "s %-" + groupWidth + "s %10d %16d %14d %14d %8d %5s %s%n" ;
174- System .out .printf (headFmt ,
175- "APP" , "LABEL" , "COUNT" , "TOTAL(us)" , "MEAN(us)" , "MAX(us)" , "WINDOW" , "PLAN" , "HASH" );
183+ System .out .printf (idFmt + " %10s %16s %14s %14s %8s %5s %s%n" ,
184+ idHead (false , tagHeader , "COUNT" , "TOTAL(us)" , "MEAN(us)" , "MAX(us)" , "WINDOW" , "PLAN" , "HASH" ));
176185 for (TopGroup r : rows ) {
177- System .out .printf (rowFmt ,
178- nv (r .app ()), nv (r .label () == null ? r .group () : r .label ()),
186+ System .out .printf (idFmt + " %10d %16d %14d %14d %8d %5s %s%n" , idRow (r , false ,
179187 z (r .count ()), z (r .totalMicros ()), z (r .meanMicros ()), z (r .maxMicros ()),
180- r .windowMinutes (), Boolean .TRUE .equals (r .planCapable ()) ? "yes" : "no" , nv (r .key ()));
188+ r .windowMinutes (), Boolean .TRUE .equals (r .planCapable ()) ? "yes" : "no" , nv (r .key ()))) ;
181189 }
182190 return ;
183191 }
184- String headFmt = "%-" + appWidth + "s %-" + groupWidth + "s %10s %16s %14s %14s %8s %6s%n" ;
185- String rowFmt = "%-" + appWidth + "s %-" + groupWidth + "s %10d %16d %14d %14d %8d %6d%n" ;
186- System .out .printf (headFmt ,
187- "APP" , "GROUP" , "COUNT" , "TOTAL(us)" , "MEAN(us)" , "MAX(us)" , "WINDOW" , "HASHES" );
192+ System .out .printf (idFmt + " %10s %16s %14s %14s %8s %6s%n" ,
193+ idHead (tagDim , tagHeader , "COUNT" , "TOTAL(us)" , "MEAN(us)" , "MAX(us)" , "WINDOW" , "HASHES" ));
188194 for (TopGroup r : rows ) {
189- System .out .printf (rowFmt ,
190- nv (r .app ()), nv (r .group ()),
195+ System .out .printf (idFmt + " %10d %16d %14d %14d %8d %6d%n" , idRow (r , tagDim ,
191196 z (r .count ()), z (r .totalMicros ()), z (r .meanMicros ()), z (r .maxMicros ()),
192- r .windowMinutes (), r .hashCount ());
197+ r .windowMinutes (), r .hashCount ()));
198+ }
199+ }
200+
201+ /** Header cells: APP, NAME, LABEL, [tag], then the supplied measure headers. */
202+ private static Object [] idHead (boolean tagDim , String tagHeader , Object ... rest ) {
203+ List <Object > cells = new java .util .ArrayList <>();
204+ cells .add ("APP" );
205+ cells .add ("NAME" );
206+ cells .add ("LABEL" );
207+ if (tagDim ) {
208+ cells .add (tagHeader );
209+ }
210+ java .util .Collections .addAll (cells , rest );
211+ return cells .toArray ();
212+ }
213+
214+ /** Row cells: app, name, label, [tag value], then the supplied measure values. */
215+ private static Object [] idRow (TopGroup r , boolean tagDim , Object ... rest ) {
216+ List <Object > cells = new java .util .ArrayList <>();
217+ cells .add (nv (r .app ()));
218+ cells .add (nv (r .name ()));
219+ cells .add (nv (r .label ()));
220+ if (tagDim ) {
221+ cells .add (nv (r .group ()));
193222 }
223+ java .util .Collections .addAll (cells , rest );
224+ return cells .toArray ();
194225 }
195226
196227 private static String nv (@ Nullable String s ) {
0 commit comments