]> git.basschouten.com Git - openhab-addons.git/blob
c80ef18867b1264e22c50d0f6d4d519355475026
[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.rrd4j.internal;
14
15 import java.io.IOException;
16 import java.nio.file.Files;
17 import java.nio.file.Path;
18 import java.time.Instant;
19 import java.time.ZoneId;
20 import java.time.ZonedDateTime;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.Locale;
27 import java.util.Map;
28 import java.util.Set;
29 import java.util.concurrent.ConcurrentHashMap;
30 import java.util.concurrent.Executors;
31 import java.util.concurrent.RejectedExecutionException;
32 import java.util.concurrent.ScheduledExecutorService;
33 import java.util.concurrent.ScheduledFuture;
34 import java.util.concurrent.TimeUnit;
35
36 import javax.measure.Quantity;
37 import javax.measure.Unit;
38
39 import org.eclipse.jdt.annotation.NonNullByDefault;
40 import org.eclipse.jdt.annotation.Nullable;
41 import org.openhab.core.OpenHAB;
42 import org.openhab.core.common.NamedThreadFactory;
43 import org.openhab.core.items.GroupItem;
44 import org.openhab.core.items.Item;
45 import org.openhab.core.items.ItemNotFoundException;
46 import org.openhab.core.items.ItemRegistry;
47 import org.openhab.core.items.ItemUtil;
48 import org.openhab.core.library.CoreItemFactory;
49 import org.openhab.core.library.items.ColorItem;
50 import org.openhab.core.library.items.ContactItem;
51 import org.openhab.core.library.items.DimmerItem;
52 import org.openhab.core.library.items.NumberItem;
53 import org.openhab.core.library.items.RollershutterItem;
54 import org.openhab.core.library.items.SwitchItem;
55 import org.openhab.core.library.types.DecimalType;
56 import org.openhab.core.library.types.OnOffType;
57 import org.openhab.core.library.types.OpenClosedType;
58 import org.openhab.core.library.types.PercentType;
59 import org.openhab.core.library.types.QuantityType;
60 import org.openhab.core.persistence.FilterCriteria;
61 import org.openhab.core.persistence.FilterCriteria.Ordering;
62 import org.openhab.core.persistence.HistoricItem;
63 import org.openhab.core.persistence.PersistenceItemInfo;
64 import org.openhab.core.persistence.PersistenceService;
65 import org.openhab.core.persistence.QueryablePersistenceService;
66 import org.openhab.core.persistence.strategy.PersistenceCronStrategy;
67 import org.openhab.core.persistence.strategy.PersistenceStrategy;
68 import org.openhab.core.types.State;
69 import org.osgi.service.component.annotations.Activate;
70 import org.osgi.service.component.annotations.Component;
71 import org.osgi.service.component.annotations.ConfigurationPolicy;
72 import org.osgi.service.component.annotations.Modified;
73 import org.osgi.service.component.annotations.Reference;
74 import org.rrd4j.ConsolFun;
75 import org.rrd4j.DsType;
76 import org.rrd4j.core.FetchData;
77 import org.rrd4j.core.FetchRequest;
78 import org.rrd4j.core.RrdDb;
79 import org.rrd4j.core.RrdDb.Builder;
80 import org.rrd4j.core.RrdDbPool;
81 import org.rrd4j.core.RrdDef;
82 import org.rrd4j.core.Sample;
83 import org.slf4j.Logger;
84 import org.slf4j.LoggerFactory;
85
86 /**
87  * This is the implementation of the RRD4j {@link PersistenceService}. To learn
88  * more about RRD4j please visit their
89  * <a href="https://github.com/rrd4j/rrd4j">website</a>.
90  *
91  * @author Kai Kreuzer - Initial contribution
92  * @author Jan N. Klug - some improvements
93  * @author Karel Goderis - remove TimerThread dependency
94  */
95 @NonNullByDefault
96 @Component(service = { PersistenceService.class,
97         QueryablePersistenceService.class }, configurationPid = "org.openhab.rrd4j", configurationPolicy = ConfigurationPolicy.OPTIONAL)
98 public class RRD4jPersistenceService implements QueryablePersistenceService {
99
100     private static final String DEFAULT_OTHER = "default_other";
101     private static final String DEFAULT_NUMERIC = "default_numeric";
102     private static final String DEFAULT_QUANTIFIABLE = "default_quantifiable";
103
104     private static final Set<String> SUPPORTED_TYPES = Set.of(CoreItemFactory.SWITCH, CoreItemFactory.CONTACT,
105             CoreItemFactory.DIMMER, CoreItemFactory.NUMBER, CoreItemFactory.ROLLERSHUTTER, CoreItemFactory.COLOR);
106
107     private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(3,
108             new NamedThreadFactory("RRD4j"));
109
110     private final Map<String, RrdDefConfig> rrdDefs = new ConcurrentHashMap<>();
111
112     private static final String DATASOURCE_STATE = "state";
113
114     private static final Path DB_FOLDER = Path.of(OpenHAB.getUserDataFolder(), "persistence", "rrd4j").toAbsolutePath();
115
116     private static final RrdDbPool DATABASE_POOL = new RrdDbPool();
117
118     private final Logger logger = LoggerFactory.getLogger(RRD4jPersistenceService.class);
119
120     private final Map<String, ScheduledFuture<?>> scheduledJobs = new HashMap<>();
121
122     private final ItemRegistry itemRegistry;
123
124     public static Path getDatabasePath(String name) {
125         return DB_FOLDER.resolve(name + ".rrd");
126     }
127
128     public static RrdDbPool getDatabasePool() {
129         return DATABASE_POOL;
130     }
131
132     @Activate
133     public RRD4jPersistenceService(final @Reference ItemRegistry itemRegistry) {
134         this.itemRegistry = itemRegistry;
135     }
136
137     @Override
138     public String getId() {
139         return "rrd4j";
140     }
141
142     @Override
143     public String getLabel(@Nullable Locale locale) {
144         return "RRD4j";
145     }
146
147     @Override
148     public synchronized void store(final Item item, @Nullable final String alias) {
149         if (!isSupportedItemType(item)) {
150             logger.trace("Ignoring item '{}' since its type {} is not supported", item.getName(), item.getType());
151             return;
152         }
153         final String name = alias == null ? item.getName() : alias;
154
155         RrdDb db = null;
156         try {
157             db = getDB(name);
158         } catch (Exception e) {
159             logger.warn("Failed to open rrd4j database '{}' to store data ({})", name, e.toString());
160         }
161         if (db == null) {
162             return;
163         }
164
165         ConsolFun function = getConsolidationFunction(db);
166         long now = System.currentTimeMillis() / 1000;
167         if (function != ConsolFun.AVERAGE) {
168             try {
169                 // we store the last value again, so that the value change
170                 // in the database is not interpolated, but
171                 // happens right at this spot
172                 if (now - 1 > db.getLastUpdateTime()) {
173                     // only do it if there is not already a value
174                     double lastValue = db.getLastDatasourceValue(DATASOURCE_STATE);
175                     if (!Double.isNaN(lastValue)) {
176                         Sample sample = db.createSample();
177                         sample.setTime(now - 1);
178                         sample.setValue(DATASOURCE_STATE, lastValue);
179                         sample.update();
180                         logger.debug("Stored '{}' as value '{}' in rrd4j database (again)", name, lastValue);
181                     }
182                 }
183             } catch (IOException e) {
184                 logger.debug("Error storing last value (again): {}", e.getMessage());
185             }
186         }
187         try {
188             Sample sample = db.createSample();
189             sample.setTime(now);
190
191             Double value = null;
192
193             if (item instanceof NumberItem && item.getState() instanceof QuantityType) {
194                 NumberItem nItem = (NumberItem) item;
195                 QuantityType<?> qState = (QuantityType<?>) item.getState();
196                 Unit<? extends Quantity<?>> unit = nItem.getUnit();
197                 if (unit != null) {
198                     QuantityType<?> convertedState = qState.toUnit(unit);
199                     if (convertedState != null) {
200                         value = convertedState.doubleValue();
201                     } else {
202                         logger.warn(
203                                 "Failed to convert state '{}' to unit '{}'. Please check your item definition for correctness.",
204                                 qState, unit);
205                     }
206                 } else {
207                     value = qState.doubleValue();
208                 }
209             } else {
210                 DecimalType state = item.getStateAs(DecimalType.class);
211                 if (state != null) {
212                     value = state.toBigDecimal().doubleValue();
213                 }
214             }
215             if (value != null) {
216                 if (db.getDatasource(DATASOURCE_STATE).getType() == DsType.COUNTER) { // counter values must be
217                                                                                       // adjusted by stepsize
218                     value = value * db.getRrdDef().getStep();
219                 }
220                 sample.setValue(DATASOURCE_STATE, value);
221                 sample.update();
222                 logger.debug("Stored '{}' as value '{}' in rrd4j database", name, value);
223             }
224         } catch (IllegalArgumentException e) {
225             String message = e.getMessage();
226             if (message != null && message.contains("at least one second step is required")) {
227                 // we try to store the value one second later
228                 ScheduledFuture<?> job = scheduledJobs.get(name);
229                 if (job != null) {
230                     job.cancel(true);
231                     scheduledJobs.remove(name);
232                 }
233                 job = scheduler.schedule(() -> store(item, name), 1, TimeUnit.SECONDS);
234                 scheduledJobs.put(name, job);
235             } else {
236                 logger.warn("Could not persist '{}' to rrd4j database: {}", name, e.getMessage());
237             }
238         } catch (Exception e) {
239             logger.warn("Could not persist '{}' to rrd4j database: {}", name, e.getMessage());
240         }
241         try {
242             db.close();
243         } catch (IOException e) {
244             logger.debug("Error closing rrd4j database: {}", e.getMessage());
245         }
246     }
247
248     @Override
249     public void store(Item item) {
250         store(item, null);
251     }
252
253     @Override
254     public Iterable<HistoricItem> query(FilterCriteria filter) {
255         ZonedDateTime filterBeginDate = filter.getBeginDate();
256         ZonedDateTime filterEndDate = filter.getEndDate();
257         if (filterBeginDate != null && filterEndDate != null && filterBeginDate.isAfter(filterEndDate)) {
258             throw new IllegalArgumentException("begin (" + filterBeginDate + ") before end (" + filterEndDate + ")");
259         }
260
261         String itemName = filter.getItemName();
262         if (itemName == null) {
263             logger.warn("Item name is missing in filter {}", filter);
264             return List.of();
265         }
266
267         RrdDb db = null;
268         try {
269             db = getDB(itemName);
270         } catch (Exception e) {
271             logger.warn("Failed to open rrd4j database '{}' for querying ({})", itemName, e.toString());
272             return List.of();
273         }
274         if (db == null) {
275             logger.debug("Could not find item '{}' in rrd4j database", itemName);
276             return List.of();
277         }
278
279         Item item = null;
280         Unit<?> unit = null;
281         try {
282             item = itemRegistry.getItem(itemName);
283             if (item instanceof NumberItem) {
284                 // we already retrieve the unit here once as it is a very costly operation,
285                 // see https://github.com/openhab/openhab-addons/issues/8928
286                 unit = ((NumberItem) item).getUnit();
287             }
288         } catch (ItemNotFoundException e) {
289             logger.debug("Could not find item '{}' in registry", itemName);
290         }
291
292         long start = 0L;
293         long end = filterEndDate == null ? System.currentTimeMillis() / 1000
294                 : filterEndDate.toInstant().getEpochSecond();
295
296         try {
297             if (filterBeginDate == null) {
298                 // as rrd goes back for years and gets more and more
299                 // inaccurate, we only support descending order
300                 // and a single return value
301                 // if there is no begin date is given - this case is
302                 // required specifically for the historicState()
303                 // query, which we want to support
304                 if (filter.getOrdering() == Ordering.DESCENDING && filter.getPageSize() == 1
305                         && filter.getPageNumber() == 0) {
306                     if (filterEndDate == null) {
307                         // we are asked only for the most recent value!
308                         double lastValue = db.getLastDatasourceValue(DATASOURCE_STATE);
309                         if (!Double.isNaN(lastValue)) {
310                             HistoricItem rrd4jItem = new RRD4jItem(itemName, mapToState(lastValue, item, unit),
311                                     ZonedDateTime.ofInstant(Instant.ofEpochMilli(db.getLastArchiveUpdateTime() * 1000),
312                                             ZoneId.systemDefault()));
313                             return List.of(rrd4jItem);
314                         } else {
315                             return List.of();
316                         }
317                     } else {
318                         start = end;
319                     }
320                 } else {
321                     throw new UnsupportedOperationException(
322                             "rrd4j does not allow querys without a begin date, unless order is descending and a single value is requested");
323                 }
324             } else {
325                 start = filterBeginDate.toInstant().getEpochSecond();
326             }
327
328             // do not call method {@link RrdDb#createFetchRequest(ConsolFun, long, long, long)} if start > end to avoid
329             // an IAE to be thrown
330             if (start > end) {
331                 logger.debug("Could not query rrd4j database for item '{}': start ({}) > end ({})", itemName, start,
332                         end);
333                 return List.of();
334             }
335
336             FetchRequest request = db.createFetchRequest(getConsolidationFunction(db), start, end, 1);
337             FetchData result = request.fetchData();
338
339             List<HistoricItem> items = new ArrayList<>();
340             long ts = result.getFirstTimestamp();
341             long step = result.getRowCount() > 1 ? result.getStep() : 0;
342             for (double value : result.getValues(DATASOURCE_STATE)) {
343                 if (!Double.isNaN(value) && (((ts >= start) && (ts <= end)) || (start == end))) {
344                     RRD4jItem rrd4jItem = new RRD4jItem(itemName, mapToState(value, item, unit),
345                             ZonedDateTime.ofInstant(Instant.ofEpochSecond(ts), ZoneId.systemDefault()));
346                     items.add(rrd4jItem);
347                 }
348                 ts += step;
349             }
350             return items;
351         } catch (IOException e) {
352             logger.warn("Could not query rrd4j database for item '{}': {}", itemName, e.getMessage());
353             return List.of();
354         } finally {
355             try {
356                 db.close();
357             } catch (IOException e) {
358                 logger.debug("Error closing rrd4j database: {}", e.getMessage());
359             }
360         }
361     }
362
363     @Override
364     public Set<PersistenceItemInfo> getItemInfo() {
365         return Set.of();
366     }
367
368     protected synchronized @Nullable RrdDb getDB(String alias) {
369         RrdDb db = null;
370         Path path = getDatabasePath(alias);
371         try {
372             Builder builder = RrdDb.getBuilder();
373             builder.setPool(DATABASE_POOL);
374
375             if (Files.exists(path)) {
376                 // recreate the RrdDb instance from the file
377                 builder.setPath(path.toString());
378                 db = builder.build();
379             } else {
380                 if (!Files.exists(DB_FOLDER)) {
381                     Files.createDirectories(DB_FOLDER);
382                 }
383                 RrdDef rrdDef = getRrdDef(alias, path);
384                 if (rrdDef != null) {
385                     // create a new database file
386                     builder.setRrdDef(rrdDef);
387                     db = builder.build();
388                 } else {
389                     logger.debug(
390                             "Did not create rrd4j database for item '{}' since no rrd definition could be determined. This is likely due to an unsupported item type.",
391                             alias);
392                 }
393             }
394         } catch (IOException e) {
395             logger.error("Could not create rrd4j database file '{}': {}", path, e.getMessage());
396         } catch (RejectedExecutionException e) {
397             // this happens if the system is shut down
398             logger.debug("Could not create rrd4j database file '{}': {}", path, e.getMessage());
399         }
400         return db;
401     }
402
403     private @Nullable RrdDefConfig getRrdDefConfig(String itemName) {
404         RrdDefConfig useRdc = null;
405         for (Map.Entry<String, RrdDefConfig> e : rrdDefs.entrySet()) {
406             // try to find special config
407             RrdDefConfig rdc = e.getValue();
408             if (rdc.appliesTo(itemName)) {
409                 useRdc = rdc;
410                 break;
411             }
412         }
413         if (useRdc == null) { // not defined, use defaults
414             try {
415                 Item item = itemRegistry.getItem(itemName);
416                 if (!isSupportedItemType(item)) {
417                     return null;
418                 }
419                 if (item instanceof NumberItem) {
420                     NumberItem numberItem = (NumberItem) item;
421                     useRdc = numberItem.getDimension() != null ? rrdDefs.get(DEFAULT_QUANTIFIABLE)
422                             : rrdDefs.get(DEFAULT_NUMERIC);
423                 } else {
424                     useRdc = rrdDefs.get(DEFAULT_OTHER);
425                 }
426             } catch (ItemNotFoundException e) {
427                 logger.debug("Could not find item '{}' in registry", itemName);
428                 return null;
429             }
430         }
431         logger.trace("Using rrd definition '{}' for item '{}'.", useRdc, itemName);
432         return useRdc;
433     }
434
435     private @Nullable RrdDef getRrdDef(String itemName, Path path) {
436         RrdDef rrdDef = new RrdDef(path.toString());
437         RrdDefConfig useRdc = getRrdDefConfig(itemName);
438         if (useRdc != null) {
439             rrdDef.setStep(useRdc.step);
440             rrdDef.setStartTime(System.currentTimeMillis() / 1000 - 1);
441             rrdDef.addDatasource(DATASOURCE_STATE, useRdc.dsType, useRdc.heartbeat, useRdc.min, useRdc.max);
442             for (RrdArchiveDef rad : useRdc.archives) {
443                 rrdDef.addArchive(rad.fcn, rad.xff, rad.steps, rad.rows);
444             }
445             return rrdDef;
446         } else {
447             return null;
448         }
449     }
450
451     public ConsolFun getConsolidationFunction(RrdDb db) {
452         try {
453             return db.getRrdDef().getArcDefs()[0].getConsolFun();
454         } catch (IOException e) {
455             return ConsolFun.MAX;
456         }
457     }
458
459     @SuppressWarnings({ "unchecked", "rawtypes" })
460     private State mapToState(double value, @Nullable Item item, @Nullable Unit unit) {
461         if (item instanceof GroupItem) {
462             item = ((GroupItem) item).getBaseItem();
463         }
464
465         if (item instanceof SwitchItem && !(item instanceof DimmerItem)) {
466             return value == 0.0d ? OnOffType.OFF : OnOffType.ON;
467         } else if (item instanceof ContactItem) {
468             return value == 0.0d ? OpenClosedType.CLOSED : OpenClosedType.OPEN;
469         } else if (item instanceof DimmerItem || item instanceof RollershutterItem || item instanceof ColorItem) {
470             // make sure Items that need PercentTypes instead of DecimalTypes do receive the right information
471             return new PercentType((int) Math.round(value * 100));
472         } else if (item instanceof NumberItem) {
473             if (unit != null) {
474                 return new QuantityType(value, unit);
475             }
476         }
477         return new DecimalType(value);
478     }
479
480     private boolean isSupportedItemType(Item item) {
481         if (item instanceof GroupItem) {
482             final Item baseItem = ((GroupItem) item).getBaseItem();
483             if (baseItem != null) {
484                 item = baseItem;
485             }
486         }
487
488         return SUPPORTED_TYPES.contains(ItemUtil.getMainItemType(item.getType()));
489     }
490
491     @Activate
492     protected void activate(final Map<String, Object> config) {
493         modified(config);
494     }
495
496     @Modified
497     protected void modified(final Map<String, Object> config) {
498         // clean existing definitions
499         rrdDefs.clear();
500
501         // add default configurations
502
503         RrdDefConfig defaultNumeric = new RrdDefConfig(DEFAULT_NUMERIC);
504         // use 10 seconds as a step size for numeric values and allow a 10 minute silence between updates
505         defaultNumeric.setDef("GAUGE,600,U,U,10");
506         // define 5 different boxes:
507         // 1. granularity of 10s for the last hour
508         // 2. granularity of 1m for the last week
509         // 3. granularity of 15m for the last year
510         // 4. granularity of 1h for the last 5 years
511         // 5. granularity of 1d for the last 10 years
512         defaultNumeric
513                 .addArchives("LAST,0.5,1,360:LAST,0.5,6,10080:LAST,0.5,90,36500:LAST,0.5,360,43800:LAST,0.5,8640,3650");
514         rrdDefs.put(DEFAULT_NUMERIC, defaultNumeric);
515
516         RrdDefConfig defaultQuantifiable = new RrdDefConfig(DEFAULT_QUANTIFIABLE);
517         // use 10 seconds as a step size for numeric values and allow a 10 minute silence between updates
518         defaultQuantifiable.setDef("GAUGE,600,U,U,10");
519         // define 5 different boxes:
520         // 1. granularity of 10s for the last hour
521         // 2. granularity of 1m for the last week
522         // 3. granularity of 15m for the last year
523         // 4. granularity of 1h for the last 5 years
524         // 5. granularity of 1d for the last 10 years
525         defaultQuantifiable.addArchives(
526                 "AVERAGE,0.5,1,360:AVERAGE,0.5,6,10080:AVERAGE,0.5,90,36500:AVERAGE,0.5,360,43800:AVERAGE,0.5,8640,3650");
527         rrdDefs.put(DEFAULT_QUANTIFIABLE, defaultQuantifiable);
528
529         RrdDefConfig defaultOther = new RrdDefConfig(DEFAULT_OTHER);
530         // use 5 seconds as a step size for discrete values and allow a 1h silence between updates
531         defaultOther.setDef("GAUGE,3600,U,U,5");
532         // define 4 different boxes:
533         // 1. granularity of 5s for the last hour
534         // 2. granularity of 1m for the last week
535         // 3. granularity of 15m for the last year
536         // 4. granularity of 4h for the last 10 years
537         defaultOther.addArchives("LAST,0.5,1,720:LAST,0.5,12,10080:LAST,0.5,180,35040:LAST,0.5,2880,21900");
538         rrdDefs.put(DEFAULT_OTHER, defaultOther);
539
540         if (config.isEmpty()) {
541             logger.debug("using default configuration only");
542             return;
543         }
544
545         Iterator<String> keys = config.keySet().iterator();
546         while (keys.hasNext()) {
547             String key = keys.next();
548
549             if ("service.pid".equals(key) || "component.name".equals(key)) {
550                 // ignore service.pid and name
551                 continue;
552             }
553
554             String[] subkeys = key.split("\\.");
555             if (subkeys.length != 2) {
556                 logger.debug("config '{}' should have the format 'name.configkey'", key);
557                 continue;
558             }
559
560             Object v = config.get(key);
561             if (v instanceof String) {
562                 String value = (String) v;
563                 String name = subkeys[0].toLowerCase();
564                 String property = subkeys[1].toLowerCase();
565
566                 if (value.isBlank()) {
567                     logger.trace("Config is empty: {}", property);
568                     continue;
569                 } else {
570                     logger.trace("Processing config: {} = {}", property, value);
571                 }
572
573                 RrdDefConfig rrdDef = rrdDefs.get(name);
574                 if (rrdDef == null) {
575                     rrdDef = new RrdDefConfig(name);
576                     rrdDefs.put(name, rrdDef);
577                 }
578
579                 try {
580                     if ("def".equals(property)) {
581                         rrdDef.setDef(value);
582                     } else if ("archives".equals(property)) {
583                         rrdDef.addArchives(value);
584                     } else if ("items".equals(property)) {
585                         rrdDef.addItems(value);
586                     } else {
587                         logger.debug("Unknown property {} : {}", property, value);
588                     }
589                 } catch (IllegalArgumentException e) {
590                     logger.warn("Ignoring illegal configuration: {}", e.getMessage());
591                 }
592             }
593         }
594
595         for (RrdDefConfig rrdDef : rrdDefs.values()) {
596             if (rrdDef.isValid()) {
597                 logger.debug("Created {}", rrdDef);
598             } else {
599                 logger.info("Removing invalid definition {}", rrdDef);
600                 rrdDefs.remove(rrdDef.name);
601             }
602         }
603     }
604
605     private static class RrdArchiveDef {
606         public @Nullable ConsolFun fcn;
607         public double xff;
608         public int steps, rows;
609
610         @Override
611         public String toString() {
612             StringBuilder sb = new StringBuilder(" " + fcn);
613             sb.append(" xff = ").append(xff);
614             sb.append(" steps = ").append(steps);
615             sb.append(" rows = ").append(rows);
616             return sb.toString();
617         }
618     }
619
620     private class RrdDefConfig {
621         public String name;
622         public @Nullable DsType dsType;
623         public int heartbeat, step;
624         public double min, max;
625         public List<RrdArchiveDef> archives;
626         public List<String> itemNames;
627
628         private boolean isInitialized;
629
630         public RrdDefConfig(String name) {
631             this.name = name;
632             archives = new ArrayList<>();
633             itemNames = new ArrayList<>();
634             isInitialized = false;
635         }
636
637         public void setDef(String defString) {
638             String[] opts = defString.split(",");
639             if (opts.length != 5) { // check if correct number of parameters
640                 logger.warn("invalid number of parameters {}: {}", name, defString);
641                 return;
642             }
643
644             if ("ABSOLUTE".equals(opts[0])) { // dsType
645                 dsType = DsType.ABSOLUTE;
646             } else if ("COUNTER".equals(opts[0])) {
647                 dsType = DsType.COUNTER;
648             } else if ("DERIVE".equals(opts[0])) {
649                 dsType = DsType.DERIVE;
650             } else if ("GAUGE".equals(opts[0])) {
651                 dsType = DsType.GAUGE;
652             } else {
653                 logger.warn("{}: dsType {} not supported", name, opts[0]);
654             }
655
656             heartbeat = Integer.parseInt(opts[1]);
657
658             if ("U".equals(opts[2])) {
659                 min = Double.NaN;
660             } else {
661                 min = Double.parseDouble(opts[2]);
662             }
663
664             if ("U".equals(opts[3])) {
665                 max = Double.NaN;
666             } else {
667                 max = Double.parseDouble(opts[3]);
668             }
669
670             step = Integer.parseInt(opts[4]);
671
672             isInitialized = true; // successfully initialized
673
674             return;
675         }
676
677         public void addArchives(String archivesString) {
678             String splitArchives[] = archivesString.split(":");
679             for (String archiveString : splitArchives) {
680                 String[] opts = archiveString.split(",");
681                 if (opts.length != 4) { // check if correct number of parameters
682                     logger.warn("invalid number of parameters {}: {}", name, archiveString);
683                     return;
684                 }
685                 RrdArchiveDef arc = new RrdArchiveDef();
686
687                 if ("AVERAGE".equals(opts[0])) {
688                     arc.fcn = ConsolFun.AVERAGE;
689                 } else if ("MIN".equals(opts[0])) {
690                     arc.fcn = ConsolFun.MIN;
691                 } else if ("MAX".equals(opts[0])) {
692                     arc.fcn = ConsolFun.MAX;
693                 } else if ("LAST".equals(opts[0])) {
694                     arc.fcn = ConsolFun.LAST;
695                 } else if ("FIRST".equals(opts[0])) {
696                     arc.fcn = ConsolFun.FIRST;
697                 } else if ("TOTAL".equals(opts[0])) {
698                     arc.fcn = ConsolFun.TOTAL;
699                 } else {
700                     logger.warn("{}: consolidation function  {} not supported", name, opts[0]);
701                 }
702                 arc.xff = Double.parseDouble(opts[1]);
703                 arc.steps = Integer.parseInt(opts[2]);
704                 arc.rows = Integer.parseInt(opts[3]);
705                 archives.add(arc);
706             }
707         }
708
709         public void addItems(String itemsString) {
710             Collections.addAll(itemNames, itemsString.split(","));
711         }
712
713         public boolean appliesTo(String item) {
714             return itemNames.contains(item);
715         }
716
717         public boolean isValid() { // a valid configuration must be initialized
718             // and contain at least one function
719             return isInitialized && !archives.isEmpty();
720         }
721
722         @Override
723         public String toString() {
724             StringBuilder sb = new StringBuilder(name);
725             sb.append(" = ").append(dsType);
726             sb.append(" heartbeat = ").append(heartbeat);
727             sb.append(" min/max = ").append(min).append("/").append(max);
728             sb.append(" step = ").append(step);
729             sb.append(" ").append(archives.size()).append(" archives(s) = [");
730             for (RrdArchiveDef arc : archives) {
731                 sb.append(arc.toString());
732             }
733             sb.append("] ");
734             sb.append(itemNames.size()).append(" items(s) = [");
735             for (String item : itemNames) {
736                 sb.append(item).append(" ");
737             }
738             sb.append("]");
739             return sb.toString();
740         }
741     }
742
743     @Override
744     public List<PersistenceStrategy> getDefaultStrategies() {
745         return List.of(PersistenceStrategy.Globals.RESTORE, PersistenceStrategy.Globals.CHANGE,
746                 new PersistenceCronStrategy("everyMinute", "0 * * * * ?"));
747     }
748 }