From 71e6ca3bd0981ccebd159ed91b51d0536bff3b37 Mon Sep 17 00:00:00 2001 From: navodzoysa Date: Mon, 1 Nov 2021 06:05:20 +0530 Subject: [PATCH] Add API to retrieve filters for analytics --- .../service/api/ReportManagementService.java | 45 +++++++++++++ .../impl/ReportManagementServiceImpl.java | 25 ++++++++ .../device/mgt/common/ReportFiltersList.java | 49 ++++++++++++++ .../report/mgt/ReportManagementService.java | 16 +++++ .../carbon/device/mgt/core/dao/DeviceDAO.java | 16 +++++ .../core/dao/impl/AbstractDeviceDAOImpl.java | 64 +++++++++++++++++++ .../mgt/ReportManagementServiceImpl.java | 56 ++++++++++++++++ 7 files changed, 271 insertions(+) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/ReportFiltersList.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/ReportManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/ReportManagementService.java index 00929b71b08..ee3e9354dde 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/ReportManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/ReportManagementService.java @@ -562,4 +562,49 @@ public interface ReportManagementService { defaultValue = "10") @QueryParam("limit") int limit); + + @GET + @Path("/filters") + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "Retrieving filters of devices for analytics.", + notes = "Provides filters in devices of Entgra IoT Server which can be used in UI for filtering." + + "Filters include device operators and agent versions for all devices.", + tags = "Device Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = Constants.SCOPE, value = "perm:devices:view") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully fetched the list of filters.", + responseHeaders = { + @ResponseHeader( + name = "Content-Type", + description = "The content type of the body"), + @ResponseHeader( + name = "ETag", + description = "Entity Tag of the response resource.\n" + + "Used by caches, or in conditional requests."), + @ResponseHeader( + name = "Last-Modified", + description = "Date and time the resource was last modified.\n" + + "Used by caches, or in conditional requests."), + }), + @ApiResponse( + code = 404, + message = "Not Found. \n There are no device filters.", + response = ErrorResponse.class), + @ApiResponse( + code = 500, + message = "Internal Server Error. " + + "\n Server error occurred while fetching the device filters list.", + response = ErrorResponse.class) + }) + Response getReportFilters(); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/ReportManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/ReportManagementServiceImpl.java index 2d49998af7a..eb953118687 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/ReportManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/ReportManagementServiceImpl.java @@ -30,6 +30,7 @@ import org.wso2.carbon.device.mgt.common.exceptions.DeviceTypeNotFoundException; import org.wso2.carbon.device.mgt.common.exceptions.ReportManagementException; import org.wso2.carbon.device.mgt.core.report.mgt.Constants; import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceList; +import org.wso2.carbon.device.mgt.common.ReportFiltersList; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.service.api.ReportManagementService; import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil; @@ -304,4 +305,28 @@ public class ReportManagementServiceImpl implements ReportManagementService { return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); } } + + @GET + @Path("/filters") + @Override + public Response getReportFilters() { + try { + List operators = DeviceMgtAPIUtils.getReportManagementService().getDeviceOperators(); + List agentVersions = DeviceMgtAPIUtils.getReportManagementService().getAgentVersions(); + if(operators.isEmpty() && agentVersions.isEmpty()) { + String msg = "There are no report filters found"; + return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); + } + else { + ReportFiltersList reportFiltersList = new ReportFiltersList(); + reportFiltersList.setDeviceOperators(operators); + reportFiltersList.setAgentVersions(agentVersions); + return Response.status(Response.Status.OK).entity(reportFiltersList).build(); + } + } catch (ReportManagementException e) { + String msg = "Error occurred while retrieving device operators."; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/ReportFiltersList.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/ReportFiltersList.java new file mode 100644 index 00000000000..cdfc9997748 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/ReportFiltersList.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.wso2.carbon.device.mgt.common; + +import java.io.Serializable; +import java.util.List; + +/** + * This class carries information related to report filters + * including operators and agent versions of devices. + */ +public class ReportFiltersList implements Serializable { + + private static final long serialVersionUID = 2L; + + private List deviceOperators; + private List agentVersions; + + public List getDeviceOperators() { + return deviceOperators; + } + + public void setDeviceOperators(List deviceOperators) { + this.deviceOperators = deviceOperators; + } + + public List getAgentVersions() { + return agentVersions; + } + + public void setAgentVersions(List agentVersions) { + this.agentVersions = agentVersions; + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/report/mgt/ReportManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/report/mgt/ReportManagementService.java index 18a83bc9b3c..6fedda463fe 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/report/mgt/ReportManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/report/mgt/ReportManagementService.java @@ -99,4 +99,20 @@ public interface ReportManagementService { PaginationResult getDeviceNotAssignedToGroups(PaginationRequest paginationRequest, List groupNames) throws ReportManagementException, DeviceTypeNotFoundException; + + /** + * This method is used to get device operators for UI filters. + * + * @return returns list of device operators. + * @throws ReportManagementException + */ + List getDeviceOperators() throws ReportManagementException; + + /** + * This method is used to get agent versions for UI filters. + * + * @return returns list of all agent versions. + * @throws ReportManagementException + */ + List getAgentVersions() throws ReportManagementException; } 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 9796dd40f49..40eefe935e1 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 @@ -781,4 +781,20 @@ public interface DeviceDAO { */ int getGroupedDevicesCount(PaginationRequest request, List deviceIds, String groupName, int tenantId) throws DeviceManagementDAOException; + + /** + * This method is used to retrieve all device operators for a tenant. + * + * @return returns list of device operators. + * @throws DeviceManagementDAOException + */ + List getOperators(int tenantId) throws DeviceManagementDAOException; + + /** + * This method is used to retrieve all agent versions for a tenant. + * + * @return returns list of agent versions. + * @throws DeviceManagementDAOException + */ + List getAgentVersions(int tenantId) 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 dd175d493a9..4e56e492b48 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 @@ -3172,4 +3172,68 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { throw new DeviceManagementDAOException(msg, e); } } + + @Override + public List getOperators(int tenantId) throws DeviceManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + List deviceOperators = new ArrayList<>(); + try { + conn = this.getConnection(); + String sql = "SELECT DISTINCT(VALUE_FIELD) AS OPERATOR " + + "FROM DM_DEVICE_INFO i " + + "INNER JOIN DM_ENROLMENT e ON " + + "e.DEVICE_ID " + + "WHERE e.DEVICE_ID = i.DEVICE_ID " + + "AND KEY_FIELD = 'operator' " + + "AND TENANT_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, tenantId); + rs = stmt.executeQuery(); + while (rs.next()) { + String operator = rs.getString("OPERATOR"); + deviceOperators.add(operator); + } + } catch (SQLException e) { + String msg = "Error occurred while listing device operators."; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + return deviceOperators; + } + + @Override + public List getAgentVersions(int tenantId) throws DeviceManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + List agentVersions = new ArrayList<>(); + try { + conn = this.getConnection(); + String sql = "SELECT DISTINCT(VALUE_FIELD) AS AGENT_VERSION " + + "FROM DM_DEVICE_INFO i " + + "INNER JOIN DM_ENROLMENT e ON " + + "e.DEVICE_ID " + + "WHERE e.DEVICE_ID = i.DEVICE_ID " + + "AND KEY_FIELD = 'AGENT_VERSION' " + + "AND TENANT_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, tenantId); + rs = stmt.executeQuery(); + while (rs.next()) { + String agentVersion = rs.getString("AGENT_VERSION"); + agentVersions.add(agentVersion); + } + } catch (SQLException e) { + String msg = "Error occurred while listing agent versions."; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + return agentVersions; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/report/mgt/ReportManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/report/mgt/ReportManagementServiceImpl.java index 7d2ff24875b..fc670248359 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/report/mgt/ReportManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/report/mgt/ReportManagementServiceImpl.java @@ -429,4 +429,60 @@ public class ReportManagementServiceImpl implements ReportManagementService { throw new ReportManagementException(msg, e); } } + + @Override + public List getDeviceOperators() throws ReportManagementException { + if (log.isDebugEnabled()) { + log.debug("Get device operators"); + } + try { + int tenantId = DeviceManagementDAOUtil.getTenantId(); + try { + DeviceManagementDAOFactory.openConnection(); + return deviceDAO.getOperators(tenantId); + } catch (DeviceManagementDAOException e) { + String msg = "Error occurred while obtaining the device operators."; + log.error(msg, e); + throw new ReportManagementException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while opening a connection to the data source."; + log.error(msg, e); + throw new ReportManagementException(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } catch (DeviceManagementDAOException e) { + String msg = "Error occurred while retrieving Tenant ID."; + log.error(msg, e); + throw new ReportManagementException(msg, e); + } + } + + @Override + public List getAgentVersions() throws ReportManagementException { + if (log.isDebugEnabled()) { + log.debug("Get agent versions"); + } + try { + int tenantId = DeviceManagementDAOUtil.getTenantId(); + try { + DeviceManagementDAOFactory.openConnection(); + return deviceDAO.getAgentVersions(tenantId); + } catch (DeviceManagementDAOException e) { + String msg = "Error occurred while obtaining the agent versions."; + log.error(msg, e); + throw new ReportManagementException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while opening a connection to the data source."; + log.error(msg, e); + throw new ReportManagementException(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } catch (DeviceManagementDAOException e) { + String msg = "Error occurred while retrieving Tenant ID."; + log.error(msg, e); + throw new ReportManagementException(msg, e); + } + } }