Search in sources :

Example 1 with Capacity

use of com.cloud.capacity.Capacity in project cosmic by MissionCriticalCloud.

the class ApiResponseHelper method createCapacityResponse.

@Override
public List<CapacityResponse> createCapacityResponse(final List<? extends Capacity> result, final DecimalFormat format) {
    final List<CapacityResponse> capacityResponses = new ArrayList<>();
    for (final Capacity summedCapacity : result) {
        final 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());
            final HostPodVO pod = ApiDBUtils.findPodById(summedCapacity.getPodId());
            if (pod != null) {
                capacityResponse.setPodId(pod.getUuid());
                capacityResponse.setPodName(pod.getName());
            }
        }
        if (summedCapacity.getClusterId() != null) {
            final ClusterVO cluster = ApiDBUtils.findClusterById(summedCapacity.getClusterId());
            if (cluster != null) {
                capacityResponse.setClusterId(cluster.getUuid());
                capacityResponse.setClusterName(cluster.getName());
                if (summedCapacity.getPodId() == null) {
                    final HostPodVO pod = ApiDBUtils.findPodById(cluster.getPodId());
                    capacityResponse.setPodId(pod.getUuid());
                    capacityResponse.setPodName(pod.getName());
                }
            }
        }
        final DataCenter zone = ApiDBUtils.findZoneById(summedCapacity.getDataCenterId());
        if (zone != null) {
            capacityResponse.setZoneId(zone.getUuid());
            capacityResponse.setZoneName(zone.getName());
        }
        if (summedCapacity.getUsedPercentage() != null) {
            capacityResponse.setPercentageAllocated(format.format(summedCapacity.getUsedPercentage() * 100f));
        } else if (summedCapacity.getTotalCapacity() != 0) {
            capacityResponse.setPercentageAllocated(format.format((float) summedCapacity.getUsedCapacity() / (float) summedCapacity.getTotalCapacity() * 100f));
        } else {
            capacityResponse.setPercentageAllocated(format.format(0L));
        }
        capacityResponse.setObjectName("capacity");
        capacityResponses.add(capacityResponse);
    }
    final List<VgpuTypesInfo> gpuCapacities;
    if (result.size() > 1 && (gpuCapacities = ApiDBUtils.getGpuCapacites(result.get(0).getDataCenterId(), result.get(0).getPodId(), result.get(0).getClusterId())) != null) {
        final 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 (final 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.GPUType.passthrough.toString())) {
                capacityMax += capacity.getMaxCapacity();
            }
        }
        final DataCenter zone = ApiDBUtils.findZoneById(result.get(0).getDataCenterId());
        final CapacityResponse capacityResponse = new CapacityResponse();
        if (zone != null) {
            capacityResponse.setZoneId(zone.getUuid());
            capacityResponse.setZoneName(zone.getName());
        }
        if (result.get(0).getPodId() != null) {
            final HostPodVO pod = ApiDBUtils.findPodById(result.get(0).getPodId());
            capacityResponse.setPodId(pod.getUuid());
            capacityResponse.setPodName(pod.getName());
        }
        if (result.get(0).getClusterId() != null) {
            final 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.setPercentageAllocated(format.format(capacityUsed / capacityMax * 100f));
        } else {
            capacityResponse.setPercentageAllocated(format.format(0));
        }
        capacityResponse.setObjectName("capacity");
        capacityResponses.add(capacityResponse);
    }
    return capacityResponses;
}
Also used : ClusterVO(com.cloud.dc.ClusterVO) ArrayList(java.util.ArrayList) HostPodVO(com.cloud.dc.HostPodVO) DataCenter(com.cloud.dc.DataCenter) CapacityResponse(com.cloud.api.response.CapacityResponse) SummedCapacity(com.cloud.capacity.dao.CapacityDaoImpl.SummedCapacity) Capacity(com.cloud.capacity.Capacity) VgpuTypesInfo(com.cloud.agent.api.VgpuTypesInfo)

Example 2 with Capacity

use of com.cloud.capacity.Capacity in project cloudstack by apache.

the class ApiResponseHelper method createCapacityResponse.

@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());
        if (summedCapacity.getAllocatedCapacity() != null) {
            capacityResponse.setCapacityAllocated(summedCapacity.getAllocatedCapacity());
        }
        capacityResponse.setCapacityType(summedCapacity.getCapacityType());
        capacityResponse.setCapacityName(CapacityVO.getCapacityName(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.GPUType.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.setCapacityName(CapacityVO.getCapacityName(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;
}
Also used : ClusterVO(com.cloud.dc.ClusterVO) ArrayList(java.util.ArrayList) HostPodVO(com.cloud.dc.HostPodVO) DataCenter(com.cloud.dc.DataCenter) CapacityResponse(org.apache.cloudstack.api.response.CapacityResponse) SummedCapacity(com.cloud.capacity.dao.CapacityDaoImpl.SummedCapacity) Capacity(com.cloud.capacity.Capacity) VgpuTypesInfo(com.cloud.agent.api.VgpuTypesInfo)

Aggregations

VgpuTypesInfo (com.cloud.agent.api.VgpuTypesInfo)2 Capacity (com.cloud.capacity.Capacity)2 SummedCapacity (com.cloud.capacity.dao.CapacityDaoImpl.SummedCapacity)2 ClusterVO (com.cloud.dc.ClusterVO)2 DataCenter (com.cloud.dc.DataCenter)2 HostPodVO (com.cloud.dc.HostPodVO)2 ArrayList (java.util.ArrayList)2 CapacityResponse (com.cloud.api.response.CapacityResponse)1 CapacityResponse (org.apache.cloudstack.api.response.CapacityResponse)1