Search in sources :

Example 1 with AnalyticsException

use of org.wso2.carbon.analytics.datasource.commons.exception.AnalyticsException in project product-iots by wso2.

the class ConnectedCupServiceImpl method getDeviceStats.

@Path("stats/{deviceId}/sensors/{sensorName}")
@GET
@Consumes("application/json")
@Produces("application/json")
public Response getDeviceStats(@PathParam("deviceId") String deviceId, @PathParam("sensorName") String sensor, @QueryParam("from") long from, @QueryParam("to") long to) {
    String fromDate = String.valueOf(from);
    String toDate = String.valueOf(to);
    String query = " deviceId:" + deviceId + " AND deviceType:" + ConnectedCupConstants.DEVICE_TYPE + " AND time : [" + fromDate + " TO " + toDate + "]";
    String sensorTableName = getSensorEventTableName(sensor);
    try {
        if (!APIUtil.getDeviceAccessAuthorizationService().isUserAuthorized(new DeviceIdentifier(deviceId, ConnectedCupConstants.DEVICE_TYPE), DeviceGroupConstants.Permissions.DEFAULT_STATS_MONITOR_PERMISSIONS)) {
            return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build();
        }
        List<SensorRecord> sensorDatas;
        List<SortByField> sortByFields = new ArrayList<>();
        SortByField sortByField = new SortByField("time", SortType.ASC);
        sortByFields.add(sortByField);
        sensorDatas = APIUtil.getAllEventsForDevice(sensorTableName, query, sortByFields);
        return Response.ok().entity(sensorDatas).build();
    } catch (AnalyticsException e) {
        String errorMsg = "Error on retrieving stats on table " + sensorTableName + " with query " + query;
        log.error(errorMsg, e);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).entity(errorMsg).build();
    } catch (DeviceAccessAuthorizationException e) {
        log.error(e.getErrorMessage(), e);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
    }
}
Also used : SortByField(org.wso2.carbon.analytics.dataservice.commons.SortByField) DeviceAccessAuthorizationException(org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException) AnalyticsException(org.wso2.carbon.analytics.datasource.commons.exception.AnalyticsException) SensorRecord(org.coffeeking.api.util.SensorRecord) ArrayList(java.util.ArrayList) DeviceIdentifier(org.wso2.carbon.device.mgt.common.DeviceIdentifier) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 2 with AnalyticsException

use of org.wso2.carbon.analytics.datasource.commons.exception.AnalyticsException in project product-iots by wso2.

the class APIUtil method getAllEventsForDevice.

public static List<SensorRecord> getAllEventsForDevice(String tableName, String query, List<SortByField> sortByFields) throws AnalyticsException {
    int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
    AnalyticsDataAPI analyticsDataAPI = getAnalyticsDataAPI();
    int eventCount = analyticsDataAPI.searchCount(tenantId, tableName, query);
    // limiting the data read from the server
    int start = 0;
    int dataCount = 100;
    if (eventCount == 0) {
        return null;
    } else if (eventCount >= dataCount) {
        start = eventCount - dataCount;
    }
    List<SearchResultEntry> resultEntries = analyticsDataAPI.search(tenantId, tableName, query, start, eventCount, sortByFields);
    List<String> recordIds = getRecordIds(resultEntries);
    AnalyticsDataResponse response = analyticsDataAPI.get(tenantId, tableName, 1, null, recordIds);
    Map<String, SensorRecord> sensorDatas = createSensorData(AnalyticsDataAPIUtil.listRecords(analyticsDataAPI, response));
    List<SensorRecord> sortedSensorData = getSortedSensorData(sensorDatas, resultEntries);
    return sortedSensorData;
}
Also used : AnalyticsDataResponse(org.wso2.carbon.analytics.dataservice.commons.AnalyticsDataResponse) AnalyticsDataAPI(org.wso2.carbon.analytics.api.AnalyticsDataAPI) SearchResultEntry(org.wso2.carbon.analytics.dataservice.commons.SearchResultEntry)

Example 3 with AnalyticsException

use of org.wso2.carbon.analytics.datasource.commons.exception.AnalyticsException in project product-iots by wso2.

the class DeviceTypeServiceImpl method getSensorStats.

/**
 * Retrieve Sensor data for the given time period.
 *
 * @param deviceId unique identifier for given device type instance
 * @param from     starting time
 * @param to       ending time
 * @return response with List<SensorRecord> object which includes sensor data which is requested
 */
@Path("device/stats/{deviceId}")
@GET
@Consumes("application/json")
@Produces("application/json")
public Response getSensorStats(@PathParam("deviceId") String deviceId, @QueryParam("from") long from, @QueryParam("to") long to, @QueryParam("sensorType") String sensorType) {
    String fromDate = String.valueOf(from);
    String toDate = String.valueOf(to);
    String query = "meta_deviceId:" + deviceId + " AND meta_deviceType:" + DeviceTypeConstants.DEVICE_TYPE + " AND meta_time : [" + fromDate + " TO " + toDate + "]";
    String sensorTableName = null;
    if (sensorType.equals(DeviceTypeConstants.SENSOR_TYPE1)) {
        sensorTableName = DeviceTypeConstants.SENSOR_TYPE1_EVENT_TABLE;
    } else if (sensorType.equals(DeviceTypeConstants.SENSOR_TYPE2)) {
        sensorTableName = DeviceTypeConstants.SENSOR_TYPE2_EVENT_TABLE;
    }
    try {
        if (!APIUtil.getDeviceAccessAuthorizationService().isUserAuthorized(new DeviceIdentifier(deviceId, DeviceTypeConstants.DEVICE_TYPE))) {
            return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build();
        }
        if (sensorTableName != null) {
            List<SortByField> sortByFields = new ArrayList<>();
            SortByField sortByField = new SortByField("meta_time", SortType.ASC);
            sortByFields.add(sortByField);
            List<SensorRecord> sensorRecords = APIUtil.getAllEventsForDevice(sensorTableName, query, sortByFields);
            return Response.status(Response.Status.OK.getStatusCode()).entity(sensorRecords).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 (DeviceAccessAuthorizationException e) {
        log.error(e.getErrorMessage(), e);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
    }
    return Response.status(Response.Status.BAD_REQUEST).build();
}
Also used : SortByField(org.wso2.carbon.analytics.dataservice.commons.SortByField) DeviceAccessAuthorizationException(org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException) AnalyticsException(org.wso2.carbon.analytics.datasource.commons.exception.AnalyticsException) ArrayList(java.util.ArrayList) SensorRecord(org.wso2.iot.sampledevice.api.dto.SensorRecord) DeviceIdentifier(org.wso2.carbon.device.mgt.common.DeviceIdentifier) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 4 with AnalyticsException

use of org.wso2.carbon.analytics.datasource.commons.exception.AnalyticsException in project product-iots by wso2.

the class APIUtil method getAllEventsForDevice.

/**
 * Get sensor data for a device.
 *
 * @param tableName    table name of the data source
 * @param query        query for the table
 * @param sortByFields list of fields to sort the data by
 * @return List of SensorRecords that is sorted by the fields provided
 * @throws AnalyticsException
 */
public static List<SensorRecord> getAllEventsForDevice(String tableName, String query, List<SortByField> sortByFields) throws AnalyticsException {
    int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
    AnalyticsDataAPI analyticsDataAPI = getAnalyticsDataAPI();
    int eventCount = analyticsDataAPI.searchCount(tenantId, tableName, query);
    if (eventCount == 0) {
        return null;
    }
    List<SearchResultEntry> resultEntries = analyticsDataAPI.search(tenantId, tableName, query, 0, eventCount, sortByFields);
    List<String> recordIds = getRecordIds(resultEntries);
    AnalyticsDataResponse response = analyticsDataAPI.get(tenantId, tableName, 1, null, recordIds);
    Map<String, SensorRecord> sensorDatas = createSensorData(AnalyticsDataAPIUtil.listRecords(analyticsDataAPI, response));
    List<SensorRecord> sortedSensorData = getSortedSensorData(sensorDatas, resultEntries);
    return sortedSensorData;
}
Also used : SensorRecord(org.wso2.iot.sampledevice.api.dto.SensorRecord) AnalyticsDataResponse(org.wso2.carbon.analytics.dataservice.commons.AnalyticsDataResponse) AnalyticsDataAPI(org.wso2.carbon.analytics.api.AnalyticsDataAPI) SearchResultEntry(org.wso2.carbon.analytics.dataservice.commons.SearchResultEntry)

Aggregations

ArrayList (java.util.ArrayList)2 Consumes (javax.ws.rs.Consumes)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 AnalyticsDataAPI (org.wso2.carbon.analytics.api.AnalyticsDataAPI)2 AnalyticsDataResponse (org.wso2.carbon.analytics.dataservice.commons.AnalyticsDataResponse)2 SearchResultEntry (org.wso2.carbon.analytics.dataservice.commons.SearchResultEntry)2 SortByField (org.wso2.carbon.analytics.dataservice.commons.SortByField)2 AnalyticsException (org.wso2.carbon.analytics.datasource.commons.exception.AnalyticsException)2 DeviceIdentifier (org.wso2.carbon.device.mgt.common.DeviceIdentifier)2 DeviceAccessAuthorizationException (org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException)2 SensorRecord (org.wso2.iot.sampledevice.api.dto.SensorRecord)2 SensorRecord (org.coffeeking.api.util.SensorRecord)1