utils: fix human-readable parsing failures (#7008)

Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>
This commit is contained in:
Abhishek Kumar 2023-01-04 16:04:34 +05:30 committed by GitHub
parent e8aff6f445
commit 89d4c7537f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 10 deletions

View File

@ -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(",");
}

View File

@ -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\"}"));
}
}