Skip to content

Commit 6446797

Browse files
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>
1 parent 6d98056 commit 6446797

2 files changed

Lines changed: 60 additions & 6 deletions

File tree

plugins/metrics/src/main/java/org/apache/cloudstack/response/HostMetricsResponse.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@
1717

1818
package org.apache.cloudstack.response;
1919

20+
import java.text.DecimalFormat;
21+
import java.text.ParseException;
22+
2023
import org.apache.cloudstack.api.response.HostResponse;
2124
import org.apache.cloudstack.outofbandmanagement.OutOfBandManagement;
2225

2326
import com.cloud.serializer.Param;
27+
import com.cloud.utils.exception.CloudRuntimeException;
2428
import com.google.gson.annotations.SerializedName;
2529

2630
public class HostMetricsResponse extends HostResponse {
@@ -118,7 +122,7 @@ public void setCpuTotal(final Integer cpuNumber, final Long cpuSpeed) {
118122

119123
public void setCpuUsed(final String cpuUsed, final Integer cpuNumber, final Long cpuSpeed) {
120124
if (cpuUsed != null && cpuNumber != null && cpuSpeed != null) {
121-
this.cpuUsed = String.format("%.2f Ghz", Double.valueOf(cpuUsed.replace("%", "")) * cpuNumber * cpuSpeed / (100.0 * 1000.0));
125+
this.cpuUsed = String.format("%.2f Ghz", parseCPU(cpuUsed) * cpuNumber * cpuSpeed / (100.0 * 1000.0));
122126
}
123127
}
124128

@@ -130,10 +134,14 @@ public void setLoadAverage(final Double loadAverage) {
130134

131135
public void setCpuAllocated(final String cpuAllocated, final Integer cpuNumber, final Long cpuSpeed) {
132136
if (cpuAllocated != null && cpuNumber != null && cpuSpeed != null) {
133-
this.cpuAllocated = String.format("%.2f Ghz", Double.valueOf(cpuAllocated.replace("%", "")) * cpuNumber * cpuSpeed / (100.0 * 1000.0));
137+
this.cpuAllocated = String.format("%.2f Ghz", parseCPU(cpuAllocated) * cpuNumber * cpuSpeed / (100.0 * 1000.0));
134138
}
135139
}
136140

141+
public String getCpuAllocatedGhz() {
142+
return cpuAllocated;
143+
}
144+
137145
public void setMemTotal(final Long memTotal) {
138146
if (memTotal != null) {
139147
this.memTotal = String.format("%.2f GB", memTotal / (1024.0 * 1024.0 * 1024.0));
@@ -166,25 +174,25 @@ public void setNetworkWrite(final Long networkWriteKbs) {
166174

167175
public void setCpuUsageThreshold(final String cpuUsed, final Double threshold) {
168176
if (cpuUsed != null && threshold != null) {
169-
this.cpuThresholdExceeded = Double.valueOf(cpuUsed.replace("%", "")) > (100.0 * threshold);
177+
this.cpuThresholdExceeded = parseCPU(cpuUsed) > (100.0 * threshold);
170178
}
171179
}
172180

173181
public void setCpuUsageDisableThreshold(final String cpuUsed, final Float threshold) {
174182
if (cpuUsed != null && threshold != null) {
175-
this.cpuDisableThresholdExceeded = Double.valueOf(cpuUsed.replace("%", "")) > (100.0 * threshold);
183+
this.cpuDisableThresholdExceeded = parseCPU(cpuUsed) > (100.0 * threshold);
176184
}
177185
}
178186

179187
public void setCpuAllocatedThreshold(final String cpuAllocated, final Double threshold) {
180188
if (cpuAllocated != null && threshold != null) {
181-
this.cpuAllocatedThresholdExceeded = Double.valueOf(cpuAllocated.replace("%", "")) > (100.0 * threshold );
189+
this.cpuAllocatedThresholdExceeded = parseCPU(cpuAllocated) > (100.0 * threshold );
182190
}
183191
}
184192

185193
public void setCpuAllocatedDisableThreshold(final String cpuAllocated, final Float threshold) {
186194
if (cpuAllocated != null && threshold != null) {
187-
this.cpuAllocatedDisableThresholdExceeded = Double.valueOf(cpuAllocated.replace("%", "")) > (100.0 * threshold);
195+
this.cpuAllocatedDisableThresholdExceeded = parseCPU(cpuAllocated) > (100.0 * threshold);
188196
}
189197
}
190198

@@ -212,4 +220,13 @@ public void setMemoryAllocatedDisableThreshold(final Long memAllocated, final Lo
212220
}
213221
}
214222

223+
private Double parseCPU(String cpu) {
224+
DecimalFormat decimalFormat = new DecimalFormat("#.##");
225+
try {
226+
return decimalFormat.parse(cpu).doubleValue();
227+
} catch (ParseException e) {
228+
throw new CloudRuntimeException(e);
229+
}
230+
}
231+
215232
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.apache.cloudstack.response;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
import com.cloud.utils.exception.CloudRuntimeException;
7+
8+
public class HostMetricsResponseTest {
9+
10+
@Test
11+
public void testSetCpuAllocatedWithZeroCpu() {
12+
final HostMetricsResponse hostResponse = new HostMetricsResponse();
13+
hostResponse.setCpuAllocated("50.25%", 0, 1000L);
14+
Assert.assertEquals("0.00 Ghz", hostResponse.getCpuAllocatedGhz());
15+
}
16+
17+
@Test
18+
public void testSetCpuAllocatedWithInfiniteCpuAllocated() {
19+
final HostMetricsResponse hostResponse = new HostMetricsResponse();
20+
hostResponse.setCpuAllocated("∞%", 10, 1000L);
21+
Assert.assertEquals("Infinity Ghz", hostResponse.getCpuAllocatedGhz());
22+
}
23+
24+
@Test(expected = CloudRuntimeException.class)
25+
public void testSetCpuAllocatedWithInvalidCpu() {
26+
final HostMetricsResponse hostResponse = new HostMetricsResponse();
27+
hostResponse.setCpuAllocated("abc", 10, 1000L);
28+
}
29+
30+
@Test
31+
public void testSetCpuAllocatedWithValidCpu() {
32+
final HostMetricsResponse hostResponse = new HostMetricsResponse();
33+
hostResponse.setCpuAllocated("50.25%", 10, 1000L);
34+
Assert.assertEquals("5.03 Ghz", hostResponse.getCpuAllocatedGhz());
35+
}
36+
37+
}

0 commit comments

Comments
 (0)