@Override
public List<CapacityResponse> createCapacityResponse(List<? extends Capacity> result, DecimalFormat format) {
List<CapacityResponse> capacityResponses = new ArrayList<CapacityResponse>();
for (Capacity summedCapacity : result) {
CapacityResponse capacityResponse = new CapacityResponse();
capacityResponse.setCapacityTotal(summedCapacity.getTotalCapacity());
capacityResponse.setCapacityType(summedCapacity.getCapacityType());
capacityResponse.setCapacityUsed(summedCapacity.getUsedCapacity());
if (summedCapacity.getPodId() != null) {
capacityResponse.setPodId(ApiDBUtils.findPodById(summedCapacity.getPodId()).getUuid());
HostPodVO pod = ApiDBUtils.findPodById(summedCapacity.getPodId());
if (pod != null) {
capacityResponse.setPodId(pod.getUuid());
capacityResponse.setPodName(pod.getName());
}
}
if (summedCapacity.getClusterId() != null) {
ClusterVO cluster = ApiDBUtils.findClusterById(summedCapacity.getClusterId());
if (cluster != null) {
capacityResponse.setClusterId(cluster.getUuid());
capacityResponse.setClusterName(cluster.getName());
if (summedCapacity.getPodId() == null) {
HostPodVO pod = ApiDBUtils.findPodById(cluster.getPodId());
capacityResponse.setPodId(pod.getUuid());
capacityResponse.setPodName(pod.getName());
}
}
}
DataCenter zone = ApiDBUtils.findZoneById(summedCapacity.getDataCenterId());
if (zone != null) {
capacityResponse.setZoneId(zone.getUuid());
capacityResponse.setZoneName(zone.getName());
}
if (summedCapacity.getUsedPercentage() != null) {
capacityResponse.setPercentUsed(format.format(summedCapacity.getUsedPercentage() * 100f));
} else if (summedCapacity.getTotalCapacity() != 0) {
capacityResponse.setPercentUsed(format.format((float)summedCapacity.getUsedCapacity() / (float)summedCapacity.getTotalCapacity() * 100f));
} else {
capacityResponse.setPercentUsed(format.format(0L));
}
capacityResponse.setObjectName("capacity");
capacityResponses.add(capacityResponse);
}
List<VgpuTypesInfo> gpuCapacities;
if (result.size() > 1 && (gpuCapacities = ApiDBUtils.getGpuCapacites(result.get(0).getDataCenterId(), result.get(0).getPodId(), result.get(0).getClusterId())) != null) {
HashMap<String, Long> vgpuVMs = ApiDBUtils.getVgpuVmsCount(result.get(0).getDataCenterId(), result.get(0).getPodId(), result.get(0).getClusterId());
float capacityUsed = 0;
long capacityMax = 0;
for (VgpuTypesInfo capacity : gpuCapacities) {
if (vgpuVMs.containsKey(capacity.getGroupName().concat(capacity.getModelName()))) {
capacityUsed += (float)vgpuVMs.get(capacity.getGroupName().concat(capacity.getModelName())) / capacity.getMaxVpuPerGpu();
}
if (capacity.getModelName().equals(GPU.vGPUType.passthrough.toString())) {
capacityMax += capacity.getMaxCapacity();
}
}
DataCenter zone = ApiDBUtils.findZoneById(result.get(0).getDataCenterId());
CapacityResponse capacityResponse = new CapacityResponse();
if (zone != null) {
capacityResponse.setZoneId(zone.getUuid());
capacityResponse.setZoneName(zone.getName());
}
if (result.get(0).getPodId() != null) {
HostPodVO pod = ApiDBUtils.findPodById(result.get(0).getPodId());
capacityResponse.setPodId(pod.getUuid());
capacityResponse.setPodName(pod.getName());
}
if (result.get(0).getClusterId() != null) {
ClusterVO cluster = ApiDBUtils.findClusterById(result.get(0).getClusterId());
capacityResponse.setClusterId(cluster.getUuid());
capacityResponse.setClusterName(cluster.getName());
}
capacityResponse.setCapacityType(Capacity.CAPACITY_TYPE_GPU);
capacityResponse.setCapacityUsed((long)Math.ceil(capacityUsed));
capacityResponse.setCapacityTotal(capacityMax);
if (capacityMax > 0) {
capacityResponse.setPercentUsed(format.format(capacityUsed / capacityMax * 100f));
} else {
capacityResponse.setPercentUsed(format.format(0));
}
capacityResponse.setObjectName("capacity");
capacityResponses.add(capacityResponse);
}
return capacityResponses;
}