Search in sources :

Example 1 with AnalyticsDataAPI

use of org.wso2.carbon.analytics.api.AnalyticsDataAPI 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 2 with AnalyticsDataAPI

use of org.wso2.carbon.analytics.api.AnalyticsDataAPI in project product-iots by wso2.

the class APIUtil method getAnalyticsDataAPI.

public static AnalyticsDataAPI getAnalyticsDataAPI() {
    PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
    AnalyticsDataAPI analyticsDataAPI = (AnalyticsDataAPI) ctx.getOSGiService(AnalyticsDataAPI.class, null);
    if (analyticsDataAPI == null) {
        throw new IllegalStateException("Analytics api service has not initialized");
    }
    return analyticsDataAPI;
}
Also used : PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) AnalyticsDataAPI(org.wso2.carbon.analytics.api.AnalyticsDataAPI)

Example 3 with AnalyticsDataAPI

use of org.wso2.carbon.analytics.api.AnalyticsDataAPI in project product-iots by wso2.

the class APIUtil method getAnalyticsDataAPI.

/**
 * @return AnalyticthsDataAPI for the context
 */
public 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;
}
Also used : PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) AnalyticsDataAPI(org.wso2.carbon.analytics.api.AnalyticsDataAPI)

Example 4 with AnalyticsDataAPI

use of org.wso2.carbon.analytics.api.AnalyticsDataAPI 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

AnalyticsDataAPI (org.wso2.carbon.analytics.api.AnalyticsDataAPI)4 AnalyticsDataResponse (org.wso2.carbon.analytics.dataservice.commons.AnalyticsDataResponse)2 SearchResultEntry (org.wso2.carbon.analytics.dataservice.commons.SearchResultEntry)2 PrivilegedCarbonContext (org.wso2.carbon.context.PrivilegedCarbonContext)2 SensorRecord (org.wso2.iot.sampledevice.api.dto.SensorRecord)1