]> git.basschouten.com Git - openhab-addons.git/blob
103ed646089a38359a024567ee04e1236adcffad
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.persistence.influxdb.internal.influx2;
14
15 import static com.influxdb.query.dsl.functions.restriction.Restrictions.measurement;
16 import static org.openhab.persistence.influxdb.internal.InfluxDBConstants.*;
17 import static org.openhab.persistence.influxdb.internal.InfluxDBStateConvertUtils.stateToObject;
18
19 import java.time.temporal.ChronoUnit;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.openhab.core.persistence.FilterCriteria;
23 import org.openhab.persistence.influxdb.internal.FilterCriteriaQueryCreator;
24 import org.openhab.persistence.influxdb.internal.InfluxDBVersion;
25
26 import com.influxdb.query.dsl.Flux;
27 import com.influxdb.query.dsl.functions.RangeFlux;
28 import com.influxdb.query.dsl.functions.restriction.Restrictions;
29
30 /**
31  * Implementation of {@link FilterCriteriaQueryCreator} for InfluxDB 2.0
32  *
33  * @author Joan Pujol Espinar - Initial contribution
34  */
35 @NonNullByDefault
36 public class Influx2FilterCriteriaQueryCreatorImpl implements FilterCriteriaQueryCreator {
37     @Override
38     public String createQuery(FilterCriteria criteria, String retentionPolicy) {
39         Flux flux = Flux.from(retentionPolicy);
40
41         RangeFlux range = flux.range();
42         if (criteria.getBeginDate() != null) {
43             range = range.withStart(criteria.getBeginDate().toInstant());
44         } else {
45             range = flux.range(-100L, ChronoUnit.YEARS); // Flux needs a mandatory start range
46         }
47         if (criteria.getEndDate() != null) {
48             range = range.withStop(criteria.getEndDate().toInstant());
49         }
50         flux = range;
51
52         if (criteria.getItemName() != null) {
53             flux = flux.filter(measurement().equal(criteria.getItemName()));
54         }
55
56         if (criteria.getState() != null && criteria.getOperator() != null) {
57             Restrictions restrictions = Restrictions.and(Restrictions.field().equal(FIELD_VALUE_NAME),
58                     Restrictions.value().custom(stateToObject(criteria.getState()),
59                             getOperationSymbol(criteria.getOperator(), InfluxDBVersion.V2)));
60             flux = flux.filter(restrictions);
61         }
62
63         if (criteria.getOrdering() != null) {
64             boolean desc = criteria.getOrdering() == FilterCriteria.Ordering.DESCENDING;
65             flux = flux.sort().withDesc(desc).withColumns(new String[] { COLUMN_TIME_NAME_V2 });
66         }
67
68         if (criteria.getPageSize() != Integer.MAX_VALUE) {
69             flux = flux.limit(criteria.getPageSize()).withPropertyValue("offset",
70                     criteria.getPageNumber() * criteria.getPageSize());
71         }
72
73         return flux.toString();
74     }
75 }