Merge branch 'report-managment' into 'master'

Create api to generate report to filter devices with outdated OS versions

See merge request entgra/carbon-device-mgt!433
feature/appm-store/pbac
Dharmakeerthi Lasantha 5 years ago
commit c8976a58fc

@ -38,6 +38,7 @@ import org.wso2.carbon.device.mgt.jaxrs.util.Constants;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
@ -309,4 +310,63 @@ public interface ReportManagementService {
value = "Provide how many device details you require from the starting pagination index/offset.")
@QueryParam("limit")
int limit) throws ReportManagementException;
@GET
@Path("expired-devices/{deviceType}")
@ApiOperation(
produces = MediaType.APPLICATION_JSON,
httpMethod = "GET",
value = "Getting Details of Registered Devices filtered by OS version build date",
notes = "Provides details of devices that have a version build date older than the provided date.",
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 devices.",
response = DeviceList.class,
responseHeaders = {
@ResponseHeader(
name = "Content-Type",
description = "The content type of the body")}),
@ApiResponse(
code = 404,
message = "Not Found. " +
"\n Device type does not exist",
response = ErrorResponse.class),
@ApiResponse(
code = 500,
message = "Internal Server Error. " +
"\n Server error occurred while fetching the devices.",
response = ErrorResponse.class)
})
Response getExpiredDevicesByOSVersion(
@ApiParam(
name = "deviceType",
value = "Name of the device type.",
required = true)
@PathParam("deviceType") String deviceType,
@ApiParam(
name = "osBuildDate",
value = "Minimum OS version build date which is used to filter the devices.",
required = true)
@QueryParam("osBuildDate") Long osBuildDate,
@ApiParam(
name = "offset",
value = "The starting pagination index for the list of filtered devices.",
defaultValue = "0")
@QueryParam("offset")
int offset,
@ApiParam(
name = "limit",
value = "Limit of the number of deices that should be returned.",
defaultValue = "5")
@QueryParam("limit")
int limit);
}

@ -25,7 +25,9 @@ import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.PaginationResult;
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.jaxrs.beans.ErrorResponse;
import org.wso2.carbon.device.mgt.jaxrs.service.api.ReportManagementService;
@ -36,6 +38,7 @@ import javax.ws.rs.Consumes;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
@ -163,4 +166,35 @@ public class ReportManagementServiceImpl implements ReportManagementService {
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
}
}
@GET
@Path("expired-devices/{deviceType}")
@Override
public Response getExpiredDevicesByOSVersion(@PathParam("deviceType") String deviceType,
@QueryParam("osBuildDate") Long osBuildDate,
@DefaultValue("0")
@QueryParam("offset") int offset,
@DefaultValue("5")
@QueryParam("limit") int limit) {
try {
PaginationRequest request = new PaginationRequest(offset, limit);
request.setDeviceType(deviceType);
request.setProperty(Constants.OS_BUILD_DATE, osBuildDate);
PaginationResult paginationResult = DeviceMgtAPIUtils
.getReportManagementService()
.getDevicesExpiredByOSVersion(request);
return Response.status(Response.Status.OK).entity(paginationResult).build();
} catch (DeviceTypeNotFoundException e) {
String msg = "Error occurred while retrieving devices list. Device type: " + deviceType +
"is not valid";
log.error(msg);
return Response.status(Response.Status.NOT_FOUND).entity(msg).build();
} catch (ReportManagementException e) {
String msg = "Error occurred while retrieving devices list with out-dated OS build versions";
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
}
}
}

@ -20,6 +20,7 @@ package org.wso2.carbon.device.mgt.common.report.mgt;
import com.google.gson.JsonObject;
import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.PaginationResult;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceTypeNotFoundException;
import org.wso2.carbon.device.mgt.common.exceptions.ReportManagementException;
import java.util.List;
@ -47,4 +48,15 @@ public interface ReportManagementService {
JsonObject getCountOfDevicesByDuration(PaginationRequest request, List<String> statusList, String fromDate, String toDate)
throws ReportManagementException;
/**
* Get a list of devices with the count which are older than the given OS version
*
* @param request {@link PaginationRequest}
* @return {@link PaginationResult}
* @throws ReportManagementException Might occur during the business logic or building database query
* @throws DeviceTypeNotFoundException Might occur while validating the device type
*/
PaginationResult getDevicesExpiredByOSVersion(PaginationRequest request)
throws ReportManagementException, DeviceTypeNotFoundException;
}

@ -611,4 +611,27 @@ public interface DeviceDAO {
*/
int getSubscribedDeviceCount(List<Integer> deviceIds, int tenantId, String status)
throws DeviceManagementDAOException;
/**
* Get a list of devices older than the given OS version of a device type
*
* @param request Object with device type and OS version info
* @param tenantId Id of the current tenant.
* @return {@link List<Device>}
* @throws DeviceManagementDAOException Thrown if error occurs while database transactions
*/
List<Device> getDevicesExpiredByOSVersion(PaginationRequest request, int tenantId)
throws DeviceManagementDAOException;
/**
* Count the number of devices older than the given OS version of a device type
*
* @param deviceType Device type name
* @param osBuildDate BUild date off the current OS version
* @param tenantId Id of the current tenant.
* @return {@link Integer}
* @throws DeviceManagementDAOException Thrown if error occurs while database transactions
*/
int getCountOfDeviceExpiredByOSVersion(String deviceType, long osBuildDate, int tenantId)
throws DeviceManagementDAOException;
}

@ -43,6 +43,7 @@ import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo.Status;
import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo;
import org.wso2.carbon.device.mgt.common.device.details.DeviceLocationHistory;
import org.wso2.carbon.device.mgt.common.configuration.mgt.DevicePropertyInfo;
import org.wso2.carbon.device.mgt.common.device.details.DeviceData;
@ -53,6 +54,7 @@ import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.device.mgt.core.geo.GeoCluster;
import org.wso2.carbon.device.mgt.core.geo.geoHash.GeoCoordinate;
import org.wso2.carbon.device.mgt.core.report.mgt.Constants;
import java.sql.Connection;
import java.sql.PreparedStatement;
@ -1791,6 +1793,107 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
}
}
@Override
public List<Device> getDevicesExpiredByOSVersion(PaginationRequest request, int tenantId)
throws DeviceManagementDAOException {
try {
Long osBuildDate = (Long) request.getProperty(Constants.OS_BUILD_DATE);
Connection conn = getConnection();
String sql = "SELECT " +
"dt.NAME AS DEVICE_TYPE, " +
"d.ID AS DEVICE_ID, " +
"d.NAME AS DEVICE_NAME, " +
"d.DESCRIPTION, " +
"d.DEVICE_IDENTIFICATION, " +
"dd.OS_VERSION, " +
"dd.OS_BUILD_DATE, " +
"e.ID AS ENROLMENT_ID, " +
"e.OWNER, " +
"e.OWNERSHIP, " +
"e.STATUS, " +
"e.DATE_OF_LAST_UPDATE, " +
"e.DATE_OF_ENROLMENT " +
"FROM DM_DEVICE d, " +
"DM_DEVICE_DETAIL dd, " +
"DM_ENROLMENT e, " +
"(SELECT ID, NAME " +
"FROM DM_DEVICE_TYPE " +
"WHERE NAME = ? " +
"AND PROVIDER_TENANT_ID = ?) dt " +
"WHERE dt.ID = d.DEVICE_TYPE_ID " +
"AND d.ID = e.DEVICE_ID " +
"AND d.ID = dd.DEVICE_ID " +
"AND dd.OS_BUILD_DATE < ? " +
"LIMIT ? OFFSET ?";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
int paramIDx = 1;
ps.setString(paramIDx++, request.getDeviceType());
ps.setInt(paramIDx++, tenantId);
ps.setLong(paramIDx++, osBuildDate);
ps.setInt(paramIDx++, request.getRowCount());
ps.setInt(paramIDx, request.getStartIndex());
try (ResultSet rs = ps.executeQuery()) {
List<Device> devices = new ArrayList<>();
DeviceInfo deviceInfo = new DeviceInfo();
if (rs.next()) {
Device device = DeviceManagementDAOUtil.loadDevice(rs);
deviceInfo.setOsVersion(rs.getString(Constants.OS_VERSION));
deviceInfo.setOsBuildDate(rs.getString(Constants.OS_BUILD_DATE));
device.setDeviceInfo(deviceInfo);
devices.add(device);
}
return devices;
}
}
} catch (SQLException e) {
String msg = "Error occurred while building or executing queries to retrieve information " +
"of devices with an older OS build date";
log.error(msg, e);
throw new DeviceManagementDAOException(msg, e);
}
}
@Override
public int getCountOfDeviceExpiredByOSVersion(String deviceType, long osBuildDate, int tenantId)
throws DeviceManagementDAOException {
try {
Connection conn = getConnection();
String sql = "SELECT " +
"COUNT(dd.DEVICE_ID) AS DEVICE_COUNT " +
"FROM DM_DEVICE d, " +
"DM_DEVICE_DETAIL dd, " +
"(SELECT ID " +
"FROM DM_DEVICE_TYPE " +
"WHERE NAME = ? " +
"AND PROVIDER_TENANT_ID = ?) dt " +
"WHERE d.DEVICE_TYPE_ID = dt.ID " +
"AND d.ID = dd.DEVICE_ID " +
"AND dd.OS_BUILD_DATE < ?";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
int paramIdx = 1;
ps.setString(paramIdx++, deviceType);
ps.setInt(paramIdx++, tenantId);
ps.setLong(paramIdx, osBuildDate);
try (ResultSet rs = ps.executeQuery()) {
int deviceCount = 0;
if (rs.next()) {
deviceCount = rs.getInt("DEVICE_COUNT");
}
return deviceCount;
}
}
} catch (SQLException e) {
String msg = "Error occurred while building or executing queries to retrieve the count " +
"of devices with an older OS build date";
log.error(msg, e);
throw new DeviceManagementDAOException(msg, e);
}
}
/***
* This method removes records of a given list of devices from the DM_DEVICE_DETAIL table
* @param conn Connection object

@ -24,10 +24,12 @@ import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.Count;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.dao.impl.AbstractDeviceDAOImpl;
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
import org.wso2.carbon.device.mgt.core.report.mgt.Constants;
import java.sql.Connection;
@ -828,6 +830,70 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl {
}
}
@Override
public List<Device> getDevicesExpiredByOSVersion(PaginationRequest request, int tenantId)
throws DeviceManagementDAOException {
try {
Long osBuildDate = (Long) request.getProperty(Constants.OS_BUILD_DATE);
Connection conn = getConnection();
String sql = "SELECT " +
"dt.NAME AS DEVICE_TYPE, " +
"d.ID AS DEVICE_ID, " +
"d.NAME AS DEVICE_NAME, " +
"d.DESCRIPTION, " +
"d.DEVICE_IDENTIFICATION, " +
"dd.OS_VERSION, " +
"dd.OS_BUILD_DATE, " +
"e.ID AS ENROLMENT_ID, " +
"e.OWNER, " +
"e.OWNERSHIP, " +
"e.STATUS, " +
"e.DATE_OF_LAST_UPDATE, " +
"e.DATE_OF_ENROLMENT " +
"FROM DM_DEVICE d, " +
"DM_DEVICE_DETAIL dd, " +
"DM_ENROLMENT e, " +
"(SELECT ID, NAME " +
"FROM DM_DEVICE_TYPE " +
"WHERE NAME = ? " +
"AND PROVIDER_TENANT_ID = ?) dt " +
"WHERE dt.ID = d.DEVICE_TYPE_ID " +
"AND d.ID = e.DEVICE_ID " +
"AND d.ID = dd.DEVICE_ID " +
"AND dd.OS_BUILD_DATE < ? " +
"ORDER BY ENROLMENT_ID " +
"OFFSET ? ROWS " +
"FETCH NEXT ? ROWS ONLY";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
int paramIDx = 1;
ps.setString(paramIDx++, request.getDeviceType());
ps.setInt(paramIDx++, tenantId);
ps.setLong(paramIDx++, osBuildDate);
ps.setInt(paramIDx++, request.getStartIndex());
ps.setInt(paramIDx, request.getRowCount());
try (ResultSet rs = ps.executeQuery()) {
List<Device> devices = new ArrayList<>();
DeviceInfo deviceInfo = new DeviceInfo();
if (rs.next()) {
Device device = DeviceManagementDAOUtil.loadDevice(rs);
deviceInfo.setOsVersion(rs.getString(Constants.OS_VERSION));
deviceInfo.setOsBuildDate(rs.getString(Constants.OS_BUILD_DATE));
device.setDeviceInfo(deviceInfo);
devices.add(device);
}
return devices;
}
}
} catch (SQLException e) {
String msg = "Error occurred while retrieving information of devices with an older OS date " +
"than the minimum date";
log.error(msg, e);
throw new DeviceManagementDAOException(msg, e);
}
}
private Connection getConnection() throws SQLException {
return DeviceManagementDAOFactory.getConnection();
}

@ -0,0 +1,27 @@
/*
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. 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.core.report.mgt;
public class Constants {
// device properties
public static final String OS_BUILD_DATE = "OS_BUILD_DATE";
public static final String OS_VERSION = "OS_VERSION";
}

@ -18,6 +18,7 @@
package org.wso2.carbon.device.mgt.core.report.mgt;
import com.google.gson.JsonObject;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.Count;
@ -25,12 +26,14 @@ import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.PaginationResult;
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.report.mgt.ReportManagementService;
import org.wso2.carbon.device.mgt.core.dao.DeviceDAO;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil;
import java.sql.SQLException;
@ -153,6 +156,67 @@ public class ReportManagementServiceImpl implements ReportManagementService {
}
}
@Override
public PaginationResult getDevicesExpiredByOSVersion(PaginationRequest request)
throws ReportManagementException, DeviceTypeNotFoundException {
if (request == null ||
StringUtils.isBlank(request.getDeviceType()) ||
StringUtils.isBlank(request.getDeviceType()) ||
!request.getProperties().containsKey(Constants.OS_BUILD_DATE) ||
(Long) request.getProperty(Constants.OS_BUILD_DATE) == 0) {
String msg = "Error Invalid data received from the request.\n" +
"osBuildDate cannot be null or 0 and device type cannot be null or empty";
log.error(msg);
throw new ReportManagementException(msg);
}
try {
int tenantId = DeviceManagementDAOUtil.getTenantId();
String deviceType = request.getDeviceType();
PaginationResult paginationResult = new PaginationResult();
DeviceManagerUtil.validateDeviceListPageSize(request);
DeviceType deviceTypeObj = DeviceManagerUtil.getDeviceType(
deviceType, tenantId);
if (deviceTypeObj == null) {
String msg = "Error, device of type: " + deviceType + " does not exist";
log.error(msg);
throw new DeviceTypeNotFoundException(msg);
}
try {
DeviceManagementDAOFactory.openConnection();
List<Device> devices = deviceDAO.getDevicesExpiredByOSVersion(request, tenantId);
int deviceCount = deviceDAO.getCountOfDeviceExpiredByOSVersion(
deviceType,
(Long) request.getProperty(Constants.OS_BUILD_DATE),
tenantId);
paginationResult.setData(devices);
paginationResult.setRecordsFiltered(devices.size());
paginationResult.setRecordsTotal(deviceCount);
return paginationResult;
} 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 expired devices by a OS build date " +
"for the tenant";
log.error(msg, e);
throw new ReportManagementException(msg, e);
} catch (DeviceManagementException e) {
String msg = "Error occurred while validating the request";
log.error(msg, e);
throw new ReportManagementException(msg, e);
}
}
//NOTE: This is just a temporary method for retrieving device counts
public JsonObject buildCount(String start, String end, List<Count> countList) throws ParseException {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

@ -1,19 +1,19 @@
/*
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
* Copyright (c) 2020, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved.
*
* WSO2 Inc. 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
* 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.
* 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.
*/

Loading…
Cancel
Save