Search in sources :

Example 11 with QuotaUsageVO

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

the class QuotaManagerImpl method updateQuotaRunningVMUsage.

public List<QuotaUsageVO> updateQuotaRunningVMUsage(UsageVO usageRecord, final BigDecimal aggregationRatio) {
    List<QuotaUsageVO> quotalist = new ArrayList<QuotaUsageVO>();
    QuotaUsageVO quota_usage;
    BigDecimal cpuquotausgage, speedquotausage, memoryquotausage, vmusage;
    BigDecimal onehourcostpercpu, onehourcostper100mhz, onehourcostper1mb, onehourcostforvmusage;
    BigDecimal rawusage;
    // get service offering details
    ServiceOfferingVO serviceoffering = _serviceOfferingDao.findServiceOffering(usageRecord.getVmInstanceId(), usageRecord.getOfferingId());
    if (serviceoffering == null)
        return quotalist;
    rawusage = new BigDecimal(usageRecord.getRawUsage());
    QuotaTariffVO tariff = _quotaTariffDao.findTariffPlanByUsageType(QuotaTypes.CPU_NUMBER, usageRecord.getEndDate());
    if (tariff != null && tariff.getCurrencyValue().compareTo(BigDecimal.ZERO) != 0 && serviceoffering.getCpu() != null) {
        BigDecimal cpu = new BigDecimal(serviceoffering.getCpu());
        onehourcostpercpu = tariff.getCurrencyValue().multiply(aggregationRatio);
        cpuquotausgage = rawusage.multiply(onehourcostpercpu).multiply(cpu);
        quota_usage = new QuotaUsageVO(usageRecord.getId(), usageRecord.getZoneId(), usageRecord.getAccountId(), usageRecord.getDomainId(), QuotaTypes.CPU_NUMBER, cpuquotausgage, usageRecord.getStartDate(), usageRecord.getEndDate());
        _quotaUsageDao.persistQuotaUsage(quota_usage);
        quotalist.add(quota_usage);
    }
    tariff = _quotaTariffDao.findTariffPlanByUsageType(QuotaTypes.CPU_CLOCK_RATE, usageRecord.getEndDate());
    if (tariff != null && tariff.getCurrencyValue().compareTo(BigDecimal.ZERO) != 0 && serviceoffering.getSpeed() != null) {
        BigDecimal speed = new BigDecimal(serviceoffering.getSpeed() / 100.00);
        onehourcostper100mhz = tariff.getCurrencyValue().multiply(aggregationRatio);
        speedquotausage = rawusage.multiply(onehourcostper100mhz).multiply(speed);
        quota_usage = new QuotaUsageVO(usageRecord.getId(), usageRecord.getZoneId(), usageRecord.getAccountId(), usageRecord.getDomainId(), QuotaTypes.CPU_CLOCK_RATE, speedquotausage, usageRecord.getStartDate(), usageRecord.getEndDate());
        _quotaUsageDao.persistQuotaUsage(quota_usage);
        quotalist.add(quota_usage);
    }
    tariff = _quotaTariffDao.findTariffPlanByUsageType(QuotaTypes.MEMORY, usageRecord.getEndDate());
    if (tariff != null && tariff.getCurrencyValue().compareTo(BigDecimal.ZERO) != 0 && serviceoffering.getRamSize() != null) {
        BigDecimal memory = new BigDecimal(serviceoffering.getRamSize());
        onehourcostper1mb = tariff.getCurrencyValue().multiply(aggregationRatio);
        memoryquotausage = rawusage.multiply(onehourcostper1mb).multiply(memory);
        quota_usage = new QuotaUsageVO(usageRecord.getId(), usageRecord.getZoneId(), usageRecord.getAccountId(), usageRecord.getDomainId(), QuotaTypes.MEMORY, memoryquotausage, usageRecord.getStartDate(), usageRecord.getEndDate());
        _quotaUsageDao.persistQuotaUsage(quota_usage);
        quotalist.add(quota_usage);
    }
    tariff = _quotaTariffDao.findTariffPlanByUsageType(QuotaTypes.RUNNING_VM, usageRecord.getEndDate());
    if (tariff != null && tariff.getCurrencyValue().compareTo(BigDecimal.ZERO) != 0) {
        onehourcostforvmusage = tariff.getCurrencyValue().multiply(aggregationRatio);
        vmusage = rawusage.multiply(onehourcostforvmusage);
        quota_usage = new QuotaUsageVO(usageRecord.getId(), usageRecord.getZoneId(), usageRecord.getAccountId(), usageRecord.getDomainId(), QuotaTypes.RUNNING_VM, vmusage, usageRecord.getStartDate(), usageRecord.getEndDate());
        _quotaUsageDao.persistQuotaUsage(quota_usage);
        quotalist.add(quota_usage);
    }
    usageRecord.setQuotaCalculated(1);
    _usageDao.persistUsage(usageRecord);
    return quotalist;
}
Also used : QuotaTariffVO(org.apache.cloudstack.quota.vo.QuotaTariffVO) ArrayList(java.util.ArrayList) ServiceOfferingVO(org.apache.cloudstack.quota.vo.ServiceOfferingVO) BigDecimal(java.math.BigDecimal) QuotaUsageVO(org.apache.cloudstack.quota.vo.QuotaUsageVO)

Example 12 with QuotaUsageVO

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

the class QuotaManagerImpl method updateQuotaRaw.

public QuotaUsageVO updateQuotaRaw(UsageVO usageRecord, final BigDecimal aggregationRatio, final int ruleType) {
    QuotaUsageVO quota_usage = null;
    QuotaTariffVO tariff = _quotaTariffDao.findTariffPlanByUsageType(ruleType, usageRecord.getEndDate());
    if (tariff != null && tariff.getCurrencyValue().compareTo(BigDecimal.ZERO) != 0) {
        BigDecimal ruleusage;
        BigDecimal onehourcost;
        onehourcost = tariff.getCurrencyValue().multiply(aggregationRatio);
        ruleusage = new BigDecimal(usageRecord.getRawUsage()).multiply(onehourcost);
        quota_usage = new QuotaUsageVO(usageRecord.getId(), usageRecord.getZoneId(), usageRecord.getAccountId(), usageRecord.getDomainId(), ruleType, ruleusage, 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 13 with QuotaUsageVO

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

the class QuotaManagerImpl method updateQuotaNetwork.

public QuotaUsageVO updateQuotaNetwork(UsageVO usageRecord, final int transferType) {
    QuotaUsageVO quota_usage = null;
    QuotaTariffVO tariff = _quotaTariffDao.findTariffPlanByUsageType(transferType, usageRecord.getEndDate());
    if (tariff != null && tariff.getCurrencyValue().compareTo(BigDecimal.ZERO) != 0) {
        BigDecimal onegbcost;
        BigDecimal rawusageingb;
        BigDecimal networkusage;
        onegbcost = tariff.getCurrencyValue();
        rawusageingb = new BigDecimal(usageRecord.getRawUsage()).divide(s_gb, 8, RoundingMode.HALF_EVEN);
        networkusage = rawusageingb.multiply(onegbcost);
        quota_usage = new QuotaUsageVO(usageRecord.getId(), usageRecord.getZoneId(), usageRecord.getAccountId(), usageRecord.getDomainId(), transferType, networkusage, 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 14 with QuotaUsageVO

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

the class QuotaStatementCmd method execute.

@Override
public void execute() {
    List<QuotaUsageVO> quotaUsage = _responseBuilder.getQuotaUsage(this);
    QuotaStatementResponse response = _responseBuilder.createQuotaStatementResponse(quotaUsage);
    response.setStartDate(startDate == null ? null : new Date(startDate.getTime()));
    response.setEndDate(endDate == null ? null : new Date(endDate.getTime()));
    response.setResponseName(getCommandName());
    setResponseObject(response);
}
Also used : QuotaStatementResponse(org.apache.cloudstack.api.response.QuotaStatementResponse) Date(java.util.Date) QuotaUsageVO(org.apache.cloudstack.quota.vo.QuotaUsageVO)

Example 15 with QuotaUsageVO

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

the class QuotaManagerImplTest method testAggregatePendingQuotaRecordsForAccount.

@Test
public void testAggregatePendingQuotaRecordsForAccount() {
    AccountVO accountVO = new AccountVO();
    accountVO.setId(2L);
    accountVO.setDomainId(1L);
    accountVO.setType(Account.ACCOUNT_TYPE_NORMAL);
    UsageVO usageVO = new UsageVO();
    usageVO.setQuotaCalculated(0);
    usageVO.setUsageType(UsageTypes.ALLOCATED_VM);
    List<UsageVO> usageVOList = new ArrayList<UsageVO>();
    usageVOList.add(usageVO);
    Pair<List<? extends UsageVO>, Integer> usageRecords = new Pair<List<? extends UsageVO>, Integer>(usageVOList, usageVOList.size());
    QuotaUsageVO quotaUsageVO = new QuotaUsageVO();
    quotaUsageVO.setAccountId(2L);
    Mockito.doReturn(quotaUsageVO).when(quotaManager).updateQuotaAllocatedVMUsage(Mockito.eq(usageVO), Mockito.any(BigDecimal.class));
    assertTrue(quotaManager.aggregatePendingQuotaRecordsForAccount(accountVO, new Pair<List<? extends UsageVO>, Integer>(null, 0)).size() == 0);
    assertTrue(quotaManager.aggregatePendingQuotaRecordsForAccount(accountVO, usageRecords).size() == 1);
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) QuotaUsageVO(org.apache.cloudstack.quota.vo.QuotaUsageVO) UsageVO(com.cloud.usage.UsageVO) QuotaAccountVO(org.apache.cloudstack.quota.vo.QuotaAccountVO) AccountVO(com.cloud.user.AccountVO) BigDecimal(java.math.BigDecimal) Pair(com.cloud.utils.Pair) QuotaUsageVO(org.apache.cloudstack.quota.vo.QuotaUsageVO) Test(org.junit.Test)

Aggregations

QuotaUsageVO (org.apache.cloudstack.quota.vo.QuotaUsageVO)16 BigDecimal (java.math.BigDecimal)11 ArrayList (java.util.ArrayList)8 QuotaTariffVO (org.apache.cloudstack.quota.vo.QuotaTariffVO)6 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 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 ServiceOfferingVO (org.apache.cloudstack.quota.vo.ServiceOfferingVO)1