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

Add DateRangeFilteredQueryLogic #2665

Open
wants to merge 7 commits into
base: integration
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
@@ -0,0 +1,72 @@
package datawave.core.query.logic.filtered;

import java.util.Set;

import org.apache.accumulo.core.client.AccumuloClient;
import org.apache.accumulo.core.security.Authorizations;
import org.apache.log4j.Logger;

import datawave.core.query.configuration.GenericQueryConfiguration;
import datawave.microservice.query.Query;

/**
* A FilteredQueryLogic which can limit the begin and end date of a Query by the configured filter dates when paired with a QueryLogicFilterByDate
*/
public class DateRangeFilteredQueryLogic extends FilteredQueryLogic {
FineAndDandy marked this conversation as resolved.
Show resolved Hide resolved
public static final Logger log = Logger.getLogger(DateRangeFilteredQueryLogic.class);

public DateRangeFilteredQueryLogic() {}

public DateRangeFilteredQueryLogic(FilteredQueryLogic other) throws CloneNotSupportedException {
super(other);
}

@Override
public GenericQueryConfiguration initialize(AccumuloClient connection, Query settings, Set<Authorizations> runtimeQueryAuthorizations) throws Exception {
if (canRunQuery(settings, runtimeQueryAuthorizations)) {
// may need to adjust the Query to run within the defined range
if (getFilter() instanceof QueryLogicFilterByDate) {
QueryLogicFilterByDate filter = (QueryLogicFilterByDate) getFilter();
applyFilterDateRange(settings, filter);
} else {
log.warn("Not adjusting date range. Filter is not QueryLogicFilterByDate filter: " + getFilter().getClass());
}
}

return super.initialize(connection, settings, runtimeQueryAuthorizations);
}

/**
* Update the Query so that the begin date is no earlier than the filters begin date, and the end date is no later than the filters end date
*
* @param settings
* the query settings to adjust
* @param filter
* the filter to use to adjust the query settings
*/
private void applyFilterDateRange(Query settings, QueryLogicFilterByDate filter) {
FineAndDandy marked this conversation as resolved.
Show resolved Hide resolved
boolean modifiedDates = false;
if (filter.getStartDate() != null && filter.getStartDate().after(settings.getBeginDate())) {
// adjust the start to match the filter start
log.info("adjusting query: " + settings.getId() + " start date from: " + settings.getBeginDate() + " to " + filter.getStartDate());
settings.setBeginDate(filter.getStartDate());
modifiedDates = true;
}

if (filter.getEndDate() != null && filter.getEndDate().before(settings.getEndDate())) {
// adjust the end date to match the filter end
log.info("adjusting query: " + settings.getId() + " end date from: " + settings.getEndDate() + " to " + filter.getEndDate());
settings.setEndDate(filter.getEndDate());
modifiedDates = true;
}

if (modifiedDates) {
log.info("final date range for query: " + settings.getId() + " " + settings.getBeginDate() + " to " + settings.getEndDate());
}
}

@Override
public Object clone() throws CloneNotSupportedException {
return new DateRangeFilteredQueryLogic(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package datawave.core.query.logic.filtered;

import java.util.Set;

import org.apache.accumulo.core.security.Authorizations;

import datawave.core.query.predicate.QueryDateRangePredicate;
import datawave.microservice.query.Query;

/**
* Tests that a Query's begin and end date overlap with the configured date range of the Predicate.
*/
public class QueryLogicFilterByDate extends QueryDateRangePredicate implements FilteredQueryLogic.QueryLogicFilter {
FineAndDandy marked this conversation as resolved.
Show resolved Hide resolved
/**
* Test the Query settings against the QueryDateRangePredicate
*
* @param settings
* the settings including a beginDate and endDate
* @param auths
* the query auths (unused for this test)
* @return true if the query overlaps with the date range specified by the QueryDateRangePredicate, false otherwise
*/
@Override
public boolean canRunQuery(Query settings, Set<Authorizations> auths) {
FineAndDandy marked this conversation as resolved.
Show resolved Hide resolved
return test(settings);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package datawave.core.query.predicate;

import java.util.Date;
import java.util.function.Predicate;

import datawave.microservice.query.Query;

/**
* Determine if a Query overlaps a defined Date range. A null startDate or endDate is automatically accepted (treated as unbounded).
*/
public class QueryDateRangePredicate implements Predicate<Query> {
private Date startDate;
private Date endDate;

@Override
public boolean test(Query query) {
// only test the start date if set
if (startDate != null) {
// does the entire query occur before the start date
if (startDate.after(query.getEndDate())) {
return false;
}
}
// only test the end date if set
if (endDate != null) {
// does the entire query occur after the end date
if (endDate.before(query.getBeginDate())) {
return false;
}
}

// some part of the query overlaps the date range
return true;
}

public Date getStartDate() {
return startDate;
}

public void setStartDate(Date startDate) {
this.startDate = startDate;
}

public Date getEndDate() {
return endDate;
}

public void setEndDate(Date endDate) {
this.endDate = endDate;
}
}
Loading
Loading