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;
}
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;
}
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;
}
Aggregations