use of com.cloud.usage.UsageNetworkVO in project CloudStack-archive by CloudStack-extras.
the class NetworkUsageParser method parse.
public static boolean parse(AccountVO account, Date startDate, Date endDate) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Parsing all Network usage events for account: " + account.getId());
}
if ((endDate == null) || endDate.after(new Date())) {
endDate = new Date();
}
// - query usage_network table for all entries for userId with
// event_date in the given range
SearchCriteria<UsageNetworkVO> sc = m_usageNetworkDao.createSearchCriteria();
sc.addAnd("accountId", SearchCriteria.Op.EQ, account.getId());
sc.addAnd("eventTimeMillis", SearchCriteria.Op.BETWEEN, startDate.getTime(), endDate.getTime());
List<UsageNetworkVO> usageNetworkVOs = m_usageNetworkDao.search(sc, null);
Map<String, NetworkInfo> networkUsageByZone = new HashMap<String, NetworkInfo>();
// Calculate the total bytes since last parsing
for (UsageNetworkVO usageNetwork : usageNetworkVOs) {
long zoneId = usageNetwork.getZoneId();
String key = "" + zoneId;
if (usageNetwork.getHostId() != 0) {
key += "-Host" + usageNetwork.getHostId();
}
NetworkInfo networkInfo = networkUsageByZone.get(key);
long bytesSent = usageNetwork.getBytesSent();
long bytesReceived = usageNetwork.getBytesReceived();
if (networkInfo != null) {
bytesSent += networkInfo.getBytesSent();
bytesReceived += networkInfo.getBytesRcvd();
}
networkUsageByZone.put(key, new NetworkInfo(zoneId, usageNetwork.getHostId(), usageNetwork.getHostType(), usageNetwork.getNetworkId(), bytesSent, bytesReceived));
}
for (String key : networkUsageByZone.keySet()) {
NetworkInfo networkInfo = networkUsageByZone.get(key);
long totalBytesSent = networkInfo.getBytesSent();
long totalBytesReceived = networkInfo.getBytesRcvd();
if ((totalBytesSent > 0L) || (totalBytesReceived > 0L)) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Creating usage record, total bytes sent:" + totalBytesSent + ", total bytes received: " + totalBytesReceived + " for account: " + account.getId() + " in availability zone " + networkInfo.getZoneId() + ", start: " + startDate + ", end: " + endDate);
}
Long hostId = null;
// Create the usage record for bytes sent
String usageDesc = "network bytes sent";
if (networkInfo.getHostId() != 0) {
hostId = networkInfo.getHostId();
usageDesc += " for Host: " + networkInfo.getHostId();
}
UsageVO usageRecord = new UsageVO(networkInfo.getZoneId(), account.getId(), account.getDomainId(), usageDesc, totalBytesSent + " bytes sent", UsageTypes.NETWORK_BYTES_SENT, new Double(totalBytesSent), hostId, networkInfo.getHostType(), networkInfo.getNetworkId(), startDate, endDate);
m_usageDao.persist(usageRecord);
// Create the usage record for bytes received
usageDesc = "network bytes received";
if (networkInfo.getHostId() != 0) {
usageDesc += " for Host: " + networkInfo.getHostId();
}
usageRecord = new UsageVO(networkInfo.getZoneId(), account.getId(), account.getDomainId(), usageDesc, totalBytesReceived + " bytes received", UsageTypes.NETWORK_BYTES_RECEIVED, new Double(totalBytesReceived), hostId, networkInfo.getHostType(), networkInfo.getNetworkId(), startDate, endDate);
m_usageDao.persist(usageRecord);
} else {
// Don't charge anything if there were zero bytes processed
if (s_logger.isDebugEnabled()) {
s_logger.debug("No usage record (0 bytes used) generated for account: " + account.getId());
}
}
}
return true;
}
use of com.cloud.usage.UsageNetworkVO in project cloudstack by apache.
the class UsageNetworkDaoImpl method saveUsageNetworks.
@Override
public void saveUsageNetworks(List<UsageNetworkVO> usageNetworks) {
TransactionLegacy txn = TransactionLegacy.currentTxn();
try {
txn.start();
String sql = INSERT_USAGE_NETWORK;
PreparedStatement pstmt = null;
// in reality I just want CLOUD_USAGE dataSource connection
pstmt = txn.prepareAutoCloseStatement(sql);
for (UsageNetworkVO usageNetwork : usageNetworks) {
pstmt.setLong(1, usageNetwork.getAccountId());
pstmt.setLong(2, usageNetwork.getZoneId());
pstmt.setLong(3, usageNetwork.getHostId());
pstmt.setString(4, usageNetwork.getHostType());
pstmt.setLong(5, usageNetwork.getNetworkId());
pstmt.setLong(6, usageNetwork.getBytesSent());
pstmt.setLong(7, usageNetwork.getBytesReceived());
pstmt.setLong(8, usageNetwork.getAggBytesReceived());
pstmt.setLong(9, usageNetwork.getAggBytesSent());
pstmt.setLong(10, usageNetwork.getEventTimeMillis());
pstmt.addBatch();
}
pstmt.executeBatch();
txn.commit();
} catch (Exception ex) {
txn.rollback();
s_logger.error("error saving usage_network to cloud_usage db", ex);
throw new CloudRuntimeException(ex.getMessage());
}
}
use of com.cloud.usage.UsageNetworkVO in project cloudstack by apache.
the class NetworkUsageParser method parse.
public static boolean parse(AccountVO account, Date startDate, Date endDate) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Parsing all Network usage events for account: " + account.getId());
}
if ((endDate == null) || endDate.after(new Date())) {
endDate = new Date();
}
// - query usage_network table for all entries for userId with
// event_date in the given range
SearchCriteria<UsageNetworkVO> sc = s_usageNetworkDao.createSearchCriteria();
sc.addAnd("accountId", SearchCriteria.Op.EQ, account.getId());
sc.addAnd("eventTimeMillis", SearchCriteria.Op.BETWEEN, startDate.getTime(), endDate.getTime());
List<UsageNetworkVO> usageNetworkVOs = s_usageNetworkDao.search(sc, null);
Map<String, NetworkInfo> networkUsageByZone = new HashMap<String, NetworkInfo>();
// Calculate the total bytes since last parsing
for (UsageNetworkVO usageNetwork : usageNetworkVOs) {
long zoneId = usageNetwork.getZoneId();
String key = "" + zoneId;
if (usageNetwork.getHostId() != 0) {
key += "-Host" + usageNetwork.getHostId() + "-Network-" + usageNetwork.getNetworkId();
}
NetworkInfo networkInfo = networkUsageByZone.get(key);
long bytesSent = usageNetwork.getBytesSent();
long bytesReceived = usageNetwork.getBytesReceived();
if (networkInfo != null) {
bytesSent += networkInfo.getBytesSent();
bytesReceived += networkInfo.getBytesRcvd();
}
networkUsageByZone.put(key, new NetworkInfo(zoneId, usageNetwork.getHostId(), usageNetwork.getHostType(), usageNetwork.getNetworkId(), bytesSent, bytesReceived));
}
List<UsageVO> usageRecords = new ArrayList<UsageVO>();
for (String key : networkUsageByZone.keySet()) {
NetworkInfo networkInfo = networkUsageByZone.get(key);
long totalBytesSent = networkInfo.getBytesSent();
long totalBytesReceived = networkInfo.getBytesRcvd();
if ((totalBytesSent > 0L) || (totalBytesReceived > 0L)) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Creating usage record, total bytes sent: " + toHumanReadableSize(totalBytesSent) + ", total bytes received: " + toHumanReadableSize(totalBytesReceived) + " for account: " + account.getId() + " in availability zone " + networkInfo.getZoneId() + ", start: " + startDate + ", end: " + endDate);
}
Long hostId = null;
// Create the usage record for bytes sent
String usageDesc = "network bytes sent";
if (networkInfo.getHostId() != 0) {
hostId = networkInfo.getHostId();
usageDesc += " for Host: " + networkInfo.getHostId();
}
UsageVO usageRecord = new UsageVO(networkInfo.getZoneId(), account.getId(), account.getDomainId(), usageDesc, totalBytesSent + " bytes sent", UsageTypes.NETWORK_BYTES_SENT, new Double(totalBytesSent), hostId, networkInfo.getHostType(), networkInfo.getNetworkId(), startDate, endDate);
usageRecords.add(usageRecord);
// Create the usage record for bytes received
usageDesc = "network bytes received";
if (networkInfo.getHostId() != 0) {
usageDesc += " for Host: " + networkInfo.getHostId();
}
usageRecord = new UsageVO(networkInfo.getZoneId(), account.getId(), account.getDomainId(), usageDesc, totalBytesReceived + " bytes received", UsageTypes.NETWORK_BYTES_RECEIVED, new Double(totalBytesReceived), hostId, networkInfo.getHostType(), networkInfo.getNetworkId(), startDate, endDate);
usageRecords.add(usageRecord);
} else {
// Don't charge anything if there were zero bytes processed
if (s_logger.isDebugEnabled()) {
s_logger.debug("No usage record (0 bytes used) generated for account: " + account.getId());
}
}
}
s_usageDao.saveUsageRecords(usageRecords);
return true;
}
use of com.cloud.usage.UsageNetworkVO in project cloudstack by apache.
the class UsageNetworkDaoImpl method getRecentNetworkStats.
@Override
public Map<String, UsageNetworkVO> getRecentNetworkStats() {
TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
String sql = SELECT_LATEST_STATS;
PreparedStatement pstmt = null;
try {
pstmt = txn.prepareAutoCloseStatement(sql);
ResultSet rs = pstmt.executeQuery();
Map<String, UsageNetworkVO> returnMap = new HashMap<String, UsageNetworkVO>();
while (rs.next()) {
long accountId = rs.getLong(1);
long zoneId = rs.getLong(2);
Long hostId = rs.getLong(3);
String hostType = rs.getString(4);
Long networkId = rs.getLong(5);
long bytesSent = rs.getLong(6);
long bytesReceived = rs.getLong(7);
long aggBytesReceived = rs.getLong(8);
long aggBytesSent = rs.getLong(9);
long eventTimeMillis = rs.getLong(10);
if (hostId != 0) {
returnMap.put(zoneId + "-" + accountId + "-Host-" + hostId + "-Network-" + networkId, new UsageNetworkVO(accountId, zoneId, hostId, hostType, networkId, bytesSent, bytesReceived, aggBytesReceived, aggBytesSent, eventTimeMillis));
} else {
returnMap.put(zoneId + "-" + accountId, new UsageNetworkVO(accountId, zoneId, hostId, hostType, networkId, bytesSent, bytesReceived, aggBytesReceived, aggBytesSent, eventTimeMillis));
}
}
return returnMap;
} catch (Exception ex) {
s_logger.error("error getting recent usage network stats", ex);
} finally {
txn.close();
}
return null;
}
Aggregations