Search in sources :

Example 6 with QuotaBalanceVO

use of org.apache.cloudstack.quota.vo.QuotaBalanceVO in project cloudstack by apache.

the class QuotaManagerImpl method processQuotaBalanceForAccount.

public void processQuotaBalanceForAccount(final AccountVO account, final List<QuotaUsageVO> quotaListForAccount) {
    if (quotaListForAccount == null || quotaListForAccount.isEmpty()) {
        return;
    }
    if (s_logger.isDebugEnabled()) {
        s_logger.debug(quotaListForAccount.get(0));
    }
    Date startDate = quotaListForAccount.get(0).getStartDate();
    Date endDate = quotaListForAccount.get(0).getEndDate();
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("processQuotaBalanceForAccount startDate " + startDate + " endDate=" + endDate);
        s_logger.debug("processQuotaBalanceForAccount last items startDate " + quotaListForAccount.get(quotaListForAccount.size() - 1).getStartDate() + " items endDate=" + quotaListForAccount.get(quotaListForAccount.size() - 1).getEndDate());
    }
    quotaListForAccount.add(new QuotaUsageVO());
    BigDecimal aggrUsage = new BigDecimal(0);
    List<QuotaBalanceVO> creditsReceived = null;
    //bootstrapping
    QuotaUsageVO lastQuotaUsage = _quotaUsageDao.findLastQuotaUsageEntry(account.getAccountId(), account.getDomainId(), startDate);
    if (lastQuotaUsage == null) {
        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 {
        QuotaBalanceVO lastRealBalanceEntry = _quotaBalanceDao.findLastBalanceEntry(account.getAccountId(), account.getDomainId(), endDate);
        if (lastRealBalanceEntry != null) {
            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) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Usage entry found " + entry);
        }
        if (entry.getQuotaUsed().compareTo(BigDecimal.ZERO) == 0) {
            // check if there were credits and aggregate
            aggrUsage = aggrUsage.add(aggregateCreditBetweenDates(account, entry.getStartDate(), entry.getEndDate()));
            continue;
        }
        if (startDate.compareTo(entry.getStartDate()) != 0) {
            saveQuotaBalance(account, aggrUsage, endDate);
            //New balance entry
            aggrUsage = new BigDecimal(0);
            startDate = entry.getStartDate();
            endDate = entry.getEndDate();
            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());
            }
            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);
}
Also used : QuotaBalanceVO(org.apache.cloudstack.quota.vo.QuotaBalanceVO) Date(java.util.Date) BigDecimal(java.math.BigDecimal) QuotaUsageVO(org.apache.cloudstack.quota.vo.QuotaUsageVO)

Example 7 with QuotaBalanceVO

use of org.apache.cloudstack.quota.vo.QuotaBalanceVO in project cloudstack by apache.

the class QuotaManagerImpl method aggregateCreditBetweenDates.

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;
}
Also used : QuotaBalanceVO(org.apache.cloudstack.quota.vo.QuotaBalanceVO) BigDecimal(java.math.BigDecimal)

Example 8 with QuotaBalanceVO

use of org.apache.cloudstack.quota.vo.QuotaBalanceVO in project cloudstack by apache.

the class QuotaResponseBuilderImplTest method testCreateQuotaLastBalanceResponse.

@Test
public void testCreateQuotaLastBalanceResponse() {
    List<QuotaBalanceVO> quotaBalance = new ArrayList<>();
    // null balance test
    try {
        quotaResponseBuilder.createQuotaLastBalanceResponse(null, new Date());
    } catch (InvalidParameterValueException e) {
        assertTrue(e.getMessage().equals("There are no balance entries on or before the requested date."));
    }
    // empty balance test
    try {
        quotaResponseBuilder.createQuotaLastBalanceResponse(quotaBalance, new Date());
    } catch (InvalidParameterValueException e) {
        assertTrue(e.getMessage().equals("There are no balance entries on or before the requested date."));
    }
    // valid balance test
    QuotaBalanceVO entry = new QuotaBalanceVO();
    entry.setAccountId(2L);
    entry.setCreditBalance(new BigDecimal(100));
    quotaBalance.add(entry);
    quotaBalance.add(entry);
    Mockito.when(quotaService.computeAdjustedTime(Mockito.any(Date.class))).thenReturn(new Date());
    QuotaBalanceResponse resp = quotaResponseBuilder.createQuotaLastBalanceResponse(quotaBalance, null);
    assertTrue(resp.getStartQuota().compareTo(new BigDecimal(200)) == 0);
}
Also used : InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ArrayList(java.util.ArrayList) QuotaBalanceVO(org.apache.cloudstack.quota.vo.QuotaBalanceVO) Date(java.util.Date) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 9 with QuotaBalanceVO

use of org.apache.cloudstack.quota.vo.QuotaBalanceVO in project cloudstack by apache.

the class QuotaServiceImplTest method testFindQuotaBalanceVO.

@Test
public void testFindQuotaBalanceVO() {
    final long accountId = 2L;
    final String accountName = "admin123";
    final long domainId = 1L;
    final Date startDate = new DateTime().minusDays(2).toDate();
    final Date endDate = new Date();
    List<QuotaBalanceVO> records = new ArrayList<>();
    QuotaBalanceVO qb = new QuotaBalanceVO();
    qb.setCreditBalance(new BigDecimal(100));
    qb.setAccountId(accountId);
    records.add(qb);
    Mockito.when(respBldr.startOfNextDay()).thenReturn(endDate);
    Mockito.when(respBldr.startOfNextDay(Mockito.any(Date.class))).thenReturn(startDate);
    Mockito.when(quotaBalanceDao.findQuotaBalance(Mockito.eq(accountId), Mockito.eq(domainId), Mockito.any(Date.class), Mockito.any(Date.class))).thenReturn(records);
    Mockito.when(quotaBalanceDao.lastQuotaBalanceVO(Mockito.eq(accountId), Mockito.eq(domainId), Mockito.any(Date.class))).thenReturn(records);
    // with enddate
    assertTrue(quotaService.findQuotaBalanceVO(accountId, accountName, domainId, startDate, endDate).get(0).equals(qb));
    // without enddate
    assertTrue(quotaService.findQuotaBalanceVO(accountId, accountName, domainId, startDate, null).get(0).equals(qb));
}
Also used : ArrayList(java.util.ArrayList) QuotaBalanceVO(org.apache.cloudstack.quota.vo.QuotaBalanceVO) Date(java.util.Date) DateTime(org.joda.time.DateTime) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 10 with QuotaBalanceVO

use of org.apache.cloudstack.quota.vo.QuotaBalanceVO in project cloudstack by apache.

the class QuotaBalanceDaoImpl method lastQuotaBalance.

public BigDecimal lastQuotaBalance(final Long accountId, final Long domainId, Date startDate) {
    List<QuotaBalanceVO> quotaBalance = lastQuotaBalanceVO(accountId, domainId, startDate);
    BigDecimal finalBalance = new BigDecimal(0);
    if (quotaBalance.isEmpty()) {
        s_logger.info("There are no balance entries on or before the requested date.");
        return finalBalance;
    }
    for (QuotaBalanceVO entry : quotaBalance) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("lastQuotaBalance Entry=" + entry);
        }
        finalBalance = finalBalance.add(entry.getCreditBalance());
    }
    return finalBalance;
}
Also used : QuotaBalanceVO(org.apache.cloudstack.quota.vo.QuotaBalanceVO) BigDecimal(java.math.BigDecimal)

Aggregations

QuotaBalanceVO (org.apache.cloudstack.quota.vo.QuotaBalanceVO)11 BigDecimal (java.math.BigDecimal)8 Date (java.util.Date)8 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)5 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)3 AccountVO (com.cloud.user.AccountVO)2 QuotaBalanceResponse (org.apache.cloudstack.api.response.QuotaBalanceResponse)2 QuotaAccountVO (org.apache.cloudstack.quota.vo.QuotaAccountVO)2 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)1 Account (com.cloud.user.Account)1 User (com.cloud.user.User)1 Filter (com.cloud.utils.db.Filter)1 Field (java.lang.reflect.Field)1 List (java.util.List)1 QuotaCreditsVO (org.apache.cloudstack.quota.vo.QuotaCreditsVO)1 QuotaUsageVO (org.apache.cloudstack.quota.vo.QuotaUsageVO)1 DateTime (org.joda.time.DateTime)1