metrics: optimise code and query to get summed cpu sockets

Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com>
This commit is contained in:
Rohit Yadav 2024-05-09 02:56:07 +05:30
parent 35462dc96d
commit 90afcf2f85
3 changed files with 17 additions and 7 deletions

View File

@ -45,6 +45,8 @@ public interface HostDao extends GenericDao<HostVO, Long>, StateDao<Status, Stat
Integer countUpAndEnabledHostsInZone(long zoneId);
Integer countAllCPUSockets();
/**
* Mark all hosts associated with a certain management server
* as disconnected.

View File

@ -512,6 +512,20 @@ public class HostDaoImpl extends GenericDaoBase<HostVO, Long> implements HostDao
return getCount(sc);
}
@Override
public Integer countAllCPUSockets() {
final String sql = "SELECT SUM(host.cpu_sockets) FROM host WHERE host.type = 'Routing' AND host.removed IS NULL";
Integer cpuSockets = 0;
TransactionLegacy txn = TransactionLegacy.currentTxn();
try (PreparedStatement pstmt = txn.prepareAutoCloseStatement(sql)) {
ResultSet rs = pstmt.executeQuery();
cpuSockets = rs.getInt(1);
} catch (SQLException e) {
s_logger.debug("SQLException caught", e);
}
return cpuSockets;
}
@Override
public List<HostVO> listByDataCenterId(long id) {
return listByDataCenterIdAndState(id, ResourceState.Enabled);

View File

@ -562,13 +562,7 @@ public class MetricsServiceImpl extends MutualExclusiveIdsManagerBase implements
response.setRouters(domainRouterDao.countAllByRole(VirtualRouter.Role.VIRTUAL_ROUTER));
response.setInternalLbs(domainRouterDao.countAllByRole(VirtualRouter.Role.INTERNAL_LB_VM));
response.setAlerts(alertDao.countAll());
int cpuSockets = 0;
for (final Host host : hostDao.listByType(Host.Type.Routing)) {
if (host.getCpuSockets() != null) {
cpuSockets += host.getCpuSockets();
}
}
response.setCpuSockets(cpuSockets);
response.setCpuSockets(hostDao.countAllCPUSockets());
response.setManagementServers(managementServerHostDao.countAll());
return response;
}