Skip to content

Commit 16ec810

Browse files
committed
Merge release branch 4.17 to main
* 4.17: utils: fix human-readable parsing failures (#7008)
2 parents 1380c60 + 89d4c75 commit 16ec810

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

utils/src/main/java/com/cloud/utils/HumanReadableJson.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,28 @@
1818
//
1919
package 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+
2423
import java.util.Iterator;
2524
import 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

3033
public 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
}

utils/src/test/java/com/cloud/utils/HumanReadableJsonTest.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818
//
1919
package com.cloud.utils;
2020

21-
import org.junit.Test;
21+
import static com.cloud.utils.HumanReadableJson.getHumanReadableBytesJson;
22+
import static org.junit.Assert.assertEquals;
2223

2324
import java.util.Locale;
24-
import static org.junit.Assert.assertEquals;
25-
import static com.cloud.utils.HumanReadableJson.getHumanReadableBytesJson;
25+
26+
import org.junit.Test;
2627

2728
public class HumanReadableJsonTest {
2829

@@ -63,4 +64,9 @@ public void localeTest() {
6364
Locale.setDefault(Locale.forLanguageTag("en-ZA")); // Other region test
6465
assertEquals("[{\"size\":\"(100,05 KB) 102456\"}]", getHumanReadableBytesJson("[{\"size\": \"102456\"}]"));
6566
}
67+
68+
@Test
69+
public void testNonNumberFieldParsing() {
70+
assertEquals("{\"size\":\"SMALL\",\"newSize\":\"LARGE\"}", getHumanReadableBytesJson("{\"size\": \"SMALL\",\"newSize\": \"LARGE\"}"));
71+
}
6672
}

0 commit comments

Comments
 (0)