Skip to content

Commit a529470

Browse files
authored
server: Fix cpuallocated value in findHostsForMIgration api (#4220)
The findHostsForMigration api displays 0% always for cpuallocated field which is wrong. Fixes #4221
1 parent 2fce8b7 commit a529470

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

server/src/main/java/com/cloud/api/query/dao/HostJoinDaoImpl.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,9 @@ public HostResponse newHostResponse(HostJoinVO host, EnumSet<HostDetails> detail
190190

191191
hostResponse.setHypervisorVersion(host.getHypervisorVersion());
192192

193-
Float cpuWithOverprovisioning = host.getCpus() * host.getSpeed() * ApiDBUtils.getCpuOverprovisioningFactor(host.getClusterId());
194-
String cpuAlloc = decimalFormat.format(((float)cpu / cpuWithOverprovisioning * 100f)) + "%";
195-
hostResponse.setCpuAllocated(cpuAlloc);
196-
hostResponse.setCpuWithOverprovisioning(cpuWithOverprovisioning.toString());
193+
float cpuWithOverprovisioning = host.getCpus() * host.getSpeed() * ApiDBUtils.getCpuOverprovisioningFactor(host.getClusterId());
194+
hostResponse.setCpuAllocated(calculateResourceAllocatedPercentage(cpu, cpuWithOverprovisioning));
195+
hostResponse.setCpuWithOverprovisioning(Float.toString(cpuWithOverprovisioning));
197196
}
198197

199198
if (details.contains(HostDetails.all) || details.contains(HostDetails.stats)) {
@@ -317,7 +316,7 @@ public HostForMigrationResponse newHostForMigrationResponse(HostJoinVO host, Enu
317316
if (details.contains(HostDetails.all) || details.contains(HostDetails.capacity)) {
318317
// set allocated capacities
319318
Long mem = host.getMemReservedCapacity() + host.getMemUsedCapacity();
320-
Long cpu = host.getCpuReservedCapacity() + host.getCpuReservedCapacity();
319+
Long cpu = host.getCpuReservedCapacity() + host.getCpuUsedCapacity();
321320

322321
hostResponse.setMemoryTotal(host.getTotalMemory());
323322
Float memWithOverprovisioning = host.getTotalMemory() * ApiDBUtils.getMemOverprovisioningFactor(host.getClusterId());
@@ -340,10 +339,9 @@ public HostForMigrationResponse newHostForMigrationResponse(HostJoinVO host, Enu
340339

341340
hostResponse.setHypervisorVersion(host.getHypervisorVersion());
342341

343-
Float cpuWithOverprovisioning = new Float(host.getCpus() * host.getSpeed() * ApiDBUtils.getCpuOverprovisioningFactor(host.getClusterId()));
344-
String cpuAlloc = decimalFormat.format(((float)cpu / cpuWithOverprovisioning * 100f)).toString() + "%";
345-
hostResponse.setCpuAllocated(cpuAlloc);
346-
hostResponse.setCpuWithOverprovisioning(cpuWithOverprovisioning.toString());
342+
float cpuWithOverprovisioning = host.getCpus() * host.getSpeed() * ApiDBUtils.getCpuOverprovisioningFactor(host.getClusterId());
343+
hostResponse.setCpuAllocated(calculateResourceAllocatedPercentage(cpu, cpuWithOverprovisioning));
344+
hostResponse.setCpuWithOverprovisioning(Float.toString(cpuWithOverprovisioning));
347345
}
348346

349347
if (details.contains(HostDetails.all) || details.contains(HostDetails.stats)) {
@@ -471,4 +469,9 @@ public List<HostJoinVO> findByClusterId(Long clusterId, Host.Type type) {
471469
return listBy(sc);
472470
}
473471

472+
private String calculateResourceAllocatedPercentage(float resource, float resourceWithOverProvision) {
473+
DecimalFormat decimalFormat = new DecimalFormat("#.##");
474+
return decimalFormat.format(((float)resource / resourceWithOverProvision * 100.0f)) + "%";
475+
}
476+
474477
}

0 commit comments

Comments
 (0)