]> git.basschouten.com Git - openhab-addons.git/blob
03b81a5940deb61ad88b42f4722c40792537f935
[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         if (criteria.getBeginDate() != null || criteria.getEndDate() != null) {
42             RangeFlux range = flux.range();
43             if (criteria.getBeginDate() != null) {
44                 range = range.withStart(criteria.getBeginDate().toInstant());
45             }
46             if (criteria.getEndDate() != null) {
47                 range = range.withStop(criteria.getEndDate().toInstant());
48             }
49             flux = range;
50         } else {
51             flux = flux.range(-100L, ChronoUnit.YEARS); // Flux needs a mandatory range
52         }
53
54         if (criteria.getItemName() != null) {
55             flux = flux.filter(measurement().equal(criteria.getItemName()));
56         }
57
58         if (criteria.getState() != null && criteria.getOperator() != null) {
59             Restrictions restrictions = Restrictions.and(Restrictions.field().equal(FIELD_VALUE_NAME),
60                     Restrictions.value().custom(stateToObject(criteria.getState()),
61                             getOperationSymbol(criteria.getOperator(), InfluxDBVersion.V2)));
62             flux = flux.filter(restrictions);
63         }
64
65         if (criteria.getOrdering() != null) {
66             boolean desc = criteria.getOrdering() == FilterCriteria.Ordering.DESCENDING;
67             flux = flux.sort().withDesc(desc).withColumns(new String[] { COLUMN_TIME_NAME_V2 });
68         }
69
70         if (criteria.getPageSize() != Integer.MAX_VALUE) {
71             flux = flux.limit(criteria.getPageSize()).withPropertyValue("offset",
72                     criteria.getPageNumber() * criteria.getPageSize());
73         }
74
75         return flux.toString();
76     }
77 }