Search in sources :

Example 1 with QuotaUsageVO

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

the class QuotaManagerImpl method updateQuotaDiskUsage.

public QuotaUsageVO updateQuotaDiskUsage(UsageVO usageRecord, final BigDecimal aggregationRatio, final int quotaType) {
    QuotaUsageVO quota_usage = null;
    QuotaTariffVO tariff = _quotaTariffDao.findTariffPlanByUsageType(quotaType, usageRecord.getEndDate());
    if (tariff != null && tariff.getCurrencyValue().compareTo(BigDecimal.ZERO) != 0) {
        BigDecimal quotaUsgage;
        BigDecimal onehourcostpergb;
        BigDecimal noofgbinuse;
        onehourcostpergb = tariff.getCurrencyValue().multiply(aggregationRatio);
        noofgbinuse = new BigDecimal(usageRecord.getSize()).divide(s_gb, 8, RoundingMode.HALF_EVEN);
        quotaUsgage = new BigDecimal(usageRecord.getRawUsage()).multiply(onehourcostpergb).multiply(noofgbinuse);
        quota_usage = new QuotaUsageVO(usageRecord.getId(), usageRecord.getZoneId(), usageRecord.getAccountId(), usageRecord.getDomainId(), usageRecord.getUsageType(), quotaUsgage, usageRecord.getStartDate(), usageRecord.getEndDate());
        _quotaUsageDao.persistQuotaUsage(quota_usage);
    }
    usageRecord.setQuotaCalculated(1);
    _usageDao.persistUsage(usageRecord);
    return quota_usage;
}
Also used : QuotaTariffVO(org.apache.cloudstack.quota.vo.QuotaTariffVO) BigDecimal(java.math.BigDecimal) QuotaUsageVO(org.apache.cloudstack.quota.vo.QuotaUsageVO)

Example 2 with QuotaUsageVO

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

the class QuotaResponseBuilderImpl method createQuotaStatementResponse.

@Override
public QuotaStatementResponse createQuotaStatementResponse(final List<QuotaUsageVO> quotaUsage) {
    if (quotaUsage == null || quotaUsage.isEmpty()) {
        throw new InvalidParameterValueException("There is no usage data found for period mentioned.");
    }
    QuotaStatementResponse statement = new QuotaStatementResponse();
    HashMap<Integer, QuotaTypes> quotaTariffMap = new HashMap<Integer, QuotaTypes>();
    Collection<QuotaTypes> result = QuotaTypes.listQuotaTypes().values();
    for (QuotaTypes quotaTariff : result) {
        quotaTariffMap.put(quotaTariff.getQuotaType(), quotaTariff);
        // add dummy record for each usage type
        QuotaUsageVO dummy = new QuotaUsageVO(quotaUsage.get(0));
        dummy.setUsageType(quotaTariff.getQuotaType());
        dummy.setQuotaUsed(new BigDecimal(0));
        quotaUsage.add(dummy);
    }
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("createQuotaStatementResponse Type=" + quotaUsage.get(0).getUsageType() + " usage=" + quotaUsage.get(0).getQuotaUsed().setScale(2, RoundingMode.HALF_EVEN) + " rec.id=" + quotaUsage.get(0).getUsageItemId() + " SD=" + quotaUsage.get(0).getStartDate() + " ED=" + quotaUsage.get(0).getEndDate());
    }
    Collections.sort(quotaUsage, new Comparator<QuotaUsageVO>() {

        @Override
        public int compare(QuotaUsageVO o1, QuotaUsageVO o2) {
            if (o1.getUsageType() == o2.getUsageType()) {
                return 0;
            }
            return o1.getUsageType() < o2.getUsageType() ? -1 : 1;
        }
    });
    List<QuotaStatementItemResponse> items = new ArrayList<QuotaStatementItemResponse>();
    QuotaStatementItemResponse lineitem;
    int type = -1;
    BigDecimal usage = new BigDecimal(0);
    BigDecimal totalUsage = new BigDecimal(0);
    // boundary
    quotaUsage.add(new QuotaUsageVO());
    QuotaUsageVO prev = quotaUsage.get(0);
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("createQuotaStatementResponse record count=" + quotaUsage.size());
    }
    for (final QuotaUsageVO quotaRecord : quotaUsage) {
        if (type != quotaRecord.getUsageType()) {
            if (type != -1) {
                lineitem = new QuotaStatementItemResponse(type);
                lineitem.setQuotaUsed(usage);
                lineitem.setAccountId(prev.getAccountId());
                lineitem.setDomainId(prev.getDomainId());
                lineitem.setUsageUnit(quotaTariffMap.get(type).getQuotaUnit());
                lineitem.setUsageName(quotaTariffMap.get(type).getQuotaName());
                lineitem.setObjectName("quotausage");
                items.add(lineitem);
                totalUsage = totalUsage.add(usage);
                usage = new BigDecimal(0);
            }
            type = quotaRecord.getUsageType();
        }
        prev = quotaRecord;
        usage = usage.add(quotaRecord.getQuotaUsed());
    }
    statement.setLineItem(items);
    statement.setTotalQuota(totalUsage);
    statement.setCurrency(QuotaConfig.QuotaCurrencySymbol.value());
    statement.setObjectName("statement");
    return statement;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) QuotaTypes(org.apache.cloudstack.quota.constant.QuotaTypes) QuotaUsageVO(org.apache.cloudstack.quota.vo.QuotaUsageVO)

Example 3 with QuotaUsageVO

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

the class QuotaStatementCmdTest method testQuotaStatementCmd.

@Test
public void testQuotaStatementCmd() throws NoSuchFieldException, IllegalAccessException {
    QuotaStatementCmd cmd = new QuotaStatementCmd();
    cmd.setAccountName("admin");
    Field rbField = QuotaStatementCmd.class.getDeclaredField("_responseBuilder");
    rbField.setAccessible(true);
    rbField.set(cmd, responseBuilder);
    List<QuotaUsageVO> quotaUsageVOList = new ArrayList<QuotaUsageVO>();
    Mockito.when(responseBuilder.getQuotaUsage(Mockito.eq(cmd))).thenReturn(quotaUsageVOList);
    Mockito.when(responseBuilder.createQuotaStatementResponse(Mockito.eq(quotaUsageVOList))).thenReturn(new QuotaStatementResponse());
    cmd.execute();
    Mockito.verify(responseBuilder, Mockito.times(1)).getQuotaUsage(Mockito.eq(cmd));
}
Also used : Field(java.lang.reflect.Field) QuotaStatementResponse(org.apache.cloudstack.api.response.QuotaStatementResponse) ArrayList(java.util.ArrayList) QuotaUsageVO(org.apache.cloudstack.quota.vo.QuotaUsageVO) Test(org.junit.Test)

Example 4 with QuotaUsageVO

use of org.apache.cloudstack.quota.vo.QuotaUsageVO 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 5 with QuotaUsageVO

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

the class QuotaManagerImpl method updateQuotaDiskUsage.

public QuotaUsageVO updateQuotaDiskUsage(UsageVO usageRecord, final int quotaType) {
    QuotaUsageVO quota_usage = null;
    QuotaTariffVO tariff = _quotaTariffDao.findTariffPlanByUsageType(quotaType, usageRecord.getEndDate());
    if (tariff != null && tariff.getCurrencyValue().compareTo(BigDecimal.ZERO) != 0) {
        BigDecimal quotaUsgage;
        BigDecimal onehourcostpergb;
        BigDecimal noofgbinuse;
        onehourcostpergb = tariff.getCurrencyValue().divide(s_hoursInMonth, 8, RoundingMode.HALF_DOWN);
        noofgbinuse = new BigDecimal(usageRecord.getSize()).divide(s_gb, 8, RoundingMode.HALF_EVEN);
        quotaUsgage = new BigDecimal(usageRecord.getRawUsage()).multiply(onehourcostpergb).multiply(noofgbinuse);
        quota_usage = new QuotaUsageVO(usageRecord.getId(), usageRecord.getZoneId(), usageRecord.getAccountId(), usageRecord.getDomainId(), usageRecord.getUsageType(), quotaUsgage, usageRecord.getStartDate(), usageRecord.getEndDate());
        _quotaUsageDao.persistQuotaUsage(quota_usage);
    }
    usageRecord.setQuotaCalculated(1);
    _usageDao.persistUsage(usageRecord);
    return quota_usage;
}
Also used : QuotaTariffVO(org.apache.cloudstack.quota.vo.QuotaTariffVO) BigDecimal(java.math.BigDecimal) QuotaUsageVO(org.apache.cloudstack.quota.vo.QuotaUsageVO)

Aggregations

QuotaUsageVO (org.apache.cloudstack.quota.vo.QuotaUsageVO)20 BigDecimal (java.math.BigDecimal)13 QuotaTariffVO (org.apache.cloudstack.quota.vo.QuotaTariffVO)10 ArrayList (java.util.ArrayList)9 UsageVO (com.cloud.usage.UsageVO)5 Test (org.junit.Test)5 AccountVO (com.cloud.user.AccountVO)4 Date (java.util.Date)4 QuotaAccountVO (org.apache.cloudstack.quota.vo.QuotaAccountVO)4 List (java.util.List)3 Pair (com.cloud.utils.Pair)2 QuotaStatementResponse (org.apache.cloudstack.api.response.QuotaStatementResponse)2 ServiceOfferingVO (org.apache.cloudstack.quota.vo.ServiceOfferingVO)2 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)1 Field (java.lang.reflect.Field)1 HashMap (java.util.HashMap)1 QuotaTypes (org.apache.cloudstack.quota.constant.QuotaTypes)1 QuotaBalanceVO (org.apache.cloudstack.quota.vo.QuotaBalanceVO)1