Search in sources :

Example 1 with MetricInfo

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

the class MetricInfoStoreServiceImpl method find.

@Override
public MetricInfo find(KapuaId scopeId, StorableId id) throws KapuaException {
    // 
    // Argument Validation
    ArgumentValidator.notNull(scopeId, "scopeId");
    ArgumentValidator.notNull(id, "id");
    // 
    // Check Access
    this.checkDataAccess(scopeId, Actions.read);
    MetricInfoQueryImpl q = new MetricInfoQueryImpl();
    q.setLimit(1);
    ArrayList<StorableId> ids = new ArrayList<StorableId>();
    ids.add(id);
    AndPredicateImpl allPredicates = new AndPredicateImpl();
    allPredicates.addPredicate(new IdsPredicateImpl(EsMessageField.ID, ids));
    MetricInfoListResult result = this.query(scopeId, q);
    if (result == null || result.size() == 0)
        return null;
    MetricInfo topicInfo = result.get(0);
    return topicInfo;
}
Also used : StorableId(org.eclipse.kapua.service.datastore.model.StorableId) IdsPredicateImpl(org.eclipse.kapua.service.datastore.internal.model.query.IdsPredicateImpl) AndPredicateImpl(org.eclipse.kapua.service.datastore.internal.model.query.AndPredicateImpl) MetricInfoListResult(org.eclipse.kapua.service.datastore.model.MetricInfoListResult) MetricInfo(org.eclipse.kapua.service.datastore.model.MetricInfo) MetricInfoQueryImpl(org.eclipse.kapua.service.datastore.internal.model.query.MetricInfoQueryImpl) ArrayList(java.util.ArrayList)

Example 2 with MetricInfo

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

the class MetricInfoBuilder method build.

public MetricInfoBuilder build(SearchHit searchHit) throws Exception {
    Map<String, SearchHitField> fields = searchHit.fields();
    String id = searchHit.getId();
    String scope = fields.get(EsSchema.METRIC_ACCOUNT).getValue();
    String name = (String) fields.get(EsSchema.METRIC_MTR_NAME_FULL).getValue();
    String type = (String) fields.get(EsSchema.METRIC_MTR_TYPE_FULL).getValue();
    String lastMsgTimestamp = (String) fields.get(EsSchema.METRIC_MTR_TYPE_FULL).getValue();
    String lastMsgId = (String) fields.get(EsSchema.METRIC_MTR_MSG_ID_FULL).getValue();
    Object value = fields.get(EsSchema.METRIC_MTR_VALUE_FULL).getValue();
    String asset = fields.get(EsSchema.METRIC_ASSET).getValue();
    String semTopic = fields.get(EsSchema.METRIC_SEM_NAME).getValue();
    Date timestamp = (Date) EsUtils.convertToKapuaObject("date", lastMsgTimestamp);
    KapuaTopic topic = new KapuaTopic(scope, asset, semTopic);
    String metricName = EsUtils.restoreMetricName(name);
    MetricInfo finalMetricInfo = new MetricInfoImpl(scope, new StorableIdImpl(id));
    finalMetricInfo.setFullTopicName(topic.getFullTopic());
    finalMetricInfo.setLastMessageId(new StorableIdImpl(lastMsgId));
    finalMetricInfo.setLastMessageTimestamp(timestamp);
    finalMetricInfo.setName(metricName);
    if (EsUtils.ES_TYPE_STRING.equals(type)) {
        finalMetricInfo.setType(EsUtils.convertToKapuaType(type));
        finalMetricInfo.setValue((String) value);
    }
    if (EsUtils.ES_TYPE_INTEGER.equals(type)) {
        finalMetricInfo.setType(EsUtils.convertToKapuaType(type));
        finalMetricInfo.setValue((Integer) value);
    }
    if (EsUtils.ES_TYPE_LONG.equals(type)) {
        Object obj = value;
        if (value != null && value instanceof Integer)
            obj = ((Integer) value).longValue();
        finalMetricInfo.setType(EsUtils.convertToKapuaType(type));
        finalMetricInfo.setValue((Long) obj);
    }
    if (EsUtils.ES_TYPE_FLOAT.equals(type)) {
        finalMetricInfo.setType(EsUtils.convertToKapuaType(type));
        finalMetricInfo.setValue((Float) value);
    }
    if (EsUtils.ES_TYPE_DOUBLE.equals(type)) {
        finalMetricInfo.setType(EsUtils.convertToKapuaType(type));
        finalMetricInfo.setValue((Double) value);
    }
    if (EsUtils.ES_TYPE_BOOL.equals(type)) {
        finalMetricInfo.setType(EsUtils.convertToKapuaType(type));
        finalMetricInfo.setValue((Boolean) value);
    }
    if (EsUtils.ES_TYPE_BINARY.equals(type)) {
        finalMetricInfo.setType(EsUtils.convertToKapuaType(type));
        finalMetricInfo.setValue((byte[]) value);
    }
    if (finalMetricInfo.getType() == null)
        throw new Exception(String.format("Unknown metric type [%s]", type));
    // FIXME - review all this generic
    this.metricInfo = finalMetricInfo;
    return this;
}
Also used : MetricInfo(org.eclipse.kapua.service.datastore.model.MetricInfo) SearchHitField(org.elasticsearch.search.SearchHitField) MetricInfoImpl(org.eclipse.kapua.service.datastore.internal.model.MetricInfoImpl) Date(java.util.Date) StorableIdImpl(org.eclipse.kapua.service.datastore.internal.model.StorableIdImpl)

Example 3 with MetricInfo

use of org.eclipse.kapua.service.datastore.model.MetricInfo 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

MetricInfo (org.eclipse.kapua.service.datastore.model.MetricInfo)3 ArrayList (java.util.ArrayList)2 MetricInfoQueryImpl (org.eclipse.kapua.service.datastore.internal.model.query.MetricInfoQueryImpl)2 MetricInfoListResult (org.eclipse.kapua.service.datastore.model.MetricInfoListResult)2 Date (java.util.Date)1 MetricInfoBuilder (org.eclipse.kapua.service.datastore.internal.elasticsearch.MetricInfoBuilder)1 MetricInfoQueryConverter (org.eclipse.kapua.service.datastore.internal.elasticsearch.MetricInfoQueryConverter)1 MetricInfoImpl (org.eclipse.kapua.service.datastore.internal.model.MetricInfoImpl)1 MetricInfoListResultImpl (org.eclipse.kapua.service.datastore.internal.model.MetricInfoListResultImpl)1 StorableIdImpl (org.eclipse.kapua.service.datastore.internal.model.StorableIdImpl)1 AndPredicateImpl (org.eclipse.kapua.service.datastore.internal.model.query.AndPredicateImpl)1 IdsPredicateImpl (org.eclipse.kapua.service.datastore.internal.model.query.IdsPredicateImpl)1 StorableId (org.eclipse.kapua.service.datastore.model.StorableId)1 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)1 SearchResponse (org.elasticsearch.action.search.SearchResponse)1 SearchHit (org.elasticsearch.search.SearchHit)1 SearchHitField (org.elasticsearch.search.SearchHitField)1 SearchHits (org.elasticsearch.search.SearchHits)1