From 4dd7db1509baabde6aa1a4c710533bc8f51fbf68 Mon Sep 17 00:00:00 2001 From: Spaceman1984 <49917670+Spaceman1984@users.noreply.github.com> Date: Tue, 13 Apr 2021 14:37:50 +0200 Subject: [PATCH] server: Fixed hosts not displaying with incompatible locale (#4900) Fixes: #4733 --- .../response/HostMetricsResponse.java | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/plugins/metrics/src/main/java/org/apache/cloudstack/response/HostMetricsResponse.java b/plugins/metrics/src/main/java/org/apache/cloudstack/response/HostMetricsResponse.java index 484cd068945..30a3fb73670 100644 --- a/plugins/metrics/src/main/java/org/apache/cloudstack/response/HostMetricsResponse.java +++ b/plugins/metrics/src/main/java/org/apache/cloudstack/response/HostMetricsResponse.java @@ -18,10 +18,14 @@ package org.apache.cloudstack.response; import com.cloud.serializer.Param; +import com.cloud.utils.exception.CloudRuntimeException; import com.google.gson.annotations.SerializedName; import org.apache.cloudstack.api.response.HostResponse; import org.apache.cloudstack.outofbandmanagement.OutOfBandManagement; +import java.text.DecimalFormat; +import java.text.ParseException; + public class HostMetricsResponse extends HostResponse { @SerializedName("powerstate") @Param(description = "out-of-band management power state") @@ -117,7 +121,7 @@ public class HostMetricsResponse extends HostResponse { public void setCpuUsed(final String cpuUsed, final Integer cpuNumber, final Long cpuSpeed) { if (cpuUsed != null && cpuNumber != null && cpuSpeed != null) { - this.cpuUsed = String.format("%.2f Ghz", Double.valueOf(cpuUsed.replace("%", "")) * cpuNumber * cpuSpeed / (100.0 * 1000.0)); + this.cpuUsed = String.format("%.2f Ghz", parseCPU(cpuUsed) * cpuNumber * cpuSpeed / (100.0 * 1000.0)); } } @@ -129,7 +133,7 @@ public class HostMetricsResponse extends HostResponse { public void setCpuAllocated(final String cpuAllocated, final Integer cpuNumber, final Long cpuSpeed) { if (cpuAllocated != null && cpuNumber != null && cpuSpeed != null) { - this.cpuAllocated = String.format("%.2f Ghz", Double.valueOf(cpuAllocated.replace("%", "")) * cpuNumber * cpuSpeed / (100.0 * 1000.0)); + this.cpuAllocated = String.format("%.2f Ghz", parseCPU(cpuAllocated) * cpuNumber * cpuSpeed / (100.0 * 1000.0)); } } @@ -165,25 +169,25 @@ public class HostMetricsResponse extends HostResponse { public void setCpuUsageThreshold(final String cpuUsed, final Double threshold) { if (cpuUsed != null && threshold != null) { - this.cpuThresholdExceeded = Double.valueOf(cpuUsed.replace("%", "")) > (100.0 * threshold); + this.cpuThresholdExceeded = parseCPU(cpuUsed) > (100.0 * threshold); } } public void setCpuUsageDisableThreshold(final String cpuUsed, final Float threshold) { if (cpuUsed != null && threshold != null) { - this.cpuDisableThresholdExceeded = Double.valueOf(cpuUsed.replace("%", "")) > (100.0 * threshold); + this.cpuDisableThresholdExceeded = parseCPU(cpuUsed) > (100.0 * threshold); } } public void setCpuAllocatedThreshold(final String cpuAllocated, final Double overCommitRatio, final Double threshold) { if (cpuAllocated != null && overCommitRatio != null && threshold != null) { - this.cpuAllocatedThresholdExceeded = Double.valueOf(cpuAllocated.replace("%", "")) > (100.0 * threshold * overCommitRatio); + this.cpuAllocatedThresholdExceeded = parseCPU(cpuAllocated) > (100.0 * threshold * overCommitRatio); } } public void setCpuAllocatedDisableThreshold(final String cpuAllocated, final Double overCommitRatio, final Float threshold) { if (cpuAllocated != null && overCommitRatio != null && threshold != null) { - this.cpuAllocatedDisableThresholdExceeded = Double.valueOf(cpuAllocated.replace("%", "")) > (100.0 * threshold * overCommitRatio); + this.cpuAllocatedDisableThresholdExceeded = parseCPU(cpuAllocated) > (100.0 * threshold * overCommitRatio); } } @@ -211,4 +215,13 @@ public class HostMetricsResponse extends HostResponse { } } + private Double parseCPU(String cpu) { + DecimalFormat decimalFormat = new DecimalFormat("#.##"); + try { + return decimalFormat.parse(cpu).doubleValue(); + } catch (ParseException e) { + throw new CloudRuntimeException(e); + } + } + }