From a0856f70693ff26cfaa654b22feb45c012200fc2 Mon Sep 17 00:00:00 2001 From: Pahansith Gunathilake Date: Wed, 1 Dec 2021 11:53:37 +0530 Subject: [PATCH 1/2] Add time duration filtering to activities EP --- .../api/ActivityInfoProviderService.java | 12 ++++++++++- .../impl/ActivityProviderServiceImpl.java | 17 +++++++++++++--- .../impl/util/RequestValidationUtil.java | 8 ++++++++ .../mgt/common/ActivityPaginationRequest.java | 17 ++++++++++++++++ .../mgt/dao/impl/GenericOperationDAOImpl.java | 20 ++++++++++++++++++- 5 files changed, 69 insertions(+), 5 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/ActivityInfoProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/ActivityInfoProviderService.java index 43bd7e847b..5185379ad9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/ActivityInfoProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/ActivityInfoProviderService.java @@ -478,6 +478,16 @@ public interface ActivityInfoProviderService { "Provide the value in the following format: EEE, d MMM yyyy HH:mm:ss Z\n." + "Example: Mon, 05 Jan 2014 15:10:00 +0200" ) - @HeaderParam("If-Modified-Since") String ifModifiedSince); + @HeaderParam("If-Modified-Since") String ifModifiedSince, + @ApiParam( + name = "startTimestamp", + value = "Starting unix timestamp value for filtering activities" + ) + @QueryParam("startTimestamp") long startTimestamp, + @ApiParam( + name = "endTimestamp", + value = "Ending unix timestamp value for filtering activities" + ) + @QueryParam("endTimestamp") long endTimestamp); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/ActivityProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/ActivityProviderServiceImpl.java index 29f1476138..8b74afc8c4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/ActivityProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/ActivityProviderServiceImpl.java @@ -228,11 +228,14 @@ public class ActivityProviderServiceImpl implements ActivityInfoProviderService @QueryParam("deviceId") String deviceId, @QueryParam("type") String type, @QueryParam("status") String status, - @HeaderParam("If-Modified-Since") String ifModifiedSince) { + @HeaderParam("If-Modified-Since") String ifModifiedSince, + @QueryParam("startTimestamp") long startTimestamp, + @QueryParam("endTimestamp") long endTimestamp) { long ifModifiedSinceTimestamp; long sinceTimestamp; long timestamp = 0; + boolean isTimeDurationProvided = false; if (log.isDebugEnabled()) { log.debug("getActivities since: " + since + " , offset: " + offset + " ,limit: " + limit + " ," + "ifModifiedSince: " + ifModifiedSince); @@ -262,9 +265,12 @@ public class ActivityProviderServiceImpl implements ActivityInfoProviderService } sinceTimestamp = sinceDate.getTime(); timestamp = sinceTimestamp / 1000; + } else if (startTimestamp > 0 && endTimestamp > 0) { + RequestValidationUtil.validateTimeDuration(startTimestamp, endTimestamp); + isTimeDurationProvided = true; } - if (timestamp == 0) { + if (timestamp == 0 && !isTimeDurationProvided) { //If timestamp is not sent by the user, a default value is set, that is equal to current time-12 hours. long time = System.currentTimeMillis() / 1000; timestamp = time - 42300; @@ -300,7 +306,12 @@ public class ActivityProviderServiceImpl implements ActivityInfoProviderService if (status != null && !status.isEmpty()) { activityPaginationRequest.setStatus(Operation.Status.valueOf(status.toUpperCase())); } - activityPaginationRequest.setSince(timestamp); + if (timestamp > 0) { + activityPaginationRequest.setSince(timestamp); + } else { + activityPaginationRequest.setStartTimestamp(startTimestamp); + activityPaginationRequest.setEndTimestamp(endTimestamp); + } if (log.isDebugEnabled()) { log.debug("Activity request: " + new Gson().toJson(activityPaginationRequest)); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/RequestValidationUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/RequestValidationUtil.java index 8846944110..4d16878f73 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/RequestValidationUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/RequestValidationUtil.java @@ -769,4 +769,12 @@ public class RequestValidationUtil { } } } + + public static void validateTimeDuration(long startTimestamp, long endTimestamp) { + if (startTimestamp > endTimestamp) { + throw new InputValidationException( + new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Request parameter startTimestamp" + + " should not be higher values than endTimestamp").build()); + } + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/ActivityPaginationRequest.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/ActivityPaginationRequest.java index bf4fe3e113..08cc89093a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/ActivityPaginationRequest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/ActivityPaginationRequest.java @@ -34,6 +34,8 @@ public class ActivityPaginationRequest { private long since; private Operation.Type type; private Operation.Status status; + private long startTimestamp; + private long endTimestamp; public ActivityPaginationRequest(int offset, int limit) { this.offset = offset; @@ -113,4 +115,19 @@ public class ActivityPaginationRequest { this.status = status; } + public long getStartTimestamp() { + return startTimestamp; + } + + public void setStartTimestamp(long startTimestamp) { + this.startTimestamp = startTimestamp; + } + + public long getEndTimestamp() { + return endTimestamp; + } + + public void setEndTimestamp(long endTimestamp) { + this.endTimestamp = endTimestamp; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java index b4229f1357..3f254fdd08 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java @@ -1726,6 +1726,7 @@ public class GenericOperationDAOImpl implements OperationDAO { public List getActivities(ActivityPaginationRequest activityPaginationRequest) throws OperationManagementDAOException { try { + boolean isTimeDurationFilteringProvided = false; Connection conn = OperationManagementDAOFactory.getConnection(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); String sql = "SELECT " + @@ -1791,6 +1792,10 @@ public class GenericOperationDAOImpl implements OperationDAO { if (activityPaginationRequest.getSince() != 0) { sql += "AND eom.UPDATED_TIMESTAMP > ? "; } + if (activityPaginationRequest.getStartTimestamp() > 0 && activityPaginationRequest.getEndTimestamp() > 0) { + isTimeDurationFilteringProvided = true; + sql += "AND eom.UPDATED_TIMESTAMP BETWEEN ? AND ? "; + } if (activityPaginationRequest.getType() != null) { sql += "AND eom.TYPE = ? "; } @@ -1818,6 +1823,10 @@ public class GenericOperationDAOImpl implements OperationDAO { if (activityPaginationRequest.getSince() != 0) { stmt.setLong(index++, activityPaginationRequest.getSince()); } + if (isTimeDurationFilteringProvided) { + stmt.setLong(index++, activityPaginationRequest.getStartTimestamp()); + stmt.setLong(index++, activityPaginationRequest.getEndTimestamp()); + } if (activityPaginationRequest.getType() != null) { stmt.setString(index++, activityPaginationRequest.getType().name()); } @@ -1872,6 +1881,7 @@ public class GenericOperationDAOImpl implements OperationDAO { public int getActivitiesCount(ActivityPaginationRequest activityPaginationRequest) throws OperationManagementDAOException { try { + boolean isTimeDurationFilteringProvided = false; Connection conn = OperationManagementDAOFactory.getConnection(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); String sql = "SELECT count(DISTINCT OPERATION_ID) AS ACTIVITY_COUNT FROM DM_ENROLMENT_OP_MAPPING " + @@ -1898,6 +1908,10 @@ public class GenericOperationDAOImpl implements OperationDAO { if (activityPaginationRequest.getStatus() != null) { sql += "AND STATUS = ? "; } + if (activityPaginationRequest.getStartTimestamp() > 0 && activityPaginationRequest.getEndTimestamp() > 0) { + isTimeDurationFilteringProvided = true; + sql += "AND UPDATED_TIMESTAMP BETWEEN ? AND ? "; + } int index = 1; try (PreparedStatement stmt = conn.prepareStatement(sql)) { @@ -1921,7 +1935,11 @@ public class GenericOperationDAOImpl implements OperationDAO { stmt.setString(index++, activityPaginationRequest.getType().name()); } if (activityPaginationRequest.getStatus() != null) { - stmt.setString(index, activityPaginationRequest.getStatus().name()); + stmt.setString(index++, activityPaginationRequest.getStatus().name()); + } + if (isTimeDurationFilteringProvided) { + stmt.setLong(index++, activityPaginationRequest.getStartTimestamp()); + stmt.setLong(index, activityPaginationRequest.getEndTimestamp()); } try (ResultSet rs = stmt.executeQuery()) { From 9f2cedd09ceffcb62a7418dc097365be46985fef Mon Sep 17 00:00:00 2001 From: Pahansith Gunathilake Date: Wed, 1 Dec 2021 17:10:45 +0530 Subject: [PATCH 2/2] Change time filtering DB column --- .../service/impl/util/RequestValidationUtil.java | 5 +++-- .../mgt/dao/impl/GenericOperationDAOImpl.java | 15 +++++++++++---- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/RequestValidationUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/RequestValidationUtil.java index 4d16878f73..116c715eb2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/RequestValidationUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/RequestValidationUtil.java @@ -773,8 +773,9 @@ public class RequestValidationUtil { public static void validateTimeDuration(long startTimestamp, long endTimestamp) { if (startTimestamp > endTimestamp) { throw new InputValidationException( - new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Request parameter startTimestamp" + - " should not be higher values than endTimestamp").build()); + new ErrorResponse.ErrorResponseBuilder().setCode(400l) + .setMessage("Request parameter startTimestamp should not be " + + "a higher value than endTimestamp").build()); } } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java index 3f254fdd08..de92618c46 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java @@ -1767,6 +1767,10 @@ public class GenericOperationDAOImpl implements OperationDAO { if (activityPaginationRequest.getSince() != 0) { sql += "AND UPDATED_TIMESTAMP > ? "; } + if (activityPaginationRequest.getStartTimestamp() > 0 && activityPaginationRequest.getEndTimestamp() > 0) { + isTimeDurationFilteringProvided = true; + sql += "AND CREATED_TIMESTAMP BETWEEN ? AND ? "; + } if (activityPaginationRequest.getType() != null) { sql += "AND TYPE = ? "; } @@ -1792,9 +1796,8 @@ public class GenericOperationDAOImpl implements OperationDAO { if (activityPaginationRequest.getSince() != 0) { sql += "AND eom.UPDATED_TIMESTAMP > ? "; } - if (activityPaginationRequest.getStartTimestamp() > 0 && activityPaginationRequest.getEndTimestamp() > 0) { - isTimeDurationFilteringProvided = true; - sql += "AND eom.UPDATED_TIMESTAMP BETWEEN ? AND ? "; + if (isTimeDurationFilteringProvided) { + sql += "AND eom.CREATED_TIMESTAMP BETWEEN ? AND ? "; } if (activityPaginationRequest.getType() != null) { sql += "AND eom.TYPE = ? "; @@ -1853,6 +1856,10 @@ public class GenericOperationDAOImpl implements OperationDAO { if (activityPaginationRequest.getSince() != 0) { stmt.setLong(index++, activityPaginationRequest.getSince()); } + if (isTimeDurationFilteringProvided) { + stmt.setLong(index++, activityPaginationRequest.getStartTimestamp()); + stmt.setLong(index++, activityPaginationRequest.getEndTimestamp()); + } if (activityPaginationRequest.getType() != null) { stmt.setString(index++, activityPaginationRequest.getType().name()); } @@ -1910,7 +1917,7 @@ public class GenericOperationDAOImpl implements OperationDAO { } if (activityPaginationRequest.getStartTimestamp() > 0 && activityPaginationRequest.getEndTimestamp() > 0) { isTimeDurationFilteringProvided = true; - sql += "AND UPDATED_TIMESTAMP BETWEEN ? AND ? "; + sql += "AND CREATED_TIMESTAMP BETWEEN ? AND ? "; } int index = 1;