1818//
1919package com .cloud .utils ;
2020
21- import com .google .gson .JsonArray ;
22- import com .google .gson .JsonElement ;
23- import com .google .gson .JsonParser ;
21+ import static com .cloud .utils .NumbersUtil .toHumanReadableSize ;
22+
2423import java .util .Iterator ;
2524import java .util .Map .Entry ;
2625
27- import static com .cloud .utils .NumbersUtil .toHumanReadableSize ;
26+ import org .apache .log4j .Logger ;
27+
28+ import com .google .gson .JsonArray ;
29+ import com .google .gson .JsonElement ;
30+ import com .google .gson .JsonParser ;
2831
2932
3033public class HumanReadableJson {
3134
35+ static final Logger LOGGER = Logger .getLogger (HumanReadableJson .class );
36+
3237 private boolean changeValue ;
3338 private StringBuilder output = new StringBuilder ();
3439 private boolean firstElement = true ;
3540
41+ private String lastKey ;
42+
3643 private final String [] elementsToMatch = {
3744 "bytesSent" ,"bytesReceived" ,"BytesWrite" ,"BytesRead" ,"bytesReadRate" ,"bytesWriteRate" ,"iopsReadRate" ,
3845 "iopsWriteRate" ,"ioRead" ,"ioWrite" ,"bytesWrite" ,"bytesRead" ,"networkkbsread" ,"networkkbswrite" ,
@@ -64,11 +71,15 @@ private void addElement(String content) {
6471 firstElement = false ;
6572 }
6673 if (jsonElement .isJsonPrimitive ()) {
74+ String changedValue = jsonElement .getAsString ();
6775 if (changeValue ) {
68- output .append ("\" " + toHumanReadableSize (jsonElement .getAsLong ()) + "\" " );
69- } else {
70- output .append ("\" " + jsonElement .getAsString () + "\" " );
76+ try {
77+ changedValue = toHumanReadableSize (jsonElement .getAsLong ());
78+ } catch (NumberFormatException nfe ) {
79+ LOGGER .debug (String .format ("Unable to parse '%s' with value: %s to human readable number format. Returning as it is" , lastKey , changedValue ), nfe );
80+ }
7181 }
82+ output .append ("\" " ).append (changedValue ).append ("\" " );
7283 firstElement = false ;
7384 }
7485 }
@@ -81,6 +92,7 @@ private void addObject(String content) {
8192 while (it .hasNext ()) {
8293 Entry <String , JsonElement > value = it .next ();
8394 String key = value .getKey ();
95+ lastKey = key ;
8496 if (!firstElement ){
8597 output .append ("," );
8698 }
0 commit comments