You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by re...@apache.org on 2015/12/12 09:34:37 UTC

[1/2] git commit: updated refs/heads/master to 879b6da

Repository: cloudstack
Updated Branches:
  refs/heads/master 6be2cc78c -> 879b6da4e


CLOUDSTACK-9122: latest credit entries should be incorporated when the balance is
calculated


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/bc8994ba
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/bc8994ba
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/bc8994ba

Branch: refs/heads/master
Commit: bc8994ba1c24f68d24576986d29ff8322d00a8ed
Parents: 312b9af
Author: Abhinandan Prateek <ab...@shapeblue.com>
Authored: Tue Dec 8 17:21:00 2015 +0530
Committer: Abhinandan Prateek <ab...@shapeblue.com>
Committed: Fri Dec 11 14:57:51 2015 +0530

----------------------------------------------------------------------
 .../cloudstack/quota/QuotaManagerImpl.java      | 78 ++++++++++----------
 1 file changed, 39 insertions(+), 39 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/bc8994ba/framework/quota/src/org/apache/cloudstack/quota/QuotaManagerImpl.java
----------------------------------------------------------------------
diff --git a/framework/quota/src/org/apache/cloudstack/quota/QuotaManagerImpl.java b/framework/quota/src/org/apache/cloudstack/quota/QuotaManagerImpl.java
index d7c301e..b039e26 100644
--- a/framework/quota/src/org/apache/cloudstack/quota/QuotaManagerImpl.java
+++ b/framework/quota/src/org/apache/cloudstack/quota/QuotaManagerImpl.java
@@ -214,29 +214,18 @@ public class QuotaManagerImpl extends ManagerBase implements QuotaManager {
         //bootstrapping
         QuotaUsageVO lastQuotaUsage = _quotaUsageDao.findLastQuotaUsageEntry(account.getAccountId(), account.getDomainId(), startDate);
         if (lastQuotaUsage == null) {
-            creditsReceived = _quotaBalanceDao.findCreditBalance(account.getAccountId(), account.getDomainId(), new Date(0), startDate);
-            if (s_logger.isDebugEnabled()) {
-                s_logger.debug("Credit entries count " + creditsReceived.size() + " on Before Date=" + startDate);
-            }
-            if (creditsReceived != null) {
-                for (QuotaBalanceVO credit : creditsReceived) {
-                    if (s_logger.isDebugEnabled()) {
-                        s_logger.debug("Credit entry found " + credit);
-                        s_logger.debug("Total = " + aggrUsage);
-                    }
-                    aggrUsage = aggrUsage.add(credit.getCreditBalance());
-                }
-            }
+            aggrUsage = aggrUsage.add(aggregateCreditBetweenDates(account, new Date(0), startDate));
             // create a balance entry for these accumulated credits
             QuotaBalanceVO firstBalance = new QuotaBalanceVO(account.getAccountId(), account.getDomainId(), aggrUsage, startDate);
             _quotaBalanceDao.saveQuotaBalance(firstBalance);
-        }
-        else {
+        } else {
             QuotaBalanceVO lastRealBalanceEntry = _quotaBalanceDao.findLastBalanceEntry(account.getAccountId(), account.getDomainId(), endDate);
             aggrUsage = aggrUsage.add(lastRealBalanceEntry.getCreditBalance());
             if (s_logger.isDebugEnabled()) {
                 s_logger.debug("Last balance entry  " + lastRealBalanceEntry + " AggrUsage=" + aggrUsage);
             }
+            // get all the credit entries after this balance and add
+            aggrUsage = aggrUsage.add(aggregateCreditBetweenDates(account, lastRealBalanceEntry.getUpdatedOn(), endDate));
         }
 
         for (QuotaUsageVO entry : quotaListForAccount) {
@@ -244,25 +233,12 @@ public class QuotaManagerImpl extends ManagerBase implements QuotaManager {
                 s_logger.debug("Usage entry found " + entry);
             }
             if (entry.getQuotaUsed().compareTo(BigDecimal.ZERO) == 0) {
-                // check if there were credits
-                creditsReceived = _quotaBalanceDao.findCreditBalance(account.getAccountId(), account.getDomainId(), entry.getStartDate(), entry.getEndDate());
-                if (creditsReceived != null) {
-                    for (QuotaBalanceVO credit : creditsReceived) {
-                        if (s_logger.isDebugEnabled()) {
-                            s_logger.debug("Credit entry found " + credit);
-                            s_logger.debug("Total = " + aggrUsage);
-                        }
-                        aggrUsage = aggrUsage.add(credit.getCreditBalance());
-                    }
-                }
+                // check if there were credits and aggregate
+                aggrUsage = aggrUsage.add(aggregateCreditBetweenDates(account, entry.getStartDate(), entry.getEndDate()));
                 continue;
             }
             if (startDate.compareTo(entry.getStartDate()) != 0) {
-                QuotaBalanceVO newBalance = new QuotaBalanceVO(account.getAccountId(), account.getDomainId(), aggrUsage, endDate);
-                _quotaBalanceDao.saveQuotaBalance(newBalance);
-                if (s_logger.isDebugEnabled()) {
-                    s_logger.debug("Saving Balance" + newBalance);
-                }
+                saveQuotaBalance(account, aggrUsage, endDate);
 
                 //New balance entry
                 aggrUsage = new BigDecimal(0);
@@ -275,24 +251,28 @@ public class QuotaManagerImpl extends ManagerBase implements QuotaManager {
                     lastBalanceDate = lastRealBalanceEntry.getUpdatedOn();
                     aggrUsage = aggrUsage.add(lastRealBalanceEntry.getCreditBalance());
                 }
-                creditsReceived = _quotaBalanceDao.findCreditBalance(account.getAccountId(), account.getDomainId(), lastBalanceDate, endDate);
-                if (creditsReceived != null) {
-                    for (QuotaBalanceVO credit : creditsReceived) {
-                        aggrUsage = aggrUsage.add(credit.getCreditBalance());
-                    }
-                }
                 if (s_logger.isDebugEnabled()) {
                     s_logger.debug("Getting Balance" + account.getAccountName() + ",Balance entry=" + aggrUsage + " on Date=" + endDate);
                 }
+                aggrUsage = aggrUsage.add(aggregateCreditBetweenDates(account, lastBalanceDate, endDate));
             }
             aggrUsage = aggrUsage.subtract(entry.getQuotaUsed());
         }
+        saveQuotaBalance(account, aggrUsage, endDate);
+
+        // update quota_balance
+        saveQuotaAccount(account, aggrUsage, endDate);
+    }
+
+    private QuotaBalanceVO saveQuotaBalance(final AccountVO account, final BigDecimal aggrUsage, final Date endDate) {
         QuotaBalanceVO newBalance = new QuotaBalanceVO(account.getAccountId(), account.getDomainId(), aggrUsage, endDate);
-        _quotaBalanceDao.saveQuotaBalance(newBalance);
         if (s_logger.isDebugEnabled()) {
             s_logger.debug("Saving Balance" + newBalance);
         }
+        return _quotaBalanceDao.saveQuotaBalance(newBalance);
+    }
 
+    private boolean saveQuotaAccount(final AccountVO account, final BigDecimal aggrUsage, final Date endDate) {
         // update quota_accounts
         QuotaAccountVO quota_account = _quotaAcc.findByIdQuotaAccount(account.getAccountId());
 
@@ -304,14 +284,34 @@ public class QuotaManagerImpl extends ManagerBase implements QuotaManager {
                 s_logger.debug(quota_account);
             }
             _quotaAcc.persistQuotaAccount(quota_account);
+            return true;
         } else {
             quota_account.setQuotaBalance(aggrUsage);
             quota_account.setQuotaBalanceDate(endDate);
             if (s_logger.isDebugEnabled()) {
                 s_logger.debug(quota_account);
             }
-            _quotaAcc.updateQuotaAccount(account.getAccountId(), quota_account);
+            return _quotaAcc.updateQuotaAccount(account.getAccountId(), quota_account);
+        }
+    }
+
+    private BigDecimal aggregateCreditBetweenDates(final AccountVO account, final Date startDate, final Date endDate) {
+        BigDecimal aggrUsage = new BigDecimal(0);
+        List<QuotaBalanceVO> creditsReceived = null;
+        creditsReceived = _quotaBalanceDao.findCreditBalance(account.getAccountId(), account.getDomainId(), startDate, endDate);
+        if (s_logger.isDebugEnabled()) {
+            s_logger.debug("Credit entries count " + creditsReceived.size() + " on Before Date=" + endDate);
+        }
+        if (creditsReceived != null) {
+            for (QuotaBalanceVO credit : creditsReceived) {
+                if (s_logger.isDebugEnabled()) {
+                    s_logger.debug("Credit entry found " + credit);
+                    s_logger.debug("Total = " + aggrUsage);
+                }
+                aggrUsage = aggrUsage.add(credit.getCreditBalance());
+            }
         }
+        return aggrUsage;
     }
 
     @Override


[2/2] git commit: updated refs/heads/master to 879b6da

Posted by re...@apache.org.
Merge pull request #1192 from shapeblue/master-9122

CLOUDSTACK-9122:  latest credit entries should be incorporated when thefuture credit entries should be incorporated when the balance is calculated

1. Some code is refactored so that unit tests (TODO in future) are more comprehensive
2. The fix is to incorporate the credit entries on line 228.

https://issues.apache.org/jira/browse/CLOUDSTACK-9122

* pr/1192:
  CLOUDSTACK-9122: latest credit entries should be incorporated when the balance is calculated

Signed-off-by: Remi Bergsma <gi...@remi.nl>


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/879b6da4
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/879b6da4
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/879b6da4

Branch: refs/heads/master
Commit: 879b6da4eda10af9a1a42018a4713ad85301a327
Parents: 6be2cc7 bc8994b
Author: Remi Bergsma <gi...@remi.nl>
Authored: Sat Dec 12 09:33:54 2015 +0100
Committer: Remi Bergsma <gi...@remi.nl>
Committed: Sat Dec 12 09:33:54 2015 +0100

----------------------------------------------------------------------
 .../cloudstack/quota/QuotaManagerImpl.java      | 78 ++++++++++----------
 1 file changed, 39 insertions(+), 39 deletions(-)
----------------------------------------------------------------------