mirror of
https://github.com/apache/cloudstack
synced 2026-08-29 16:44:23 +00:00
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 <github@remi.nl>
This commit is contained in:
commit
879b6da4ed
@ -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,16 +284,36 @@ 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
|
||||
public boolean calculateQuotaUsage() {
|
||||
List<AccountVO> accounts = _accountDao.listAll();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user