]> git.basschouten.com Git - openhab-addons.git/blob
4e7dfb33daa4f7af00cf6e02a55927cad0699223
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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;
14
15 import static org.openhab.persistence.influxdb.internal.InfluxDBConstants.*;
16
17 import java.time.Instant;
18 import java.time.ZoneId;
19 import java.time.ZonedDateTime;
20 import java.util.ArrayList;
21 import java.util.HashMap;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Locale;
25 import java.util.Map;
26 import java.util.Objects;
27 import java.util.Set;
28 import java.util.concurrent.BlockingQueue;
29 import java.util.concurrent.CompletableFuture;
30 import java.util.concurrent.LinkedBlockingQueue;
31 import java.util.concurrent.ScheduledFuture;
32 import java.util.concurrent.TimeUnit;
33 import java.util.stream.Collectors;
34
35 import org.eclipse.jdt.annotation.NonNullByDefault;
36 import org.eclipse.jdt.annotation.Nullable;
37 import org.openhab.core.common.ThreadPoolManager;
38 import org.openhab.core.config.core.ConfigurableService;
39 import org.openhab.core.items.Item;
40 import org.openhab.core.items.ItemFactory;
41 import org.openhab.core.items.ItemRegistry;
42 import org.openhab.core.items.ItemUtil;
43 import org.openhab.core.persistence.FilterCriteria;
44 import org.openhab.core.persistence.HistoricItem;
45 import org.openhab.core.persistence.ModifiablePersistenceService;
46 import org.openhab.core.persistence.PersistenceItemInfo;
47 import org.openhab.core.persistence.PersistenceService;
48 import org.openhab.core.persistence.QueryablePersistenceService;
49 import org.openhab.core.persistence.strategy.PersistenceStrategy;
50 import org.openhab.core.types.State;
51 import org.openhab.core.types.UnDefType;
52 import org.openhab.persistence.influxdb.internal.FilterCriteriaQueryCreator;
53 import org.openhab.persistence.influxdb.internal.InfluxDBConfiguration;
54 import org.openhab.persistence.influxdb.internal.InfluxDBHistoricItem;
55 import org.openhab.persistence.influxdb.internal.InfluxDBMetadataService;
56 import org.openhab.persistence.influxdb.internal.InfluxDBPersistentItemInfo;
57 import org.openhab.persistence.influxdb.internal.InfluxDBRepository;
58 import org.openhab.persistence.influxdb.internal.InfluxDBStateConvertUtils;
59 import org.openhab.persistence.influxdb.internal.InfluxPoint;
60 import org.openhab.persistence.influxdb.internal.influx1.InfluxDB1RepositoryImpl;
61 import org.openhab.persistence.influxdb.internal.influx2.InfluxDB2RepositoryImpl;
62 import org.osgi.framework.Constants;
63 import org.osgi.service.component.annotations.Activate;
64 import org.osgi.service.component.annotations.Component;
65 import org.osgi.service.component.annotations.Deactivate;
66 import org.osgi.service.component.annotations.Reference;
67 import org.osgi.service.component.annotations.ReferenceCardinality;
68 import org.osgi.service.component.annotations.ReferencePolicy;
69 import org.slf4j.Logger;
70 import org.slf4j.LoggerFactory;
71
72 /**
73  * This is the implementation of the InfluxDB {@link PersistenceService}. It
74  * persists item values using the <a href="http://influxdb.org">InfluxDB time
75  * series database. The states ( {@link State}) of an {@link Item} are persisted
76  * by default in a time series with names equal to the name of the item.
77  *
78  * This addon supports 1.X and 2.X versions, as two versions are incompatible
79  * and use different drivers the specific code for each version is accessed by
80  * {@link InfluxDBRepository} and {@link FilterCriteriaQueryCreator} interfaces
81  * and specific implementation reside in
82  * {@link org.openhab.persistence.influxdb.internal.influx1} and
83  * {@link org.openhab.persistence.influxdb.internal.influx2} packages
84  *
85  * @author Theo Weiss - Initial contribution, rewrite of
86  *         org.openhab.persistence.influxdb
87  * @author Joan Pujol Espinar - Addon rewrite refactoring code and adding
88  *         support for InfluxDB 2.0. Some tag code is based from not integrated
89  *         branch from Dominik Vorreiter
90  */
91 @NonNullByDefault
92 @Component(service = { PersistenceService.class,
93         QueryablePersistenceService.class }, configurationPid = "org.openhab.influxdb", //
94         property = Constants.SERVICE_PID + "=org.openhab.influxdb")
95 @ConfigurableService(category = "persistence", label = "InfluxDB Persistence Service", description_uri = InfluxDBPersistenceService.CONFIG_URI)
96 public class InfluxDBPersistenceService implements ModifiablePersistenceService {
97     public static final String SERVICE_NAME = "influxdb";
98
99     private final Logger logger = LoggerFactory.getLogger(InfluxDBPersistenceService.class);
100
101     private static final int COMMIT_INTERVAL = 3; // in s
102     protected static final String CONFIG_URI = "persistence:influxdb";
103
104     // External dependencies
105     private final ItemRegistry itemRegistry;
106     private final InfluxDBMetadataService influxDBMetadataService;
107
108     private final InfluxDBConfiguration configuration;
109     private final InfluxDBRepository influxDBRepository;
110     private boolean serviceActivated;
111
112     // storage
113     private final ScheduledFuture<?> storeJob;
114     private final BlockingQueue<InfluxPoint> pointsQueue = new LinkedBlockingQueue<>();
115
116     // conversion
117     private final Set<ItemFactory> itemFactories = new HashSet<>();
118     private Map<String, Class<? extends State>> desiredClasses = new HashMap<>();
119
120     @Activate
121     public InfluxDBPersistenceService(final @Reference ItemRegistry itemRegistry,
122             final @Reference InfluxDBMetadataService influxDBMetadataService, Map<String, Object> config) {
123         this.itemRegistry = itemRegistry;
124         this.influxDBMetadataService = influxDBMetadataService;
125         this.configuration = new InfluxDBConfiguration(config);
126         if (configuration.isValid()) {
127             this.influxDBRepository = createInfluxDBRepository();
128             this.influxDBRepository.connect();
129             this.storeJob = ThreadPoolManager.getScheduledPool("org.openhab.influxdb")
130                     .scheduleWithFixedDelay(this::commit, COMMIT_INTERVAL, COMMIT_INTERVAL, TimeUnit.SECONDS);
131             serviceActivated = true;
132         } else {
133             throw new IllegalArgumentException("Configuration invalid.");
134         }
135
136         logger.info("InfluxDB persistence service started.");
137     }
138
139     // Visible for testing
140     protected InfluxDBRepository createInfluxDBRepository() throws IllegalArgumentException {
141         return switch (configuration.getVersion()) {
142             case V1 -> new InfluxDB1RepositoryImpl(configuration, influxDBMetadataService);
143             case V2 -> new InfluxDB2RepositoryImpl(configuration, influxDBMetadataService);
144             default -> throw new IllegalArgumentException("Failed to instantiate repository.");
145         };
146     }
147
148     /**
149      * Disconnect from database when service is deactivated
150      */
151     @Deactivate
152     public void deactivate() {
153         serviceActivated = false;
154
155         storeJob.cancel(false);
156         commit(); // ensure we at least tried to store the data;
157
158         if (!pointsQueue.isEmpty()) {
159             logger.warn("InfluxDB failed to finally store {} points.", pointsQueue.size());
160         }
161
162         influxDBRepository.disconnect();
163         logger.info("InfluxDB persistence service stopped.");
164     }
165
166     @Override
167     public String getId() {
168         return SERVICE_NAME;
169     }
170
171     @Override
172     public String getLabel(@Nullable Locale locale) {
173         return "InfluxDB persistence layer";
174     }
175
176     @Override
177     public Set<PersistenceItemInfo> getItemInfo() {
178         if (checkConnection()) {
179             return influxDBRepository.getStoredItemsCount().entrySet().stream().map(InfluxDBPersistentItemInfo::new)
180                     .collect(Collectors.toUnmodifiableSet());
181         } else {
182             logger.info("getItemInfo ignored, InfluxDB is not connected");
183             return Set.of();
184         }
185     }
186
187     @Override
188     public void store(Item item) {
189         store(item, null);
190     }
191
192     @Override
193     public void store(Item item, @Nullable String alias) {
194         store(item, ZonedDateTime.now(), item.getState(), alias);
195     }
196
197     @Override
198     public void store(Item item, ZonedDateTime date, State state) {
199         store(item, date, state, null);
200     }
201
202     public void store(Item item, ZonedDateTime date, State state, @Nullable String alias) {
203         if (!serviceActivated) {
204             logger.warn("InfluxDB service not ready. Storing {} rejected.", item);
205             return;
206         }
207         convert(item, state, date.toInstant(), null).thenAccept(point -> {
208             if (point == null) {
209                 logger.trace("Ignoring item {}, conversion to an InfluxDB point failed.", item.getName());
210                 return;
211             }
212             if (pointsQueue.offer(point)) {
213                 logger.trace("Queued {} for item {}", point, item);
214             } else {
215                 logger.warn("Failed to queue {} for item {}", point, item);
216             }
217         });
218     }
219
220     @Override
221     public boolean remove(FilterCriteria filter) throws IllegalArgumentException {
222         if (serviceActivated && checkConnection()) {
223             if (filter.getItemName() == null) {
224                 logger.warn("Item name is missing in filter {} when trying to remove data.", filter);
225                 return false;
226             }
227             return influxDBRepository.remove(filter);
228         } else {
229             logger.debug("Remove query {} ignored, InfluxDB is not connected.", filter);
230             return false;
231         }
232     }
233
234     @Override
235     public Iterable<HistoricItem> query(FilterCriteria filter) {
236         if (serviceActivated && checkConnection()) {
237             logger.trace(
238                     "Query-Filter: itemname: {}, ordering: {}, state: {},  operator: {}, getBeginDate: {}, getEndDate: {}, getPageSize: {}, getPageNumber: {}",
239                     filter.getItemName(), filter.getOrdering().toString(), filter.getState(), filter.getOperator(),
240                     filter.getBeginDate(), filter.getEndDate(), filter.getPageSize(), filter.getPageNumber());
241             if (filter.getItemName() == null) {
242                 logger.warn("Item name is missing in filter {} when querying data.", filter);
243                 return List.of();
244             }
245
246             List<InfluxDBRepository.InfluxRow> results = influxDBRepository.query(filter,
247                     configuration.getRetentionPolicy());
248             return results.stream().map(this::mapRowToHistoricItem).collect(Collectors.toList());
249         } else {
250             logger.debug("Query for persisted data ignored, InfluxDB is not connected");
251             return List.of();
252         }
253     }
254
255     private HistoricItem mapRowToHistoricItem(InfluxDBRepository.InfluxRow row) {
256         State state = InfluxDBStateConvertUtils.objectToState(row.value(), row.itemName(), itemRegistry);
257         return new InfluxDBHistoricItem(row.itemName(), state,
258                 ZonedDateTime.ofInstant(row.time(), ZoneId.systemDefault()));
259     }
260
261     @Override
262     public List<PersistenceStrategy> getDefaultStrategies() {
263         return List.of(PersistenceStrategy.Globals.RESTORE, PersistenceStrategy.Globals.CHANGE);
264     }
265
266     /**
267      * check connection and try reconnect
268      *
269      * @return true if connected
270      */
271     private boolean checkConnection() {
272         if (influxDBRepository.isConnected()) {
273             return true;
274         } else if (serviceActivated) {
275             logger.debug("Connection lost, trying re-connection");
276             return influxDBRepository.connect();
277         }
278         return false;
279     }
280
281     private void commit() {
282         if (!pointsQueue.isEmpty() && checkConnection()) {
283             List<InfluxPoint> points = new ArrayList<>();
284             pointsQueue.drainTo(points);
285             if (!influxDBRepository.write(points)) {
286                 logger.warn("Re-queuing {} elements, failed to write batch.", points.size());
287                 pointsQueue.addAll(points);
288             } else {
289                 logger.trace("Wrote {} elements to database", points.size());
290             }
291         }
292     }
293
294     /**
295      * Convert incoming data to an {@link InfluxPoint} for further processing. This is needed because storage is
296      * asynchronous and the item data may have changed.
297      * <p />
298      * The method is package-private for testing.
299      *
300      * @param item the {@link Item} that needs conversion
301      * @param storeAlias an (optional) alias for the item
302      * @return a {@link CompletableFuture} that contains either <code>null</code> for item states that cannot be
303      *         converted or the corresponding {@link InfluxPoint}
304      */
305     CompletableFuture<@Nullable InfluxPoint> convert(Item item, State state, Instant timeStamp,
306             @Nullable String storeAlias) {
307         String itemName = item.getName();
308         String itemLabel = item.getLabel();
309         String category = item.getCategory();
310         String itemType = item.getType();
311
312         if (state instanceof UnDefType) {
313             return CompletableFuture.completedFuture(null);
314         }
315
316         return CompletableFuture.supplyAsync(() -> {
317             String measurementName = storeAlias != null && !storeAlias.isBlank() ? storeAlias : itemName;
318             measurementName = influxDBMetadataService.getMeasurementNameOrDefault(itemName, measurementName);
319
320             if (configuration.isReplaceUnderscore()) {
321                 measurementName = measurementName.replace('_', '.');
322             }
323
324             State storeState = Objects
325                     .requireNonNullElse(state.as(desiredClasses.get(ItemUtil.getMainItemType(itemType))), state);
326             Object value = InfluxDBStateConvertUtils.stateToObject(storeState);
327
328             InfluxPoint.Builder pointBuilder = InfluxPoint.newBuilder(measurementName).withTime(timeStamp)
329                     .withValue(value).withTag(TAG_ITEM_NAME, itemName);
330
331             if (configuration.isAddCategoryTag()) {
332                 String categoryName = Objects.requireNonNullElse(category, "n/a");
333                 pointBuilder.withTag(TAG_CATEGORY_NAME, categoryName);
334             }
335
336             if (configuration.isAddTypeTag()) {
337                 pointBuilder.withTag(TAG_TYPE_NAME, itemType);
338             }
339
340             if (configuration.isAddLabelTag()) {
341                 String labelName = Objects.requireNonNullElse(itemLabel, "n/a");
342                 pointBuilder.withTag(TAG_LABEL_NAME, labelName);
343             }
344
345             influxDBMetadataService.getMetaData(itemName)
346                     .ifPresent(metadata -> metadata.getConfiguration().forEach(pointBuilder::withTag));
347
348             return pointBuilder.build();
349         });
350     }
351
352     @Reference(cardinality = ReferenceCardinality.AT_LEAST_ONE, policy = ReferencePolicy.DYNAMIC)
353     public void setItemFactory(ItemFactory itemFactory) {
354         itemFactories.add(itemFactory);
355         calculateItemTypeClasses();
356     }
357
358     public void unsetItemFactory(ItemFactory itemFactory) {
359         itemFactories.remove(itemFactory);
360         calculateItemTypeClasses();
361     }
362
363     private synchronized void calculateItemTypeClasses() {
364         Map<String, Class<? extends State>> desiredClasses = new HashMap<>();
365         itemFactories.forEach(factory -> {
366             for (String itemType : factory.getSupportedItemTypes()) {
367                 Item item = factory.createItem(itemType, "influxItem");
368                 if (item != null) {
369                     item.getAcceptedCommandTypes().stream()
370                             .filter(commandType -> commandType.isAssignableFrom(State.class)).findFirst()
371                             .map(commandType -> (Class<? extends State>) commandType.asSubclass(State.class))
372                             .ifPresent(desiredClass -> desiredClasses.put(itemType, desiredClass));
373                 }
374             }
375         });
376         this.desiredClasses = desiredClasses;
377     }
378 }