Skip to content

Commit bc8994b

Browse files
CLOUDSTACK-9122: latest credit entries should be incorporated when the balance is
calculated
1 parent 312b9af commit bc8994b

1 file changed

Lines changed: 39 additions & 39 deletions

File tree

framework/quota/src/org/apache/cloudstack/quota/QuotaManagerImpl.java

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -214,55 +214,31 @@ public void processQuotaBalanceForAccount(final AccountVO account, final List<Qu
214214
//bootstrapping
215215
QuotaUsageVO lastQuotaUsage = _quotaUsageDao.findLastQuotaUsageEntry(account.getAccountId(), account.getDomainId(), startDate);
216216
if (lastQuotaUsage == null) {
217-
creditsReceived = _quotaBalanceDao.findCreditBalance(account.getAccountId(), account.getDomainId(), new Date(0), startDate);
218-
if (s_logger.isDebugEnabled()) {
219-
s_logger.debug("Credit entries count " + creditsReceived.size() + " on Before Date=" + startDate);
220-
}
221-
if (creditsReceived != null) {
222-
for (QuotaBalanceVO credit : creditsReceived) {
223-
if (s_logger.isDebugEnabled()) {
224-
s_logger.debug("Credit entry found " + credit);
225-
s_logger.debug("Total = " + aggrUsage);
226-
}
227-
aggrUsage = aggrUsage.add(credit.getCreditBalance());
228-
}
229-
}
217+
aggrUsage = aggrUsage.add(aggregateCreditBetweenDates(account, new Date(0), startDate));
230218
// create a balance entry for these accumulated credits
231219
QuotaBalanceVO firstBalance = new QuotaBalanceVO(account.getAccountId(), account.getDomainId(), aggrUsage, startDate);
232220
_quotaBalanceDao.saveQuotaBalance(firstBalance);
233-
}
234-
else {
221+
} else {
235222
QuotaBalanceVO lastRealBalanceEntry = _quotaBalanceDao.findLastBalanceEntry(account.getAccountId(), account.getDomainId(), endDate);
236223
aggrUsage = aggrUsage.add(lastRealBalanceEntry.getCreditBalance());
237224
if (s_logger.isDebugEnabled()) {
238225
s_logger.debug("Last balance entry " + lastRealBalanceEntry + " AggrUsage=" + aggrUsage);
239226
}
227+
// get all the credit entries after this balance and add
228+
aggrUsage = aggrUsage.add(aggregateCreditBetweenDates(account, lastRealBalanceEntry.getUpdatedOn(), endDate));
240229
}
241230

242231
for (QuotaUsageVO entry : quotaListForAccount) {
243232
if (s_logger.isDebugEnabled()) {
244233
s_logger.debug("Usage entry found " + entry);
245234
}
246235
if (entry.getQuotaUsed().compareTo(BigDecimal.ZERO) == 0) {
247-
// check if there were credits
248-
creditsReceived = _quotaBalanceDao.findCreditBalance(account.getAccountId(), account.getDomainId(), entry.getStartDate(), entry.getEndDate());
249-
if (creditsReceived != null) {
250-
for (QuotaBalanceVO credit : creditsReceived) {
251-
if (s_logger.isDebugEnabled()) {
252-
s_logger.debug("Credit entry found " + credit);
253-
s_logger.debug("Total = " + aggrUsage);
254-
}
255-
aggrUsage = aggrUsage.add(credit.getCreditBalance());
256-
}
257-
}
236+
// check if there were credits and aggregate
237+
aggrUsage = aggrUsage.add(aggregateCreditBetweenDates(account, entry.getStartDate(), entry.getEndDate()));
258238
continue;
259239
}
260240
if (startDate.compareTo(entry.getStartDate()) != 0) {
261-
QuotaBalanceVO newBalance = new QuotaBalanceVO(account.getAccountId(), account.getDomainId(), aggrUsage, endDate);
262-
_quotaBalanceDao.saveQuotaBalance(newBalance);
263-
if (s_logger.isDebugEnabled()) {
264-
s_logger.debug("Saving Balance" + newBalance);
265-
}
241+
saveQuotaBalance(account, aggrUsage, endDate);
266242

267243
//New balance entry
268244
aggrUsage = new BigDecimal(0);
@@ -275,24 +251,28 @@ public void processQuotaBalanceForAccount(final AccountVO account, final List<Qu
275251
lastBalanceDate = lastRealBalanceEntry.getUpdatedOn();
276252
aggrUsage = aggrUsage.add(lastRealBalanceEntry.getCreditBalance());
277253
}
278-
creditsReceived = _quotaBalanceDao.findCreditBalance(account.getAccountId(), account.getDomainId(), lastBalanceDate, endDate);
279-
if (creditsReceived != null) {
280-
for (QuotaBalanceVO credit : creditsReceived) {
281-
aggrUsage = aggrUsage.add(credit.getCreditBalance());
282-
}
283-
}
284254
if (s_logger.isDebugEnabled()) {
285255
s_logger.debug("Getting Balance" + account.getAccountName() + ",Balance entry=" + aggrUsage + " on Date=" + endDate);
286256
}
257+
aggrUsage = aggrUsage.add(aggregateCreditBetweenDates(account, lastBalanceDate, endDate));
287258
}
288259
aggrUsage = aggrUsage.subtract(entry.getQuotaUsed());
289260
}
261+
saveQuotaBalance(account, aggrUsage, endDate);
262+
263+
// update quota_balance
264+
saveQuotaAccount(account, aggrUsage, endDate);
265+
}
266+
267+
private QuotaBalanceVO saveQuotaBalance(final AccountVO account, final BigDecimal aggrUsage, final Date endDate) {
290268
QuotaBalanceVO newBalance = new QuotaBalanceVO(account.getAccountId(), account.getDomainId(), aggrUsage, endDate);
291-
_quotaBalanceDao.saveQuotaBalance(newBalance);
292269
if (s_logger.isDebugEnabled()) {
293270
s_logger.debug("Saving Balance" + newBalance);
294271
}
272+
return _quotaBalanceDao.saveQuotaBalance(newBalance);
273+
}
295274

275+
private boolean saveQuotaAccount(final AccountVO account, final BigDecimal aggrUsage, final Date endDate) {
296276
// update quota_accounts
297277
QuotaAccountVO quota_account = _quotaAcc.findByIdQuotaAccount(account.getAccountId());
298278

@@ -304,14 +284,34 @@ public void processQuotaBalanceForAccount(final AccountVO account, final List<Qu
304284
s_logger.debug(quota_account);
305285
}
306286
_quotaAcc.persistQuotaAccount(quota_account);
287+
return true;
307288
} else {
308289
quota_account.setQuotaBalance(aggrUsage);
309290
quota_account.setQuotaBalanceDate(endDate);
310291
if (s_logger.isDebugEnabled()) {
311292
s_logger.debug(quota_account);
312293
}
313-
_quotaAcc.updateQuotaAccount(account.getAccountId(), quota_account);
294+
return _quotaAcc.updateQuotaAccount(account.getAccountId(), quota_account);
295+
}
296+
}
297+
298+
private BigDecimal aggregateCreditBetweenDates(final AccountVO account, final Date startDate, final Date endDate) {
299+
BigDecimal aggrUsage = new BigDecimal(0);
300+
List<QuotaBalanceVO> creditsReceived = null;
301+
creditsReceived = _quotaBalanceDao.findCreditBalance(account.getAccountId(), account.getDomainId(), startDate, endDate);
302+
if (s_logger.isDebugEnabled()) {
303+
s_logger.debug("Credit entries count " + creditsReceived.size() + " on Before Date=" + endDate);
304+
}
305+
if (creditsReceived != null) {
306+
for (QuotaBalanceVO credit : creditsReceived) {
307+
if (s_logger.isDebugEnabled()) {
308+
s_logger.debug("Credit entry found " + credit);
309+
s_logger.debug("Total = " + aggrUsage);
310+
}
311+
aggrUsage = aggrUsage.add(credit.getCreditBalance());
312+
}
314313
}
314+
return aggrUsage;
315315
}
316316

317317
@Override

0 commit comments

Comments
 (0)