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);
}
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);
}
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;
}
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());
}
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());
}
}
Aggregations