filter implementation which gets all records

4.x.x
lashanfaliq95 6 years ago
parent c6a1508ee7
commit 431342fd0f

@ -4,12 +4,15 @@ import org.apache.axis2.AxisFault;
import org.apache.axis2.client.Stub; import org.apache.axis2.client.Stub;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; 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.AnalyticsDataAPI;
import org.wso2.carbon.analytics.api.AnalyticsDataAPIUtil; import org.wso2.carbon.analytics.api.AnalyticsDataAPIUtil;
import org.wso2.carbon.analytics.dataservice.commons.AnalyticsDataResponse; import org.wso2.carbon.analytics.dataservice.commons.AnalyticsDataResponse;
import org.wso2.carbon.analytics.dataservice.commons.SearchResultEntry; import org.wso2.carbon.analytics.dataservice.commons.SearchResultEntry;
import org.wso2.carbon.analytics.dataservice.commons.SortByField; import org.wso2.carbon.analytics.dataservice.commons.SortByField;
import org.wso2.carbon.analytics.dataservice.commons.SortType; import org.wso2.carbon.analytics.dataservice.commons.SortType;
import org.wso2.carbon.analytics.datasource.commons.Record;
import org.wso2.carbon.analytics.stream.persistence.stub import org.wso2.carbon.analytics.stream.persistence.stub
.EventStreamPersistenceAdminServiceEventStreamPersistenceAdminServiceExceptionException; .EventStreamPersistenceAdminServiceEventStreamPersistenceAdminServiceExceptionException;
import org.wso2.carbon.analytics.stream.persistence.stub.EventStreamPersistenceAdminServiceStub; 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 javax.ws.rs.core.Response;
import java.rmi.RemoteException; import java.rmi.RemoteException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import java.util.List; 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 * 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. * Returns the filterd device list. Devices are filterd using the paramter given.
* parameter can be given as a range or a single value. * parameter can be given as a range or a single value.
@ -438,19 +442,16 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe
@GET @GET
@Path("filter/{type}/{parameter}") @Path("filter/{type}/{parameter}")
@Override @Override
public Response getFilteredDevices( @PathParam("type") String deviceType, @PathParam("parameter") String parameter, public Response getFilteredDevices(@PathParam("type") String deviceType, @PathParam("parameter") String parameter,
@QueryParam("min") int min,@QueryParam("max") int max) { @QueryParam("min") int min, @QueryParam("max") int max) {
String query; String query;
if (min != 0 & max != 0) { if (min != 0 & max != 0) {
query = "DISTINCT "+DEFAULT_META_DEVICE_ID_ATTRIBUTE query = parameter + " : [" + min + " TO " + max + "]";
+ " AND " + parameter + " : [" + min + " TO " + max + "]";
} else if (min != 0 & max == 0) { } else if (min != 0 & max == 0) {
query = "DISTINCT "+DEFAULT_META_DEVICE_ID_ATTRIBUTE query = parameter + " : [" + min + " TO " + max + "]";
+ " AND " + parameter + " : [" + min + " TO " + max + "]"; } else if (max != 0 & min == 0) {
}else if(max != 0 & min==0){ query = parameter + " : [" + min + " TO " + max + "]";
query = "DISTINCT "+DEFAULT_META_DEVICE_ID_ATTRIBUTE } else {
+ " AND " + parameter + " : [" + min + " TO " + max + "]";
}else{
String errorMessage = "One of the range values need to be given"; String errorMessage = "One of the range values need to be given";
log.error(errorMessage); log.error(errorMessage);
return Response.status(Response.Status.BAD_REQUEST).build(); return Response.status(Response.Status.BAD_REQUEST).build();
@ -471,7 +472,23 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe
sortByFields.add(sortByField); sortByFields.add(sortByField);
EventRecords eventRecords = getAllEventsForDevice(sensorTableName, query, sortByFields, 0, 1); EventRecords eventRecords = getAllEventsForDevice(sensorTableName, query, sortByFields, 0, 1);
return Response.status(Response.Status.OK.getStatusCode()).entity(eventRecords).build();
List<Record> filterdEvents = eventRecords.getRecord();
List<Record> uniqueFilterdEvents = new ArrayList<Record>();
Set<String> 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) { } catch (AnalyticsException e) {
String errorMsg = "Error on retrieving stats on table " + sensorTableName + " with query " + query; String errorMsg = "Error on retrieving stats on table " + sensorTableName + " with query " + query;

Loading…
Cancel
Save