mirror of https://github.com/apache/cloudstack.git
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 <rakeshv@apache.org>
This commit is contained in:
parent
65a48dcb74
commit
00e014ca58
|
|
@ -251,6 +251,14 @@ public class NetworkResponse extends BaseResponse implements ControlledEntityRes
|
|||
@Param(description = "the date this network was created", since = "4.16.0")
|
||||
private Date created;
|
||||
|
||||
@SerializedName(ApiConstants.RECEIVED_BYTES)
|
||||
@Param(description = "the total number of network traffic bytes received")
|
||||
private Long bytesReceived;
|
||||
|
||||
@SerializedName(ApiConstants.SENT_BYTES)
|
||||
@Param(description = "the total number of network traffic bytes sent")
|
||||
private Long bytesSent;
|
||||
|
||||
public Boolean getDisplayNetwork() {
|
||||
return displayNetwork;
|
||||
}
|
||||
|
|
@ -495,4 +503,12 @@ public class NetworkResponse extends BaseResponse implements ControlledEntityRes
|
|||
public void setCreated(Date created) {
|
||||
this.created = created;
|
||||
}
|
||||
|
||||
public void setBytesReceived(Long bytesReceived) {
|
||||
this.bytesReceived = bytesReceived;
|
||||
}
|
||||
|
||||
public void setBytesSent(final Long bytesSent) {
|
||||
this.bytesSent = bytesSent;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -320,6 +320,14 @@ public class UserVmResponse extends BaseResponseWithTagInformation implements Co
|
|||
@Param(description = "the pool type of the virtual machine", since = "4.16")
|
||||
private String poolType;
|
||||
|
||||
@SerializedName(ApiConstants.RECEIVED_BYTES)
|
||||
@Param(description = "the total number of network traffic bytes received")
|
||||
private Long bytesReceived;
|
||||
|
||||
@SerializedName(ApiConstants.SENT_BYTES)
|
||||
@Param(description = "the total number of network traffic bytes sent")
|
||||
private Long bytesSent;
|
||||
|
||||
public UserVmResponse() {
|
||||
securityGroupList = new LinkedHashSet<SecurityGroupResponse>();
|
||||
nics = new TreeSet<>(Comparator.comparingInt(x -> Integer.parseInt(x.getDeviceId())));
|
||||
|
|
@ -923,4 +931,12 @@ public class UserVmResponse extends BaseResponseWithTagInformation implements Co
|
|||
public Date getLastUpdated() {
|
||||
return lastUpdated;
|
||||
}
|
||||
|
||||
public void setBytesReceived(Long bytesReceived) {
|
||||
this.bytesReceived = bytesReceived;
|
||||
}
|
||||
|
||||
public void setBytesSent(Long bytesSent) {
|
||||
this.bytesSent = bytesSent;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -328,6 +328,8 @@ import com.cloud.user.AccountManager;
|
|||
import com.cloud.user.SSHKeyPair;
|
||||
import com.cloud.user.User;
|
||||
import com.cloud.user.UserAccount;
|
||||
import com.cloud.user.UserStatisticsVO;
|
||||
import com.cloud.user.dao.UserStatisticsDao;
|
||||
import com.cloud.uservm.UserVm;
|
||||
import com.cloud.utils.Pair;
|
||||
import com.cloud.utils.StringUtils;
|
||||
|
|
@ -335,6 +337,7 @@ import com.cloud.utils.crypt.DBEncryptionUtil;
|
|||
import com.cloud.utils.db.EntityManager;
|
||||
import com.cloud.utils.db.SearchBuilder;
|
||||
import com.cloud.utils.db.SearchCriteria;
|
||||
import com.cloud.utils.db.SearchCriteria.Op;
|
||||
import com.cloud.utils.exception.CloudRuntimeException;
|
||||
import com.cloud.utils.net.Dhcp;
|
||||
import com.cloud.utils.net.Ip;
|
||||
|
|
@ -401,6 +404,8 @@ public class ApiResponseHelper implements ResponseGenerator {
|
|||
private GuestOSCategoryDao _guestOsCategoryDao;
|
||||
@Inject
|
||||
private GuestOSDao _guestOsDao;
|
||||
@Inject
|
||||
private UserStatisticsDao userStatsDao;
|
||||
|
||||
@Override
|
||||
public UserResponse createUserResponse(User user) {
|
||||
|
|
@ -2355,6 +2360,20 @@ public class ApiResponseHelper implements ResponseGenerator {
|
|||
response.setExternalId(network.getExternalId());
|
||||
response.setRedundantRouter(network.isRedundant());
|
||||
response.setCreated(network.getCreated());
|
||||
|
||||
Long bytesReceived = 0L;
|
||||
Long bytesSent = 0L;
|
||||
SearchBuilder<UserStatisticsVO> sb = userStatsDao.createSearchBuilder();
|
||||
sb.and("networkId", sb.entity().getNetworkId(), Op.EQ);
|
||||
SearchCriteria<UserStatisticsVO> sc = sb.create();
|
||||
sc.setParameters("networkId", network.getId());
|
||||
for (UserStatisticsVO stat: userStatsDao.search(sc, null)) {
|
||||
bytesReceived += stat.getNetBytesReceived() + stat.getCurrentBytesReceived();
|
||||
bytesSent += stat.getNetBytesSent() + stat.getCurrentBytesSent();
|
||||
}
|
||||
response.setBytesReceived(bytesReceived);
|
||||
response.setBytesSent(bytesSent);
|
||||
|
||||
response.setObjectName("network");
|
||||
return response;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -51,10 +51,13 @@ import com.cloud.storage.GuestOS;
|
|||
import com.cloud.user.Account;
|
||||
import com.cloud.user.AccountManager;
|
||||
import com.cloud.user.User;
|
||||
import com.cloud.user.UserStatisticsVO;
|
||||
import com.cloud.user.dao.UserDao;
|
||||
import com.cloud.user.dao.UserStatisticsDao;
|
||||
import com.cloud.uservm.UserVm;
|
||||
import com.cloud.utils.db.SearchBuilder;
|
||||
import com.cloud.utils.db.SearchCriteria;
|
||||
import com.cloud.utils.db.SearchCriteria.Op;
|
||||
import com.cloud.utils.net.Dhcp;
|
||||
import com.cloud.vm.UserVmDetailVO;
|
||||
import com.cloud.vm.UserVmManager;
|
||||
|
|
@ -78,6 +81,8 @@ public class UserVmJoinDaoImpl extends GenericDaoBaseWithTagInformation<UserVmJo
|
|||
private UserDao _userDao;
|
||||
@Inject
|
||||
private NicExtraDhcpOptionDao _nicExtraDhcpOptionDao;
|
||||
@Inject
|
||||
UserStatisticsDao userStatsDao;
|
||||
|
||||
private final SearchBuilder<UserVmJoinVO> VmDetailSearch;
|
||||
private final SearchBuilder<UserVmJoinVO> activeVmByIsoSearch;
|
||||
|
|
@ -366,9 +371,26 @@ public class UserVmJoinDaoImpl extends GenericDaoBaseWithTagInformation<UserVmJo
|
|||
userVmResponse.setDynamicallyScalable(userVm.isDynamicallyScalable());
|
||||
}
|
||||
|
||||
addVmRxTxDataToResponse(userVm, userVmResponse);
|
||||
|
||||
return userVmResponse;
|
||||
}
|
||||
|
||||
private void addVmRxTxDataToResponse(UserVmJoinVO userVm, UserVmResponse userVmResponse) {
|
||||
Long bytesReceived = 0L;
|
||||
Long bytesSent = 0L;
|
||||
SearchBuilder<UserStatisticsVO> sb = userStatsDao.createSearchBuilder();
|
||||
sb.and("deviceId", sb.entity().getDeviceId(), Op.EQ);
|
||||
SearchCriteria<UserStatisticsVO> sc = sb.create();
|
||||
sc.setParameters("deviceId", userVm.getId());
|
||||
for (UserStatisticsVO stat: userStatsDao.search(sc, null)) {
|
||||
bytesReceived += stat.getNetBytesReceived() + stat.getCurrentBytesReceived();
|
||||
bytesSent += stat.getNetBytesSent() + stat.getCurrentBytesSent();
|
||||
}
|
||||
userVmResponse.setBytesReceived(bytesReceived);
|
||||
userVmResponse.setBytesSent(bytesSent);
|
||||
}
|
||||
|
||||
/**
|
||||
* The resulting Response attempts to be in line with what is returned from
|
||||
* @see com.cloud.api.ApiResponseHelper#createNicResponse(Nic)
|
||||
|
|
|
|||
Loading…
Reference in New Issue