quota: for quota calc from start date to end date, adjust the credits/debits done during this period

This commit is contained in:
Abhinandan Prateek 2015-07-21 15:22:30 +05:30
parent ab68f38fd1
commit 945103b1f7
2 changed files with 13 additions and 26 deletions

View File

@ -93,14 +93,13 @@ public class QuotaDBUtilsImpl implements QuotaDBUtils {
}
});
QuotaBalanceResponse resp = new QuotaBalanceResponse();
BigDecimal lastCredits=new BigDecimal(0);
BigDecimal lastCredits = new BigDecimal(0);
for (Iterator<QuotaBalanceVO> it = quotaBalance.iterator(); it.hasNext();) {
QuotaBalanceVO entry = it.next();
//s_logger.info("Date=" + entry.getUpdatedOn().toGMTString() + " balance=" + entry.getCreditBalance() + " credit=" + entry.getCreditsId());
s_logger.info("Date=" + entry.getUpdatedOn().toGMTString() + " balance=" + entry.getCreditBalance() + " credit=" + entry.getCreditsId());
if (entry.getCreditsId() > 0) {
lastCredits=lastCredits.add(entry.getCreditBalance());
}
else {
lastCredits = lastCredits.add(entry.getCreditBalance());
} else {
resp.setStartQuota(entry.getCreditBalance().add(lastCredits));
break; // add only consecutive credit entries
}
@ -117,14 +116,16 @@ public class QuotaDBUtilsImpl implements QuotaDBUtils {
}
Collections.sort(quotaBalance, new Comparator<QuotaBalanceVO>() {
public int compare(QuotaBalanceVO o1, QuotaBalanceVO o2) {
return o1.getUpdatedOn().compareTo(o2.getUpdatedOn()); //asc
return o1.getUpdatedOn().compareTo(o2.getUpdatedOn()); // asc
}
});
QuotaBalanceResponse resp = new QuotaBalanceResponse();
QuotaBalanceResponse resp = new QuotaBalanceResponse();
BigDecimal lastCredits = new BigDecimal(0);
for (Iterator<QuotaBalanceVO> it = quotaBalance.iterator(); it.hasNext();) {
QuotaBalanceVO entry = it.next();
if (entry.getCreditsId() > 0) {
lastCredits = lastCredits.add(entry.getCreditBalance());
resp.addCredits(entry);
it.remove();
}
@ -136,7 +137,7 @@ public class QuotaDBUtilsImpl implements QuotaDBUtils {
resp.setStartDate(startDate);
resp.setStartQuota(startItem.getCreditBalance());
resp.setEndDate(endDate);
resp.setEndQuota(endItem.getCreditBalance());
resp.setEndQuota(endItem.getCreditBalance().add(lastCredits));
} else {
resp.setStartQuota(new BigDecimal(0));
resp.setEndQuota(new BigDecimal(0));
@ -218,7 +219,7 @@ public class QuotaDBUtilsImpl implements QuotaDBUtils {
@Override
public QuotaTariffVO updateQuotaTariffPlan(QuotaTariffUpdateCmd cmd) {
short opendb=TransactionLegacy.currentTxn().getDatabaseId();
short opendb = TransactionLegacy.currentTxn().getDatabaseId();
TransactionLegacy.open(TransactionLegacy.USAGE_DB).close();
final int resourceType = cmd.getUsageType();
final BigDecimal quotaCost = new BigDecimal(cmd.getValue());
@ -258,7 +259,7 @@ public class QuotaDBUtilsImpl implements QuotaDBUtils {
@Override
@SuppressWarnings("deprecation")
public Pair<List<? extends UsageVO>, Integer> getUsageRecords(long accountId, long domainId) {
short opendb=TransactionLegacy.currentTxn().getDatabaseId();
short opendb = TransactionLegacy.currentTxn().getDatabaseId();
TransactionLegacy.open(TransactionLegacy.USAGE_DB).close();
s_logger.debug("getting usage records for account: " + accountId + ", domainId: " + domainId);
Filter usageFilter = new Filter(UsageVO.class, "startDate", true, 0L, s_recordtofetch);
@ -278,7 +279,7 @@ public class QuotaDBUtilsImpl implements QuotaDBUtils {
@Override
public ServiceOfferingVO findServiceOffering(Long vmId, long serviceOfferingId) {
short opendb=TransactionLegacy.currentTxn().getDatabaseId();
short opendb = TransactionLegacy.currentTxn().getDatabaseId();
TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
ServiceOfferingVO result;
try {

View File

@ -124,21 +124,7 @@ public class QuotaBalanceDaoImpl extends GenericDaoBase<QuotaBalanceVO, Long> im
return new ArrayList<QuotaBalanceVO>();
}
quotaUsageRecords = listBy(sc);
// get records before startDate to find start balance
List<QuotaBalanceVO> quotaUsageOnOrBeforeStartDate = findQuotaBalance(accountId, domainId, startDate);
for (Iterator<QuotaBalanceVO> it = quotaUsageOnOrBeforeStartDate.iterator(); it.hasNext();) {
QuotaBalanceVO entry = it.next();
s_logger.info("Date=" + entry.getUpdatedOn().toGMTString() + " balance=" + entry.getCreditBalance() + " credit=" + entry.getCreditsId());
if (entry.getCreditsId() > 0) {
quotaUsageRecords.add(entry);
} else {
quotaUsageRecords.add(entry);
break; // add only consecutive credit entries and last
// balance entry
}
}
quotaUsageRecords.addAll(findQuotaBalance(accountId, domainId, startDate));
} finally {
txn.close();
}