mirror of
				https://github.com/apache/cloudstack.git
				synced 2025-11-04 00:02:37 +01:00 
			
		
		
		
	Bug 13059: Introduce a new api to list top consumed resources in cloudstack.
Reviewed-By: Kishan
This commit is contained in:
		
							parent
							
								
									ce22712293
								
							
						
					
					
						commit
						93b8178f19
					
				@ -60,6 +60,9 @@ public class ListCapacityCmd extends BaseListCmd {
 | 
			
		||||
    @Parameter(name=ApiConstants.FETCH_LATEST, type=CommandType.BOOLEAN, description="recalculate capacities and fetch the latest")
 | 
			
		||||
    private Boolean fetchLatest;
 | 
			
		||||
    
 | 
			
		||||
    @Parameter(name=ApiConstants.RESOURCE_STATE, type=CommandType.BOOLEAN, description="list capacities by resource state. Resource state represents current state determined by admin of the resource, value can be one of [Enabled, Disabled, Maintenance]")
 | 
			
		||||
    private Boolean resourceState;
 | 
			
		||||
    
 | 
			
		||||
    @Parameter(name=ApiConstants.TYPE, type=CommandType.INTEGER, description="lists capacity by type" +
 | 
			
		||||
    																		 "* CAPACITY_TYPE_MEMORY = 0" +
 | 
			
		||||
    																		 "* CAPACITY_TYPE_CPU = 1" +
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										104
									
								
								api/src/com/cloud/api/commands/ListTopConsumedResources.java
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										104
									
								
								api/src/com/cloud/api/commands/ListTopConsumedResources.java
									
									
									
									
									
										Executable file
									
								
							@ -0,0 +1,104 @@
 | 
			
		||||
package com.cloud.api.commands;
 | 
			
		||||
 | 
			
		||||
import java.text.DecimalFormat;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
import org.apache.log4j.Logger;
 | 
			
		||||
 | 
			
		||||
import com.cloud.api.ApiConstants;
 | 
			
		||||
import com.cloud.api.BaseListCmd;
 | 
			
		||||
import com.cloud.api.IdentityMapper;
 | 
			
		||||
import com.cloud.api.Implementation;
 | 
			
		||||
import com.cloud.api.Parameter;
 | 
			
		||||
import com.cloud.api.ServerApiException;
 | 
			
		||||
import com.cloud.api.BaseCmd.CommandType;
 | 
			
		||||
import com.cloud.api.response.CapacityResponse;
 | 
			
		||||
import com.cloud.api.response.ListResponse;
 | 
			
		||||
import com.cloud.capacity.Capacity;
 | 
			
		||||
import com.cloud.exception.ConcurrentOperationException;
 | 
			
		||||
import com.cloud.exception.InsufficientCapacityException;
 | 
			
		||||
import com.cloud.exception.NetworkRuleConflictException;
 | 
			
		||||
import com.cloud.exception.ResourceAllocationException;
 | 
			
		||||
import com.cloud.exception.ResourceUnavailableException;
 | 
			
		||||
 | 
			
		||||
@Implementation(description="Lists all the system wide capacities.", responseObject=CapacityResponse.class)
 | 
			
		||||
public class ListTopConsumedResources extends BaseListCmd {
 | 
			
		||||
 | 
			
		||||
        public static final Logger s_logger = Logger.getLogger(ListCapacityCmd.class.getName());
 | 
			
		||||
        private static final DecimalFormat s_percentFormat = new DecimalFormat("##.##");
 | 
			
		||||
 | 
			
		||||
        private static final String s_name = "listcapacityresponse";
 | 
			
		||||
 | 
			
		||||
        /////////////////////////////////////////////////////
 | 
			
		||||
        //////////////// API parameters /////////////////////
 | 
			
		||||
        /////////////////////////////////////////////////////
 | 
			
		||||
     
 | 
			
		||||
        @IdentityMapper(entityTableName="data_center")
 | 
			
		||||
        @Parameter(name=ApiConstants.ZONE_ID, type=CommandType.LONG, description="lists capacity by the Zone ID")
 | 
			
		||||
        private Long zoneId;
 | 
			
		||||
 | 
			
		||||
        @IdentityMapper(entityTableName="host_pod_ref")
 | 
			
		||||
        @Parameter(name=ApiConstants.POD_ID, type=CommandType.LONG, description="lists capacity by the Pod ID")
 | 
			
		||||
        private Long podId;
 | 
			
		||||
        
 | 
			
		||||
        @IdentityMapper(entityTableName="cluster")
 | 
			
		||||
        @Parameter(name=ApiConstants.CLUSTER_ID, type=CommandType.LONG, description="lists capacity by the Cluster ID")
 | 
			
		||||
        private Long clusterId;
 | 
			
		||||
        
 | 
			
		||||
        @Parameter(name=ApiConstants.TYPE, type=CommandType.INTEGER, description="lists capacity by type" +
 | 
			
		||||
                                                                                 "* CAPACITY_TYPE_MEMORY = 0" +
 | 
			
		||||
                                                                                 "* CAPACITY_TYPE_CPU = 1" +
 | 
			
		||||
                                                                                 "* CAPACITY_TYPE_STORAGE = 2" +
 | 
			
		||||
                                                                                 "* CAPACITY_TYPE_STORAGE_ALLOCATED = 3" +
 | 
			
		||||
                                                                                 "* CAPACITY_TYPE_VIRTUAL_NETWORK_PUBLIC_IP = 4" +
 | 
			
		||||
                                                                                 "* CAPACITY_TYPE_PRIVATE_IP = 5" +
 | 
			
		||||
                                                                                 "* CAPACITY_TYPE_SECONDARY_STORAGE = 6" +
 | 
			
		||||
                                                                                 "* CAPACITY_TYPE_VLAN = 7" +
 | 
			
		||||
                                                                                 "* CAPACITY_TYPE_DIRECT_ATTACHED_PUBLIC_IP = 8" +
 | 
			
		||||
                                                                                 "* CAPACITY_TYPE_LOCAL_STORAGE = 9.")
 | 
			
		||||
 | 
			
		||||
        private Integer type;
 | 
			
		||||
 | 
			
		||||
        /////////////////////////////////////////////////////
 | 
			
		||||
        /////////////////// Accessors ///////////////////////
 | 
			
		||||
        /////////////////////////////////////////////////////
 | 
			
		||||
 | 
			
		||||
        public Long getZoneId() {
 | 
			
		||||
            return zoneId;
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        public Long getPodId() {
 | 
			
		||||
            return podId;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public Long getClusterId() {
 | 
			
		||||
            return clusterId;
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        public Integer getType() {
 | 
			
		||||
            return type;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        /////////////////////////////////////////////////////
 | 
			
		||||
        /////////////// API Implementation///////////////////
 | 
			
		||||
        /////////////////////////////////////////////////////
 | 
			
		||||
 | 
			
		||||
        @Override
 | 
			
		||||
        public String getCommandName() {
 | 
			
		||||
            return s_name;
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        @Override
 | 
			
		||||
        public void execute(){
 | 
			
		||||
            List<? extends Capacity> result = _mgr.listTopConsumedResources(this);
 | 
			
		||||
            ListResponse<CapacityResponse> response = new ListResponse<CapacityResponse>();
 | 
			
		||||
            List<CapacityResponse> capacityResponses = _responseGenerator.createCapacityResponse(result, s_percentFormat);
 | 
			
		||||
            response.setResponses(capacityResponses);
 | 
			
		||||
            response.setResponseName(getCommandName());
 | 
			
		||||
            this.setResponseObject(response);
 | 
			
		||||
        }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -49,6 +49,9 @@ public interface Capacity {
 | 
			
		||||
 | 
			
		||||
    public short getCapacityType();    
 | 
			
		||||
 | 
			
		||||
    long getReservedCapacity();
 | 
			
		||||
    public long getReservedCapacity();
 | 
			
		||||
    
 | 
			
		||||
    public Float getUsedPercentage();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -50,6 +50,7 @@ import com.cloud.api.commands.ListServiceOfferingsCmd;
 | 
			
		||||
import com.cloud.api.commands.ListStoragePoolsCmd;
 | 
			
		||||
import com.cloud.api.commands.ListSystemVMsCmd;
 | 
			
		||||
import com.cloud.api.commands.ListTemplatesCmd;
 | 
			
		||||
import com.cloud.api.commands.ListTopConsumedResources;
 | 
			
		||||
import com.cloud.api.commands.ListVMGroupsCmd;
 | 
			
		||||
import com.cloud.api.commands.ListVlanIpRangesCmd;
 | 
			
		||||
import com.cloud.api.commands.ListZonesByCmd;
 | 
			
		||||
@ -434,4 +435,12 @@ public interface ManagementService {
 | 
			
		||||
 | 
			
		||||
    HypervisorCapabilities updateHypervisorCapabilities(Long id, Long maxGuestsLimit, Boolean securityGroupEnabled);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * list all the top consumed resources across different capacity types
 | 
			
		||||
     * 
 | 
			
		||||
     * @param cmd
 | 
			
		||||
     * @return List of capacities
 | 
			
		||||
     */
 | 
			
		||||
    List<? extends Capacity> listTopConsumedResources(ListTopConsumedResources cmd);
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -177,6 +177,7 @@ listAlerts=com.cloud.api.commands.ListAlertsCmd;3
 | 
			
		||||
 | 
			
		||||
#### system capacity commands
 | 
			
		||||
listCapacity=com.cloud.api.commands.ListCapacityCmd;3
 | 
			
		||||
listTopConsumedResources=com.cloud.api.commands.ListTopConsumedResources;3
 | 
			
		||||
 | 
			
		||||
#### swift commands^M
 | 
			
		||||
addSwift=com.cloud.api.commands.AddSwiftCmd;1
 | 
			
		||||
 | 
			
		||||
@ -18,6 +18,7 @@
 | 
			
		||||
 | 
			
		||||
package com.cloud.capacity;
 | 
			
		||||
 | 
			
		||||
import javax.persistence.Transient;
 | 
			
		||||
import java.util.Date;
 | 
			
		||||
 | 
			
		||||
import javax.persistence.Column;
 | 
			
		||||
@ -70,6 +71,8 @@ public class CapacityVO implements Capacity {
 | 
			
		||||
    @Temporal(value=TemporalType.TIMESTAMP)
 | 
			
		||||
    protected Date updateTime;
 | 
			
		||||
    
 | 
			
		||||
    @Transient
 | 
			
		||||
    private float usedPercentage;
 | 
			
		||||
    
 | 
			
		||||
    public CapacityVO() {}
 | 
			
		||||
 | 
			
		||||
@ -84,6 +87,14 @@ public class CapacityVO implements Capacity {
 | 
			
		||||
        this.updateTime = new Date();
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    public CapacityVO(long dataCenterId, Long podId, Long clusterId, short capacityType, float usedPercentage) {        
 | 
			
		||||
        this.dataCenterId = dataCenterId;
 | 
			
		||||
        this.podId = podId;
 | 
			
		||||
        this.clusterId = clusterId;
 | 
			
		||||
        this.capacityType = capacityType;
 | 
			
		||||
        this.usedPercentage = usedPercentage;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public long getId() {
 | 
			
		||||
        return id;
 | 
			
		||||
@ -164,4 +175,13 @@ public class CapacityVO implements Capacity {
 | 
			
		||||
	public void setUpdateTime(Date updateTime) {
 | 
			
		||||
		this.updateTime = updateTime;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
    public Float getUsedPercentage() {
 | 
			
		||||
        return usedPercentage;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setUsedPercentage(float usedPercentage) {
 | 
			
		||||
        this.usedPercentage = usedPercentage;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -2442,6 +2442,8 @@ public class ApiResponseHelper implements ResponseGenerator {
 | 
			
		||||
            capacityResponse.setZoneName(ApiDBUtils.findZoneById(summedCapacity.getDataCenterId()).getName());
 | 
			
		||||
            if (summedCapacity.getTotalCapacity() != 0) {
 | 
			
		||||
                capacityResponse.setPercentUsed(format.format((float) summedCapacity.getUsedCapacity() / (float) summedCapacity.getTotalCapacity() * 100f));
 | 
			
		||||
            } else if (summedCapacity.getUsedPercentage() != null){
 | 
			
		||||
                capacityResponse.setPercentUsed(format.format(summedCapacity.getUsedPercentage() * 100f));
 | 
			
		||||
            } else {
 | 
			
		||||
                capacityResponse.setPercentUsed(format.format(0L));
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
@ -38,4 +38,7 @@ public interface CapacityDao extends GenericDao<CapacityVO, Long> {
 | 
			
		||||
	
 | 
			
		||||
    List<Long> listPodsByHostCapacities(long zoneId, int requiredCpu, long requiredRam, short capacityType, float cpuOverprovisioningFactor);
 | 
			
		||||
    Pair<List<Long>, Map<Long, Double>> orderPodsByAggregateCapacity(long zoneId, short capacityType, float cpuOverprovisioningFactor);
 | 
			
		||||
    List<SummedCapacity> findCapacityBy(Integer capacityType, Long zoneId,
 | 
			
		||||
            Long podId, Long clusterId, String resourceState);
 | 
			
		||||
    List<SummedCapacity> listCapacitiesGroupedByLevelAndType(Integer capacityType, Long zoneId, Long podId, Long clusterId, int level, Integer limit);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -83,6 +83,32 @@ public class CapacityDaoImpl extends GenericDaoBase<CapacityVO, Long> implements
 | 
			
		||||
    private static final String ORDER_PODS_BY_AGGREGATE_CAPACITY = "SELECT pod_id, SUM(used_capacity+reserved_capacity)/SUM(total_capacity * ?) FROM `cloud`.`op_host_capacity` WHERE data_center_id = ? " +
 | 
			
		||||
                                                                   " AND capacity_type = ? GROUP BY pod_id ORDER BY SUM(used_capacity+reserved_capacity)/SUM(total_capacity * ?) ASC";
 | 
			
		||||
    
 | 
			
		||||
    private static final String LIST_CAPACITY_BY_RESOURCE_STATE = "SELECT capacity.data_center_id, sum(capacity.used_capacity), sum(capacity.reserved_quantity), sum(capacity.total_capacity), capacity_capacity_type "+
 | 
			
		||||
                                                                  "FROM `cloud`.`op_host_capacity` capacity INNER JOIN `cloud`.`data_center` dc ON (dc.id = capacity.data_center_id AND dc.removed is NULL)"+
 | 
			
		||||
                                                                  "FROM `cloud`.`op_host_capacity` capacity INNER JOIN `cloud`.`host_pod_ref` pod ON (pod.id = capacity.pod_id AND pod.removed is NULL)"+
 | 
			
		||||
                                                                  "FROM `cloud`.`op_host_capacity` capacity INNER JOIN `cloud`.`cluster` cluster ON (cluster.id = capacity.cluster_id AND cluster.removed is NULL)"+
 | 
			
		||||
                                                                  "FROM `cloud`.`op_host_capacity` capacity INNER JOIN `cloud`.`host` host ON (host.id = capacity.host_id AND host.removed is NULL)"+
 | 
			
		||||
                                                                  "WHERE dc.allocation_state = ? AND pod.allocation_state = ? AND cluster.allocation_state = ? AND host.resource_state = ? AND capacity_type not in (3,4) ";
 | 
			
		||||
    
 | 
			
		||||
    private static final String LIST_CAPACITY_GROUP_BY_ZONE_TYPE_PART1 = "SELECT ((sum(capacity.used_capacity) + sum(capacity.reserved_capacity)) / (case capacity_type when 1 then (sum(total_capacity) * (select value from `cloud`.`configuration` where name like 'cpu.overprovisioning.factor')) else sum(total_capacity) end)) percent," +
 | 
			
		||||
                                                                         " capacity.capacity_type, capacity.data_center_id "+
 | 
			
		||||
                                                                         "FROM `cloud`.`op_host_capacity` capacity "+
 | 
			
		||||
                                                                         "WHERE  total_capacity > 0 AND data_center_id is not null ";
 | 
			
		||||
    private static final String LIST_CAPACITY_GROUP_BY_ZONE_TYPE_PART2 = " GROUP BY data_center_id, capacity_type order by percent desc limit ";
 | 
			
		||||
    private static final String LIST_CAPACITY_GROUP_BY_POD_TYPE_PART1 = "SELECT ((sum(capacity.used_capacity) + sum(capacity.reserved_capacity)) / (case capacity_type when 1 then (sum(total_capacity) * (select value from `cloud`.`configuration` where name like 'cpu.overprovisioning.factor')) else sum(total_capacity) end)) percent," +
 | 
			
		||||
                                                                        " capacity.capacity_type, capacity.data_center_id, pod_id "+
 | 
			
		||||
                                                                        "FROM `cloud`.`op_host_capacity` capacity "+
 | 
			
		||||
                                                                        "WHERE  total_capacity > 0 AND pod_id is not null ";
 | 
			
		||||
    private static final String LIST_CAPACITY_GROUP_BY_POD_TYPE_PART2 = " GROUP BY pod_id, capacity_type order by percent desc limit ";
 | 
			
		||||
    
 | 
			
		||||
    private static final String LIST_CAPACITY_GROUP_BY_CLUSTER_TYPE_PART1 = "SELECT ((sum(capacity.used_capacity) + sum(capacity.reserved_capacity)) / (case capacity_type when 1 then (sum(total_capacity) * (select value from `cloud`.`configuration` where name like 'cpu.overprovisioning.factor')) else sum(total_capacity) end)) percent,"+
 | 
			
		||||
                                                                            "capacity.capacity_type, capacity.data_center_id, pod_id, cluster_id "+
 | 
			
		||||
                                                                            "FROM `cloud`.`op_host_capacity` capacity "+
 | 
			
		||||
                                                                            "WHERE  total_capacity > 0 AND cluster_id is not null ";
 | 
			
		||||
    private static final String LIST_CAPACITY_GROUP_BY_CLUSTER_TYPE_PART2 = " GROUP BY cluster_id, capacity_type order by percent desc limit ";
 | 
			
		||||
    
 | 
			
		||||
    
 | 
			
		||||
    
 | 
			
		||||
    public CapacityDaoImpl() {
 | 
			
		||||
    	_hostIdTypeSearch = createSearchBuilder();
 | 
			
		||||
    	_hostIdTypeSearch.and("hostId", _hostIdTypeSearch.entity().getHostOrPoolId(), SearchCriteria.Op.EQ);
 | 
			
		||||
@ -104,6 +130,126 @@ public class CapacityDaoImpl extends GenericDaoBase<CapacityVO, Long> implements
 | 
			
		||||
    	_allFieldsSearch.done();
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    @Override
 | 
			
		||||
    public  List<SummedCapacity> findCapacityBy(Integer capacityType, Long zoneId, Long podId, Long clusterId, String resource_state){
 | 
			
		||||
        
 | 
			
		||||
        Transaction txn = Transaction.currentTxn();
 | 
			
		||||
        PreparedStatement pstmt = null;
 | 
			
		||||
        List<SummedCapacity> result = new ArrayList<SummedCapacity>();
 | 
			
		||||
 | 
			
		||||
        StringBuilder sql = new StringBuilder(LIST_CAPACITY_BY_RESOURCE_STATE);           
 | 
			
		||||
        List<Long> resourceIdList = new ArrayList<Long>();
 | 
			
		||||
        
 | 
			
		||||
        if (zoneId != null){
 | 
			
		||||
            sql.append(" AND capacity.data_center_id = ?");
 | 
			
		||||
            resourceIdList.add(zoneId);
 | 
			
		||||
        }
 | 
			
		||||
        if (podId != null){
 | 
			
		||||
            sql.append(" AND capacity.pod_id = ?");
 | 
			
		||||
            resourceIdList.add(podId);
 | 
			
		||||
        }
 | 
			
		||||
        if (clusterId != null){
 | 
			
		||||
            sql.append(" AND capacity.cluster_id = ?");
 | 
			
		||||
            resourceIdList.add(clusterId);
 | 
			
		||||
        }
 | 
			
		||||
        if (capacityType != null){
 | 
			
		||||
            sql.append(" AND capacity.capacity_type = ?");
 | 
			
		||||
            resourceIdList.add(capacityType.longValue());
 | 
			
		||||
        }   
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            pstmt = txn.prepareAutoCloseStatement(sql.toString());
 | 
			
		||||
            pstmt.setString(1, resource_state);
 | 
			
		||||
            pstmt.setString(2, resource_state);
 | 
			
		||||
            pstmt.setString(3, resource_state);
 | 
			
		||||
            pstmt.setString(4, resource_state);
 | 
			
		||||
            for (int i = 0; i < resourceIdList.size(); i++){                
 | 
			
		||||
                pstmt.setLong( 5+i, resourceIdList.get(i));
 | 
			
		||||
            }            
 | 
			
		||||
            ResultSet rs = pstmt.executeQuery();
 | 
			
		||||
            while (rs.next()) {
 | 
			
		||||
                SummedCapacity summedCapacity = new SummedCapacity(rs.getLong(2), rs.getLong(3), rs.getLong(4), (short)rs.getLong(5), null, null, rs.getLong(1));
 | 
			
		||||
                result.add(summedCapacity);
 | 
			
		||||
            }
 | 
			
		||||
            return result;
 | 
			
		||||
        } catch (SQLException e) {
 | 
			
		||||
            throw new CloudRuntimeException("DB Exception on: " + sql, e);
 | 
			
		||||
        } catch (Throwable e) {
 | 
			
		||||
            throw new CloudRuntimeException("Caught: " + sql, e);
 | 
			
		||||
        }        
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    @Override
 | 
			
		||||
    public  List<SummedCapacity> listCapacitiesGroupedByLevelAndType(Integer capacityType, Long zoneId, Long podId, Long clusterId, int level, Integer limit){
 | 
			
		||||
        
 | 
			
		||||
        StringBuilder finalQuery = new StringBuilder(); 
 | 
			
		||||
        Transaction txn = Transaction.currentTxn();
 | 
			
		||||
        PreparedStatement pstmt = null;
 | 
			
		||||
        List<SummedCapacity> result = new ArrayList<SummedCapacity>();
 | 
			
		||||
        
 | 
			
		||||
        switch(level){
 | 
			
		||||
            case 1: // List all the capacities grouped by zone, capacity Type
 | 
			
		||||
                finalQuery.append(LIST_CAPACITY_GROUP_BY_ZONE_TYPE_PART1);
 | 
			
		||||
                break;
 | 
			
		||||
                
 | 
			
		||||
            case 2: // List all the capacities grouped by pod, capacity Type
 | 
			
		||||
                finalQuery.append(LIST_CAPACITY_GROUP_BY_POD_TYPE_PART1);
 | 
			
		||||
                break;
 | 
			
		||||
                
 | 
			
		||||
            case 3: // List all the capacities grouped by cluster, capacity Type
 | 
			
		||||
                finalQuery.append(LIST_CAPACITY_GROUP_BY_CLUSTER_TYPE_PART1);
 | 
			
		||||
                break;
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        if (zoneId != null){
 | 
			
		||||
            finalQuery.append(" AND data_center_id="+zoneId);
 | 
			
		||||
        }
 | 
			
		||||
        if (podId != null){
 | 
			
		||||
            finalQuery.append(" AND pod_id="+podId);
 | 
			
		||||
        }
 | 
			
		||||
        if (clusterId != null){
 | 
			
		||||
            finalQuery.append(" AND cluster_id="+clusterId);
 | 
			
		||||
        }
 | 
			
		||||
        if (capacityType != null){
 | 
			
		||||
            finalQuery.append(" AND capacity_type="+capacityType);   
 | 
			
		||||
        }                
 | 
			
		||||
        
 | 
			
		||||
        switch(level){
 | 
			
		||||
        case 1: // List all the capacities grouped by zone, capacity Type
 | 
			
		||||
            finalQuery.append(LIST_CAPACITY_GROUP_BY_ZONE_TYPE_PART2);
 | 
			
		||||
            break;
 | 
			
		||||
            
 | 
			
		||||
        case 2: // List all the capacities grouped by pod, capacity Type
 | 
			
		||||
            finalQuery.append(LIST_CAPACITY_GROUP_BY_POD_TYPE_PART2);
 | 
			
		||||
            break;
 | 
			
		||||
            
 | 
			
		||||
        case 3: // List all the capacities grouped by cluster, capacity Type
 | 
			
		||||
            finalQuery.append(LIST_CAPACITY_GROUP_BY_CLUSTER_TYPE_PART2);
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        finalQuery.append(limit.toString());
 | 
			
		||||
        
 | 
			
		||||
        try {
 | 
			
		||||
            pstmt = txn.prepareAutoCloseStatement(finalQuery.toString());        
 | 
			
		||||
            ResultSet rs = pstmt.executeQuery();
 | 
			
		||||
            while (rs.next()) {                
 | 
			
		||||
                SummedCapacity summedCapacity = new SummedCapacity( rs.getFloat(1), (short)rs.getLong(2),
 | 
			
		||||
                                                                   rs.getLong(3),
 | 
			
		||||
                                                                   level == 3 ? rs.getLong(5): null,
 | 
			
		||||
                                                                   level != 1 ? rs.getLong(4): null);
 | 
			
		||||
                                                                   
 | 
			
		||||
                result.add(summedCapacity);
 | 
			
		||||
            }
 | 
			
		||||
            return result;
 | 
			
		||||
        } catch (SQLException e) {
 | 
			
		||||
            throw new CloudRuntimeException("DB Exception on: " + finalQuery, e);
 | 
			
		||||
        } catch (Throwable e) {
 | 
			
		||||
            throw new CloudRuntimeException("Caught: " + finalQuery, e);
 | 
			
		||||
        }                     
 | 
			
		||||
        
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    @Override
 | 
			
		||||
    public  List<SummedCapacity> findCapacityBy(Integer capacityType, Long zoneId, Long podId, Long clusterId){
 | 
			
		||||
    	
 | 
			
		||||
@ -264,6 +410,7 @@ public class CapacityDaoImpl extends GenericDaoBase<CapacityVO, Long> implements
 | 
			
		||||
	    public long sumUsed;
 | 
			
		||||
	    public long sumReserved;
 | 
			
		||||
	    public long sumTotal;
 | 
			
		||||
	    public Float percentUsed;
 | 
			
		||||
	    public short capacityType;
 | 
			
		||||
	    public Long clusterId;
 | 
			
		||||
	    public Long podId;
 | 
			
		||||
@ -285,6 +432,16 @@ public class CapacityDaoImpl extends GenericDaoBase<CapacityVO, Long> implements
 | 
			
		||||
		    this(sumUsed, sumReserved, sumTotal, capacityType, clusterId, podId);
 | 
			
		||||
	        this.dcId = zoneId;
 | 
			
		||||
		}
 | 
			
		||||
		
 | 
			
		||||
		public SummedCapacity(float percentUsed, short capacityType, Long zoneId, Long podId, Long clusterId) {
 | 
			
		||||
		    super();
 | 
			
		||||
		    this.percentUsed = percentUsed;
 | 
			
		||||
		    this.capacityType = capacityType;
 | 
			
		||||
            this.clusterId = clusterId;
 | 
			
		||||
            this.podId = podId;
 | 
			
		||||
            this.dcId = zoneId;
 | 
			
		||||
        }
 | 
			
		||||
		
 | 
			
		||||
		public Short getCapacityType() {				
 | 
			
		||||
			return capacityType;
 | 
			
		||||
		}
 | 
			
		||||
@ -299,6 +456,15 @@ public class CapacityDaoImpl extends GenericDaoBase<CapacityVO, Long> implements
 | 
			
		||||
		}
 | 
			
		||||
		public Long getDataCenterId() {
 | 
			
		||||
            return dcId;
 | 
			
		||||
        }
 | 
			
		||||
		public Long getClusterId() {
 | 
			
		||||
            return clusterId;
 | 
			
		||||
        }
 | 
			
		||||
        public Long getPodId() {
 | 
			
		||||
            return podId;
 | 
			
		||||
        }
 | 
			
		||||
        public Float getPercentUsed() {
 | 
			
		||||
            return percentUsed;
 | 
			
		||||
        }
 | 
			
		||||
	}
 | 
			
		||||
	public List<SummedCapacity> findByClusterPodZone(Long zoneId, Long podId, Long clusterId){
 | 
			
		||||
 | 
			
		||||
@ -25,6 +25,7 @@ import java.net.URISyntaxException;
 | 
			
		||||
import java.net.UnknownHostException;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.Calendar;
 | 
			
		||||
import java.util.Comparator;
 | 
			
		||||
import java.util.Date;
 | 
			
		||||
import java.util.Enumeration;
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
@ -51,6 +52,7 @@ import com.cloud.agent.api.GetVncPortAnswer;
 | 
			
		||||
import com.cloud.agent.api.GetVncPortCommand;
 | 
			
		||||
import com.cloud.agent.api.storage.CopyVolumeAnswer;
 | 
			
		||||
import com.cloud.agent.api.storage.CopyVolumeCommand;
 | 
			
		||||
import com.cloud.agent.api.to.VolumeTO;
 | 
			
		||||
import com.cloud.agent.manager.allocator.HostAllocator;
 | 
			
		||||
import com.cloud.alert.Alert;
 | 
			
		||||
import com.cloud.alert.AlertManager;
 | 
			
		||||
@ -83,6 +85,7 @@ import com.cloud.api.commands.ListServiceOfferingsCmd;
 | 
			
		||||
import com.cloud.api.commands.ListStoragePoolsCmd;
 | 
			
		||||
import com.cloud.api.commands.ListSystemVMsCmd;
 | 
			
		||||
import com.cloud.api.commands.ListTemplatesCmd;
 | 
			
		||||
import com.cloud.api.commands.ListTopConsumedResources;
 | 
			
		||||
import com.cloud.api.commands.ListVMGroupsCmd;
 | 
			
		||||
import com.cloud.api.commands.ListVlanIpRangesCmd;
 | 
			
		||||
import com.cloud.api.commands.ListZonesByCmd;
 | 
			
		||||
@ -243,6 +246,7 @@ import com.cloud.vm.dao.UserVmDao;
 | 
			
		||||
import com.cloud.vm.dao.VMInstanceDao;
 | 
			
		||||
 | 
			
		||||
import edu.emory.mathcs.backport.java.util.Arrays;
 | 
			
		||||
import edu.emory.mathcs.backport.java.util.Collections;
 | 
			
		||||
 | 
			
		||||
public class ManagementServerImpl implements ManagementServer {
 | 
			
		||||
    public static final Logger s_logger = Logger.getLogger(ManagementServerImpl.class.getName());
 | 
			
		||||
@ -1935,6 +1939,94 @@ public class ManagementServerImpl implements ManagementServer {
 | 
			
		||||
        return _alertDao.search(sc, searchFilter);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    @Override
 | 
			
		||||
    public List<CapacityVO> listTopConsumedResources(ListTopConsumedResources cmd) {
 | 
			
		||||
        
 | 
			
		||||
        Integer capacityType = cmd.getType();
 | 
			
		||||
        Long zoneId = cmd.getZoneId();
 | 
			
		||||
        Long podId = cmd.getPodId();
 | 
			
		||||
        Long clusterId = cmd.getClusterId();
 | 
			
		||||
        zoneId = _accountMgr.checkAccessAndSpecifyAuthority(UserContext.current().getCaller(), zoneId);
 | 
			
		||||
        List<SummedCapacity> summedCapacities = new ArrayList<SummedCapacity>();
 | 
			
		||||
        
 | 
			
		||||
        if (zoneId == null && podId == null){//Group by Zone, capacity type
 | 
			
		||||
            List<SummedCapacity> summedCapacitiesAtZone = _capacityDao.listCapacitiesGroupedByLevelAndType(capacityType, zoneId, podId, clusterId, 1, cmd.getPageSize());
 | 
			
		||||
            if(summedCapacitiesAtZone != null){
 | 
			
		||||
                summedCapacities.addAll(summedCapacitiesAtZone);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        if (podId == null){//Group by Pod, capacity type
 | 
			
		||||
            List<SummedCapacity> summedCapacitiesAtPod = _capacityDao.listCapacitiesGroupedByLevelAndType(capacityType, zoneId, podId, clusterId, 2, cmd.getPageSize());
 | 
			
		||||
            if(summedCapacitiesAtPod != null){
 | 
			
		||||
                summedCapacities.addAll(summedCapacitiesAtPod);
 | 
			
		||||
            }                           
 | 
			
		||||
            List<SummedCapacity> summedCapacitiesForSecStorage = getSecStorageUsed(zoneId, capacityType);
 | 
			
		||||
            if (summedCapacitiesForSecStorage != null){
 | 
			
		||||
                summedCapacities.addAll(summedCapacitiesForSecStorage);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        //Group by Cluster, capacity type
 | 
			
		||||
        List<SummedCapacity> summedCapacitiesAtCluster = _capacityDao.listCapacitiesGroupedByLevelAndType(capacityType, zoneId, podId, clusterId, 3, cmd.getPageSize());
 | 
			
		||||
        if(summedCapacitiesAtCluster != null){
 | 
			
		||||
            summedCapacities.addAll(summedCapacitiesAtCluster);
 | 
			
		||||
        }                       
 | 
			
		||||
        
 | 
			
		||||
        //Sort Capacities
 | 
			
		||||
        Collections.sort(summedCapacities, new Comparator<SummedCapacity>() {
 | 
			
		||||
            @Override
 | 
			
		||||
            public int compare(SummedCapacity arg0, SummedCapacity arg1) {
 | 
			
		||||
                if (arg0.getPercentUsed() < arg1.getPercentUsed()) {
 | 
			
		||||
                    return 1;
 | 
			
		||||
                } else if (arg0.getPercentUsed()== arg1.getPercentUsed()) {
 | 
			
		||||
                    return 0;
 | 
			
		||||
                }
 | 
			
		||||
                return -1;
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
        
 | 
			
		||||
        List<CapacityVO> capacities = new ArrayList<CapacityVO>();
 | 
			
		||||
        
 | 
			
		||||
        summedCapacities = summedCapacities.subList(0, summedCapacities.size() < cmd.getPageSize() ? summedCapacities.size() : cmd.getPageSize());
 | 
			
		||||
        for (SummedCapacity summedCapacity : summedCapacities){
 | 
			
		||||
            CapacityVO capacity = new CapacityVO(summedCapacity.getDataCenterId(), summedCapacity.getPodId() , summedCapacity.getClusterId(),
 | 
			
		||||
                                                 summedCapacity.getCapacityType(), summedCapacity.getPercentUsed());            
 | 
			
		||||
            capacities.add(capacity);       
 | 
			
		||||
        }        
 | 
			
		||||
        return capacities;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    List<SummedCapacity> getSecStorageUsed(Long zoneId, Integer capacityType){
 | 
			
		||||
        if (capacityType == null || capacityType == Capacity.CAPACITY_TYPE_SECONDARY_STORAGE){
 | 
			
		||||
            List<SummedCapacity> list = new ArrayList<SummedCapacity>();
 | 
			
		||||
            
 | 
			
		||||
            if (zoneId != null){
 | 
			
		||||
                CapacityVO capacity = _storageMgr.getSecondaryStorageUsedStats(null, zoneId);
 | 
			
		||||
                if (capacity.getTotalCapacity()!= 0){
 | 
			
		||||
                    capacity.setUsedPercentage( capacity.getUsedCapacity() / capacity.getTotalCapacity() );    
 | 
			
		||||
                }else {
 | 
			
		||||
                    capacity.setUsedPercentage(0);
 | 
			
		||||
                }
 | 
			
		||||
                SummedCapacity summedCapacity = new SummedCapacity(capacity.getUsedPercentage(), capacity.getCapacityType(), capacity.getDataCenterId(), capacity.getPodId(), capacity.getClusterId());
 | 
			
		||||
                list.add(summedCapacity) ;
 | 
			
		||||
            }else {
 | 
			
		||||
                List<DataCenterVO> dcList = ApiDBUtils.listZones();
 | 
			
		||||
                for(DataCenterVO dc : dcList){
 | 
			
		||||
                    CapacityVO capacity = _storageMgr.getSecondaryStorageUsedStats(null, dc.getId());                    
 | 
			
		||||
                    if (capacity.getTotalCapacity()!= 0){
 | 
			
		||||
                        capacity.setUsedPercentage( capacity.getUsedCapacity() / capacity.getTotalCapacity() );    
 | 
			
		||||
                    }else {
 | 
			
		||||
                        capacity.setUsedPercentage(0);
 | 
			
		||||
                    }
 | 
			
		||||
                    SummedCapacity summedCapacity = new SummedCapacity(capacity.getUsedPercentage(), capacity.getCapacityType(), capacity.getDataCenterId(), capacity.getPodId(), capacity.getClusterId());
 | 
			
		||||
                    list.add(summedCapacity);
 | 
			
		||||
                }//End of for                
 | 
			
		||||
            }
 | 
			
		||||
            return list;
 | 
			
		||||
        }
 | 
			
		||||
        return null;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    @Override
 | 
			
		||||
    public List<CapacityVO> listCapacities(ListCapacityCmd cmd) {
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user