From 65cfca7024eb6984966b623404b2aa369a2aad8d Mon Sep 17 00:00:00 2001 From: lashanfaliq95 Date: Fri, 23 Feb 2018 09:00:18 +0530 Subject: [PATCH] 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 {