Search in sources :

Example 6 with UsageVO

use of com.cloud.usage.UsageVO in project CloudStack-archive by CloudStack-extras.

the class VMInstanceUsageParser method createUsageRecord.

private static void createUsageRecord(int type, long runningTime, Date startDate, Date endDate, AccountVO account, long vmId, String vmName, long zoneId, long serviceOfferingId, long templateId, String hypervisorType) {
    // 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 VM usage record for vm: " + vmName + ", type: " + type + ", usage: " + usageDisplay + ", startDate: " + startDate + ", endDate: " + endDate + ", for account: " + account.getId());
    }
    // Create the usage record
    String usageDesc = vmName;
    if (type == UsageTypes.ALLOCATED_VM) {
        usageDesc += " allocated";
    } else {
        usageDesc += " running time";
    }
    usageDesc += " (ServiceOffering: " + serviceOfferingId + ") (Template: " + templateId + ")";
    UsageVO usageRecord = new UsageVO(Long.valueOf(zoneId), account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", type, new Double(usage), Long.valueOf(vmId), vmName, Long.valueOf(serviceOfferingId), Long.valueOf(templateId), Long.valueOf(vmId), startDate, endDate, hypervisorType);
    m_usageDao.persist(usageRecord);
}
Also used : DecimalFormat(java.text.DecimalFormat) UsageVO(com.cloud.usage.UsageVO)

Example 7 with UsageVO

use of com.cloud.usage.UsageVO in project CloudStack-archive by CloudStack-extras.

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);
    m_usageDao.persist(usageRecord);
}
Also used : DecimalFormat(java.text.DecimalFormat) UsageVO(com.cloud.usage.UsageVO)

Example 8 with UsageVO

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

the class QuotaManagerImpl method calculateQuotaUsage.

@Override
public boolean calculateQuotaUsage() {
    List<AccountVO> accounts = _accountDao.listAll();
    for (AccountVO account : accounts) {
        Pair<List<? extends UsageVO>, Integer> usageRecords = _usageDao.getUsageRecordsPendingQuotaAggregation(account.getAccountId(), account.getDomainId());
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Usage entries size = " + usageRecords.second().intValue() + ", accId" + account.getAccountId() + ", domId" + account.getDomainId());
        }
        List<QuotaUsageVO> quotaListForAccount = aggregatePendingQuotaRecordsForAccount(account, usageRecords);
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Quota entries size = " + quotaListForAccount.size() + ", accId" + account.getAccountId() + ", domId" + account.getDomainId());
        }
        processQuotaBalanceForAccount(account, quotaListForAccount);
    }
    return true;
}
Also used : 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) QuotaUsageVO(org.apache.cloudstack.quota.vo.QuotaUsageVO)

Example 9 with UsageVO

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

the class QuotaManagerImplTest method testCalculateQuotaUsage.

@Test
public void testCalculateQuotaUsage() {
    AccountVO accountVO = new AccountVO();
    accountVO.setId(2L);
    accountVO.setDomainId(1L);
    accountVO.setType(Account.ACCOUNT_TYPE_NORMAL);
    List<AccountVO> accountVOList = new ArrayList<>();
    accountVOList.add(accountVO);
    Mockito.when(accountDao.listAll()).thenReturn(accountVOList);
    UsageVO usageVO = new UsageVO();
    usageVO.setQuotaCalculated(0);
    List<UsageVO> usageVOList = new ArrayList<UsageVO>();
    usageVOList.add(usageVO);
    Pair<List<? extends UsageVO>, Integer> usageRecords = new Pair<List<? extends UsageVO>, Integer>(usageVOList, usageVOList.size());
    Mockito.when(usageDao.getUsageRecordsPendingQuotaAggregation(Mockito.anyLong(), Mockito.anyLong())).thenReturn(usageRecords);
    QuotaUsageVO quotaUsageVO = new QuotaUsageVO();
    quotaUsageVO.setAccountId(2L);
    List<QuotaUsageVO> quotaListForAccount = new ArrayList<>();
    quotaListForAccount.add(quotaUsageVO);
    Mockito.doReturn(quotaListForAccount).when(quotaManager).aggregatePendingQuotaRecordsForAccount(Mockito.eq(accountVO), Mockito.eq(usageRecords));
    Mockito.doNothing().when(quotaManager).processQuotaBalanceForAccount(Mockito.eq(accountVO), Mockito.eq(quotaListForAccount));
    assertTrue(quotaManager.calculateQuotaUsage());
}
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) Pair(com.cloud.utils.Pair) QuotaUsageVO(org.apache.cloudstack.quota.vo.QuotaUsageVO) Test(org.junit.Test)

Example 10 with UsageVO

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

the class UsageDaoImpl method saveUsageRecords.

@Override
public void saveUsageRecords(List<UsageVO> usageRecords) {
    TransactionLegacy txn = TransactionLegacy.currentTxn();
    try {
        txn.start();
        String sql = INSERT_USAGE_RECORDS;
        PreparedStatement pstmt = null;
        // in reality I just want CLOUD_USAGE dataSource connection
        pstmt = txn.prepareAutoCloseStatement(sql);
        for (UsageVO usageRecord : usageRecords) {
            pstmt.setLong(1, usageRecord.getZoneId());
            pstmt.setLong(2, usageRecord.getAccountId());
            pstmt.setLong(3, usageRecord.getDomainId());
            pstmt.setString(4, usageRecord.getDescription());
            pstmt.setString(5, usageRecord.getUsageDisplay());
            pstmt.setInt(6, usageRecord.getUsageType());
            pstmt.setDouble(7, usageRecord.getRawUsage());
            if (usageRecord.getVmInstanceId() != null) {
                pstmt.setLong(8, usageRecord.getVmInstanceId());
            } else {
                pstmt.setNull(8, Types.BIGINT);
            }
            pstmt.setString(9, usageRecord.getVmName());
            if (usageRecord.getOfferingId() != null) {
                pstmt.setLong(10, usageRecord.getOfferingId());
            } else {
                pstmt.setNull(10, Types.BIGINT);
            }
            if (usageRecord.getTemplateId() != null) {
                pstmt.setLong(11, usageRecord.getTemplateId());
            } else {
                pstmt.setNull(11, Types.BIGINT);
            }
            if (usageRecord.getUsageId() != null) {
                pstmt.setLong(12, usageRecord.getUsageId());
            } else {
                pstmt.setNull(12, Types.BIGINT);
            }
            pstmt.setString(13, usageRecord.getType());
            if (usageRecord.getSize() != null) {
                pstmt.setLong(14, usageRecord.getSize());
            } else {
                pstmt.setNull(14, Types.BIGINT);
            }
            if (usageRecord.getNetworkId() != null) {
                pstmt.setLong(15, usageRecord.getNetworkId());
            } else {
                pstmt.setNull(15, Types.BIGINT);
            }
            pstmt.setString(16, DateUtil.getDateDisplayString(s_gmtTimeZone, usageRecord.getStartDate()));
            pstmt.setString(17, DateUtil.getDateDisplayString(s_gmtTimeZone, usageRecord.getEndDate()));
            if (usageRecord.getVirtualSize() != null) {
                pstmt.setLong(18, usageRecord.getVirtualSize());
            } else {
                pstmt.setNull(18, Types.BIGINT);
            }
            pstmt.addBatch();
        }
        pstmt.executeBatch();
        txn.commit();
    } catch (Exception ex) {
        txn.rollback();
        s_logger.error("error saving usage records to cloud_usage db", ex);
        throw new CloudRuntimeException(ex.getMessage());
    }
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) PreparedStatement(java.sql.PreparedStatement) UsageVO(com.cloud.usage.UsageVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

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