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.mrt22@gmail.com>

* eof newline

Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>

Co-authored-by: Spaceman1984 <49917670+Spaceman1984@users.noreply.github.com>
This commit is contained in:
Abhishek Kumar 2021-08-19 10:00:01 +05:30 committed by GitHub
parent 6d98056d32
commit 6446797fdc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 6 deletions

View File

@ -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);
}
}
}

View File

@ -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());
}
}