Search in sources :

Example 1 with MetricInfoListResultImpl

use of org.eclipse.kapua.service.datastore.internal.model.MetricInfoListResultImpl in project kapua by eclipse.

the class MetricInfoStoreServiceImpl method query.

@Override
public MetricInfoListResult query(KapuaId scopeId, MetricInfoQuery query) throws KapuaException {
    // 
    // Argument Validation
    ArgumentValidator.notNull(scopeId, "scopeId");
    ArgumentValidator.notNull(query, "query");
    // 
    // Check Access
    this.checkDataAccess(scopeId, Actions.read);
    // 
    // Do the find
    AccountInfo accountInfo = getAccountServicePlan(scopeId);
    String scopeName = accountInfo.getAccount().getName();
    LocalServicePlan accountServicePlan = accountInfo.getServicePlan();
    long ttl = accountServicePlan.getDataTimeToLive() * DAY_MILLIS;
    if (!accountServicePlan.getDataStorageEnabled() || ttl == LocalServicePlan.DISABLED) {
        logger.debug("Storage not enabled for account {}, returning empty result", scopeName);
        return new MetricInfoListResultImpl();
    }
    try {
        String everyIndex = EsUtils.getAnyIndexName(scopeName);
        MetricInfoListResult result = null;
        result = EsMetricDAO.connection(EsClient.getcurrent()).instance(everyIndex, EsSchema.METRIC_TYPE_NAME).query(query);
        return result;
    } catch (Exception exc) {
        // CassandraUtils.handleException(e);
        throw KapuaException.internalError(exc);
    }
}
Also used : MetricInfoListResult(org.eclipse.kapua.service.datastore.model.MetricInfoListResult) LocalServicePlan(org.eclipse.kapua.service.datastore.internal.elasticsearch.LocalServicePlan) MetricInfoListResultImpl(org.eclipse.kapua.service.datastore.internal.model.MetricInfoListResultImpl) KapuaException(org.eclipse.kapua.KapuaException)

Example 2 with MetricInfoListResultImpl

use of org.eclipse.kapua.service.datastore.internal.model.MetricInfoListResultImpl in project kapua by eclipse.

the class EsMetricDAO method query.

public MetricInfoListResult query(MetricInfoQuery query) throws Exception {
    // get one plus (if there is one) to later get the next key value
    MetricInfoQueryImpl localQuery = new MetricInfoQueryImpl();
    localQuery.copy(query);
    localQuery.setLimit(query.getLimit() + 1);
    MetricInfoQueryConverter mic = new MetricInfoQueryConverter();
    SearchRequestBuilder builder = mic.toSearchRequestBuilder(esTypeDAO.getIndexName(), esTypeDAO.getTypeName(), localQuery);
    SearchResponse response = builder.get(TimeValue.timeValueMillis(EsUtils.getQueryTimeout()));
    SearchHits searchHits = response.getHits();
    if (searchHits == null || searchHits.getTotalHits() == 0)
        return new MetricInfoListResultImpl();
    int i = 0;
    int searchHitsSize = searchHits.getHits().length;
    List<MetricInfo> metricInfos = new ArrayList<MetricInfo>();
    MetricInfoBuilder metricInfoBuilder = new MetricInfoBuilder();
    for (SearchHit searchHit : searchHits.getHits()) {
        if (i < query.getLimit()) {
            MetricInfo metricInfo = metricInfoBuilder.build(searchHit).getKapuaMetricInfo();
            metricInfos.add(metricInfo);
        }
        i++;
    }
    // TODO check equivalence with CX with Pierantonio
    // TODO what is this nextKey
    Object nextKey = null;
    if (searchHitsSize > query.getLimit()) {
        nextKey = query.getLimit();
    }
    MetricInfoListResult result = new MetricInfoListResultImpl(nextKey, metricInfos.size());
    result.addAll(metricInfos);
    return result;
}
Also used : MetricInfoListResult(org.eclipse.kapua.service.datastore.model.MetricInfoListResult) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) MetricInfoQueryImpl(org.eclipse.kapua.service.datastore.internal.model.query.MetricInfoQueryImpl) ArrayList(java.util.ArrayList) MetricInfoListResultImpl(org.eclipse.kapua.service.datastore.internal.model.MetricInfoListResultImpl) SearchResponse(org.elasticsearch.action.search.SearchResponse) MetricInfoBuilder(org.eclipse.kapua.service.datastore.internal.elasticsearch.MetricInfoBuilder) MetricInfo(org.eclipse.kapua.service.datastore.model.MetricInfo) SearchHits(org.elasticsearch.search.SearchHits) MetricInfoQueryConverter(org.eclipse.kapua.service.datastore.internal.elasticsearch.MetricInfoQueryConverter)

Aggregations

MetricInfoListResultImpl (org.eclipse.kapua.service.datastore.internal.model.MetricInfoListResultImpl)2 MetricInfoListResult (org.eclipse.kapua.service.datastore.model.MetricInfoListResult)2 ArrayList (java.util.ArrayList)1 KapuaException (org.eclipse.kapua.KapuaException)1 LocalServicePlan (org.eclipse.kapua.service.datastore.internal.elasticsearch.LocalServicePlan)1 MetricInfoBuilder (org.eclipse.kapua.service.datastore.internal.elasticsearch.MetricInfoBuilder)1 MetricInfoQueryConverter (org.eclipse.kapua.service.datastore.internal.elasticsearch.MetricInfoQueryConverter)1 MetricInfoQueryImpl (org.eclipse.kapua.service.datastore.internal.model.query.MetricInfoQueryImpl)1 MetricInfo (org.eclipse.kapua.service.datastore.model.MetricInfo)1 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)1 SearchResponse (org.elasticsearch.action.search.SearchResponse)1 SearchHit (org.elasticsearch.search.SearchHit)1 SearchHits (org.elasticsearch.search.SearchHits)1