Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter messages according to metrics in Data view #2674

Merged
merged 1 commit into from
Aug 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import org.eclipse.kapua.service.datastore.model.query.ClientInfoQuery;
import org.eclipse.kapua.service.datastore.model.query.MessageQuery;
import org.eclipse.kapua.service.datastore.model.query.MetricInfoQuery;
import org.eclipse.kapua.service.datastore.model.query.OrPredicate;
import org.eclipse.kapua.service.datastore.model.query.RangePredicate;
import org.eclipse.kapua.service.datastore.model.query.SortDirection;
import org.eclipse.kapua.service.datastore.model.query.SortField;
Expand Down Expand Up @@ -442,6 +443,13 @@ private PagingLoadResult<GwtMessage> findMessages(PagingLoadConfig loadConfig, S
}
RangePredicate dateRangePredicate = STORABLE_PREDICATE_FACTORY.newRangePredicate(MessageField.TIMESTAMP.field(), startDate, endDate);
andPredicate.getPredicates().add(dateRangePredicate);
if (headers != null) {
OrPredicate metricsPredicate = STORABLE_PREDICATE_FACTORY.newOrPredicate();
for (GwtHeader header : headers) {
metricsPredicate.getPredicates().add(STORABLE_PREDICATE_FACTORY.newExistsPredicate(String.format(MessageSchema.MESSAGE_METRICS + ".%s", header.getName())));
}
andPredicate.getPredicates().add(metricsPredicate);
}
query.setPredicate(andPredicate);
if (!StringUtils.isEmpty(loadConfig.getSortField())) {
String sortField = loadConfig.getSortField();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*******************************************************************************
* Copyright (c) 2019 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;

import java.util.List;

/**
* Query "or" aggregation definition
*
* @since 1.0
*/
public interface OrPredicate extends StorablePredicate {

/**
* Get the {@link StorablePredicate} list
*
* @return
*/
List<StorablePredicate> getPredicates();
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,7 @@ public interface StorablePredicateFactory extends KapuaObjectFactory {
* @return The newly instantiated {@link ExistsPredicate}.
*/
ExistsPredicate newExistsPredicate(String fieldName);

OrPredicate newOrPredicate();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*******************************************************************************
* Copyright (c) 2019 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 java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.eclipse.kapua.service.datastore.client.DatamodelMappingException;
import org.eclipse.kapua.service.datastore.internal.schema.SchemaUtil;
import org.eclipse.kapua.service.datastore.model.query.OrPredicate;
import org.eclipse.kapua.service.datastore.model.query.StorablePredicate;

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

/**
* Implementation of query "or" aggregation
*
* @since 1.0
*/
public class OrPredicateImpl implements OrPredicate {

private List<StorablePredicate> predicates = new ArrayList<StorablePredicate>();

/**
* Default constructor
*/
public OrPredicateImpl() {
}

/**
* Creates an "or" predicate for the given predicates collection
*
* @param predicates
*/
public OrPredicateImpl(Collection<StorablePredicate> predicates) {
predicates.addAll(predicates);
}

@Override
public List<StorablePredicate> getPredicates() {
return this.predicates;
}

/**
* Add the storable predicate to the predicates collection
*
* @param predicate
* @return
*/
public OrPredicate addPredicate(StorablePredicate predicate) {
this.predicates.add(predicate);
return this;

}

/**
* Clear the predicates collection
*
* @return
*/
public OrPredicate clearPredicates() {
this.predicates.clear();
return this;
}

@Override
public ObjectNode toSerializedMap() throws DatamodelMappingException {
ObjectNode rootNode = SchemaUtil.getObjectNode();
ObjectNode termNode = SchemaUtil.getObjectNode();
ArrayNode conditionsNode = SchemaUtil.getArrayNode();
for (StorablePredicate predicate : predicates) {
conditionsNode.add(predicate.toSerializedMap());
}
termNode.set(PredicateConstants.SHOULD_KEY, conditionsNode);
rootNode.set(PredicateConstants.BOOL_KEY, termNode);
return rootNode;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public interface PredicateConstants {
*/
String MUST_KEY = "must";

/**
* Must term
*/
String SHOULD_KEY = "should";

/**
* Prefix term
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
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.MetricPredicate;
import org.eclipse.kapua.service.datastore.model.query.OrPredicate;
import org.eclipse.kapua.service.datastore.model.query.RangePredicate;
import org.eclipse.kapua.service.datastore.model.query.StorableField;
import org.eclipse.kapua.service.datastore.model.query.StorablePredicateFactory;
Expand Down Expand Up @@ -54,4 +55,9 @@ public ExistsPredicate newExistsPredicate(String fieldName) {
return new ExistsPredicateImpl(fieldName);
}

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

}