From 65cfca7024eb6984966b623404b2aa369a2aad8d Mon Sep 17 00:00:00 2001 From: lashanfaliq95 Date: Fri, 23 Feb 2018 09:00:18 +0530 Subject: [PATCH 1/4] Updated the last-known api to be able to return a number of records. --- .../service/api/DeviceEventManagementService.java | 6 +++++- .../impl/DeviceEventManagementServiceImpl.java | 12 +++++++++--- 2 files changed, 14 insertions(+), 4 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 8b6d7ed030..f4fcadba01 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 @@ -290,7 +290,11 @@ public interface DeviceEventManagementService { Response getLastKnownData(@ApiParam(name = "deviceId", value = "id of the device ", required = false) @PathParam("deviceId") String deviceId, @ApiParam(name = "type", value = "name of the device type", required = false) - @PathParam("type") String deviceType); + @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("/{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 5a16fbccdb..0b9827372d 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 @@ -340,12 +340,12 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe } /** - * Returns the last know data point of the device type. + * Returns the last know data point of the device type or last known data points upto the limit. */ @GET @Path("/last-known/{type}/{deviceId}") @Override - public Response getLastKnownData(@PathParam("deviceId") String deviceId, @PathParam("type") String deviceType) { + public Response getLastKnownData(@PathParam("deviceId") String deviceId, @PathParam("type") String deviceType,@QueryParam("limit") int limit) { String query = DEFAULT_META_DEVICE_ID_ATTRIBUTE + ":" + deviceId; String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); String sensorTableName = getTableName(DeviceMgtAPIUtils.getStreamDefinition(deviceType, tenantDomain)); @@ -363,8 +363,13 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe List sortByFields = new ArrayList<>(); SortByField sortByField = new SortByField(TIMESTAMP_FIELD_NAME, SortType.DESC); sortByFields.add(sortByField); + if(limit == 0){ EventRecords eventRecords = getAllEventsForDevice(sensorTableName, query, sortByFields, 0, 1); - return Response.status(Response.Status.OK.getStatusCode()).entity(eventRecords).build(); + return Response.status(Response.Status.OK.getStatusCode()).entity(eventRecords).build();} + else{ + EventRecords eventRecords = getAllEventsForDevice(sensorTableName, query, sortByFields, 0, limit); + 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); @@ -379,6 +384,7 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe } } + private void publishEventReceivers(String streamNameWithVersion, TransportType transportType , String requestedTenantDomain, String deviceType) throws RemoteException, UserStoreException, JWTClientException { From 7a1c17081b63623089627fc2719810f75d5acc5f Mon Sep 17 00:00:00 2001 From: lashanfaliq95 Date: Fri, 23 Feb 2018 18:15:15 +0530 Subject: [PATCH 2/4] fixed the issues --- .../api/DeviceEventManagementService.java | 14 +-- .../DeviceEventManagementServiceImpl.java | 105 +++++++++--------- 2 files changed, 60 insertions(+), 59 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 f4fcadba01..f2413ec828 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 @@ -288,13 +288,11 @@ public interface DeviceEventManagementService { } ) Response getLastKnownData(@ApiParam(name = "deviceId", value = "id of the device ", required = false) - @PathParam("deviceId") String deviceId, - @ApiParam(name = "type", value = "name of the device type", required = false) - @PathParam("type") String deviceType, - @ApiParam(name = "limit", value = "limit of the records that needs to be picked up", required = false) - @QueryParam("limit") int limit); - - + @PathParam("deviceId") String deviceId, + @ApiParam(name = "type", value = "name of the device type", required = false) + @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("/{type}") @@ -348,4 +346,4 @@ public interface DeviceEventManagementService { Response getDeviceTypeEventDefinition(@ApiParam(name = "type", value = "name of the device type", required = false) @PathParam("type")String deviceType) ; -} +} \ No newline at end of file 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 0b9827372d..e2c3c5642e 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 @@ -76,8 +76,47 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe private static final String MQTT_CONTENT_VALIDATOR = "default"; private static final String TIMESTAMP_FIELD_NAME = "_timestamp"; + private static AnalyticsDataAPI getAnalyticsDataAPI() { + PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + AnalyticsDataAPI analyticsDataAPI = + (AnalyticsDataAPI) ctx.getOSGiService(AnalyticsDataAPI.class, null); + if (analyticsDataAPI == null) { + String msg = "Analytics api service has not initialized."; + log.error(msg); + throw new IllegalStateException(msg); + } + return analyticsDataAPI; + } + + private static EventRecords getAllEventsForDevice(String tableName, String query, List sortByFields + , int offset, int limit) throws AnalyticsException { + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + AnalyticsDataAPI analyticsDataAPI = getAnalyticsDataAPI(); + EventRecords eventRecords = new EventRecords(); + int eventCount = analyticsDataAPI.searchCount(tenantId, tableName, query); + if (eventCount == 0) { + eventRecords.setCount(0); + } + List resultEntries = analyticsDataAPI.search(tenantId, tableName, query, offset, limit, + sortByFields); + List recordIds = getRecordIds(resultEntries); + AnalyticsDataResponse response = analyticsDataAPI.get(tenantId, tableName, 1, null, recordIds); + eventRecords.setCount(eventCount); + eventRecords.setList(AnalyticsDataAPIUtil.listRecords(analyticsDataAPI, response)); + return eventRecords; + } + + private static List getRecordIds(List searchResults) { + List ids = new ArrayList<>(); + for (SearchResultEntry searchResult : searchResults) { + ids.add(searchResult.getId()); + } + return ids; + } + /** * Retrieves the stream definition from das for the given device type. + * * @return dynamic event attribute list */ @GET @@ -196,7 +235,7 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); } catch (EventStreamPersistenceAdminServiceEventStreamPersistenceAdminServiceExceptionException e) { log.error("Failed to create event store for, tenantDomain: " + tenantDomain + " deviceType" + deviceType, - e); + e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); } } @@ -229,9 +268,11 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe } eventStreamAdminServiceStub.removeEventStreamDefinition(streamName, Constants.DEFAULT_STREAM_VERSION); EventReceiverAdminServiceCallbackHandler eventReceiverAdminServiceCallbackHandler = - new EventReceiverAdminServiceCallbackHandler() {}; + new EventReceiverAdminServiceCallbackHandler() { + }; EventPublisherAdminServiceCallbackHandler eventPublisherAdminServiceCallbackHandler = - new EventPublisherAdminServiceCallbackHandler() {}; + new EventPublisherAdminServiceCallbackHandler() { + }; String eventReceiverName = getReceiverName(deviceType, tenantDomain, TransportType.MQTT); @@ -254,7 +295,7 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe tenantBasedEventReceiverAdminServiceStub = DeviceMgtAPIUtils.getEventReceiverAdminServiceStub(); tenantBasedEventStreamAdminServiceStub = DeviceMgtAPIUtils.getEventStreamAdminServiceStub(); tenantBasedEventStreamAdminServiceStub.removeEventStreamDefinition(streamName, - Constants.DEFAULT_STREAM_VERSION); + Constants.DEFAULT_STREAM_VERSION); tenantBasedEventReceiverAdminServiceStub.startundeployInactiveEventReceiverConfiguration( eventReceiverName, eventReceiverAdminServiceCallbackHandler); @@ -298,7 +339,7 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe @Override public Response getData(@PathParam("deviceId") String deviceId, @QueryParam("from") long from, @QueryParam("to") long to, @PathParam("type") String deviceType, @QueryParam("offset") - int offset, @QueryParam("limit") int limit) { + int offset, @QueryParam("limit") int limit) { if (from == 0 || to == 0) { String errorMessage = "Invalid values for from/to"; return Response.status(Response.Status.BAD_REQUEST).entity(errorMessage).build(); @@ -345,7 +386,7 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe @GET @Path("/last-known/{type}/{deviceId}") @Override - public Response getLastKnownData(@PathParam("deviceId") String deviceId, @PathParam("type") String deviceType,@QueryParam("limit") int limit) { + public Response getLastKnownData(@PathParam("deviceId") String deviceId, @PathParam("type") String deviceType, @QueryParam("limit") int limit) { String query = DEFAULT_META_DEVICE_ID_ATTRIBUTE + ":" + deviceId; String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); String sensorTableName = getTableName(DeviceMgtAPIUtils.getStreamDefinition(deviceType, tenantDomain)); @@ -363,10 +404,10 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe List sortByFields = new ArrayList<>(); SortByField sortByField = new SortByField(TIMESTAMP_FIELD_NAME, SortType.DESC); sortByFields.add(sortByField); - if(limit == 0){ - EventRecords eventRecords = getAllEventsForDevice(sensorTableName, query, sortByFields, 0, 1); - return Response.status(Response.Status.OK.getStatusCode()).entity(eventRecords).build();} - else{ + if (limit == 0) { + EventRecords eventRecords = getAllEventsForDevice(sensorTableName, query, sortByFields, 0, 1); + return Response.status(Response.Status.OK.getStatusCode()).entity(eventRecords).build(); + } else { EventRecords eventRecords = getAllEventsForDevice(sensorTableName, query, sortByFields, 0, limit); return Response.status(Response.Status.OK.getStatusCode()).entity(eventRecords).build(); } @@ -384,7 +425,6 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe } } - private void publishEventReceivers(String streamNameWithVersion, TransportType transportType , String requestedTenantDomain, String deviceType) throws RemoteException, UserStoreException, JWTClientException { @@ -399,7 +439,8 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe .getActiveEventReceiverConfiguration(eventRecieverNameTobeRemoved); if (eventReceiverConfigurationDto != null) { EventReceiverAdminServiceCallbackHandler eventReceiverAdminServiceCallbackHandler = - new EventReceiverAdminServiceCallbackHandler() {}; + new EventReceiverAdminServiceCallbackHandler() { + }; receiverAdminServiceStub.startundeployActiveEventReceiverConfiguration(eventRecieverNameTobeRemoved , eventReceiverAdminServiceCallbackHandler); } @@ -474,7 +515,7 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe private void publishEventStore(String streamName, String version, EventAttributeList eventAttributes) throws RemoteException, UserStoreException, JWTClientException, - EventStreamPersistenceAdminServiceEventStreamPersistenceAdminServiceExceptionException { + EventStreamPersistenceAdminServiceEventStreamPersistenceAdminServiceExceptionException { EventStreamPersistenceAdminServiceStub eventStreamPersistenceAdminServiceStub = DeviceMgtAPIUtils.getEventStreamPersistenceAdminServiceStub(); try { @@ -545,44 +586,6 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe return deviceType.replace(" ", "_").trim() + "-" + tenantDomain + "-" + transportType.toString() + "-receiver"; } - private static AnalyticsDataAPI getAnalyticsDataAPI() { - PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); - AnalyticsDataAPI analyticsDataAPI = - (AnalyticsDataAPI) ctx.getOSGiService(AnalyticsDataAPI.class, null); - if (analyticsDataAPI == null) { - String msg = "Analytics api service has not initialized."; - log.error(msg); - throw new IllegalStateException(msg); - } - return analyticsDataAPI; - } - - private static EventRecords getAllEventsForDevice(String tableName, String query, List sortByFields - , int offset, int limit) throws AnalyticsException { - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - AnalyticsDataAPI analyticsDataAPI = getAnalyticsDataAPI(); - EventRecords eventRecords = new EventRecords(); - int eventCount = analyticsDataAPI.searchCount(tenantId, tableName, query); - if (eventCount == 0) { - eventRecords.setCount(0); - } - List resultEntries = analyticsDataAPI.search(tenantId, tableName, query, offset, limit, - sortByFields); - List recordIds = getRecordIds(resultEntries); - AnalyticsDataResponse response = analyticsDataAPI.get(tenantId, tableName, 1, null, recordIds); - eventRecords.setCount(eventCount); - eventRecords.setList(AnalyticsDataAPIUtil.listRecords(analyticsDataAPI, response)); - return eventRecords; - } - - private static List getRecordIds(List searchResults) { - List ids = new ArrayList<>(); - for (SearchResultEntry searchResult : searchResults) { - ids.add(searchResult.getId()); - } - return ids; - } - private void cleanup(Stub stub) { if (stub != null) { try { From b4f78337fadbe7b6b08e081e5f02e19d4ec3f2b6 Mon Sep 17 00:00:00 2001 From: lashanfaliq95 Date: Wed, 14 Mar 2018 13:49:11 +0530 Subject: [PATCH 3/4] last-known api updated the comment of the api and handled the error when a negative value is passed as the limit. --- .../service/impl/DeviceEventManagementServiceImpl.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 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 e2c3c5642e..1d1482899c 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 @@ -381,7 +381,8 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe } /** - * Returns the last know data point of the device type or last known data points upto the limit. + * Returns last known data points up to the limit if limit is specified. Otherwise returns last known data point. + * Limit need to be zero or positive. */ @GET @Path("/last-known/{type}/{deviceId}") @@ -407,10 +408,14 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe if (limit == 0) { EventRecords eventRecords = getAllEventsForDevice(sensorTableName, query, sortByFields, 0, 1); return Response.status(Response.Status.OK.getStatusCode()).entity(eventRecords).build(); - } else { + } else if(limit>0){ EventRecords eventRecords = getAllEventsForDevice(sensorTableName, query, sortByFields, 0, limit); return Response.status(Response.Status.OK.getStatusCode()).entity(eventRecords).build(); } + else{ + String errorMessage = "Invalid limit value"; + return Response.status(Response.Status.BAD_REQUEST).entity(errorMessage).build(); + } } catch (AnalyticsException e) { String errorMsg = "Error on retrieving stats on table " + sensorTableName + " with query " + query; log.error(errorMsg); From d60b88504528e3187d074146a8cf05434275ad6b Mon Sep 17 00:00:00 2001 From: lashanfaliq95 Date: Wed, 14 Mar 2018 14:13:56 +0530 Subject: [PATCH 4/4] changed the parameters "type" and "deviceId" as required for the last-know api. --- .../jaxrs/service/api/DeviceEventManagementService.java | 4 ++-- .../service/impl/DeviceEventManagementServiceImpl.java | 7 +++---- 2 files changed, 5 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/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 2a29f962af..59e7e2e11b 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 @@ -290,9 +290,9 @@ public interface DeviceEventManagementService { response = ErrorResponse.class) } ) - Response getLastKnownData(@ApiParam(name = "deviceId", value = "id of the device ", required = false) + Response getLastKnownData(@ApiParam(name = "deviceId", value = "id of the device ", required = true) @PathParam("deviceId") String deviceId, - @ApiParam(name = "type", value = "name of the device type", required = false) + @ApiParam(name = "type", value = "name of the device type", required = true) @PathParam("type") String deviceType, @ApiParam(name = "limit", value = "limit of the records that needs to be picked up", required = false) @QueryParam("limit") int limit); 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 1d1482899c..cba8879dcb 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 @@ -382,7 +382,7 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe /** * Returns last known data points up to the limit if limit is specified. Otherwise returns last known data point. - * Limit need to be zero or positive. + * Limit parameter needs to be zero or positive. */ @GET @Path("/last-known/{type}/{deviceId}") @@ -408,11 +408,10 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe if (limit == 0) { EventRecords eventRecords = getAllEventsForDevice(sensorTableName, query, sortByFields, 0, 1); return Response.status(Response.Status.OK.getStatusCode()).entity(eventRecords).build(); - } else if(limit>0){ + } else if (limit > 0) { EventRecords eventRecords = getAllEventsForDevice(sensorTableName, query, sortByFields, 0, limit); return Response.status(Response.Status.OK.getStatusCode()).entity(eventRecords).build(); - } - else{ + } else { String errorMessage = "Invalid limit value"; return Response.status(Response.Status.BAD_REQUEST).entity(errorMessage).build(); }