This is an automated email from the ASF dual-hosted git repository.
dahn pushed a commit to branch 4.17
in repository https://gitbox.apache.org/repos/asf/cloudstack.git
The following commit(s) were added to refs/heads/4.17 by this push:
new 89d4c7537fd utils: fix human-readable parsing failures (#7008)
89d4c7537fd is described below
commit 89d4c7537fde8092f99c0fc61f8a5e5921ea5733
Author: Abhishek Kumar <[email protected]>
AuthorDate: Wed Jan 4 16:04:34 2023 +0530
utils: fix human-readable parsing failures (#7008)
Signed-off-by: Abhishek Kumar <[email protected]>
---
.../java/com/cloud/utils/HumanReadableJson.java | 26 ++++++++++++++++------
.../com/cloud/utils/HumanReadableJsonTest.java | 12 +++++++---
2 files changed, 28 insertions(+), 10 deletions(-)
diff --git a/utils/src/main/java/com/cloud/utils/HumanReadableJson.java
b/utils/src/main/java/com/cloud/utils/HumanReadableJson.java
index eeb1a70b747..b751a3a5d43 100644
--- a/utils/src/main/java/com/cloud/utils/HumanReadableJson.java
+++ b/utils/src/main/java/com/cloud/utils/HumanReadableJson.java
@@ -18,21 +18,28 @@
//
package com.cloud.utils;
-import com.google.gson.JsonArray;
-import com.google.gson.JsonElement;
-import com.google.gson.JsonParser;
+import static com.cloud.utils.NumbersUtil.toHumanReadableSize;
+
import java.util.Iterator;
import java.util.Map.Entry;
-import static com.cloud.utils.NumbersUtil.toHumanReadableSize;
+import org.apache.log4j.Logger;
+
+import com.google.gson.JsonArray;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonParser;
public class HumanReadableJson {
+ static final Logger LOGGER = Logger.getLogger(HumanReadableJson.class);
+
private boolean changeValue;
private StringBuilder output = new StringBuilder();
private boolean firstElement = true;
+ private String lastKey;
+
private final String[] elementsToMatch = {
"bytesSent","bytesReceived","BytesWrite","BytesRead","bytesReadRate","bytesWriteRate","iopsReadRate",
"iopsWriteRate","ioRead","ioWrite","bytesWrite","bytesRead","networkkbsread","networkkbswrite",
@@ -64,11 +71,15 @@ public class HumanReadableJson {
firstElement = false;
}
if (jsonElement.isJsonPrimitive()) {
+ String changedValue = jsonElement.getAsString();
if (changeValue) {
- output.append("\"" +
toHumanReadableSize(jsonElement.getAsLong()) + "\"");
- } else {
- output.append("\"" + jsonElement.getAsString() + "\"");
+ try {
+ changedValue =
toHumanReadableSize(jsonElement.getAsLong());
+ } catch (NumberFormatException nfe) {
+ LOGGER.debug(String.format("Unable to parse '%s' with
value: %s to human readable number format. Returning as it is", lastKey,
changedValue), nfe);
+ }
}
+ output.append("\"").append(changedValue).append("\"");
firstElement = false;
}
}
@@ -81,6 +92,7 @@ public class HumanReadableJson {
while(it.hasNext()) {
Entry<String, JsonElement> value = it.next();
String key = value.getKey();
+ lastKey = key;
if (!firstElement){
output.append(",");
}
diff --git a/utils/src/test/java/com/cloud/utils/HumanReadableJsonTest.java
b/utils/src/test/java/com/cloud/utils/HumanReadableJsonTest.java
index de96756fcb9..27d0dfd4292 100644
--- a/utils/src/test/java/com/cloud/utils/HumanReadableJsonTest.java
+++ b/utils/src/test/java/com/cloud/utils/HumanReadableJsonTest.java
@@ -18,11 +18,12 @@
//
package com.cloud.utils;
-import org.junit.Test;
+import static com.cloud.utils.HumanReadableJson.getHumanReadableBytesJson;
+import static org.junit.Assert.assertEquals;
import java.util.Locale;
-import static org.junit.Assert.assertEquals;
-import static com.cloud.utils.HumanReadableJson.getHumanReadableBytesJson;
+
+import org.junit.Test;
public class HumanReadableJsonTest {
@@ -63,4 +64,9 @@ public class HumanReadableJsonTest {
Locale.setDefault(Locale.forLanguageTag("en-ZA")); // Other region test
assertEquals("[{\"size\":\"(100,05 KB) 102456\"}]",
getHumanReadableBytesJson("[{\"size\": \"102456\"}]"));
}
+
+ @Test
+ public void testNonNumberFieldParsing() {
+ assertEquals("{\"size\":\"SMALL\",\"newSize\":\"LARGE\"}",
getHumanReadableBytesJson("{\"size\": \"SMALL\",\"newSize\": \"LARGE\"}"));
+ }
}