CLOUDSTACK-8592: bootstrapping the balance entries

This commit is contained in:
Abhinandan Prateek 2015-10-29 19:18:09 +05:30
parent fe21b4ddb6
commit b3592b2073
3 changed files with 47 additions and 2 deletions

View File

@ -17,6 +17,7 @@
package org.apache.cloudstack.quota.dao;
import com.cloud.utils.db.GenericDao;
import org.apache.cloudstack.quota.vo.QuotaUsageVO;
import java.math.BigDecimal;
@ -30,4 +31,6 @@ public interface QuotaUsageDao extends GenericDao<QuotaUsageVO, Long> {
List<QuotaUsageVO> findQuotaUsage(Long accountId, Long domainId, Integer usageType, Date startDate, Date endDate);
BigDecimal findTotalQuotaUsage(Long accountId, Long domainId, Integer usageType, Date startDate, Date endDate);
QuotaUsageVO findLastQuotaUsageEntry(Long accountId, Long domainId, Date beforeThis);
}

View File

@ -16,6 +16,7 @@
//under the License.
package org.apache.cloudstack.quota.dao;
import com.cloud.utils.db.Filter;
import com.cloud.utils.db.GenericDaoBase;
import com.cloud.utils.db.QueryBuilder;
import com.cloud.utils.db.SearchCriteria;
@ -74,6 +75,22 @@ public class QuotaUsageDaoImpl extends GenericDaoBase<QuotaUsageVO, Long> implem
});
}
public QuotaUsageVO findLastQuotaUsageEntry(final Long accountId, final Long domainId, final Date beforeThis) {
return Transaction.execute(TransactionLegacy.USAGE_DB, new TransactionCallback<QuotaUsageVO>() {
@Override
public QuotaUsageVO doInTransaction(final TransactionStatus status) {
List<QuotaUsageVO> quotaUsageEntries = new ArrayList<>();
Filter filter = new Filter(QuotaUsageVO.class, "startDate", false, 0L, 1L);
QueryBuilder<QuotaUsageVO> qb = QueryBuilder.create(QuotaUsageVO.class);
qb.and(qb.entity().getAccountId(), SearchCriteria.Op.EQ, accountId);
qb.and(qb.entity().getDomainId(), SearchCriteria.Op.EQ, domainId);
qb.and(qb.entity().getStartDate(), SearchCriteria.Op.LT, beforeThis);
quotaUsageEntries = search(qb.create(), filter);
return !quotaUsageEntries.isEmpty() ? quotaUsageEntries.get(0) : null;
}
});
}
public QuotaUsageVO persistQuotaUsage(final QuotaUsageVO quotaUsage) {
return Transaction.execute(TransactionLegacy.USAGE_DB, new TransactionCallback<QuotaUsageVO>() {
@Override

View File

@ -186,8 +186,33 @@ public class QuotaManagerImpl extends ManagerBase implements QuotaManager {
Date startDate = quotaListForAccount.get(0).getStartDate();
Date endDate = quotaListForAccount.get(0).getEndDate();
BigDecimal aggrUsage = new BigDecimal(0);
List<QuotaBalanceVO> creditsReceived = null;
//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 (creditsReceived != null) {
for (QuotaBalanceVO credit : creditsReceived) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Credit entry found " + aggrUsage.setScale(6, RoundingMode.HALF_EVEN).toString() + " on Date=" + startDate);
}
aggrUsage = aggrUsage.add(credit.getCreditBalance());
}
}
}
for (QuotaUsageVO entry : quotaListForAccount) {
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 " + aggrUsage.setScale(6, RoundingMode.HALF_EVEN).toString() + " on Date=" + entry.getStartDate());
}
aggrUsage = aggrUsage.add(credit.getCreditBalance());
}
}
continue;
}
if (s_logger.isDebugEnabled()) {
@ -207,13 +232,13 @@ public class QuotaManagerImpl extends ManagerBase implements QuotaManager {
startDate = entry.getStartDate();
endDate = entry.getEndDate();
QuotaBalanceVO lastRealBalanceEntry = _quotaBalanceDao.findLastBalanceEntry(account.getAccountId(), account.getDomainId(), startDate);
QuotaBalanceVO lastRealBalanceEntry = _quotaBalanceDao.findLastBalanceEntry(account.getAccountId(), account.getDomainId(), endDate);
Date lastBalanceDate = new Date(0);
if (lastRealBalanceEntry != null) {
lastBalanceDate = lastRealBalanceEntry.getUpdatedOn();
aggrUsage = aggrUsage.add(lastRealBalanceEntry.getCreditBalance());
}
List<QuotaBalanceVO> creditsReceived = _quotaBalanceDao.findCreditBalance(account.getAccountId(), account.getDomainId(), lastBalanceDate, endDate);
creditsReceived = _quotaBalanceDao.findCreditBalance(account.getAccountId(), account.getDomainId(), lastBalanceDate, endDate);
if (creditsReceived != null) {
for (QuotaBalanceVO credit : creditsReceived) {
aggrUsage = aggrUsage.add(credit.getCreditBalance());