From 8658cd58889921fe407f67e3ed21282f9b51dde4 Mon Sep 17 00:00:00 2001 From: Darrin Husselmann Date: Thu, 8 Apr 2021 12:18:47 +0200 Subject: [PATCH 1/3] Fixed hosts not displaying with incompatible locale --- .../cloudstack/response/HostMetricsResponse.java | 12 ++++++------ 1 file changed, 6 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 484cd068945d..02403df047d7 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 @@ -117,7 +117,7 @@ public void setCpuTotal(final Integer cpuNumber, final Long cpuSpeed, final Doub 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", Double.valueOf(cpuUsed.replace(",",".").replace("%", "")) * cpuNumber * cpuSpeed / (100.0 * 1000.0)); } } @@ -129,7 +129,7 @@ public void setLoadAverage(final Double loadAverage) { 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", Double.valueOf(cpuAllocated.replace(",",".").replace("%", "")) * cpuNumber * cpuSpeed / (100.0 * 1000.0)); } } @@ -165,25 +165,25 @@ public void setNetworkWrite(final Long networkWriteKbs) { 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 = Double.valueOf(cpuUsed.replace(",",".").replace("%", "")) > (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 = Double.valueOf(cpuUsed.replace(",",".").replace("%", "")) > (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 = Double.valueOf(cpuAllocated.replace(",",".").replace("%", "")) > (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 = Double.valueOf(cpuAllocated.replace(",",".").replace("%", "")) > (100.0 * threshold * overCommitRatio); } } From 6cd7909a778546b3c94b4093269728c585cc22f8 Mon Sep 17 00:00:00 2001 From: Darrin Husselmann Date: Thu, 8 Apr 2021 16:45:38 +0200 Subject: [PATCH 2/3] Improved solution --- .../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 02403df047d7..0de4f8eba8cf 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 void setCpuTotal(final Integer cpuNumber, final Long cpuSpeed, final Doub 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(",",".").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 void setLoadAverage(final Double loadAverage) { 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(",",".").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 void setNetworkWrite(final Long networkWriteKbs) { public void setCpuUsageThreshold(final String cpuUsed, final Double threshold) { if (cpuUsed != null && threshold != null) { - this.cpuThresholdExceeded = Double.valueOf(cpuUsed.replace(",",".").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(",",".").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(",",".").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(",",".").replace("%", "")) > (100.0 * threshold * overCommitRatio); + this.cpuAllocatedDisableThresholdExceeded = parseCPU(cpuAllocated) > (100.0 * threshold * overCommitRatio); } } @@ -211,4 +215,13 @@ public void setMemoryAllocatedDisableThreshold(final Long memAllocated, final Lo } } + private Double parseCPU(String cpu) { + DecimalFormat decimalFormat = new DecimalFormat("#.##"); + try { + return (Double)decimalFormat.parse(cpu); + } catch (ParseException e) { + throw new CloudRuntimeException(e); + } + } + } From 7b483b19cdf0f25a74665cdeed3004cfa9854fb2 Mon Sep 17 00:00:00 2001 From: Darrin Husselmann Date: Mon, 12 Apr 2021 11:00:22 +0200 Subject: [PATCH 3/3] Using method instead of cast --- .../org/apache/cloudstack/response/HostMetricsResponse.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 0de4f8eba8cf..30a3fb736700 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 @@ -218,7 +218,7 @@ public void setMemoryAllocatedDisableThreshold(final Long memAllocated, final Lo private Double parseCPU(String cpu) { DecimalFormat decimalFormat = new DecimalFormat("#.##"); try { - return (Double)decimalFormat.parse(cpu); + return decimalFormat.parse(cpu).doubleValue(); } catch (ParseException e) { throw new CloudRuntimeException(e); }