From c6a1508ee77c5cf4707210e20ead78351d40ec50 Mon Sep 17 00:00:00 2001 From: lashanfaliq95 Date: Wed, 11 Jul 2018 09:26:01 +0530 Subject: [PATCH 1/7] add the filter api --- .../api/DeviceEventManagementService.java | 61 +++++++++++++++++++ .../DeviceEventManagementServiceImpl.java | 55 +++++++++++++++++ 2 files changed, 116 insertions(+) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceEventManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceEventManagementService.java index 59e7e2e11b5..449df8e8f5a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceEventManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceEventManagementService.java @@ -296,6 +296,67 @@ public interface DeviceEventManagementService { @PathParam("type") String deviceType, @ApiParam(name = "limit", value = "limit of the records that needs to be picked up", required = false) @QueryParam("limit") int limit); + + @GET + @Path("filter/{type}/{parameter}") + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "Getting the filtered devices", + notes = "Get the list of devices based on the filter parameter", + tags = "Device Event Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = Constants.SCOPE, value = "perm:device-types:events:view") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully fetched the event.", + response = EventRecords.class, + 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 = 400, + message = + "Bad Request. \n"), + @ApiResponse( + code = 406, + message = "Not Acceptable.\n The requested media type is not supported"), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Server error occurred while fetching the " + + "list of supported device types.", + response = ErrorResponse.class) + } + ) + Response getFilteredDevices( + @ApiParam(name = "type", value = "name of the device type", required = true) + @PathParam("type") String deviceType, + @ApiParam(name = "type", value = "name of the parameter", required = true) + @PathParam("type") String parameter, + @ApiParam(name = "limit", value = "minimum value the parameter can have", required = false) + @QueryParam("min") int min, + @ApiParam(name = "max", value = "max value the parameter can have", required = false) + @QueryParam("max") int max + ); + @GET @Path("/{type}") diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceEventManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceEventManagementServiceImpl.java index cba8879dcb2..e224cf3ae48 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceEventManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceEventManagementServiceImpl.java @@ -429,6 +429,61 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe } } + + + /** + * Returns the filterd device list. Devices are filterd using the paramter given. + * parameter can be given as a range or a single value. + */ + @GET + @Path("filter/{type}/{parameter}") + @Override + public Response getFilteredDevices( @PathParam("type") String deviceType, @PathParam("parameter") String parameter, + @QueryParam("min") int min,@QueryParam("max") int max) { + String query; + if (min != 0 & max != 0) { + query = "DISTINCT "+DEFAULT_META_DEVICE_ID_ATTRIBUTE + + " AND " + parameter + " : [" + min + " TO " + max + "]"; + } else if (min != 0 & max == 0) { + query = "DISTINCT "+DEFAULT_META_DEVICE_ID_ATTRIBUTE + + " AND " + parameter + " : [" + min + " TO " + max + "]"; + }else if(max != 0 & min==0){ + query = "DISTINCT "+DEFAULT_META_DEVICE_ID_ATTRIBUTE + + " AND " + parameter + " : [" + min + " TO " + max + "]"; + }else{ + String errorMessage = "One of the range values need to be given"; + log.error(errorMessage); + return Response.status(Response.Status.BAD_REQUEST).build(); + } + + String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); + String sensorTableName = getTableName(DeviceMgtAPIUtils.getStreamDefinition(deviceType, tenantDomain)); + try { + if (deviceType == null || + !DeviceMgtAPIUtils.getDeviceManagementService().getAvailableDeviceTypes().contains(deviceType)) { + String errorMessage = "Invalid device type"; + log.error(errorMessage); + return Response.status(Response.Status.BAD_REQUEST).build(); + } + + List sortByFields = new ArrayList<>(); + SortByField sortByField = new SortByField(TIMESTAMP_FIELD_NAME, SortType.DESC); + sortByFields.add(sortByField); + + EventRecords eventRecords = getAllEventsForDevice(sensorTableName, query, sortByFields, 0, 1); + return Response.status(Response.Status.OK.getStatusCode()).entity(eventRecords).build(); + + } catch (AnalyticsException e) { + String errorMsg = "Error on retrieving stats on table " + sensorTableName + " with query " + query; + log.error(errorMsg); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).entity(errorMsg).build(); + } catch (DeviceManagementException e) { + String errorMsg = "Error on retrieving stats on table " + sensorTableName + " with query " + query; + log.error(errorMsg); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).entity(errorMsg).build(); + } + } + private void publishEventReceivers(String streamNameWithVersion, TransportType transportType , String requestedTenantDomain, String deviceType) throws RemoteException, UserStoreException, JWTClientException { From 431342fd0f571123da0cc57ccc22b574c2fd1a56 Mon Sep 17 00:00:00 2001 From: lashanfaliq95 Date: Wed, 11 Jul 2018 19:51:12 +0530 Subject: [PATCH 2/7] filter implementation which gets all records --- .../DeviceEventManagementServiceImpl.java | 41 +++++++++++++------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceEventManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceEventManagementServiceImpl.java index e224cf3ae48..b166c7127b1 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceEventManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceEventManagementServiceImpl.java @@ -4,12 +4,15 @@ import org.apache.axis2.AxisFault; import org.apache.axis2.client.Stub; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.json.JSONArray; +import org.json.JSONObject; import org.wso2.carbon.analytics.api.AnalyticsDataAPI; import org.wso2.carbon.analytics.api.AnalyticsDataAPIUtil; import org.wso2.carbon.analytics.dataservice.commons.AnalyticsDataResponse; import org.wso2.carbon.analytics.dataservice.commons.SearchResultEntry; import org.wso2.carbon.analytics.dataservice.commons.SortByField; import org.wso2.carbon.analytics.dataservice.commons.SortType; +import org.wso2.carbon.analytics.datasource.commons.Record; import org.wso2.carbon.analytics.stream.persistence.stub .EventStreamPersistenceAdminServiceEventStreamPersistenceAdminServiceExceptionException; import org.wso2.carbon.analytics.stream.persistence.stub.EventStreamPersistenceAdminServiceStub; @@ -53,7 +56,9 @@ import javax.ws.rs.QueryParam; import javax.ws.rs.core.Response; import java.rmi.RemoteException; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; /** * This is used for device type integration with DAS, to create streams and receiver dynamically and a common endpoint @@ -430,7 +435,6 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe } - /** * Returns the filterd device list. Devices are filterd using the paramter given. * parameter can be given as a range or a single value. @@ -438,19 +442,16 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe @GET @Path("filter/{type}/{parameter}") @Override - public Response getFilteredDevices( @PathParam("type") String deviceType, @PathParam("parameter") String parameter, - @QueryParam("min") int min,@QueryParam("max") int max) { + public Response getFilteredDevices(@PathParam("type") String deviceType, @PathParam("parameter") String parameter, + @QueryParam("min") int min, @QueryParam("max") int max) { String query; if (min != 0 & max != 0) { - query = "DISTINCT "+DEFAULT_META_DEVICE_ID_ATTRIBUTE - + " AND " + parameter + " : [" + min + " TO " + max + "]"; + query = parameter + " : [" + min + " TO " + max + "]"; } else if (min != 0 & max == 0) { - query = "DISTINCT "+DEFAULT_META_DEVICE_ID_ATTRIBUTE - + " AND " + parameter + " : [" + min + " TO " + max + "]"; - }else if(max != 0 & min==0){ - query = "DISTINCT "+DEFAULT_META_DEVICE_ID_ATTRIBUTE - + " AND " + parameter + " : [" + min + " TO " + max + "]"; - }else{ + query = parameter + " : [" + min + " TO " + max + "]"; + } else if (max != 0 & min == 0) { + query = parameter + " : [" + min + " TO " + max + "]"; + } else { String errorMessage = "One of the range values need to be given"; log.error(errorMessage); return Response.status(Response.Status.BAD_REQUEST).build(); @@ -471,7 +472,23 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe sortByFields.add(sortByField); EventRecords eventRecords = getAllEventsForDevice(sensorTableName, query, sortByFields, 0, 1); - return Response.status(Response.Status.OK.getStatusCode()).entity(eventRecords).build(); + + List filterdEvents = eventRecords.getRecord(); + List uniqueFilterdEvents = new ArrayList(); + Set devices = new HashSet<>(); + + for (int i = 0; i < filterdEvents.size(); i++) { + + String deviceid = (String) filterdEvents.get(i).getValue("meta_deviceId"); + if (!devices.contains(deviceid)) { + devices.add(deviceid); + uniqueFilterdEvents.add(filterdEvents.get(i)); + } + } + + EventRecords filterdRecords=new EventRecords(); + filterdRecords.setList(uniqueFilterdEvents); + return Response.status(Response.Status.OK.getStatusCode()).entity(filterdRecords).build(); } catch (AnalyticsException e) { String errorMsg = "Error on retrieving stats on table " + sensorTableName + " with query " + query; From e1ffd6341de51a35dbb7ebd44de60e48289595de Mon Sep 17 00:00:00 2001 From: lashanfaliq95 Date: Fri, 13 Jul 2018 17:50:39 +0530 Subject: [PATCH 3/7] filter implementation which gets all records and based on the timestamp --- .../api/DeviceEventManagementService.java | 4 ++-- .../DeviceEventManagementServiceImpl.java | 20 +++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceEventManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceEventManagementService.java index 449df8e8f5a..26904a9cdff 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceEventManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceEventManagementService.java @@ -352,9 +352,9 @@ public interface DeviceEventManagementService { @ApiParam(name = "type", value = "name of the parameter", required = true) @PathParam("type") String parameter, @ApiParam(name = "limit", value = "minimum value the parameter can have", required = false) - @QueryParam("min") int min, + @QueryParam("min") double min, @ApiParam(name = "max", value = "max value the parameter can have", required = false) - @QueryParam("max") int max + @QueryParam("max") double max ); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceEventManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceEventManagementServiceImpl.java index b166c7127b1..ed855767130 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceEventManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceEventManagementServiceImpl.java @@ -55,10 +55,7 @@ import javax.ws.rs.PathParam; import javax.ws.rs.QueryParam; import javax.ws.rs.core.Response; import java.rmi.RemoteException; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; +import java.util.*; /** * This is used for device type integration with DAS, to create streams and receiver dynamically and a common endpoint @@ -443,14 +440,14 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe @Path("filter/{type}/{parameter}") @Override public Response getFilteredDevices(@PathParam("type") String deviceType, @PathParam("parameter") String parameter, - @QueryParam("min") int min, @QueryParam("max") int max) { + @QueryParam("min") double min, @QueryParam("max") double max) { String query; if (min != 0 & max != 0) { query = parameter + " : [" + min + " TO " + max + "]"; } else if (min != 0 & max == 0) { - query = parameter + " : [" + min + " TO " + max + "]"; + query = parameter + " : [ " + min + " TO *]"; } else if (max != 0 & min == 0) { - query = parameter + " : [" + min + " TO " + max + "]"; + query = parameter + " : [* TO " + max + "]"; } else { String errorMessage = "One of the range values need to be given"; log.error(errorMessage); @@ -471,7 +468,7 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe SortByField sortByField = new SortByField(TIMESTAMP_FIELD_NAME, SortType.DESC); sortByFields.add(sortByField); - EventRecords eventRecords = getAllEventsForDevice(sensorTableName, query, sortByFields, 0, 1); + EventRecords eventRecords = getAllEventsForDevice(sensorTableName, query, sortByFields, 0, 100); List filterdEvents = eventRecords.getRecord(); List uniqueFilterdEvents = new ArrayList(); @@ -480,13 +477,16 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe for (int i = 0; i < filterdEvents.size(); i++) { String deviceid = (String) filterdEvents.get(i).getValue("meta_deviceId"); - if (!devices.contains(deviceid)) { + long timestamp=(long).filterdEvents.get(i).getTimestamp(); + Calendar c = java.util.Calendar.getInstance(); + long currentTimestamp = c.getTimeInMillis(); + if (!devices.contains(deviceid) && (currentTimestamp-timestamp<=300*1000)) { devices.add(deviceid); uniqueFilterdEvents.add(filterdEvents.get(i)); } } - EventRecords filterdRecords=new EventRecords(); + EventRecords filterdRecords = new EventRecords(); filterdRecords.setList(uniqueFilterdEvents); return Response.status(Response.Status.OK.getStatusCode()).entity(filterdRecords).build(); From 3a7136abf90c2f066b86f97d62aa40f7059eeb42 Mon Sep 17 00:00:00 2001 From: lashanfaliq95 Date: Sun, 15 Jul 2018 10:58:09 +0530 Subject: [PATCH 4/7] complete the event filter --- .../impl/DeviceEventManagementServiceImpl.java | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceEventManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceEventManagementServiceImpl.java index ed855767130..7a627ba9cc4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceEventManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceEventManagementServiceImpl.java @@ -444,12 +444,8 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe String query; if (min != 0 & max != 0) { query = parameter + " : [" + min + " TO " + max + "]"; - } else if (min != 0 & max == 0) { - query = parameter + " : [ " + min + " TO *]"; - } else if (max != 0 & min == 0) { - query = parameter + " : [* TO " + max + "]"; - } else { - String errorMessage = "One of the range values need to be given"; + } else { + String errorMessage = "The of range values need to be given"; log.error(errorMessage); return Response.status(Response.Status.BAD_REQUEST).build(); } @@ -467,17 +463,14 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe List sortByFields = new ArrayList<>(); SortByField sortByField = new SortByField(TIMESTAMP_FIELD_NAME, SortType.DESC); sortByFields.add(sortByField); - EventRecords eventRecords = getAllEventsForDevice(sensorTableName, query, sortByFields, 0, 100); - List filterdEvents = eventRecords.getRecord(); List uniqueFilterdEvents = new ArrayList(); Set devices = new HashSet<>(); for (int i = 0; i < filterdEvents.size(); i++) { - String deviceid = (String) filterdEvents.get(i).getValue("meta_deviceId"); - long timestamp=(long).filterdEvents.get(i).getTimestamp(); + long timestamp=(long)filterdEvents.get(i).getTimestamp(); Calendar c = java.util.Calendar.getInstance(); long currentTimestamp = c.getTimeInMillis(); if (!devices.contains(deviceid) && (currentTimestamp-timestamp<=300*1000)) { From c8d0b10a38b312b07f3efd828b470ff4cc84930b Mon Sep 17 00:00:00 2001 From: lashanfaliq95 Date: Mon, 16 Jul 2018 18:08:52 +0530 Subject: [PATCH 5/7] update the device filter api --- .../DeviceEventManagementServiceImpl.java | 46 +++++++++---------- 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceEventManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceEventManagementServiceImpl.java index 7a627ba9cc4..3f48f898e45 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceEventManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceEventManagementServiceImpl.java @@ -4,8 +4,6 @@ import org.apache.axis2.AxisFault; import org.apache.axis2.client.Stub; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.json.JSONArray; -import org.json.JSONObject; import org.wso2.carbon.analytics.api.AnalyticsDataAPI; import org.wso2.carbon.analytics.api.AnalyticsDataAPIUtil; import org.wso2.carbon.analytics.dataservice.commons.AnalyticsDataResponse; @@ -13,8 +11,8 @@ import org.wso2.carbon.analytics.dataservice.commons.SearchResultEntry; import org.wso2.carbon.analytics.dataservice.commons.SortByField; import org.wso2.carbon.analytics.dataservice.commons.SortType; import org.wso2.carbon.analytics.datasource.commons.Record; -import org.wso2.carbon.analytics.stream.persistence.stub - .EventStreamPersistenceAdminServiceEventStreamPersistenceAdminServiceExceptionException; +import org.wso2.carbon.analytics.datasource.commons.exception.AnalyticsException; +import org.wso2.carbon.analytics.stream.persistence.stub.EventStreamPersistenceAdminServiceEventStreamPersistenceAdminServiceExceptionException; import org.wso2.carbon.analytics.stream.persistence.stub.EventStreamPersistenceAdminServiceStub; import org.wso2.carbon.analytics.stream.persistence.stub.dto.AnalyticsTable; import org.wso2.carbon.analytics.stream.persistence.stub.dto.AnalyticsTableRecord; @@ -24,12 +22,7 @@ import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException; -import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.DeviceTypeEvent; -import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.EventRecords; -import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.Attribute; -import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.AttributeType; -import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.EventAttributeList; -import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.TransportType; +import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.*; import org.wso2.carbon.device.mgt.jaxrs.service.api.DeviceEventManagementService; import org.wso2.carbon.device.mgt.jaxrs.util.Constants; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; @@ -44,15 +37,9 @@ import org.wso2.carbon.event.stream.stub.types.EventStreamAttributeDto; import org.wso2.carbon.event.stream.stub.types.EventStreamDefinitionDto; import org.wso2.carbon.identity.jwt.client.extension.exception.JWTClientException; import org.wso2.carbon.user.api.UserStoreException; -import org.wso2.carbon.analytics.datasource.commons.exception.AnalyticsException; import javax.validation.Valid; -import javax.ws.rs.DELETE; -import javax.ws.rs.GET; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.PathParam; -import javax.ws.rs.QueryParam; +import javax.ws.rs.*; import javax.ws.rs.core.Response; import java.rmi.RemoteException; import java.util.*; @@ -433,8 +420,8 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe /** - * Returns the filterd device list. Devices are filterd using the paramter given. - * parameter can be given as a range or a single value. + * Returns the filterd device list. Devices are filterd using the paramter given and the timestamp of the record. + * parameter should given as a range. */ @GET @Path("filter/{type}/{parameter}") @@ -442,9 +429,15 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe public Response getFilteredDevices(@PathParam("type") String deviceType, @PathParam("parameter") String parameter, @QueryParam("min") double min, @QueryParam("max") double max) { String query; + Calendar c = java.util.Calendar.getInstance(); + long currentTimestamp = c.getTimeInMillis(); + long previousTimestamp = currentTimestamp - 300 * 1000; + String fromDate = String.valueOf(previousTimestamp); + String toDate = String.valueOf(currentTimestamp); if (min != 0 & max != 0) { - query = parameter + " : [" + min + " TO " + max + "]"; - } else { + query = parameter + " : [" + min + " TO " + max + "]" + + " AND _timestamp : [" + fromDate + " TO " + toDate + "]"; + } else { String errorMessage = "The of range values need to be given"; log.error(errorMessage); return Response.status(Response.Status.BAD_REQUEST).build(); @@ -470,10 +463,8 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe for (int i = 0; i < filterdEvents.size(); i++) { String deviceid = (String) filterdEvents.get(i).getValue("meta_deviceId"); - long timestamp=(long)filterdEvents.get(i).getTimestamp(); - Calendar c = java.util.Calendar.getInstance(); - long currentTimestamp = c.getTimeInMillis(); - if (!devices.contains(deviceid) && (currentTimestamp-timestamp<=300*1000)) { + if (!devices.contains(deviceid) && DeviceMgtAPIUtils.getDeviceAccessAuthorizationService().isUserAuthorized( + new DeviceIdentifier(deviceid, deviceType))) { devices.add(deviceid); uniqueFilterdEvents.add(filterdEvents.get(i)); } @@ -491,9 +482,13 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe String errorMsg = "Error on retrieving stats on table " + sensorTableName + " with query " + query; log.error(errorMsg); return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).entity(errorMsg).build(); + } catch (DeviceAccessAuthorizationException e) { + e.printStackTrace(); + return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build(); } } + private void publishEventReceivers(String streamNameWithVersion, TransportType transportType , String requestedTenantDomain, String deviceType) throws RemoteException, UserStoreException, JWTClientException { @@ -655,6 +650,7 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe return deviceType.replace(" ", "_").trim() + "-" + tenantDomain + "-" + transportType.toString() + "-receiver"; } + private void cleanup(Stub stub) { if (stub != null) { try { From 1fedf9650ca24c691abd02e2fed9178b9e1e48e2 Mon Sep 17 00:00:00 2001 From: lashanfaliq95 Date: Fri, 20 Jul 2018 10:05:18 +0530 Subject: [PATCH 6/7] remove wildcard imports and log the error msg in filter api --- .../DeviceEventManagementServiceImpl.java | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceEventManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceEventManagementServiceImpl.java index 3f48f898e45..277d9b74aab 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceEventManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceEventManagementServiceImpl.java @@ -22,7 +22,12 @@ import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException; -import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.*; +import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.Attribute; +import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.AttributeType; +import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.DeviceTypeEvent; +import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.EventAttributeList; +import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.EventRecords; +import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.TransportType; import org.wso2.carbon.device.mgt.jaxrs.service.api.DeviceEventManagementService; import org.wso2.carbon.device.mgt.jaxrs.util.Constants; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; @@ -39,10 +44,20 @@ import org.wso2.carbon.identity.jwt.client.extension.exception.JWTClientExceptio import org.wso2.carbon.user.api.UserStoreException; import javax.validation.Valid; -import javax.ws.rs.*; +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.QueryParam; import javax.ws.rs.core.Response; import java.rmi.RemoteException; -import java.util.*; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + /** * This is used for device type integration with DAS, to create streams and receiver dynamically and a common endpoint @@ -483,7 +498,9 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe log.error(errorMsg); return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).entity(errorMsg).build(); } catch (DeviceAccessAuthorizationException e) { + String errorMsg = "Error on retrieving stats on table " + sensorTableName + " with query " + query; e.printStackTrace(); + log.error(errorMsg); return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build(); } } From d843b71e1f571033a240b9db9416286a71221b21 Mon Sep 17 00:00:00 2001 From: lashanfaliq95 Date: Fri, 20 Jul 2018 16:15:07 +0530 Subject: [PATCH 7/7] remove stack trace --- .../mgt/jaxrs/service/impl/DeviceEventManagementServiceImpl.java | 1 - 1 file changed, 1 deletion(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceEventManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceEventManagementServiceImpl.java index 277d9b74aab..f9faa784f1f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceEventManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceEventManagementServiceImpl.java @@ -499,7 +499,6 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).entity(errorMsg).build(); } catch (DeviceAccessAuthorizationException e) { String errorMsg = "Error on retrieving stats on table " + sensorTableName + " with query " + query; - e.printStackTrace(); log.error(errorMsg); return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build(); }