1717
1818package org .apache .cloudstack .response ;
1919
20+ import java .text .DecimalFormat ;
21+ import java .text .ParseException ;
22+
2023import org .apache .cloudstack .api .response .HostResponse ;
2124import org .apache .cloudstack .outofbandmanagement .OutOfBandManagement ;
2225
2326import com .cloud .serializer .Param ;
27+ import com .cloud .utils .exception .CloudRuntimeException ;
2428import com .google .gson .annotations .SerializedName ;
2529
2630public 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}
0 commit comments