This is an automated email from the ASF dual-hosted git repository.

nvazquez pushed a commit to branch 4.15
in repository https://gitbox.apache.org/repos/asf/cloudstack.git


The following commit(s) were added to refs/heads/4.15 by this push:
     new 6446797  metrics: fix hostsmetricsresponse for zero cpu, locale (#5329)
6446797 is described below

commit 6446797fdc546cb05a1f207503e682e577483b7a
Author: Abhishek Kumar <abhishek.mr...@gmail.com>
AuthorDate: Thu Aug 19 10:00:01 2021 +0530

    metrics: fix hostsmetricsresponse for zero cpu, locale (#5329)
    
    * server: Fixed hosts not displaying with incompatible locale (#4900)
    
    Fixes: #4733
    
    * added unit test
    
    Signed-off-by: Abhishek Kumar <abhishek.mr...@gmail.com>
    
    * eof newline
    
    Signed-off-by: Abhishek Kumar <abhishek.mr...@gmail.com>
    
    Co-authored-by: Spaceman1984 
<49917670+spaceman1...@users.noreply.github.com>
---
 .../cloudstack/response/HostMetricsResponse.java   | 29 +++++++++++++----
 .../response/HostMetricsResponseTest.java          | 37 ++++++++++++++++++++++
 2 files changed, 60 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 05e2f5f..30f7e06 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
@@ -17,10 +17,14 @@
 
 package org.apache.cloudstack.response;
 
+import java.text.DecimalFormat;
+import java.text.ParseException;
+
 import org.apache.cloudstack.api.response.HostResponse;
 import org.apache.cloudstack.outofbandmanagement.OutOfBandManagement;
 
 import com.cloud.serializer.Param;
+import com.cloud.utils.exception.CloudRuntimeException;
 import com.google.gson.annotations.SerializedName;
 
 public class HostMetricsResponse extends HostResponse {
@@ -118,7 +122,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));
         }
     }
 
@@ -130,10 +134,14 @@ 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));
         }
     }
 
+    public String getCpuAllocatedGhz() {
+        return cpuAllocated;
+    }
+
     public void setMemTotal(final Long memTotal) {
         if (memTotal != null) {
             this.memTotal = String.format("%.2f GB", memTotal / (1024.0 * 
1024.0 * 1024.0));
@@ -166,25 +174,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 threshold) {
         if (cpuAllocated != null && threshold != null) {
-            this.cpuAllocatedThresholdExceeded = 
Double.valueOf(cpuAllocated.replace("%", "")) > (100.0 * threshold );
+            this.cpuAllocatedThresholdExceeded = parseCPU(cpuAllocated) > 
(100.0 * threshold );
         }
     }
 
     public void setCpuAllocatedDisableThreshold(final String cpuAllocated, 
final Float threshold) {
         if (cpuAllocated != null && threshold != null) {
-            this.cpuAllocatedDisableThresholdExceeded = 
Double.valueOf(cpuAllocated.replace("%", "")) > (100.0 * threshold);
+            this.cpuAllocatedDisableThresholdExceeded = parseCPU(cpuAllocated) 
> (100.0 * threshold);
         }
     }
 
@@ -212,4 +220,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);
+        }
+    }
+
 }
diff --git 
a/plugins/metrics/src/test/java/org/apache/cloudstack/response/HostMetricsResponseTest.java
 
b/plugins/metrics/src/test/java/org/apache/cloudstack/response/HostMetricsResponseTest.java
new file mode 100644
index 0000000..4e6f35a
--- /dev/null
+++ 
b/plugins/metrics/src/test/java/org/apache/cloudstack/response/HostMetricsResponseTest.java
@@ -0,0 +1,37 @@
+package org.apache.cloudstack.response;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import com.cloud.utils.exception.CloudRuntimeException;
+
+public class HostMetricsResponseTest {
+
+    @Test
+    public void testSetCpuAllocatedWithZeroCpu() {
+        final HostMetricsResponse hostResponse = new HostMetricsResponse();
+        hostResponse.setCpuAllocated("50.25%", 0, 1000L);
+        Assert.assertEquals("0.00 Ghz", hostResponse.getCpuAllocatedGhz());
+    }
+
+    @Test
+    public void testSetCpuAllocatedWithInfiniteCpuAllocated() {
+        final HostMetricsResponse hostResponse = new HostMetricsResponse();
+        hostResponse.setCpuAllocated("∞%", 10, 1000L);
+        Assert.assertEquals("Infinity Ghz", hostResponse.getCpuAllocatedGhz());
+    }
+
+    @Test(expected = CloudRuntimeException.class)
+    public void testSetCpuAllocatedWithInvalidCpu() {
+        final HostMetricsResponse hostResponse = new HostMetricsResponse();
+        hostResponse.setCpuAllocated("abc", 10, 1000L);
+    }
+
+    @Test
+    public void testSetCpuAllocatedWithValidCpu() {
+        final HostMetricsResponse hostResponse = new HostMetricsResponse();
+        hostResponse.setCpuAllocated("50.25%", 10, 1000L);
+        Assert.assertEquals("5.03 Ghz", hostResponse.getCpuAllocatedGhz());
+    }
+
+}

Reply via email to