1616// under the License.
1717package com .cloud .configuration .dao ;
1818
19+ import java .sql .PreparedStatement ;
20+ import java .sql .ResultSet ;
21+ import java .sql .SQLException ;
1922import java .util .ArrayList ;
2023import java .util .HashSet ;
2124import java .util .List ;
2427import javax .annotation .PostConstruct ;
2528import javax .inject .Inject ;
2629
27- import com .cloud .domain .DomainVO ;
28- import com .cloud .user .AccountVO ;
29- import com .cloud .utils .db .JoinBuilder ;
3030import org .springframework .stereotype .Component ;
3131
3232import com .cloud .configuration .Resource ;
3333import com .cloud .configuration .Resource .ResourceOwnerType ;
3434import com .cloud .configuration .Resource .ResourceType ;
3535import com .cloud .configuration .ResourceCountVO ;
3636import com .cloud .configuration .ResourceLimit ;
37+ import com .cloud .domain .DomainVO ;
3738import com .cloud .domain .dao .DomainDao ;
3839import com .cloud .exception .UnsupportedServiceException ;
40+ import com .cloud .user .AccountVO ;
3941import com .cloud .user .dao .AccountDao ;
4042import com .cloud .utils .db .DB ;
4143import com .cloud .utils .db .GenericDaoBase ;
44+ import com .cloud .utils .db .JoinBuilder ;
4245import com .cloud .utils .db .SearchBuilder ;
4346import com .cloud .utils .db .SearchCriteria ;
4447import com .cloud .utils .db .TransactionLegacy ;
48+ import com .cloud .utils .exception .CloudRuntimeException ;
4549
4650@ Component
4751public class ResourceCountDaoImpl extends GenericDaoBase <ResourceCountVO , Long > implements ResourceCountDao {
@@ -51,9 +55,9 @@ public class ResourceCountDaoImpl extends GenericDaoBase<ResourceCountVO, Long>
5155 private final SearchBuilder <ResourceCountVO > DomainSearch ;
5256
5357 @ Inject
54- protected DomainDao _domainDao ;
58+ private DomainDao _domainDao ;
5559 @ Inject
56- protected AccountDao _accountDao ;
60+ private AccountDao _accountDao ;
5761
5862 public ResourceCountDaoImpl () {
5963 TypeSearch = createSearchBuilder ();
@@ -248,4 +252,41 @@ public long removeEntriesByOwner(long ownerId, ResourceOwnerType ownerType) {
248252 return 0 ;
249253 }
250254
255+ private String baseSqlCountComputingResourceAllocatedToAccount = "Select "
256+ + " SUM((CASE "
257+ + " WHEN so.%s is not null THEN so.%s "
258+ + " ELSE CONVERT(vmd.value, UNSIGNED INTEGER) "
259+ + " END)) as total "
260+ + " from vm_instance vm "
261+ + " join service_offering_view so on so.id = vm.service_offering_id "
262+ + " left join user_vm_details vmd on vmd.vm_id = vm.id and vmd.name = '%s' "
263+ + " where vm.type = 'User' and state not in ('Destroyed', 'Error', 'Expunging') and display_vm = true and account_id = ? " ;
264+ @ Override
265+ public long countCpuNumberAllocatedToAccount (long accountId ) {
266+ String sqlCountCpuNumberAllocatedToAccount = String .format (baseSqlCountComputingResourceAllocatedToAccount , ResourceType .cpu , ResourceType .cpu , "cpuNumber" );
267+ return executeSqlCountComputingResourcesForAccount (accountId , sqlCountCpuNumberAllocatedToAccount );
268+ }
269+
270+ @ Override
271+ public long countMemoryAllocatedToAccount (long accountId ) {
272+ String serviceOfferingRamSizeField = "ram_size" ;
273+ String sqlCountCpuNumberAllocatedToAccount = String .format (baseSqlCountComputingResourceAllocatedToAccount , serviceOfferingRamSizeField , serviceOfferingRamSizeField , "memory" );
274+ return executeSqlCountComputingResourcesForAccount (accountId , sqlCountCpuNumberAllocatedToAccount );
275+ }
276+
277+ private long executeSqlCountComputingResourcesForAccount (long accountId , String sqlCountComputingResourcesAllocatedToAccount ) {
278+ try (TransactionLegacy tx = TransactionLegacy .currentTxn ()) {
279+ PreparedStatement pstmt = tx .prepareAutoCloseStatement (sqlCountComputingResourcesAllocatedToAccount );
280+ pstmt .setLong (1 , accountId );
281+
282+ ResultSet rs = pstmt .executeQuery ();
283+ if (!rs .next ()) {
284+ throw new CloudRuntimeException (String .format ("An unexpected case happened while counting allocated computing resources for account: " + accountId ));
285+ }
286+ return rs .getLong ("total" );
287+ } catch (SQLException e ) {
288+ throw new CloudRuntimeException (e );
289+ }
290+ }
291+
251292}
0 commit comments