Merge branch 'feature/filter-apis' into 'master'

Add API to retrieve filters for analytics

See merge request entgra/carbon-device-mgt!817
feature/traccar-sync
Pahansith Gunathilake 3 years ago
commit 2026b9a6fa

@ -562,4 +562,49 @@ public interface ReportManagementService {
defaultValue = "10") defaultValue = "10")
@QueryParam("limit") @QueryParam("limit")
int 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();
} }

@ -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.common.exceptions.ReportManagementException;
import org.wso2.carbon.device.mgt.core.report.mgt.Constants; 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.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.beans.ErrorResponse;
import org.wso2.carbon.device.mgt.jaxrs.service.api.ReportManagementService; import org.wso2.carbon.device.mgt.jaxrs.service.api.ReportManagementService;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil; 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(); return Response.status(Response.Status.NOT_FOUND).entity(msg).build();
} }
} }
@GET
@Path("/filters")
@Override
public Response getReportFilters() {
try {
List<String> operators = DeviceMgtAPIUtils.getReportManagementService().getDeviceOperators();
List<String> 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();
}
}
} }

@ -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<String> deviceOperators;
private List<String> agentVersions;
public List<String> getDeviceOperators() {
return deviceOperators;
}
public void setDeviceOperators(List<String> deviceOperators) {
this.deviceOperators = deviceOperators;
}
public List<String> getAgentVersions() {
return agentVersions;
}
public void setAgentVersions(List<String> agentVersions) {
this.agentVersions = agentVersions;
}
}

@ -99,4 +99,20 @@ public interface ReportManagementService {
PaginationResult getDeviceNotAssignedToGroups(PaginationRequest paginationRequest, PaginationResult getDeviceNotAssignedToGroups(PaginationRequest paginationRequest,
List<String> groupNames) List<String> groupNames)
throws ReportManagementException, DeviceTypeNotFoundException; throws ReportManagementException, DeviceTypeNotFoundException;
/**
* This method is used to get device operators for UI filters.
*
* @return returns list of device operators.
* @throws ReportManagementException
*/
List<String> getDeviceOperators() throws ReportManagementException;
/**
* This method is used to get agent versions for UI filters.
*
* @return returns list of all agent versions.
* @throws ReportManagementException
*/
List<String> getAgentVersions() throws ReportManagementException;
} }

@ -781,4 +781,20 @@ public interface DeviceDAO {
*/ */
int getGroupedDevicesCount(PaginationRequest request, List<Integer> deviceIds, String groupName, int tenantId) int getGroupedDevicesCount(PaginationRequest request, List<Integer> deviceIds, String groupName, int tenantId)
throws DeviceManagementDAOException; throws DeviceManagementDAOException;
/**
* This method is used to retrieve all device operators for a tenant.
*
* @return returns list of device operators.
* @throws DeviceManagementDAOException
*/
List<String> 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<String> getAgentVersions(int tenantId) throws DeviceManagementDAOException;
} }

@ -3172,4 +3172,68 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
throw new DeviceManagementDAOException(msg, e); throw new DeviceManagementDAOException(msg, e);
} }
} }
@Override
public List<String> getOperators(int tenantId) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
List<String> 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<String> getAgentVersions(int tenantId) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
List<String> 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;
}
} }

@ -429,4 +429,60 @@ public class ReportManagementServiceImpl implements ReportManagementService {
throw new ReportManagementException(msg, e); throw new ReportManagementException(msg, e);
} }
} }
@Override
public List<String> 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<String> 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);
}
}
} }

Loading…
Cancel
Save