From cb7323fc020325ee75720fe3569d71e95d1089c4 Mon Sep 17 00:00:00 2001 From: charitha Date: Tue, 3 Mar 2020 09:20:07 +0530 Subject: [PATCH] Add Health Check API --- .../carbon/device/mgt/core/dao/DeviceDAO.java | 2 ++ .../core/dao/impl/AbstractDeviceDAOImpl.java | 21 ++++++++++++++++ .../DeviceManagementProviderService.java | 2 ++ .../DeviceManagementProviderServiceImpl.java | 24 +++++++++++++++++++ 4 files changed, 49 insertions(+) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java index 072e12b61c..a17cbb81d7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java @@ -674,4 +674,6 @@ public interface DeviceDAO { int tenantId, String packageName, String version) throws DeviceManagementDAOException; + + int getFunctioningDevicesInSystem() throws DeviceManagementDAOException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java index b01925016f..83d8109634 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java @@ -2617,4 +2617,25 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { return joiner.toString(); } + + public int getFunctioningDevicesInSystem() throws DeviceManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + int deviceCount = 0; + try { + conn = this.getConnection(); + String sql = "SELECT COUNT(e.DEVICE_ID) AS DEVICE_COUNT FROM DM_ENROLMENT e WHERE STATUS != 'REMOVED'"; + stmt = conn.prepareStatement(sql); + ResultSet rs = stmt.executeQuery(); + + if (rs.next()) { + deviceCount = rs.getInt("DEVICE_COUNT"); + } + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while fetching count of functioning devices", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, null); + } + return deviceCount; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java index 74d06fa7c8..4389119dbf 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java @@ -882,4 +882,6 @@ public interface DeviceManagementProviderService { * @throws ApplicationManagementException if any service level or DAO level error occurs. */ List getAppVersions(String packageName) throws ApplicationManagementException; + + int getFunctioningDevicesInSystem() throws DeviceManagementException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index cd768fdd8e..aad18333e4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -4120,4 +4120,28 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv deviceConfiguration.setDeviceOwner(deviceOwner); return deviceConfiguration; } + + public int getFunctioningDevicesInSystem() throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("Get functioning devices count"); + } + try { + DeviceManagementDAOFactory.openConnection(); + return deviceDAO.getFunctioningDevicesInSystem(); + } catch (DeviceManagementDAOException e) { + String msg = "Error occurred while retrieving the device count"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while opening a connection to the data source"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred in getDeviceCount"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } }