Skip to content

Commit

Permalink
Adjust to core changes (openhab#14952)
Browse files Browse the repository at this point in the history
Signed-off-by: Jan N. Klug <[email protected]>
Signed-off-by: Jørgen Austvik <[email protected]>
  • Loading branch information
J-N-K authored and austvik committed Mar 27, 2024
1 parent d6d5ea5 commit 4138a3f
Show file tree
Hide file tree
Showing 14 changed files with 124 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,10 @@ public Iterable<HistoricItem> query(FilterCriteria filter) {
// Proceed with query
//
String itemName = filter.getItemName();
if (itemName == null) {
logger.warn("Item name is missing in filter {}", filter);
return List.of();
}
Item item = getItemFromRegistry(itemName);
if (item == null) {
logger.warn("Could not get item {} from registry! Returning empty query results.", itemName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ public static QueryEnhancedRequest createQueryExpression(Class<? extends DynamoD
}
QueryEnhancedRequest.Builder queryBuilder = QueryEnhancedRequest.builder()
.scanIndexForward(filter.getOrdering() == Ordering.ASCENDING);
addFilterbyItemAndTimeFilter(queryBuilder, expectedTableSchema, filter.getItemName(), filter);
String itemName = filter.getItemName();
if (itemName == null) {
throw new IllegalArgumentException("Item name not set");
}
addFilterbyItemAndTimeFilter(queryBuilder, expectedTableSchema, itemName, filter);
addStateFilter(queryBuilder, expectedTableSchema, item, dtoClass, filter);
addProjection(dtoClass, expectedTableSchema, queryBuilder);
return queryBuilder.build();
Expand Down Expand Up @@ -154,26 +158,25 @@ private static void addStateFilter(QueryEnhancedRequest.Builder queryBuilder,

private static void addFilterbyItemAndTimeFilter(QueryEnhancedRequest.Builder queryBuilder,
ExpectedTableSchema expectedTableSchema, String partition, final FilterCriteria filter) {
boolean hasBegin = filter.getBeginDate() != null;
boolean hasEnd = filter.getEndDate() != null;
ZonedDateTime begin = filter.getBeginDate();
ZonedDateTime end = filter.getEndDate();
boolean legacy = expectedTableSchema == ExpectedTableSchema.LEGACY;

AttributeConverter<ZonedDateTime> timeConverter = AbstractDynamoDBItem.getTimestampConverter(legacy);

if (!hasBegin && !hasEnd) {
// No need to place time filter filter but we do filter by partition
if (begin == null && end == null) {
// No need to place time filter, but we do filter by partition
queryBuilder.queryConditional(QueryConditional.keyEqualTo(k -> k.partitionValue(partition)));
} else if (hasBegin && !hasEnd) {
queryBuilder.queryConditional(QueryConditional.sortGreaterThan(
k -> k.partitionValue(partition).sortValue(timeConverter.transformFrom(filter.getBeginDate()))));
} else if (!hasBegin && hasEnd) {
queryBuilder.queryConditional(QueryConditional.sortLessThan(
k -> k.partitionValue(partition).sortValue(timeConverter.transformFrom(filter.getEndDate()))));
} else {
assert hasBegin && hasEnd; // invariant
} else if (begin != null && end == null) {
queryBuilder.queryConditional(QueryConditional
.sortGreaterThan(k -> k.partitionValue(partition).sortValue(timeConverter.transformFrom(begin))));
} else if (begin == null && end != null) {
queryBuilder.queryConditional(QueryConditional
.sortLessThan(k -> k.partitionValue(partition).sortValue(timeConverter.transformFrom(end))));
} else if (begin != null && end != null) {
queryBuilder.queryConditional(QueryConditional.sortBetween(
k -> k.partitionValue(partition).sortValue(timeConverter.transformFrom(filter.getBeginDate())),
k -> k.partitionValue(partition).sortValue(timeConverter.transformFrom(filter.getEndDate()))));
k -> k.partitionValue(partition).sortValue(timeConverter.transformFrom(begin)),
k -> k.partitionValue(partition).sortValue(timeConverter.transformFrom(end))));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,10 @@ public void testQueryUsingNameAndStartAndEndWithGTOperator() {

FilterCriteria criteria = new FilterCriteria();
criteria.setOperator(Operator.GT);
criteria.setState(getQueryItemStateBetween());
State filterState = getQueryItemStateBetween();
if (filterState != null) {
criteria.setState(filterState);
}
criteria.setItemName(getItemName());
criteria.setBeginDate(beforeStore);
criteria.setEndDate(afterStore2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
Expand Down Expand Up @@ -70,7 +71,7 @@ public void testPagingFirstPage() {
waitForAssert(() -> {
FilterCriteria criteria = new FilterCriteria();
criteria.setItemName(NAME);
criteria.setBeginDate(storeStart);
criteria.setBeginDate(Objects.requireNonNull(storeStart));
criteria.setOrdering(Ordering.ASCENDING);
criteria.setPageNumber(0);
criteria.setPageSize(3);
Expand All @@ -84,7 +85,7 @@ public void testPagingSecondPage() {
waitForAssert(() -> {
FilterCriteria criteria = new FilterCriteria();
criteria.setItemName(NAME);
criteria.setBeginDate(storeStart);
criteria.setBeginDate(Objects.requireNonNull(storeStart));
criteria.setOrdering(Ordering.ASCENDING);
criteria.setPageNumber(1);
criteria.setPageSize(3);
Expand All @@ -98,7 +99,7 @@ public void testPagingPagePartialPage() {
waitForAssert(() -> {
FilterCriteria criteria = new FilterCriteria();
criteria.setItemName(NAME);
criteria.setBeginDate(storeStart);
criteria.setBeginDate(Objects.requireNonNull(storeStart));
criteria.setOrdering(Ordering.ASCENDING);
criteria.setPageNumber(3);
criteria.setPageSize(3);
Expand All @@ -112,7 +113,7 @@ public void testPagingPageOutOfBounds() {
waitForAssert(() -> {
FilterCriteria criteria = new FilterCriteria();
criteria.setItemName(NAME);
criteria.setBeginDate(storeStart);
criteria.setBeginDate(Objects.requireNonNull(storeStart));
criteria.setOrdering(Ordering.ASCENDING);
criteria.setPageNumber(4);
criteria.setPageSize(3);
Expand All @@ -126,7 +127,7 @@ public void testPagingPage0Descending() {
waitForAssert(() -> {
FilterCriteria criteria = new FilterCriteria();
criteria.setItemName(NAME);
criteria.setBeginDate(storeStart);
criteria.setBeginDate(Objects.requireNonNull(storeStart));
criteria.setOrdering(Ordering.DESCENDING);
criteria.setPageNumber(0);
criteria.setPageSize(3);
Expand All @@ -140,7 +141,7 @@ public void testPagingPage0HugePageSize() {
waitForAssert(() -> {
FilterCriteria criteria = new FilterCriteria();
criteria.setItemName(NAME);
criteria.setBeginDate(storeStart);
criteria.setBeginDate(Objects.requireNonNull(storeStart));
criteria.setOrdering(Ordering.ASCENDING);
criteria.setPageNumber(0);
criteria.setPageSize(900);
Expand All @@ -154,7 +155,7 @@ public void testPagingFirstPageWithFilter() {
waitForAssert(() -> {
FilterCriteria criteria = new FilterCriteria();
criteria.setItemName(NAME);
criteria.setBeginDate(storeStart);
criteria.setBeginDate(Objects.requireNonNull(storeStart));
criteria.setOrdering(Ordering.ASCENDING);
criteria.setPageNumber(0);
criteria.setPageSize(3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,12 @@ private void assertQueryFilterByState(String item, @Nullable Operator operator,
FilterCriteria criteria = new FilterCriteria();
criteria.setOrdering(Ordering.ASCENDING);
criteria.setItemName(item);
criteria.setOperator(operator);
criteria.setState(state);
if (operator != null) {
criteria.setOperator(operator);
}
if (state != null) {
criteria.setState(state);
}
@SuppressWarnings("null")
Iterable<HistoricItem> iterable = BaseIntegrationTest.service.query(criteria);
List<State> actualStatesList = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ public Iterable<HistoricItem> query(FilterCriteria filter) {
"Query-Filter: itemname: {}, ordering: {}, state: {}, operator: {}, getBeginDate: {}, getEndDate: {}, getPageSize: {}, getPageNumber: {}",
filter.getItemName(), filter.getOrdering().toString(), filter.getState(), filter.getOperator(),
filter.getBeginDate(), filter.getEndDate(), filter.getPageSize(), filter.getPageNumber());
if (filter.getItemName() == null) {
logger.warn("Item name is missing in filter {}", filter);
return List.of();
}
String query = influxDBRepository.createQueryCreator().createQuery(filter,
configuration.getRetentionPolicy());
logger.trace("Query {}", query);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import static org.openhab.persistence.influxdb.internal.InfluxDBConstants.*;
import static org.openhab.persistence.influxdb.internal.InfluxDBStateConvertUtils.stateToObject;

import java.util.Objects;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.influxdb.dto.Query;
Expand All @@ -24,6 +26,7 @@
import org.influxdb.querybuilder.Where;
import org.influxdb.querybuilder.clauses.SimpleClause;
import org.openhab.core.persistence.FilterCriteria;
import org.openhab.core.types.State;
import org.openhab.persistence.influxdb.internal.FilterCriteriaQueryCreator;
import org.openhab.persistence.influxdb.internal.InfluxDBConfiguration;
import org.openhab.persistence.influxdb.internal.InfluxDBMetadataService;
Expand All @@ -48,7 +51,7 @@ public InfluxDB1FilterCriteriaQueryCreatorImpl(InfluxDBConfiguration configurati

@Override
public String createQuery(FilterCriteria criteria, String retentionPolicy) {
final String itemName = criteria.getItemName();
final String itemName = Objects.requireNonNull(criteria.getItemName()); // we checked non-null before
final String tableName = getTableName(itemName);
final boolean hasCriteriaName = itemName != null;

Expand All @@ -68,10 +71,10 @@ public String createQuery(FilterCriteria criteria, String retentionPolicy) {
where.and(BuiltQuery.QueryBuilder.lte(COLUMN_TIME_NAME_V1, criteria.getEndDate().toInstant().toString()));
}

if (criteria.getState() != null && criteria.getOperator() != null) {
State filterState = criteria.getState();
if (filterState != null && criteria.getOperator() != null) {
where.and(new SimpleClause(COLUMN_VALUE_NAME_V1,
getOperationSymbol(criteria.getOperator(), InfluxDBVersion.V1),
stateToObject(criteria.getState())));
getOperationSymbol(criteria.getOperator(), InfluxDBVersion.V1), stateToObject(filterState)));
}

if (criteria.getOrdering() == FilterCriteria.Ordering.DESCENDING) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
import static org.openhab.persistence.influxdb.internal.InfluxDBStateConvertUtils.stateToObject;

import java.time.temporal.ChronoUnit;
import java.util.Objects;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.persistence.FilterCriteria;
import org.openhab.core.types.State;
import org.openhab.persistence.influxdb.internal.FilterCriteriaQueryCreator;
import org.openhab.persistence.influxdb.internal.InfluxDBConfiguration;
import org.openhab.persistence.influxdb.internal.InfluxDBMetadataService;
Expand Down Expand Up @@ -63,23 +65,22 @@ public String createQuery(FilterCriteria criteria, String retentionPolicy) {
}
flux = range;

String itemName = criteria.getItemName();
if (itemName != null) {
String name = influxDBMetadataService.getMeasurementNameOrDefault(itemName, itemName);
String measurementName = configuration.isReplaceUnderscore() ? name.replace('_', '.') : name;
flux = flux.filter(measurement().equal(measurementName));
if (!measurementName.equals(itemName)) {
flux = flux.filter(tag(TAG_ITEM_NAME).equal(itemName));
flux = flux.keep(new String[] { FIELD_MEASUREMENT_NAME, COLUMN_TIME_NAME_V2, COLUMN_VALUE_NAME_V2,
TAG_ITEM_NAME });
} else {
flux = flux.keep(new String[] { FIELD_MEASUREMENT_NAME, COLUMN_TIME_NAME_V2, COLUMN_VALUE_NAME_V2 });
}
String itemName = Objects.requireNonNull(criteria.getItemName()); // we checked non-null before
String name = influxDBMetadataService.getMeasurementNameOrDefault(itemName, itemName);
String measurementName = configuration.isReplaceUnderscore() ? name.replace('_', '.') : name;
flux = flux.filter(measurement().equal(measurementName));
if (!measurementName.equals(itemName)) {
flux = flux.filter(tag(TAG_ITEM_NAME).equal(itemName));
flux = flux.keep(
new String[] { FIELD_MEASUREMENT_NAME, COLUMN_TIME_NAME_V2, COLUMN_VALUE_NAME_V2, TAG_ITEM_NAME });
} else {
flux = flux.keep(new String[] { FIELD_MEASUREMENT_NAME, COLUMN_TIME_NAME_V2, COLUMN_VALUE_NAME_V2 });
}

if (criteria.getState() != null && criteria.getOperator() != null) {
State filterState = criteria.getState();
if (filterState != null && criteria.getOperator() != null) {
Restrictions restrictions = Restrictions.and(Restrictions.field().equal(FIELD_VALUE_NAME),
Restrictions.value().custom(stateToObject(criteria.getState()),
Restrictions.value().custom(stateToObject(filterState),
getOperationSymbol(criteria.getOperator(), InfluxDBVersion.V2)));
flux = flux.filter(restrictions);
}
Expand Down
Loading

0 comments on commit 4138a3f

Please sign in to comment.