This is an automated email from the ASF dual-hosted git repository.

dahn pushed a commit to branch 4.16
in repository https://gitbox.apache.org/repos/asf/cloudstack.git


The following commit(s) were added to refs/heads/4.16 by this push:
     new 6c9f0c1  Fix #3448 quota calculation for monthly tariffs (#5517)
6c9f0c1 is described below

commit 6c9f0c1f3fd9e8075c0083681d3dd00ff042250e
Author: jairov4 <[email protected]>
AuthorDate: Mon Feb 7 07:34:37 2022 -0500

    Fix #3448 quota calculation for monthly tariffs (#5517)
    
    * Fix quota calculation for monthly tariffs
    
    * Remove unused parameter
---
 .../apache/cloudstack/quota/QuotaManagerImpl.java  | 33 +++++++++++-----------
 .../cloudstack/quota/QuotaManagerImplTest.java     |  8 +++---
 2 files changed, 20 insertions(+), 21 deletions(-)

diff --git 
a/framework/quota/src/main/java/org/apache/cloudstack/quota/QuotaManagerImpl.java
 
b/framework/quota/src/main/java/org/apache/cloudstack/quota/QuotaManagerImpl.java
index 3eb949f..139ad2b 100644
--- 
a/framework/quota/src/main/java/org/apache/cloudstack/quota/QuotaManagerImpl.java
+++ 
b/framework/quota/src/main/java/org/apache/cloudstack/quota/QuotaManagerImpl.java
@@ -139,16 +139,15 @@ public class QuotaManagerImpl extends ManagerBase 
implements QuotaManager {
         }
         s_logger.info("Getting pending quota records for account=" + 
account.getAccountName());
         for (UsageVO usageRecord : usageRecords.first()) {
-            BigDecimal aggregationRatio = new 
BigDecimal(_aggregationDuration).divide(s_minutesInMonth, 8, 
RoundingMode.HALF_EVEN);
             switch (usageRecord.getUsageType()) {
             case QuotaTypes.RUNNING_VM:
-                List<QuotaUsageVO> lq = updateQuotaRunningVMUsage(usageRecord, 
aggregationRatio);
+                List<QuotaUsageVO> lq = updateQuotaRunningVMUsage(usageRecord);
                 if (!lq.isEmpty()) {
                     quotaListForAccount.addAll(lq);
                 }
                 break;
             case QuotaTypes.ALLOCATED_VM:
-                QuotaUsageVO qu = updateQuotaAllocatedVMUsage(usageRecord, 
aggregationRatio);
+                QuotaUsageVO qu = updateQuotaAllocatedVMUsage(usageRecord);
                 if (qu != null) {
                     quotaListForAccount.add(qu);
                 }
@@ -159,7 +158,7 @@ public class QuotaManagerImpl extends ManagerBase 
implements QuotaManager {
             case QuotaTypes.VOLUME:
             case QuotaTypes.VM_SNAPSHOT:
             case QuotaTypes.BACKUP:
-                qu = updateQuotaDiskUsage(usageRecord, aggregationRatio, 
usageRecord.getUsageType());
+                qu = updateQuotaDiskUsage(usageRecord, 
usageRecord.getUsageType());
                 if (qu != null) {
                     quotaListForAccount.add(qu);
                 }
@@ -170,7 +169,7 @@ public class QuotaManagerImpl extends ManagerBase 
implements QuotaManager {
             case QuotaTypes.NETWORK_OFFERING:
             case QuotaTypes.SECURITY_GROUP:
             case QuotaTypes.VPN_USERS:
-                qu = updateQuotaRaw(usageRecord, aggregationRatio, 
usageRecord.getUsageType());
+                qu = updateQuotaRaw(usageRecord, usageRecord.getUsageType());
                 if (qu != null) {
                     quotaListForAccount.add(qu);
                 }
@@ -333,14 +332,14 @@ public class QuotaManagerImpl extends ManagerBase 
implements QuotaManager {
         return true;
     }
 
-    public QuotaUsageVO updateQuotaDiskUsage(UsageVO usageRecord, final 
BigDecimal aggregationRatio, final int quotaType) {
+    public QuotaUsageVO updateQuotaDiskUsage(UsageVO usageRecord, final int 
quotaType) {
         QuotaUsageVO quota_usage = null;
         QuotaTariffVO tariff = 
_quotaTariffDao.findTariffPlanByUsageType(quotaType, usageRecord.getEndDate());
         if (tariff != null && 
tariff.getCurrencyValue().compareTo(BigDecimal.ZERO) != 0) {
             BigDecimal quotaUsgage;
             BigDecimal onehourcostpergb;
             BigDecimal noofgbinuse;
-            onehourcostpergb = 
tariff.getCurrencyValue().multiply(aggregationRatio);
+            onehourcostpergb = 
tariff.getCurrencyValue().divide(s_hoursInMonth, 8, RoundingMode.HALF_DOWN);
             noofgbinuse = new BigDecimal(usageRecord.getSize()).divide(s_gb, 
8, RoundingMode.HALF_EVEN);
             quotaUsgage = new 
BigDecimal(usageRecord.getRawUsage()).multiply(onehourcostpergb).multiply(noofgbinuse);
             quota_usage = new QuotaUsageVO(usageRecord.getId(), 
usageRecord.getZoneId(), usageRecord.getAccountId(), usageRecord.getDomainId(), 
usageRecord.getUsageType(),
@@ -352,7 +351,7 @@ public class QuotaManagerImpl extends ManagerBase 
implements QuotaManager {
         return quota_usage;
     }
 
-    public List<QuotaUsageVO> updateQuotaRunningVMUsage(UsageVO usageRecord, 
final BigDecimal aggregationRatio) {
+    public List<QuotaUsageVO> updateQuotaRunningVMUsage(UsageVO usageRecord) {
         List<QuotaUsageVO> quotalist = new ArrayList<QuotaUsageVO>();
         QuotaUsageVO quota_usage;
         BigDecimal cpuquotausgage, speedquotausage, memoryquotausage, vmusage;
@@ -368,7 +367,7 @@ public class QuotaManagerImpl extends ManagerBase 
implements QuotaManager {
         QuotaTariffVO tariff = 
_quotaTariffDao.findTariffPlanByUsageType(QuotaTypes.CPU_NUMBER, 
usageRecord.getEndDate());
         if (tariff != null && 
tariff.getCurrencyValue().compareTo(BigDecimal.ZERO) != 0 && 
serviceoffering.getCpu() != null) {
             BigDecimal cpu = new BigDecimal(serviceoffering.getCpu());
-            onehourcostpercpu = 
tariff.getCurrencyValue().multiply(aggregationRatio);
+            onehourcostpercpu = 
tariff.getCurrencyValue().divide(s_hoursInMonth, 8, RoundingMode.HALF_DOWN);
             cpuquotausgage = 
rawusage.multiply(onehourcostpercpu).multiply(cpu);
             quota_usage = new QuotaUsageVO(usageRecord.getId(), 
usageRecord.getZoneId(), usageRecord.getAccountId(), usageRecord.getDomainId(), 
QuotaTypes.CPU_NUMBER,
                     cpuquotausgage, usageRecord.getStartDate(), 
usageRecord.getEndDate());
@@ -377,8 +376,8 @@ public class QuotaManagerImpl extends ManagerBase 
implements QuotaManager {
         }
         tariff = 
_quotaTariffDao.findTariffPlanByUsageType(QuotaTypes.CPU_CLOCK_RATE, 
usageRecord.getEndDate());
         if (tariff != null && 
tariff.getCurrencyValue().compareTo(BigDecimal.ZERO) != 0 && 
serviceoffering.getSpeed() != null) {
-            BigDecimal speed = new BigDecimal(serviceoffering.getSpeed() / 
100.00);
-            onehourcostper100mhz = 
tariff.getCurrencyValue().multiply(aggregationRatio);
+            BigDecimal speed = new 
BigDecimal(serviceoffering.getSpeed()*serviceoffering.getCpu() / 100.00);
+            onehourcostper100mhz = 
tariff.getCurrencyValue().divide(s_hoursInMonth, 8, RoundingMode.HALF_DOWN);
             speedquotausage = 
rawusage.multiply(onehourcostper100mhz).multiply(speed);
             quota_usage = new QuotaUsageVO(usageRecord.getId(), 
usageRecord.getZoneId(), usageRecord.getAccountId(), usageRecord.getDomainId(), 
QuotaTypes.CPU_CLOCK_RATE,
                     speedquotausage, usageRecord.getStartDate(), 
usageRecord.getEndDate());
@@ -388,7 +387,7 @@ public class QuotaManagerImpl extends ManagerBase 
implements QuotaManager {
         tariff = _quotaTariffDao.findTariffPlanByUsageType(QuotaTypes.MEMORY, 
usageRecord.getEndDate());
         if (tariff != null && 
tariff.getCurrencyValue().compareTo(BigDecimal.ZERO) != 0 && 
serviceoffering.getRamSize() != null) {
             BigDecimal memory = new BigDecimal(serviceoffering.getRamSize());
-            onehourcostper1mb = 
tariff.getCurrencyValue().multiply(aggregationRatio);
+            onehourcostper1mb = 
tariff.getCurrencyValue().divide(s_hoursInMonth, 8, RoundingMode.HALF_DOWN);
             memoryquotausage = 
rawusage.multiply(onehourcostper1mb).multiply(memory);
             quota_usage = new QuotaUsageVO(usageRecord.getId(), 
usageRecord.getZoneId(), usageRecord.getAccountId(), usageRecord.getDomainId(), 
QuotaTypes.MEMORY, memoryquotausage,
                     usageRecord.getStartDate(), usageRecord.getEndDate());
@@ -397,7 +396,7 @@ public class QuotaManagerImpl extends ManagerBase 
implements QuotaManager {
         }
         tariff = 
_quotaTariffDao.findTariffPlanByUsageType(QuotaTypes.RUNNING_VM, 
usageRecord.getEndDate());
         if (tariff != null && 
tariff.getCurrencyValue().compareTo(BigDecimal.ZERO) != 0) {
-            onehourcostforvmusage = 
tariff.getCurrencyValue().multiply(aggregationRatio);
+            onehourcostforvmusage = 
tariff.getCurrencyValue().divide(s_hoursInMonth, 8, RoundingMode.HALF_DOWN);
             vmusage = rawusage.multiply(onehourcostforvmusage);
             quota_usage = new QuotaUsageVO(usageRecord.getId(), 
usageRecord.getZoneId(), usageRecord.getAccountId(), usageRecord.getDomainId(), 
QuotaTypes.RUNNING_VM, vmusage,
                     usageRecord.getStartDate(), usageRecord.getEndDate());
@@ -410,13 +409,13 @@ public class QuotaManagerImpl extends ManagerBase 
implements QuotaManager {
         return quotalist;
     }
 
-    public QuotaUsageVO updateQuotaAllocatedVMUsage(UsageVO usageRecord, final 
BigDecimal aggregationRatio) {
+    public QuotaUsageVO updateQuotaAllocatedVMUsage(UsageVO usageRecord) {
         QuotaUsageVO quota_usage = null;
         QuotaTariffVO tariff = 
_quotaTariffDao.findTariffPlanByUsageType(QuotaTypes.ALLOCATED_VM, 
usageRecord.getEndDate());
         if (tariff != null && 
tariff.getCurrencyValue().compareTo(BigDecimal.ZERO) != 0) {
             BigDecimal vmusage;
             BigDecimal onehourcostforvmusage;
-            onehourcostforvmusage = 
tariff.getCurrencyValue().multiply(aggregationRatio);
+            onehourcostforvmusage = 
tariff.getCurrencyValue().divide(s_hoursInMonth, 8, RoundingMode.HALF_DOWN);
             vmusage = new 
BigDecimal(usageRecord.getRawUsage()).multiply(onehourcostforvmusage);
             quota_usage = new QuotaUsageVO(usageRecord.getId(), 
usageRecord.getZoneId(), usageRecord.getAccountId(), usageRecord.getDomainId(), 
QuotaTypes.ALLOCATED_VM, vmusage,
                     usageRecord.getStartDate(), usageRecord.getEndDate());
@@ -428,13 +427,13 @@ public class QuotaManagerImpl extends ManagerBase 
implements QuotaManager {
         return quota_usage;
     }
 
-    public QuotaUsageVO updateQuotaRaw(UsageVO usageRecord, final BigDecimal 
aggregationRatio, final int ruleType) {
+    public QuotaUsageVO updateQuotaRaw(UsageVO usageRecord, final int 
ruleType) {
         QuotaUsageVO quota_usage = null;
         QuotaTariffVO tariff = 
_quotaTariffDao.findTariffPlanByUsageType(ruleType, usageRecord.getEndDate());
         if (tariff != null && 
tariff.getCurrencyValue().compareTo(BigDecimal.ZERO) != 0) {
             BigDecimal ruleusage;
             BigDecimal onehourcost;
-            onehourcost = tariff.getCurrencyValue().multiply(aggregationRatio);
+            onehourcost = tariff.getCurrencyValue().divide(s_hoursInMonth, 8, 
RoundingMode.HALF_DOWN);
             ruleusage = new 
BigDecimal(usageRecord.getRawUsage()).multiply(onehourcost);
             quota_usage = new QuotaUsageVO(usageRecord.getId(), 
usageRecord.getZoneId(), usageRecord.getAccountId(), usageRecord.getDomainId(), 
ruleType, ruleusage,
                     usageRecord.getStartDate(), usageRecord.getEndDate());
diff --git 
a/framework/quota/src/test/java/org/apache/cloudstack/quota/QuotaManagerImplTest.java
 
b/framework/quota/src/test/java/org/apache/cloudstack/quota/QuotaManagerImplTest.java
index 0253fc4..71f4394 100644
--- 
a/framework/quota/src/test/java/org/apache/cloudstack/quota/QuotaManagerImplTest.java
+++ 
b/framework/quota/src/test/java/org/apache/cloudstack/quota/QuotaManagerImplTest.java
@@ -151,7 +151,7 @@ public class QuotaManagerImplTest extends TestCase {
 
         QuotaUsageVO quotaUsageVO = new QuotaUsageVO();
         quotaUsageVO.setAccountId(2L);
-        
Mockito.doReturn(quotaUsageVO).when(quotaManager).updateQuotaAllocatedVMUsage(Mockito.eq(usageVO),
 Mockito.any(BigDecimal.class));
+        
Mockito.doReturn(quotaUsageVO).when(quotaManager).updateQuotaAllocatedVMUsage(Mockito.eq(usageVO));
 
         
assertTrue(quotaManager.aggregatePendingQuotaRecordsForAccount(accountVO, new 
Pair<List<? extends UsageVO>, Integer>(null, 0)).size() == 0);
         
assertTrue(quotaManager.aggregatePendingQuotaRecordsForAccount(accountVO, 
usageRecords).size() == 1);
@@ -172,11 +172,11 @@ public class QuotaManagerImplTest extends TestCase {
 
         QuotaUsageVO qu = quotaManager.updateQuotaNetwork(usageVO, 
UsageTypes.NETWORK_BYTES_SENT);
         assertTrue(qu.getQuotaUsed().compareTo(BigDecimal.ZERO) > 0);
-        qu = quotaManager.updateQuotaAllocatedVMUsage(usageVO, new 
BigDecimal(0.5));
+        qu = quotaManager.updateQuotaAllocatedVMUsage(usageVO);
         assertTrue(qu.getQuotaUsed().compareTo(BigDecimal.ZERO) > 0);
-        qu = quotaManager.updateQuotaDiskUsage(usageVO, new BigDecimal(0.5), 
UsageTypes.VOLUME);
+        qu = quotaManager.updateQuotaDiskUsage(usageVO, UsageTypes.VOLUME);
         assertTrue(qu.getQuotaUsed().compareTo(BigDecimal.ZERO) > 0);
-        qu = quotaManager.updateQuotaRaw(usageVO, new BigDecimal(0.5), 
UsageTypes.VPN_USERS);
+        qu = quotaManager.updateQuotaRaw(usageVO, UsageTypes.VPN_USERS);
         assertTrue(qu.getQuotaUsed().compareTo(BigDecimal.ZERO) > 0);
 
         Mockito.verify(quotaUsageDao, 
Mockito.times(4)).persistQuotaUsage(Mockito.any(QuotaUsageVO.class));

Reply via email to