Skip to content

Commit

Permalink
Fix MetricPredicate and add MetricExistsPredicate
Browse files Browse the repository at this point in the history
Signed-off-by: Claudio Mezzasalma <claudio.mezzasalma@eurotech.com>
  • Loading branch information
Claudio Mezzasalma authored and Coduz committed Mar 20, 2020
1 parent 436e982 commit 88fac6e
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.eclipse.kapua.service.datastore.model.MessageListResult;
import org.eclipse.kapua.service.datastore.model.StorableId;
import org.eclipse.kapua.service.datastore.model.query.AndPredicate;
import org.eclipse.kapua.service.datastore.model.query.ExistsPredicate;
import org.eclipse.kapua.service.datastore.model.query.MessageQuery;
import org.eclipse.kapua.service.datastore.model.query.MetricPredicate;
import org.eclipse.kapua.service.datastore.model.query.RangePredicate;
Expand Down Expand Up @@ -121,11 +122,17 @@ public <V extends Comparable<V>> MessageListResult simpleQuery( //
}

if (!Strings.isNullOrEmpty(metricName)) {
V minValue = (V) ObjectValueConverter.fromString(metricMinValue, metricType.getType());
V maxValue = (V) ObjectValueConverter.fromString(metricMaxValue, metricType.getType());
if (metricMinValue == null && metricMaxValue == null) {
Class<V> type = metricType != null ? metricType.getType() : null;
ExistsPredicate existsPredicate = STORABLE_PREDICATE_FACTORY.newMetricExistsPredicate(metricName, type);
andPredicate.getPredicates().add(existsPredicate);
} else {
V minValue = (V) ObjectValueConverter.fromString(metricMinValue, metricType.getType());
V maxValue = (V) ObjectValueConverter.fromString(metricMaxValue, metricType.getType());

MetricPredicate metricPredicate = STORABLE_PREDICATE_FACTORY.newMetricPredicate(metricName, metricType.getType(), minValue, maxValue);
andPredicate.getPredicates().add(metricPredicate);
MetricPredicate metricPredicate = STORABLE_PREDICATE_FACTORY.newMetricPredicate(metricName, metricType.getType(), minValue, maxValue);
andPredicate.getPredicates().add(metricPredicate);
}
}

MessageQuery query = DATASTORE_OBJECT_FACTORY.newDatastoreMessageQuery(scopeId);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*******************************************************************************
* Copyright (c) 2011, 2020 Eurotech and/or its affiliates and others
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.service.datastore.model.query;

/**
* Query predicate for matching messages with existing metrics
*
* @since 1.2.0
*/
public interface MetricExistsPredicate extends ExistsPredicate {

/**
* Gets the metric type to search.
* This is required because metric with the same name can have different types.
*
* @return The metric type
* @since 1.2.0
*/
Class<?> getType();

/**
* Sets the metric type so search.
*
* @param type The metric type to search.
* @since 1.2.0
*/
void setType(Class<?> type);

}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ public interface StorablePredicateFactory extends KapuaObjectFactory {
*/
ExistsPredicate newExistsPredicate(String fieldName);

<V extends Comparable<V>> MetricExistsPredicate newMetricExistsPredicate(String fieldName, Class<V> type);

OrPredicate newOrPredicate();

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@

/**
* Implementation of query predicate for checking if a field exists
*
*
* @since 1.0
*
*/
public class ExistsPredicateImpl implements ExistsPredicate {

private String name;
protected String name;

/**
* Creates an exists predicate for the given field name
*
*
* @param name
*/
public ExistsPredicateImpl(String name) {
Expand All @@ -39,7 +39,7 @@ public ExistsPredicateImpl(String name) {

/**
* Creates an exists predicate concatenating the given fields name with a dot (useful for composite fileds)
*
*
* @param paths
*/
public ExistsPredicateImpl(String... paths) {
Expand Down Expand Up @@ -68,7 +68,7 @@ public String getName() {
*/
public ObjectNode toSerializedMap() throws DatamodelMappingException {
ObjectNode rootNode = SchemaUtil.getObjectNode();
ObjectNode termNode = SchemaUtil.getField(new KeyValueEntry[] { new KeyValueEntry(PredicateConstants.FIELD_KEY, (String) name) });
ObjectNode termNode = SchemaUtil.getField(new KeyValueEntry[] { new KeyValueEntry(PredicateConstants.FIELD_KEY, name) });
rootNode.set(PredicateConstants.EXISTS_KEY, termNode);
return rootNode;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*******************************************************************************
* Copyright (c) 2011, 2020 Eurotech and/or its affiliates and others
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.service.datastore.internal.model.query;

import org.eclipse.kapua.service.datastore.client.DatamodelMappingException;
import org.eclipse.kapua.service.datastore.internal.mediator.DatastoreUtils;
import org.eclipse.kapua.service.datastore.internal.schema.KeyValueEntry;
import org.eclipse.kapua.service.datastore.internal.schema.SchemaUtil;
import org.eclipse.kapua.service.datastore.model.query.MetricExistsPredicate;

import com.fasterxml.jackson.databind.node.ObjectNode;

/**
* Implementation of query predicate for checking if a field exists
*
* @since 1.2.0
*
*/
public class MetricExistsPredicateImpl extends ExistsPredicateImpl implements MetricExistsPredicate {

private Class<?> type;

public <V extends Comparable<V>> MetricExistsPredicateImpl(String fieldName, Class<V> type) {
super(fieldName);

this.type = type;
}

@Override
public Class<?> getType() {
return type;
}

@Override
public void setType(Class<?> type) {
this.type = type;
}

@Override
public String getName() {
return name;
}

@Override
/**
* <pre>
* {
* "query": {
* "exists" : { "field" : "metrics.metric.typ" }
* }
* }
* </pre>
*/
public ObjectNode toSerializedMap() throws DatamodelMappingException {
ObjectNode rootNode = SchemaUtil.getObjectNode();
String fieldName = type == null ? String.format("metrics.%s", name) : String.format("metrics.%s.%s", name, DatastoreUtils.getClientMetricFromAcronym(type.getSimpleName().toLowerCase()));
ObjectNode termNode = SchemaUtil.getField(new KeyValueEntry[] { new KeyValueEntry(PredicateConstants.FIELD_KEY, fieldName) });
rootNode.set(PredicateConstants.EXISTS_KEY, termNode);
return rootNode;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@
*******************************************************************************/
package org.eclipse.kapua.service.datastore.internal.model.query;

import org.eclipse.kapua.service.datastore.client.DatamodelMappingException;
import org.eclipse.kapua.service.datastore.internal.mediator.DatastoreUtils;
import org.eclipse.kapua.service.datastore.internal.schema.SchemaUtil;
import org.eclipse.kapua.service.datastore.model.query.MetricPredicate;

import com.fasterxml.jackson.databind.node.ObjectNode;

/**
* Implementation of query predicate for matching range values
*
Expand All @@ -38,4 +43,36 @@ public Class<?> getType() {
public void setType(Class<?> type) {
this.type = type;
}

/**
* <pre>
* {
* "query": {
* "range" : {
* "metrics.temperature.int" : {
* "gte" : 10,
* "lte" : 20,
* }
* }
* }
* }
* </pre>
*
* @throws DatamodelMappingException
*/
public ObjectNode toSerializedMap() throws DatamodelMappingException {
ObjectNode rootNode = SchemaUtil.getObjectNode();
ObjectNode valuesNode = SchemaUtil.getObjectNode();
if (maxValue != null) {
SchemaUtil.appendField(valuesNode, PredicateConstants.LTE_KEY, maxValue);
}
if (minValue != null) {
SchemaUtil.appendField(valuesNode, PredicateConstants.GTE_KEY, minValue);
}
ObjectNode termNode = SchemaUtil.getObjectNode();
termNode.set(String.format("metrics.%s.%s", field, DatastoreUtils.getClientMetricFromAcronym(type.getSimpleName().toLowerCase())), valuesNode);
rootNode.set(PredicateConstants.RANGE_KEY, termNode);
return rootNode;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.eclipse.kapua.service.datastore.model.query.AndPredicate;
import org.eclipse.kapua.service.datastore.model.query.ChannelMatchPredicate;
import org.eclipse.kapua.service.datastore.model.query.ExistsPredicate;
import org.eclipse.kapua.service.datastore.model.query.MetricExistsPredicate;
import org.eclipse.kapua.service.datastore.model.query.MetricPredicate;
import org.eclipse.kapua.service.datastore.model.query.OrPredicate;
import org.eclipse.kapua.service.datastore.model.query.RangePredicate;
Expand Down Expand Up @@ -55,6 +56,11 @@ public ExistsPredicate newExistsPredicate(String fieldName) {
return new ExistsPredicateImpl(fieldName);
}

@Override
public <V extends Comparable<V>> MetricExistsPredicate newMetricExistsPredicate(String fieldName, Class<V> type) {
return new MetricExistsPredicateImpl(fieldName, type);
}

@Override
public OrPredicate newOrPredicate() {
return new OrPredicateImpl();
Expand Down

0 comments on commit 88fac6e

Please sign in to comment.