Skip to content

Commit 00e014c

Browse files
raveningRakesh Venkatesh
andauthored
Add sent and received bytes to listNetworks and listVirtualMachines. (#4776)
* Add sent and receivedbytes to listNetworks and listVirtualMachines. Display the traffic data in networks and vm api response * follow code convention * remove final keyword Co-authored-by: Rakesh Venkatesh <[email protected]>
1 parent 65a48dc commit 00e014c

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

api/src/main/java/org/apache/cloudstack/api/response/NetworkResponse.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,14 @@ public class NetworkResponse extends BaseResponse implements ControlledEntityRes
251251
@Param(description = "the date this network was created", since = "4.16.0")
252252
private Date created;
253253

254+
@SerializedName(ApiConstants.RECEIVED_BYTES)
255+
@Param(description = "the total number of network traffic bytes received")
256+
private Long bytesReceived;
257+
258+
@SerializedName(ApiConstants.SENT_BYTES)
259+
@Param(description = "the total number of network traffic bytes sent")
260+
private Long bytesSent;
261+
254262
public Boolean getDisplayNetwork() {
255263
return displayNetwork;
256264
}
@@ -495,4 +503,12 @@ public Date getCreated() {
495503
public void setCreated(Date created) {
496504
this.created = created;
497505
}
506+
507+
public void setBytesReceived(Long bytesReceived) {
508+
this.bytesReceived = bytesReceived;
509+
}
510+
511+
public void setBytesSent(final Long bytesSent) {
512+
this.bytesSent = bytesSent;
513+
}
498514
}

api/src/main/java/org/apache/cloudstack/api/response/UserVmResponse.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,14 @@ public class UserVmResponse extends BaseResponseWithTagInformation implements Co
320320
@Param(description = "the pool type of the virtual machine", since = "4.16")
321321
private String poolType;
322322

323+
@SerializedName(ApiConstants.RECEIVED_BYTES)
324+
@Param(description = "the total number of network traffic bytes received")
325+
private Long bytesReceived;
326+
327+
@SerializedName(ApiConstants.SENT_BYTES)
328+
@Param(description = "the total number of network traffic bytes sent")
329+
private Long bytesSent;
330+
323331
public UserVmResponse() {
324332
securityGroupList = new LinkedHashSet<SecurityGroupResponse>();
325333
nics = new TreeSet<>(Comparator.comparingInt(x -> Integer.parseInt(x.getDeviceId())));
@@ -923,4 +931,12 @@ public void setLastUpdated(Date lastUpdated) {
923931
public Date getLastUpdated() {
924932
return lastUpdated;
925933
}
934+
935+
public void setBytesReceived(Long bytesReceived) {
936+
this.bytesReceived = bytesReceived;
937+
}
938+
939+
public void setBytesSent(Long bytesSent) {
940+
this.bytesSent = bytesSent;
941+
}
926942
}

server/src/main/java/com/cloud/api/ApiResponseHelper.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,13 +328,16 @@
328328
import com.cloud.user.SSHKeyPair;
329329
import com.cloud.user.User;
330330
import com.cloud.user.UserAccount;
331+
import com.cloud.user.UserStatisticsVO;
332+
import com.cloud.user.dao.UserStatisticsDao;
331333
import com.cloud.uservm.UserVm;
332334
import com.cloud.utils.Pair;
333335
import com.cloud.utils.StringUtils;
334336
import com.cloud.utils.crypt.DBEncryptionUtil;
335337
import com.cloud.utils.db.EntityManager;
336338
import com.cloud.utils.db.SearchBuilder;
337339
import com.cloud.utils.db.SearchCriteria;
340+
import com.cloud.utils.db.SearchCriteria.Op;
338341
import com.cloud.utils.exception.CloudRuntimeException;
339342
import com.cloud.utils.net.Dhcp;
340343
import com.cloud.utils.net.Ip;
@@ -401,6 +404,8 @@ public class ApiResponseHelper implements ResponseGenerator {
401404
private GuestOSCategoryDao _guestOsCategoryDao;
402405
@Inject
403406
private GuestOSDao _guestOsDao;
407+
@Inject
408+
private UserStatisticsDao userStatsDao;
404409

405410
@Override
406411
public UserResponse createUserResponse(User user) {
@@ -2355,6 +2360,20 @@ public NetworkResponse createNetworkResponse(ResponseView view, Network network)
23552360
response.setExternalId(network.getExternalId());
23562361
response.setRedundantRouter(network.isRedundant());
23572362
response.setCreated(network.getCreated());
2363+
2364+
Long bytesReceived = 0L;
2365+
Long bytesSent = 0L;
2366+
SearchBuilder<UserStatisticsVO> sb = userStatsDao.createSearchBuilder();
2367+
sb.and("networkId", sb.entity().getNetworkId(), Op.EQ);
2368+
SearchCriteria<UserStatisticsVO> sc = sb.create();
2369+
sc.setParameters("networkId", network.getId());
2370+
for (UserStatisticsVO stat: userStatsDao.search(sc, null)) {
2371+
bytesReceived += stat.getNetBytesReceived() + stat.getCurrentBytesReceived();
2372+
bytesSent += stat.getNetBytesSent() + stat.getCurrentBytesSent();
2373+
}
2374+
response.setBytesReceived(bytesReceived);
2375+
response.setBytesSent(bytesSent);
2376+
23582377
response.setObjectName("network");
23592378
return response;
23602379
}

server/src/main/java/com/cloud/api/query/dao/UserVmJoinDaoImpl.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,13 @@
5151
import com.cloud.user.Account;
5252
import com.cloud.user.AccountManager;
5353
import com.cloud.user.User;
54+
import com.cloud.user.UserStatisticsVO;
5455
import com.cloud.user.dao.UserDao;
56+
import com.cloud.user.dao.UserStatisticsDao;
5557
import com.cloud.uservm.UserVm;
5658
import com.cloud.utils.db.SearchBuilder;
5759
import com.cloud.utils.db.SearchCriteria;
60+
import com.cloud.utils.db.SearchCriteria.Op;
5861
import com.cloud.utils.net.Dhcp;
5962
import com.cloud.vm.UserVmDetailVO;
6063
import com.cloud.vm.UserVmManager;
@@ -78,6 +81,8 @@ public class UserVmJoinDaoImpl extends GenericDaoBaseWithTagInformation<UserVmJo
7881
private UserDao _userDao;
7982
@Inject
8083
private NicExtraDhcpOptionDao _nicExtraDhcpOptionDao;
84+
@Inject
85+
UserStatisticsDao userStatsDao;
8186

8287
private final SearchBuilder<UserVmJoinVO> VmDetailSearch;
8388
private final SearchBuilder<UserVmJoinVO> activeVmByIsoSearch;
@@ -366,9 +371,26 @@ public UserVmResponse newUserVmResponse(ResponseView view, String objectName, Us
366371
userVmResponse.setDynamicallyScalable(userVm.isDynamicallyScalable());
367372
}
368373

374+
addVmRxTxDataToResponse(userVm, userVmResponse);
375+
369376
return userVmResponse;
370377
}
371378

379+
private void addVmRxTxDataToResponse(UserVmJoinVO userVm, UserVmResponse userVmResponse) {
380+
Long bytesReceived = 0L;
381+
Long bytesSent = 0L;
382+
SearchBuilder<UserStatisticsVO> sb = userStatsDao.createSearchBuilder();
383+
sb.and("deviceId", sb.entity().getDeviceId(), Op.EQ);
384+
SearchCriteria<UserStatisticsVO> sc = sb.create();
385+
sc.setParameters("deviceId", userVm.getId());
386+
for (UserStatisticsVO stat: userStatsDao.search(sc, null)) {
387+
bytesReceived += stat.getNetBytesReceived() + stat.getCurrentBytesReceived();
388+
bytesSent += stat.getNetBytesSent() + stat.getCurrentBytesSent();
389+
}
390+
userVmResponse.setBytesReceived(bytesReceived);
391+
userVmResponse.setBytesSent(bytesSent);
392+
}
393+
372394
/**
373395
* The resulting Response attempts to be in line with what is returned from
374396
* @see com.cloud.api.ApiResponseHelper#createNicResponse(Nic)

0 commit comments

Comments
 (0)