use of com.cloud.usage.UsageStorageVO in project cloudstack by apache.
the class UsageStorageDaoImpl method getUsageRecords.
@Override
public List<UsageStorageVO> getUsageRecords(Long accountId, Long domainId, Date startDate, Date endDate, boolean limit, int page) {
List<UsageStorageVO> usageRecords = new ArrayList<UsageStorageVO>();
Long param1 = null;
String sql = null;
if (accountId != null) {
sql = GET_USAGE_RECORDS_BY_ACCOUNT;
param1 = accountId;
} else if (domainId != null) {
sql = GET_USAGE_RECORDS_BY_DOMAIN;
param1 = domainId;
} else {
sql = GET_ALL_USAGE_RECORDS;
}
if (limit) {
int startIndex = 0;
if (page > 0) {
startIndex = 500 * (page - 1);
}
sql += " LIMIT " + startIndex + ",500";
}
TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
int i = 1;
try (PreparedStatement pstmt = txn.prepareStatement(sql)) {
if (param1 != null) {
pstmt.setLong(i++, param1);
}
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));
try (ResultSet rs = pstmt.executeQuery()) {
while (rs.next()) {
//id, zone_id, account_id, domain_id, storage_type, size, created, deleted
Long id = Long.valueOf(rs.getLong(1));
Long zoneId = Long.valueOf(rs.getLong(2));
Long acctId = Long.valueOf(rs.getLong(3));
Long dId = Long.valueOf(rs.getLong(4));
Integer type = Integer.valueOf(rs.getInt(5));
Long sourceId = Long.valueOf(rs.getLong(6));
Long size = Long.valueOf(rs.getLong(7));
Long virtualSize = Long.valueOf(rs.getLong(10));
Date createdDate = null;
Date deletedDate = null;
String createdTS = rs.getString(8);
String deletedTS = rs.getString(9);
if (createdTS != null) {
createdDate = DateUtil.parseDateString(s_gmtTimeZone, createdTS);
}
if (deletedTS != null) {
deletedDate = DateUtil.parseDateString(s_gmtTimeZone, deletedTS);
}
usageRecords.add(new UsageStorageVO(id, zoneId, acctId, dId, type, sourceId, size, virtualSize, createdDate, deletedDate));
}
} catch (SQLException e) {
throw new CloudException("getUsageRecords:" + e.getMessage(), e);
}
} catch (Exception e) {
txn.rollback();
s_logger.error("getUsageRecords:Exception:" + e.getMessage(), e);
} finally {
txn.close();
}
return usageRecords;
}
use of com.cloud.usage.UsageStorageVO in project cloudstack by apache.
the class StorageUsageParser method parse.
public static boolean parse(AccountVO account, Date startDate, Date endDate) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Parsing all Storage usage events for account: " + account.getId());
}
if ((endDate == null) || endDate.after(new Date())) {
endDate = new Date();
}
// - query usage_volume table with the following criteria:
// - look for an entry for accountId with start date in the given range
// - look for an entry for accountId with end date in the given range
// - look for an entry for accountId with end date null (currently running vm or owned IP)
// - look for an entry for accountId with start date before given range *and* end date after given range
List<UsageStorageVO> usageUsageStorages = s_usageStorageDao.getUsageRecords(account.getId(), account.getDomainId(), startDate, endDate, false, 0);
if (usageUsageStorages.isEmpty()) {
s_logger.debug("No Storage usage events for this period");
return true;
}
// This map has both the running time *and* the usage amount.
Map<String, Pair<Long, Long>> usageMap = new HashMap<String, Pair<Long, Long>>();
Map<String, StorageInfo> storageMap = new HashMap<String, StorageInfo>();
// loop through all the usage volumes, create a usage record for each
for (UsageStorageVO usageStorage : usageUsageStorages) {
long storageId = usageStorage.getId();
int storage_type = usageStorage.getStorageType();
long size = usageStorage.getSize();
Long virtualSize = usageStorage.getVirtualSize();
long zoneId = usageStorage.getZoneId();
Long sourceId = usageStorage.getSourceId();
String key = "" + storageId + "Z" + zoneId + "T" + storage_type;
// store the info in the storage map
storageMap.put(key, new StorageInfo(zoneId, storageId, storage_type, sourceId, size, virtualSize));
Date storageCreateDate = usageStorage.getCreated();
Date storageDeleteDate = usageStorage.getDeleted();
if ((storageDeleteDate == null) || storageDeleteDate.after(endDate)) {
storageDeleteDate = endDate;
}
// clip the start date to the beginning of our aggregation range if the vm has been running for a while
if (storageCreateDate.before(startDate)) {
storageCreateDate = startDate;
}
if (storageCreateDate.after(endDate)) {
//Ignore records created after endDate
continue;
}
// make sure this is an inclusive check for milliseconds (i.e. use n - m + 1 to find total number of millis to charge)
long currentDuration = (storageDeleteDate.getTime() - storageCreateDate.getTime()) + 1;
updateStorageUsageData(usageMap, key, usageStorage.getId(), currentDuration);
}
for (String storageIdKey : usageMap.keySet()) {
Pair<Long, Long> storagetimeInfo = usageMap.get(storageIdKey);
long useTime = storagetimeInfo.second().longValue();
// Only create a usage record if we have a runningTime of bigger than zero.
if (useTime > 0L) {
StorageInfo info = storageMap.get(storageIdKey);
createUsageRecord(info.getZoneId(), info.getStorageType(), useTime, startDate, endDate, account, info.getStorageId(), info.getSourceId(), info.getSize(), info.getVirtualSize());
}
}
return true;
}
use of com.cloud.usage.UsageStorageVO in project CloudStack-archive by CloudStack-extras.
the class StorageUsageParser method parse.
public static boolean parse(AccountVO account, Date startDate, Date endDate) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Parsing all Storage usage events for account: " + account.getId());
}
if ((endDate == null) || endDate.after(new Date())) {
endDate = new Date();
}
// - query usage_volume table with the following criteria:
// - look for an entry for accountId with start date in the given range
// - look for an entry for accountId with end date in the given range
// - look for an entry for accountId with end date null (currently running vm or owned IP)
// - look for an entry for accountId with start date before given range *and* end date after given range
List<UsageStorageVO> usageUsageStorages = m_usageStorageDao.getUsageRecords(account.getId(), account.getDomainId(), startDate, endDate, false, 0);
if (usageUsageStorages.isEmpty()) {
s_logger.debug("No Storage usage events for this period");
return true;
}
// This map has both the running time *and* the usage amount.
Map<String, Pair<Long, Long>> usageMap = new HashMap<String, Pair<Long, Long>>();
Map<String, StorageInfo> storageMap = new HashMap<String, StorageInfo>();
// loop through all the usage volumes, create a usage record for each
for (UsageStorageVO usageStorage : usageUsageStorages) {
long storageId = usageStorage.getId();
int storage_type = usageStorage.getStorageType();
long size = usageStorage.getSize();
long zoneId = usageStorage.getZoneId();
Long sourceId = usageStorage.getSourceId();
String key = "" + storageId + "Z" + zoneId + "T" + storage_type;
// store the info in the storage map
storageMap.put(key, new StorageInfo(zoneId, storageId, storage_type, sourceId, size));
Date storageCreateDate = usageStorage.getCreated();
Date storageDeleteDate = usageStorage.getDeleted();
if ((storageDeleteDate == null) || storageDeleteDate.after(endDate)) {
storageDeleteDate = endDate;
}
// clip the start date to the beginning of our aggregation range if the vm has been running for a while
if (storageCreateDate.before(startDate)) {
storageCreateDate = startDate;
}
// make sure this is an inclusive check for milliseconds (i.e. use n - m + 1 to find total number of millis to charge)
long currentDuration = (storageDeleteDate.getTime() - storageCreateDate.getTime()) + 1;
updateStorageUsageData(usageMap, key, usageStorage.getId(), currentDuration);
}
for (String storageIdKey : usageMap.keySet()) {
Pair<Long, Long> storagetimeInfo = usageMap.get(storageIdKey);
long useTime = storagetimeInfo.second().longValue();
// Only create a usage record if we have a runningTime of bigger than zero.
if (useTime > 0L) {
StorageInfo info = storageMap.get(storageIdKey);
createUsageRecord(info.getZoneId(), info.getStorageType(), useTime, startDate, endDate, account, info.getStorageId(), info.getSourceId(), info.getSize());
}
}
return true;
}
Aggregations