mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
utils: fix human-readable parsing failures (#7008)
Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>
This commit is contained in:
parent
e8aff6f445
commit
89d4c7537f
@ -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(",");
|
||||
}
|
||||
|
||||
@ -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\"}"));
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user