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; package com.cloud.utils;
import com.google.gson.JsonArray; import static com.cloud.utils.NumbersUtil.toHumanReadableSize;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map.Entry; 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 { public class HumanReadableJson {
static final Logger LOGGER = Logger.getLogger(HumanReadableJson.class);
private boolean changeValue; private boolean changeValue;
private StringBuilder output = new StringBuilder(); private StringBuilder output = new StringBuilder();
private boolean firstElement = true; private boolean firstElement = true;
private String lastKey;
private final String[] elementsToMatch = { private final String[] elementsToMatch = {
"bytesSent","bytesReceived","BytesWrite","BytesRead","bytesReadRate","bytesWriteRate","iopsReadRate", "bytesSent","bytesReceived","BytesWrite","BytesRead","bytesReadRate","bytesWriteRate","iopsReadRate",
"iopsWriteRate","ioRead","ioWrite","bytesWrite","bytesRead","networkkbsread","networkkbswrite", "iopsWriteRate","ioRead","ioWrite","bytesWrite","bytesRead","networkkbsread","networkkbswrite",
@ -64,11 +71,15 @@ public class HumanReadableJson {
firstElement = false; firstElement = false;
} }
if (jsonElement.isJsonPrimitive()) { if (jsonElement.isJsonPrimitive()) {
String changedValue = jsonElement.getAsString();
if (changeValue) { if (changeValue) {
output.append("\"" + toHumanReadableSize(jsonElement.getAsLong()) + "\""); try {
} else { changedValue = toHumanReadableSize(jsonElement.getAsLong());
output.append("\"" + jsonElement.getAsString() + "\""); } 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; firstElement = false;
} }
} }
@ -81,6 +92,7 @@ public class HumanReadableJson {
while(it.hasNext()) { while(it.hasNext()) {
Entry<String, JsonElement> value = it.next(); Entry<String, JsonElement> value = it.next();
String key = value.getKey(); String key = value.getKey();
lastKey = key;
if (!firstElement){ if (!firstElement){
output.append(","); output.append(",");
} }

View File

@ -18,11 +18,12 @@
// //
package com.cloud.utils; 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 java.util.Locale;
import static org.junit.Assert.assertEquals;
import static com.cloud.utils.HumanReadableJson.getHumanReadableBytesJson; import org.junit.Test;
public class HumanReadableJsonTest { public class HumanReadableJsonTest {
@ -63,4 +64,9 @@ public class HumanReadableJsonTest {
Locale.setDefault(Locale.forLanguageTag("en-ZA")); // Other region test Locale.setDefault(Locale.forLanguageTag("en-ZA")); // Other region test
assertEquals("[{\"size\":\"(100,05 KB) 102456\"}]", getHumanReadableBytesJson("[{\"size\": \"102456\"}]")); assertEquals("[{\"size\":\"(100,05 KB) 102456\"}]", getHumanReadableBytesJson("[{\"size\": \"102456\"}]"));
} }
@Test
public void testNonNumberFieldParsing() {
assertEquals("{\"size\":\"SMALL\",\"newSize\":\"LARGE\"}", getHumanReadableBytesJson("{\"size\": \"SMALL\",\"newSize\": \"LARGE\"}"));
}
} }