2929import org .apache .log4j .Logger ;
3030
3131import com .cloud .capacity .CapacityVO ;
32+ import com .cloud .storage .VolumeVO ;
33+ import com .cloud .storage .dao .VolumeDaoImpl .SumCount ;
34+ import com .cloud .utils .Pair ;
35+ import com .cloud .utils .db .Filter ;
3236import com .cloud .utils .db .GenericDaoBase ;
37+ import com .cloud .utils .db .GenericSearchBuilder ;
3338import com .cloud .utils .db .SearchBuilder ;
3439import com .cloud .utils .db .SearchCriteria ;
3540import com .cloud .utils .db .Transaction ;
41+ import com .cloud .utils .db .SearchCriteria .Func ;
42+ import com .cloud .utils .db .SearchCriteria .Op ;
3643import com .cloud .utils .exception .CloudRuntimeException ;
3744
3845@ Local (value = { CapacityDao .class })
@@ -53,6 +60,7 @@ public class CapacityDaoImpl extends GenericDaoBase<CapacityVO, Long> implements
5360
5461 private SearchBuilder <CapacityVO > _hostIdTypeSearch ;
5562 private SearchBuilder <CapacityVO > _hostOrPoolIdSearch ;
63+ protected GenericSearchBuilder <CapacityVO , SummedCapacity > SummedCapactySearch ;
5664
5765 private static final String LIST_HOSTS_IN_CLUSTER_WITH_ENOUGH_CAPACITY = "SELECT a.host_id FROM (host JOIN op_host_capacity a ON host.id = a.host_id AND host.cluster_id = ? AND host.type = ? " +
5866 "AND (a.total_capacity * ? - a.used_capacity) >= ? and a.capacity_type = 1) " +
@@ -68,6 +76,44 @@ public CapacityDaoImpl() {
6876 _hostOrPoolIdSearch .and ("hostId" , _hostOrPoolIdSearch .entity ().getHostOrPoolId (), SearchCriteria .Op .EQ );
6977 _hostOrPoolIdSearch .done ();
7078 }
79+
80+ @ Override
81+ public List <SummedCapacity > findCapacityByType (short capacityType , Long zoneId , Long podId , Long clusterId , Long startIndex , Long pageSize ){
82+
83+ SummedCapactySearch = createSearchBuilder (SummedCapacity .class );
84+ SummedCapactySearch .select ("sumUsed" , Func .SUM , SummedCapactySearch .entity ().getUsedCapacity ());
85+ SummedCapactySearch .select ("sumTotal" , Func .SUM , SummedCapactySearch .entity ().getTotalCapacity ());
86+ SummedCapactySearch .select ("clusterId" , Func .NATIVE , SummedCapactySearch .entity ().getClusterId ());
87+ SummedCapactySearch .select ("podId" , Func .NATIVE , SummedCapactySearch .entity ().getPodId ());
88+
89+ SummedCapactySearch .and ("dcId" , SummedCapactySearch .entity ().getDataCenterId (), Op .EQ );
90+ SummedCapactySearch .and ("capacityType" , SummedCapactySearch .entity ().getCapacityType (), Op .EQ );
91+ SummedCapactySearch .groupBy (SummedCapactySearch .entity ().getClusterId ());
92+
93+ if (podId != null ){
94+ SummedCapactySearch .and ("podId" , SummedCapactySearch .entity ().getPodId (), Op .EQ );
95+ }
96+ if (clusterId != null ){
97+ SummedCapactySearch .and ("clusterId" , SummedCapactySearch .entity ().getClusterId (), Op .EQ );
98+ }
99+ SummedCapactySearch .done ();
100+
101+
102+ SearchCriteria <SummedCapacity > sc = SummedCapactySearch .create ();
103+ sc .setParameters ("dcId" , zoneId );
104+ sc .setParameters ("capacityType" , capacityType );
105+ if (podId != null ){
106+ sc .setParameters ("podId" , podId );
107+ }
108+ if (clusterId != null ){
109+ sc .setParameters ("clusterId" , clusterId );
110+ }
111+
112+ Filter filter = new Filter (CapacityVO .class , null , true , startIndex , pageSize );
113+ List <SummedCapacity > results = customSearchIncludingRemoved (sc , filter );
114+ return results ;
115+
116+ }
71117
72118 public void updateAllocated (Long hostId , long allocatedAmount , short capacityType , boolean add ) {
73119 Transaction txn = Transaction .currentTxn ();
@@ -231,5 +277,14 @@ public List<Long> listHostsWithEnoughCapacity(int requiredCpu, long requiredRam,
231277 } catch (Throwable e ) {
232278 throw new CloudRuntimeException ("Caught: " + sql , e );
233279 }
234- }
280+ }
281+
282+ public static class SummedCapacity {
283+ public long sumUsed ;
284+ public long sumTotal ;
285+ public long clusterId ;
286+ public long podId ;
287+ public SummedCapacity () {
288+ }
289+ }
235290}
0 commit comments