Search in sources :

Example 26 with UsageVO

use of com.cloud.usage.UsageVO in project cloudstack by apache.

the class VPNUserUsageParser method createUsageRecord.

private static void createUsageRecord(int type, long runningTime, Date startDate, Date endDate, AccountVO account, long userId, String userName, long zoneId) {
    // Our smallest increment is hourly for now
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Total running time " + runningTime + "ms");
    }
    float usage = runningTime / 1000f / 60f / 60f;
    DecimalFormat dFormat = new DecimalFormat("#.######");
    String usageDisplay = dFormat.format(usage);
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Creating VPN user:" + userId + " usage record, usage: " + usageDisplay + ", startDate: " + startDate + ", endDate: " + endDate + ", for account: " + account.getId());
    }
    // Create the usage record
    String usageDesc = "VPN User: " + userName + ", Id: " + userId + " usage time";
    UsageVO usageRecord = new UsageVO(zoneId, account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", type, new Double(usage), null, null, null, null, userId, null, startDate, endDate);
    s_usageDao.persist(usageRecord);
}
Also used : DecimalFormat(java.text.DecimalFormat) UsageVO(com.cloud.usage.UsageVO)

Example 27 with UsageVO

use of com.cloud.usage.UsageVO in project cloudstack by apache.

the class VolumeUsageParser method createUsageRecord.

private static void createUsageRecord(int type, long runningTime, Date startDate, Date endDate, AccountVO account, long volId, long zoneId, Long doId, Long templateId, long size) {
    // Our smallest increment is hourly for now
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Total running time " + runningTime + "ms");
    }
    float usage = runningTime / 1000f / 60f / 60f;
    DecimalFormat dFormat = new DecimalFormat("#.######");
    String usageDisplay = dFormat.format(usage);
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Creating Volume usage record for vol: " + volId + ", usage: " + usageDisplay + ", startDate: " + startDate + ", endDate: " + endDate + ", for account: " + account.getId());
    }
    // Create the usage record
    String usageDesc = "Volume Id: " + volId + " usage time";
    if (templateId != null) {
        usageDesc += " (Template: " + templateId + ")";
    } else if (doId != null) {
        usageDesc += " (DiskOffering: " + doId + ")";
    }
    UsageVO usageRecord = new UsageVO(zoneId, account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", type, new Double(usage), null, null, doId, templateId, volId, size, startDate, endDate);
    s_usageDao.persist(usageRecord);
}
Also used : DecimalFormat(java.text.DecimalFormat) UsageVO(com.cloud.usage.UsageVO)

Example 28 with UsageVO

use of com.cloud.usage.UsageVO in project cloudstack by apache.

the class ApiResponseHelperTest method testUsageRecordResponse.

@Test
public void testUsageRecordResponse() {
    // Creating the usageVO object to be passed to the createUsageResponse.
    Long zoneId = null;
    Long accountId = 1L;
    Long domainId = 1L;
    String Description = "Test Object";
    String usageDisplay = " ";
    int usageType = -1;
    Double rawUsage = null;
    Long vmId = null;
    String vmName = " ";
    Long offeringId = null;
    Long templateId = null;
    Long usageId = null;
    Date startDate = null;
    Date endDate = null;
    String type = " ";
    UsageVO usage = new UsageVO(zoneId, accountId, domainId, Description, usageDisplay, usageType, rawUsage, vmId, vmName, offeringId, templateId, usageId, startDate, endDate, type);
    DomainVO domain = new DomainVO();
    domain.setName("DomainName");
    AccountVO account = new AccountVO();
    PowerMockito.mockStatic(ApiDBUtils.class);
    when(ApiDBUtils.findAccountById(anyLong())).thenReturn(account);
    when(ApiDBUtils.findDomainById(anyLong())).thenReturn(domain);
    UsageRecordResponse MockResponse = helper.createUsageResponse(usage);
    assertEquals("DomainName", MockResponse.getDomainName());
}
Also used : UsageRecordResponse(org.apache.cloudstack.api.response.UsageRecordResponse) DomainVO(com.cloud.domain.DomainVO) Matchers.anyLong(org.mockito.Matchers.anyLong) UsageVO(com.cloud.usage.UsageVO) AccountVO(com.cloud.user.AccountVO) Date(java.util.Date) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 29 with UsageVO

use of com.cloud.usage.UsageVO in project cloudstack by apache.

the class QuotaManagerImpl method aggregatePendingQuotaRecordsForAccount.

public List<QuotaUsageVO> aggregatePendingQuotaRecordsForAccount(final AccountVO account, final Pair<List<? extends UsageVO>, Integer> usageRecords) {
    List<QuotaUsageVO> quotaListForAccount = new ArrayList<>();
    if (usageRecords == null || usageRecords.first() == null || usageRecords.first().isEmpty()) {
        return quotaListForAccount;
    }
    s_logger.info("Getting pending quota records for account=" + account.getAccountName());
    for (UsageVO usageRecord : usageRecords.first()) {
        switch(usageRecord.getUsageType()) {
            case QuotaTypes.RUNNING_VM:
                List<QuotaUsageVO> lq = updateQuotaRunningVMUsage(usageRecord);
                if (!lq.isEmpty()) {
                    quotaListForAccount.addAll(lq);
                }
                break;
            case QuotaTypes.ALLOCATED_VM:
                QuotaUsageVO qu = updateQuotaAllocatedVMUsage(usageRecord);
                if (qu != null) {
                    quotaListForAccount.add(qu);
                }
                break;
            case QuotaTypes.SNAPSHOT:
            case QuotaTypes.TEMPLATE:
            case QuotaTypes.ISO:
            case QuotaTypes.VOLUME:
            case QuotaTypes.VM_SNAPSHOT:
            case QuotaTypes.BACKUP:
                qu = updateQuotaDiskUsage(usageRecord, usageRecord.getUsageType());
                if (qu != null) {
                    quotaListForAccount.add(qu);
                }
                break;
            case QuotaTypes.LOAD_BALANCER_POLICY:
            case QuotaTypes.PORT_FORWARDING_RULE:
            case QuotaTypes.IP_ADDRESS:
            case QuotaTypes.NETWORK_OFFERING:
            case QuotaTypes.SECURITY_GROUP:
            case QuotaTypes.VPN_USERS:
                qu = updateQuotaRaw(usageRecord, usageRecord.getUsageType());
                if (qu != null) {
                    quotaListForAccount.add(qu);
                }
                break;
            case QuotaTypes.NETWORK_BYTES_RECEIVED:
            case QuotaTypes.NETWORK_BYTES_SENT:
                qu = updateQuotaNetwork(usageRecord, usageRecord.getUsageType());
                if (qu != null) {
                    quotaListForAccount.add(qu);
                }
                break;
            case QuotaTypes.VM_DISK_IO_READ:
            case QuotaTypes.VM_DISK_IO_WRITE:
            case QuotaTypes.VM_DISK_BYTES_READ:
            case QuotaTypes.VM_DISK_BYTES_WRITE:
            default:
                break;
        }
    }
    return quotaListForAccount;
}
Also used : ArrayList(java.util.ArrayList) QuotaUsageVO(org.apache.cloudstack.quota.vo.QuotaUsageVO) UsageVO(com.cloud.usage.UsageVO) QuotaUsageVO(org.apache.cloudstack.quota.vo.QuotaUsageVO)

Example 30 with UsageVO

use of com.cloud.usage.UsageVO in project cloudstack by apache.

the class QuotaManagerImplTest method testUpdateQuotaRecords.

@Test
public void testUpdateQuotaRecords() {
    UsageVO usageVO = new UsageVO();
    usageVO.setId(100L);
    usageVO.setQuotaCalculated(0);
    usageVO.setUsageType(UsageTypes.NETWORK_BYTES_SENT);
    usageVO.setRawUsage(9000000000.0);
    usageVO.setSize(1010101010L);
    QuotaTariffVO tariffVO = new QuotaTariffVO();
    tariffVO.setCurrencyValue(new BigDecimal(1));
    Mockito.when(quotaTariffDao.findTariffPlanByUsageType(nullable(Integer.class), nullable(Date.class))).thenReturn(tariffVO);
    QuotaUsageVO qu = quotaManager.updateQuotaNetwork(usageVO, UsageTypes.NETWORK_BYTES_SENT);
    assertTrue(qu.getQuotaUsed().compareTo(BigDecimal.ZERO) > 0);
    qu = quotaManager.updateQuotaAllocatedVMUsage(usageVO);
    assertTrue(qu.getQuotaUsed().compareTo(BigDecimal.ZERO) > 0);
    qu = quotaManager.updateQuotaDiskUsage(usageVO, UsageTypes.VOLUME);
    assertTrue(qu.getQuotaUsed().compareTo(BigDecimal.ZERO) > 0);
    qu = quotaManager.updateQuotaRaw(usageVO, UsageTypes.VPN_USERS);
    assertTrue(qu.getQuotaUsed().compareTo(BigDecimal.ZERO) > 0);
    Mockito.verify(quotaUsageDao, Mockito.times(4)).persistQuotaUsage(Mockito.any(QuotaUsageVO.class));
    Mockito.verify(usageDao, Mockito.times(4)).persistUsage(Mockito.any(UsageVO.class));
}
Also used : QuotaTariffVO(org.apache.cloudstack.quota.vo.QuotaTariffVO) QuotaUsageVO(org.apache.cloudstack.quota.vo.QuotaUsageVO) UsageVO(com.cloud.usage.UsageVO) BigDecimal(java.math.BigDecimal) Date(java.util.Date) QuotaUsageVO(org.apache.cloudstack.quota.vo.QuotaUsageVO) Test(org.junit.Test)

Aggregations

UsageVO (com.cloud.usage.UsageVO)31 DecimalFormat (java.text.DecimalFormat)21 ArrayList (java.util.ArrayList)6 Date (java.util.Date)6 QuotaUsageVO (org.apache.cloudstack.quota.vo.QuotaUsageVO)5 AccountVO (com.cloud.user.AccountVO)4 Test (org.junit.Test)4 HashMap (java.util.HashMap)3 List (java.util.List)3 QuotaAccountVO (org.apache.cloudstack.quota.vo.QuotaAccountVO)3 UsageNetworkVO (com.cloud.usage.UsageNetworkVO)2 Pair (com.cloud.utils.Pair)2 DomainVO (com.cloud.domain.DomainVO)1 UsageBackupVO (com.cloud.usage.UsageBackupVO)1 UsageVmDiskVO (com.cloud.usage.UsageVmDiskVO)1 TransactionLegacy (com.cloud.utils.db.TransactionLegacy)1 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 BigDecimal (java.math.BigDecimal)1 PreparedStatement (java.sql.PreparedStatement)1 UsageRecordResponse (org.apache.cloudstack.api.response.UsageRecordResponse)1