}
if(bytesSent == 0L && bytesRcvd == 0L){
s_logger.trace("Ignore zero bytes for IP: "+publicIp);
continue;
}
UserStatisticsVO stats = new UserStatisticsVO(usageIp.getAccountId(), zoneId, null, null, null, null);
stats.setCurrentBytesSent(bytesSent);
stats.setCurrentBytesReceived(bytesRcvd);
collectedStats.add(stats);
}
}
}
//Get usage for Ips which were assigned for part of the duration period
for(UsageIPAddressVO usageIp : IpPartialUsage){
IpList = new ArrayList<String>() ;
IpList.add(usageIp.getAddress());
DirectNetworkUsageCommand cmd = new DirectNetworkUsageCommand(IpList, usageIp.getAssigned(), usageIp.getReleased());
DirectNetworkUsageAnswer answer = (DirectNetworkUsageAnswer) _agentMgr.easySend(host.getId(), cmd);
if (answer == null || !answer.getResult()) {
String details = (answer != null) ? answer.getDetails() : "details unavailable";
String msg = "Unable to get network usage stats from " + host.getId() + " due to: " + details + ".";
s_logger.error(msg);
return false;
} else {
String publicIp = usageIp.getAddress();
long[] bytesSentRcvd = answer.get(publicIp);
Long bytesSent = bytesSentRcvd[0];
Long bytesRcvd = bytesSentRcvd[1];
if(bytesSent == null || bytesRcvd == null){
s_logger.debug("Incorrect bytes for IP: "+publicIp);
continue;
}
if(bytesSent == 0L && bytesRcvd == 0L){
s_logger.trace("Ignore zero bytes for IP: "+publicIp);
continue;
}
UserStatisticsVO stats = new UserStatisticsVO(usageIp.getAccountId(), zoneId, null, null, null, null);
stats.setCurrentBytesSent(bytesSent);
stats.setCurrentBytesReceived(bytesRcvd);
collectedStats.add(stats);
}
}
if(collectedStats.size() == 0){
s_logger.debug("No new direct network stats. No need to persist");
return false;
}
//Persist all the stats and last_collection time in a single transaction
Transaction txn = Transaction.open(Transaction.CLOUD_DB);
try {
txn.start();
for(UserStatisticsVO stat : collectedStats){
UserStatisticsVO stats = _statsDao.lock(stat.getAccountId(), stat.getDataCenterId(), 0L, null, host.getId(), "DirectNetwork");
if (stats == null) {
stats = new UserStatisticsVO(stat.getAccountId(), zoneId, null, host.getId(), "DirectNetwork", 0L);
stats.setCurrentBytesSent(stat.getCurrentBytesSent());
stats.setCurrentBytesReceived(stat.getCurrentBytesReceived());
_statsDao.persist(stats);
} else {
stats.setCurrentBytesSent(stats.getCurrentBytesSent() + stat.getCurrentBytesSent());
stats.setCurrentBytesReceived(stats.getCurrentBytesReceived() + stat.getCurrentBytesReceived());
_statsDao.update(stats.getId(), stats);
}
}
lastCollectDetail.setValue(""+now.getTime());
_detailsDao.update(lastCollectDetail.getId(), lastCollectDetail);
txn.commit();