diff --git a/engine/schema/src/com/cloud/capacity/dao/CapacityDaoImpl.java b/engine/schema/src/com/cloud/capacity/dao/CapacityDaoImpl.java index cea18bd2679..85740a22775 100755 --- a/engine/schema/src/com/cloud/capacity/dao/CapacityDaoImpl.java +++ b/engine/schema/src/com/cloud/capacity/dao/CapacityDaoImpl.java @@ -113,7 +113,7 @@ public class CapacityDaoImpl extends GenericDaoBase implements + "else sum(total_capacity) end)," + "((sum(capacity.used_capacity) + sum(capacity.reserved_capacity)) / ( case capacity_type when 1 then (sum(total_capacity) * (select value from `cloud`.`cluster_details` where cluster_details.name= 'cpuOvercommitRatio' AND cluster_details.cluster_id=capacity.cluster_id))" + "when '0' then (sum(total_capacity) * (select value from `cloud`.`cluster_details` where cluster_details.name='memoryOvercommitRatio' AND cluster_details.cluster_id=capacity.cluster_id))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 AND capacity_state='Enabled'"; + + "capacity.capacity_type, capacity.data_center_id, pod_id, cluster_id FROM `cloud`.`op_host_capacity` capacity WHERE total_capacity > 0 AND data_center_id is not null AND capacity_state='Enabled'"; 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 = @@ -122,7 +122,7 @@ public class CapacityDaoImpl extends GenericDaoBase implements + "when '0' then (sum(total_capacity) * (select value from `cloud`.`cluster_details` where cluster_details.name= 'memoryOvercommitRatio' AND cluster_details.cluster_id=capacity.cluster_id))else sum(total_capacity) end)," + "((sum(capacity.used_capacity) + sum(capacity.reserved_capacity)) / ( case capacity_type when 1 then (sum(total_capacity) * (select value from `cloud`.`cluster_details` where cluster_details.name= 'cpuOvercommitRatio' AND cluster_details.cluster_id=capacity.cluster_id)) " + "when '0' then (sum(total_capacity) * (select value from `cloud`.`cluster_details` where cluster_details.name= 'memoryOvercommitRatio' AND cluster_details.cluster_id=capacity.cluster_id))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 data_center_id is not null AND capacity_state='Enabled' "; + + "capacity.capacity_type, capacity.data_center_id, pod_id, cluster_id FROM `cloud`.`op_host_capacity` capacity WHERE total_capacity > 0 AND data_center_id is not null AND capacity_state='Enabled' "; private static final String LIST_CAPACITY_GROUP_BY_POD_TYPE_PART2 = " GROUP BY pod_id, capacity_type order by percent desc limit "; @@ -134,7 +134,7 @@ public class CapacityDaoImpl extends GenericDaoBase implements + "when '0' then (sum(total_capacity) * (select value from `cloud`.`cluster_details` where cluster_details.name= 'memoryOvercommitRatio' AND cluster_details.cluster_id=capacity.cluster_id))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 data_center_id is not null AND capacity_state='Enabled' "; - private static final String LIST_CAPACITY_GROUP_BY_CLUSTER_TYPE_PART2 = " GROUP BY cluster_id, capacity_type order by percent desc limit "; + private static final String LIST_CAPACITY_GROUP_BY_CLUSTER_TYPE_PART2 = " GROUP BY cluster_id, capacity_type, pod_id order by percent desc limit "; private static final String UPDATE_CAPACITY_STATE = "UPDATE `cloud`.`op_host_capacity` SET capacity_state = ? WHERE "; private static final String LIST_CAPACITY_GROUP_BY_CAPACITY_PART1= "SELECT sum(capacity.used_capacity), sum(capacity.reserved_capacity)," + @@ -317,7 +317,7 @@ public class CapacityDaoImpl extends GenericDaoBase implements StringBuilder finalQuery = new StringBuilder(); TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; - List result = new ArrayList(); + List results = new ArrayList(); List resourceIdList = new ArrayList(); @@ -354,11 +354,11 @@ public class CapacityDaoImpl extends GenericDaoBase implements switch (level) { case 1: // List all the capacities grouped by zone, capacity Type - finalQuery.append(LIST_CAPACITY_GROUP_BY_ZONE_TYPE_PART2); + finalQuery.append(LIST_CAPACITY_GROUP_BY_CLUSTER_TYPE_PART2); break; case 2: // List all the capacities grouped by pod, capacity Type - finalQuery.append(LIST_CAPACITY_GROUP_BY_POD_TYPE_PART2); + finalQuery.append(LIST_CAPACITY_GROUP_BY_CLUSTER_TYPE_PART2); break; case 3: // List all the capacities grouped by cluster, capacity Type @@ -387,9 +387,25 @@ public class CapacityDaoImpl extends GenericDaoBase implements SummedCapacity summedCapacity = new SummedCapacity(rs.getLong(1), rs.getLong(2), rs.getLong(3), rs.getFloat(4), (short)rs.getLong(5), rs.getLong(6), capacityPodId, capacityClusterId); - result.add(summedCapacity); + results.add(summedCapacity); } - return result; + + HashMap capacityMap = new HashMap(); + for (SummedCapacity result: results) { + if (capacityMap.containsKey(result.getCapacityType().intValue())) { + SummedCapacity tempCapacity = capacityMap.get(result.getCapacityType().intValue()); + tempCapacity.setUsedCapacity(tempCapacity.getUsedCapacity()+result.getUsedCapacity()); + tempCapacity.setReservedCapacity(tempCapacity.getReservedCapacity()+result.getReservedCapacity()); + tempCapacity.setSumTotal(tempCapacity.getTotalCapacity()+result.getTotalCapacity()); + }else { + capacityMap.put(result.getCapacityType().intValue(),result); + } + } + List summedCapacityList = new ArrayList(); + for (Integer capacity_type : capacityMap.keySet()) { + summedCapacityList.add(capacityMap.get(capacity_type)); + } + return summedCapacityList; } catch (SQLException e) { throw new CloudRuntimeException("DB Exception on: " + finalQuery, e); } catch (Throwable e) {