use of com.cloud.usage.UsageBackupVO in project cloudstack by apache.
the class UsageBackupDaoImpl method getUsageRecords.
@Override
public List<UsageBackupVO> getUsageRecords(Long accountId, Date startDate, Date endDate) {
List<UsageBackupVO> usageRecords = new ArrayList<UsageBackupVO>();
TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
PreparedStatement pstmt;
try {
int i = 1;
pstmt = txn.prepareAutoCloseStatement(GET_USAGE_RECORDS_BY_ACCOUNT);
pstmt.setLong(i++, accountId);
pstmt.setString(i++, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), endDate));
pstmt.setString(i++, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), startDate));
pstmt.setString(i++, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), endDate));
pstmt.setString(i++, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), startDate));
pstmt.setString(i++, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), endDate));
pstmt.setString(i++, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), startDate));
pstmt.setString(i++, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), endDate));
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
// id, zone_id, account_id, domain_id, vm_id, disk_offering_id, size, created, processed
Long id = Long.valueOf(rs.getLong(1));
Long zoneId = Long.valueOf(rs.getLong(2));
Long acctId = Long.valueOf(rs.getLong(3));
Long domId = Long.valueOf(rs.getLong(4));
Long vmId = Long.valueOf(rs.getLong(5));
Long backupOfferingId = Long.valueOf(rs.getLong(6));
Long size = Long.valueOf(rs.getLong(7));
Long pSize = Long.valueOf(rs.getLong(8));
Date createdDate = null;
Date removedDate = null;
String createdTS = rs.getString(9);
String removedTS = rs.getString(10);
if (createdTS != null) {
createdDate = DateUtil.parseDateString(s_gmtTimeZone, createdTS);
}
if (removedTS != null) {
removedDate = DateUtil.parseDateString(s_gmtTimeZone, removedTS);
}
usageRecords.add(new UsageBackupVO(id, zoneId, acctId, domId, vmId, backupOfferingId, size, pSize, createdDate, removedDate));
}
} catch (Exception e) {
txn.rollback();
LOGGER.warn("Error getting VM backup usage records", e);
} finally {
txn.close();
}
return usageRecords;
}
use of com.cloud.usage.UsageBackupVO in project cloudstack by apache.
the class BackupUsageParser method parse.
public static boolean parse(AccountVO account, Date startDate, Date endDate) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Parsing all VM Backup usage events for account: " + account.getId());
}
if ((endDate == null) || endDate.after(new Date())) {
endDate = new Date();
}
final List<UsageBackupVO> usageBackups = s_usageBackupDao.getUsageRecords(account.getId(), startDate, endDate);
if (usageBackups == null || usageBackups.isEmpty()) {
LOGGER.debug("No VM Backup usage for this period");
return true;
}
for (final UsageBackupVO usageBackup : usageBackups) {
final Long vmId = usageBackup.getVmId();
final Long zoneId = usageBackup.getZoneId();
final Long offeringId = usageBackup.getBackupOfferingId();
Date createdDate = usageBackup.getCreated();
Date removedDate = usageBackup.getRemoved();
if (createdDate.before(startDate)) {
createdDate = startDate;
}
if (removedDate == null || removedDate.after(endDate)) {
removedDate = endDate;
}
final long duration = (removedDate.getTime() - createdDate.getTime()) + 1;
final float usage = duration / 1000f / 60f / 60f;
DecimalFormat dFormat = new DecimalFormat("#.######");
String usageDisplay = dFormat.format(usage);
final Double rawUsage = (double) usageBackup.getSize();
final String description = String.format("Backup usage VM ID: %d, backup offering: %d", vmId, offeringId);
final UsageVO usageRecord = new UsageVO(zoneId, account.getAccountId(), account.getDomainId(), description, usageDisplay + " Hrs", UsageTypes.BACKUP, new Double(usage), vmId, null, offeringId, null, vmId, usageBackup.getSize(), usageBackup.getProtectedSize(), startDate, endDate);
s_usageDao.persist(usageRecord);
}
return true;
}
use of com.cloud.usage.UsageBackupVO in project cloudstack by apache.
the class UsageBackupDaoImpl method updateMetrics.
@Override
public void updateMetrics(final Long vmId, final Long size, final Long virtualSize) {
try (TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB)) {
SearchCriteria<UsageBackupVO> sc = this.createSearchCriteria();
sc.addAnd("vmId", SearchCriteria.Op.EQ, vmId);
UsageBackupVO vo = findOneBy(sc);
if (vo != null) {
vo.setSize(size);
vo.setProtectedSize(virtualSize);
update(vo.getId(), vo);
}
} catch (final Exception e) {
LOGGER.error("Error updating backup metrics: " + e.getMessage(), e);
}
}
Aggregations