JoaoJandre commented on code in PR #7274:
URL: https://github.com/apache/cloudstack/pull/7274#discussion_r1186011515
##########
server/src/main/java/com/cloud/resourcelimit/ResourceLimitManagerImpl.java:
##########
@@ -978,20 +988,74 @@ public long countCpusForAccount(long accountId) {
SearchCriteria<UserVmJoinVO> sc1 = userVmSearch.create();
sc1.setParameters("accountId", accountId);
- if (VirtualMachineManager.ResourceCountRunningVMsonly.value())
- sc1.setParameters("state", new Object[] {State.Destroyed,
State.Error, State.Expunging, State.Stopped});
- else
- sc1.setParameters("state", new Object[] {State.Destroyed,
State.Error, State.Expunging});
+ if (VirtualMachineManager.ResourceCountRunningVMsonly.value()) {
+ sc1.setParameters("state", State.Destroyed, State.Error,
State.Expunging, State.Stopped);
+ } else {
+ sc1.setParameters("state", State.Destroyed, State.Error,
State.Expunging);
+ }
sc1.setParameters("displayVm", 1);
List<UserVmJoinVO> userVms = _userVmJoinDao.search(sc1,null);
for (UserVmJoinVO vm : userVms) {
- cputotal += Long.valueOf(vm.getCpu());
+ cputotal += vm.getCpu();
+ }
+
+ final Pair<Boolean, Long> totalByAccountId =
calculateCpuTotalByAccountId(accountId);
+ if (totalByAccountId.first()) {
+ return totalByAccountId.second();
+ } else {
+ return cputotal;
}
- return cputotal;
+ }
+
+ private Pair<Boolean, Long> calculateCpuTotalByAccountId(long accountId) {
+ long cputotal = 0;
+ boolean hasCpus = false;
+ final Account owner = _accountDao.findById(accountId);
+ final DomainVO domain = _domainDao.findById(owner.getDomainId());
+ final ServiceOffering defaultRouterOffering =
getServiceOfferingByConfig();
+
+ GenericSearchBuilder<ServiceOfferingVO, SumCount> cpuSearch =
serviceOfferingDao.createSearchBuilder(SumCount.class);
+ cpuSearch.select("sum", Func.SUM, cpuSearch.entity().getCpu());
+ cpuSearch.select("count", Func.COUNT, (Object[])null);
+ SearchBuilder<VMInstanceVO> join1 = _vmDao.createSearchBuilder();
+ join1.and("accountId", join1.entity().getAccountId(), Op.EQ);
+ join1.and("type", join1.entity().getType(), Op.IN);
+ join1.and("state", join1.entity().getState(), SearchCriteria.Op.NIN);
+ join1.and("displayVm", join1.entity().isDisplayVm(), Op.EQ);
+ cpuSearch.join(OFFERINGS_NAME, join1, cpuSearch.entity().getId(),
join1.entity().getServiceOfferingId(), JoinBuilder.JoinType.INNER);
+ cpuSearch.done();
+
+ SearchCriteria<SumCount> sc = cpuSearch.create();
+ sc.setJoinParameters(OFFERINGS_NAME, "accountId", accountId);
+ sc.setJoinParameters(OFFERINGS_NAME, "type",
VirtualMachine.Type.DomainRouter); // domain routers
+
+ if
(Boolean.TRUE.equals(VirtualMachineManager.ResourceCountRunningVMsonly.value()))
{
+ sc.setJoinParameters(OFFERINGS_NAME, "state", State.Destroyed,
State.Error, State.Expunging, State.Stopped);
+ } else {
+ sc.setJoinParameters(OFFERINGS_NAME, "state", State.Destroyed,
State.Error, State.Expunging);
+ }
+ sc.setJoinParameters(OFFERINGS_NAME, "displayVm", 1);
+ List<SumCount> cpus = serviceOfferingDao.customSearch(sc, null);
Review Comment:
I would refactor this to a method in the serviceOfferingDao, this is mostly
duplicated code from another method.
##########
server/src/main/java/com/cloud/resourcelimit/ResourceLimitManagerImpl.java:
##########
@@ -1002,16 +1066,68 @@ public long calculateMemoryForAccount(long accountId) {
SearchCriteria<UserVmJoinVO> sc1 = userVmSearch.create();
sc1.setParameters("accountId", accountId);
- if (VirtualMachineManager.ResourceCountRunningVMsonly.value())
- sc1.setParameters("state", new Object[] {State.Destroyed,
State.Error, State.Expunging, State.Stopped});
- else
- sc1.setParameters("state", new Object[] {State.Destroyed,
State.Error, State.Expunging});
+ if
(Boolean.TRUE.equals(VirtualMachineManager.ResourceCountRunningVMsonly.value()))
{
+ sc1.setParameters("state", State.Destroyed, State.Error,
State.Expunging, State.Stopped);
+ } else {
+ sc1.setParameters("state", State.Destroyed, State.Error,
State.Expunging);
+ }
sc1.setParameters("displayVm", 1);
List<UserVmJoinVO> userVms = _userVmJoinDao.search(sc1,null);
for (UserVmJoinVO vm : userVms) {
- ramtotal += Long.valueOf(vm.getRamSize());
+ ramtotal += vm.getRamSize();
+ }
+
+ final Pair<Boolean, Long> totalByAccountId =
calculateMemoryTotalByAccountId(accountId);
+ if (Boolean.TRUE.equals(totalByAccountId.first())) {
+ return totalByAccountId.second();
+ } else {
+ return ramtotal;
}
- return ramtotal;
+ }
+
+ public Pair<Boolean, Long> calculateMemoryTotalByAccountId(long accountId)
{
+ long ramtotal = 0;
+ boolean hasMemory = false;
+ final Account owner = _accountDao.findById(accountId);
+ final DomainVO domain = _domainDao.findById(owner.getDomainId());
+ final ServiceOffering defaultRouterOffering =
getServiceOfferingByConfig();
+
+ GenericSearchBuilder<ServiceOfferingVO, SumCount> memorySearch =
serviceOfferingDao.createSearchBuilder(SumCount.class);
+ memorySearch.select("sum", Func.SUM,
memorySearch.entity().getRamSize());
+ memorySearch.select("count", Func.COUNT, null);
+ SearchBuilder<VMInstanceVO> join1 = _vmDao.createSearchBuilder();
+ join1.and("accountId", join1.entity().getAccountId(), Op.EQ);
+ join1.and("type", join1.entity().getType(), Op.IN);
+ join1.and("state", join1.entity().getState(), SearchCriteria.Op.NIN);
+ join1.and("displayVm", join1.entity().isDisplayVm(), Op.EQ);
+ memorySearch.join(OFFERINGS_NAME, join1,
memorySearch.entity().getId(), join1.entity().getServiceOfferingId(),
JoinBuilder.JoinType.INNER);
+ memorySearch.done();
+
+ SearchCriteria<SumCount> sc = memorySearch.create();
+ sc.setJoinParameters(OFFERINGS_NAME, "accountId", accountId);
+ sc.setJoinParameters(OFFERINGS_NAME, "type",
VirtualMachine.Type.DomainRouter); // domain routers
+ sc.setJoinParameters(OFFERINGS_NAME, "displayVm", 1);
+
+ if
(Boolean.TRUE.equals(VirtualMachineManager.ResourceCountRunningVMsonly.value()))
{
+ sc.setJoinParameters(OFFERINGS_NAME, "state", State.Destroyed,
State.Error, State.Expunging, State.Stopped);
+ } else {
+ sc.setJoinParameters(OFFERINGS_NAME, "state", State.Destroyed,
State.Error, State.Expunging);
+ }
+ sc.setJoinParameters(OFFERINGS_NAME, "displayVm", 1);
+ List<SumCount> memory = serviceOfferingDao.customSearch(sc, null);
Review Comment:
Same thing about refactoring
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]