2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.persistence.rrd4j.internal;
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.Iterator;
24 import java.util.List;
25 import java.util.Locale;
28 import java.util.concurrent.ConcurrentHashMap;
29 import java.util.concurrent.ConcurrentSkipListMap;
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 import java.util.stream.Collectors;
36 import java.util.stream.Stream;
38 import javax.measure.Quantity;
39 import javax.measure.Unit;
41 import org.eclipse.jdt.annotation.NonNullByDefault;
42 import org.eclipse.jdt.annotation.Nullable;
43 import org.openhab.core.OpenHAB;
44 import org.openhab.core.common.NamedThreadFactory;
45 import org.openhab.core.items.GroupItem;
46 import org.openhab.core.items.Item;
47 import org.openhab.core.items.ItemNotFoundException;
48 import org.openhab.core.items.ItemRegistry;
49 import org.openhab.core.items.ItemUtil;
50 import org.openhab.core.library.CoreItemFactory;
51 import org.openhab.core.library.items.ColorItem;
52 import org.openhab.core.library.items.ContactItem;
53 import org.openhab.core.library.items.DimmerItem;
54 import org.openhab.core.library.items.NumberItem;
55 import org.openhab.core.library.items.RollershutterItem;
56 import org.openhab.core.library.items.SwitchItem;
57 import org.openhab.core.library.types.DecimalType;
58 import org.openhab.core.library.types.OnOffType;
59 import org.openhab.core.library.types.OpenClosedType;
60 import org.openhab.core.library.types.PercentType;
61 import org.openhab.core.library.types.QuantityType;
62 import org.openhab.core.persistence.FilterCriteria;
63 import org.openhab.core.persistence.FilterCriteria.Ordering;
64 import org.openhab.core.persistence.HistoricItem;
65 import org.openhab.core.persistence.PersistenceItemInfo;
66 import org.openhab.core.persistence.PersistenceService;
67 import org.openhab.core.persistence.QueryablePersistenceService;
68 import org.openhab.core.persistence.strategy.PersistenceCronStrategy;
69 import org.openhab.core.persistence.strategy.PersistenceStrategy;
70 import org.openhab.core.types.State;
71 import org.osgi.service.component.annotations.Activate;
72 import org.osgi.service.component.annotations.Component;
73 import org.osgi.service.component.annotations.ConfigurationPolicy;
74 import org.osgi.service.component.annotations.Deactivate;
75 import org.osgi.service.component.annotations.Modified;
76 import org.osgi.service.component.annotations.Reference;
77 import org.rrd4j.ConsolFun;
78 import org.rrd4j.DsType;
79 import org.rrd4j.core.FetchData;
80 import org.rrd4j.core.FetchRequest;
81 import org.rrd4j.core.RrdDb;
82 import org.rrd4j.core.RrdDb.Builder;
83 import org.rrd4j.core.RrdDbPool;
84 import org.rrd4j.core.RrdDef;
85 import org.rrd4j.core.Sample;
86 import org.slf4j.Logger;
87 import org.slf4j.LoggerFactory;
90 * This is the implementation of the RRD4j {@link PersistenceService}. To learn
91 * more about RRD4j please visit their
92 * <a href="https://github.com/rrd4j/rrd4j">website</a>.
94 * @author Kai Kreuzer - Initial contribution
95 * @author Jan N. Klug - some improvements
96 * @author Karel Goderis - remove TimerThread dependency
99 @Component(service = { PersistenceService.class,
100 QueryablePersistenceService.class }, configurationPid = "org.openhab.rrd4j", configurationPolicy = ConfigurationPolicy.OPTIONAL)
101 public class RRD4jPersistenceService implements QueryablePersistenceService {
103 public static final String SERVICE_ID = "rrd4j";
105 private static final String DEFAULT_OTHER = "default_other";
106 private static final String DEFAULT_NUMERIC = "default_numeric";
107 private static final String DEFAULT_QUANTIFIABLE = "default_quantifiable";
109 private static final Set<String> SUPPORTED_TYPES = Set.of(CoreItemFactory.SWITCH, CoreItemFactory.CONTACT,
110 CoreItemFactory.DIMMER, CoreItemFactory.NUMBER, CoreItemFactory.ROLLERSHUTTER, CoreItemFactory.COLOR);
112 private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1,
113 new NamedThreadFactory("RRD4j"));
115 private final Map<String, RrdDefConfig> rrdDefs = new ConcurrentHashMap<>();
117 private final ConcurrentSkipListMap<Long, Map<String, Double>> storageMap = new ConcurrentSkipListMap<>();
119 private static final String DATASOURCE_STATE = "state";
121 private static final Path DB_FOLDER = Path.of(OpenHAB.getUserDataFolder(), "persistence", "rrd4j").toAbsolutePath();
123 private static final RrdDbPool DATABASE_POOL = new RrdDbPool();
125 private final Logger logger = LoggerFactory.getLogger(RRD4jPersistenceService.class);
126 private final ItemRegistry itemRegistry;
127 private boolean active = false;
129 public static Path getDatabasePath(String name) {
130 return DB_FOLDER.resolve(name + ".rrd");
133 public static RrdDbPool getDatabasePool() {
134 return DATABASE_POOL;
137 private final ScheduledFuture<?> storeJob;
140 public RRD4jPersistenceService(final @Reference ItemRegistry itemRegistry, Map<String, Object> config) {
141 this.itemRegistry = itemRegistry;
142 storeJob = scheduler.scheduleWithFixedDelay(() -> doStore(false), 1, 1, TimeUnit.SECONDS);
148 protected void modified(final Map<String, Object> config) {
149 // clean existing definitions
152 // add default configurations
154 RrdDefConfig defaultNumeric = new RrdDefConfig(DEFAULT_NUMERIC);
155 // use 10 seconds as a step size for numeric values and allow a 10 minute silence between updates
156 defaultNumeric.setDef("GAUGE,600,U,U,10");
157 // define 5 different boxes:
158 // 1. granularity of 10s for the last hour
159 // 2. granularity of 1m for the last week
160 // 3. granularity of 15m for the last year
161 // 4. granularity of 1h for the last 5 years
162 // 5. granularity of 1d for the last 10 years
164 .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");
165 rrdDefs.put(DEFAULT_NUMERIC, defaultNumeric);
167 RrdDefConfig defaultQuantifiable = new RrdDefConfig(DEFAULT_QUANTIFIABLE);
168 // use 10 seconds as a step size for numeric values and allow a 10 minute silence between updates
169 defaultQuantifiable.setDef("GAUGE,600,U,U,10");
170 // define 5 different boxes:
171 // 1. granularity of 10s for the last hour
172 // 2. granularity of 1m for the last week
173 // 3. granularity of 15m for the last year
174 // 4. granularity of 1h for the last 5 years
175 // 5. granularity of 1d for the last 10 years
176 defaultQuantifiable.addArchives(
177 "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");
178 rrdDefs.put(DEFAULT_QUANTIFIABLE, defaultQuantifiable);
180 RrdDefConfig defaultOther = new RrdDefConfig(DEFAULT_OTHER);
181 // use 5 seconds as a step size for discrete values and allow a 1h silence between updates
182 defaultOther.setDef("GAUGE,3600,U,U,5");
183 // define 4 different boxes:
184 // 1. granularity of 5s for the last hour
185 // 2. granularity of 1m for the last week
186 // 3. granularity of 15m for the last year
187 // 4. granularity of 4h for the last 10 years
188 defaultOther.addArchives("LAST,0.5,1,720:LAST,0.5,12,10080:LAST,0.5,180,35040:LAST,0.5,2880,21900");
189 rrdDefs.put(DEFAULT_OTHER, defaultOther);
191 if (config.isEmpty()) {
192 logger.debug("using default configuration only");
196 Iterator<String> keys = config.keySet().iterator();
197 while (keys.hasNext()) {
198 String key = keys.next();
200 if ("service.pid".equals(key) || "component.name".equals(key)) {
201 // ignore service.pid and name
205 String[] subkeys = key.split("\\.");
206 if (subkeys.length != 2) {
207 logger.debug("config '{}' should have the format 'name.configkey'", key);
211 Object v = config.get(key);
212 if (v instanceof String) {
213 String value = (String) v;
214 String name = subkeys[0].toLowerCase();
215 String property = subkeys[1].toLowerCase();
217 if (value.isBlank()) {
218 logger.trace("Config is empty: {}", property);
221 logger.trace("Processing config: {} = {}", property, value);
224 RrdDefConfig rrdDef = rrdDefs.get(name);
225 if (rrdDef == null) {
226 rrdDef = new RrdDefConfig(name);
227 rrdDefs.put(name, rrdDef);
231 if ("def".equals(property)) {
232 rrdDef.setDef(value);
233 } else if ("archives".equals(property)) {
234 rrdDef.addArchives(value);
235 } else if ("items".equals(property)) {
236 rrdDef.addItems(value);
238 logger.debug("Unknown property {} : {}", property, value);
240 } catch (IllegalArgumentException e) {
241 logger.warn("Ignoring illegal configuration: {}", e.getMessage());
246 for (RrdDefConfig rrdDef : rrdDefs.values()) {
247 if (rrdDef.isValid()) {
248 logger.debug("Created {}", rrdDef);
250 logger.info("Removing invalid definition {}", rrdDef);
251 rrdDefs.remove(rrdDef.name);
257 protected void deactivate() {
259 storeJob.cancel(false);
261 // make sure we really store everything
266 public String getId() {
271 public String getLabel(@Nullable Locale locale) {
276 public void store(final Item item, @Nullable final String alias) {
278 logger.warn("Tried to store {} but service is not yet ready (or shutting down).", item);
282 if (!isSupportedItemType(item)) {
283 logger.trace("Ignoring item '{}' since its type {} is not supported", item.getName(), item.getType());
286 final String name = alias == null ? item.getName() : alias;
290 if (item instanceof NumberItem nItem && item.getState() instanceof QuantityType<?> qState) {
291 Unit<? extends Quantity<?>> unit = nItem.getUnit();
293 QuantityType<?> convertedState = qState.toUnit(unit);
294 if (convertedState != null) {
295 value = convertedState.doubleValue();
299 "Failed to convert state '{}' to unit '{}'. Please check your item definition for correctness.",
303 value = qState.doubleValue();
306 DecimalType state = item.getStateAs(DecimalType.class);
308 value = state.toBigDecimal().doubleValue();
315 // we could not convert the value
319 long now = System.currentTimeMillis() / 1000;
320 Double oldValue = storageMap.computeIfAbsent(now, t -> new ConcurrentHashMap<>()).put(name, value);
321 if (oldValue != null && !oldValue.equals(value)) {
323 "Discarding value {} for item {} with timestamp {} because a new value ({}) arrived with the same timestamp.",
324 oldValue, name, now, value);
328 private void doStore(boolean force) {
329 while (!storageMap.isEmpty()) {
330 long timestamp = storageMap.firstKey();
331 long now = System.currentTimeMillis() / 1000;
332 if (now > timestamp || force) {
333 // no new elements can be added for this timestamp because we are already past that time or the service
334 // requires forced storing
335 Map<String, Double> values = storageMap.pollFirstEntry().getValue();
336 values.forEach((name, value) -> writePointToDatabase(name, value, timestamp));
343 private synchronized void writePointToDatabase(String name, double value, long timestamp) {
346 db = getDB(name, true);
347 } catch (Exception e) {
348 logger.warn("Failed to open rrd4j database '{}' to store data ({})", name, e.toString());
354 ConsolFun function = getConsolidationFunction(db);
355 if (function != ConsolFun.AVERAGE) {
357 // we store the last value again, so that the value change
358 // in the database is not interpolated, but
359 // happens right at this spot
360 if (timestamp - 1 > db.getLastUpdateTime()) {
361 // only do it if there is not already a value
362 double lastValue = db.getLastDatasourceValue(DATASOURCE_STATE);
363 if (!Double.isNaN(lastValue)) {
364 Sample sample = db.createSample();
365 sample.setTime(timestamp - 1);
366 sample.setValue(DATASOURCE_STATE, lastValue);
368 logger.debug("Stored '{}' as value '{}' with timestamp {} in rrd4j database (again)", name,
369 lastValue, timestamp - 1);
372 } catch (IOException e) {
373 logger.debug("Error storing last value (again) for {}: {}", e.getMessage(), name);
377 Sample sample = db.createSample();
378 sample.setTime(timestamp);
379 double storeValue = value;
380 if (db.getDatasource(DATASOURCE_STATE).getType() == DsType.COUNTER) {
381 // counter values must be adjusted by stepsize
382 storeValue = value * db.getRrdDef().getStep();
384 sample.setValue(DATASOURCE_STATE, storeValue);
386 logger.debug("Stored '{}' as value '{}' with timestamp {} in rrd4j database", name, storeValue, timestamp);
387 } catch (Exception e) {
388 logger.warn("Could not persist '{}' to rrd4j database: {}", name, e.getMessage());
392 } catch (IOException e) {
393 logger.debug("Error closing rrd4j database: {}", e.getMessage());
398 public void store(Item item) {
403 public Iterable<HistoricItem> query(FilterCriteria filter) {
404 ZonedDateTime filterBeginDate = filter.getBeginDate();
405 ZonedDateTime filterEndDate = filter.getEndDate();
406 if (filterBeginDate != null && filterEndDate != null && filterBeginDate.isAfter(filterEndDate)) {
407 throw new IllegalArgumentException("begin (" + filterBeginDate + ") before end (" + filterEndDate + ")");
410 String itemName = filter.getItemName();
411 if (itemName == null) {
412 logger.warn("Item name is missing in filter {}", filter);
415 logger.trace("Querying rrd4j database for item '{}'", itemName);
419 db = getDB(itemName, false);
420 } catch (Exception e) {
421 logger.warn("Failed to open rrd4j database '{}' for querying ({})", itemName, e.toString());
425 logger.debug("Could not find item '{}' in rrd4j database", itemName);
432 item = itemRegistry.getItem(itemName);
433 if (item instanceof NumberItem) {
434 // we already retrieve the unit here once as it is a very costly operation,
435 // see https://github.com/openhab/openhab-addons/issues/8928
436 unit = ((NumberItem) item).getUnit();
438 } catch (ItemNotFoundException e) {
439 logger.debug("Could not find item '{}' in registry", itemName);
443 long end = filterEndDate == null ? System.currentTimeMillis() / 1000
444 : filterEndDate.toInstant().getEpochSecond();
447 if (filterBeginDate == null) {
448 // as rrd goes back for years and gets more and more
449 // inaccurate, we only support descending order
450 // and a single return value
451 // if there is no begin date is given - this case is
452 // required specifically for the historicState()
453 // query, which we want to support
454 if (filter.getOrdering() == Ordering.DESCENDING && filter.getPageSize() == 1
455 && filter.getPageNumber() == 0) {
456 if (filterEndDate == null) {
457 // we are asked only for the most recent value!
458 double lastValue = db.getLastDatasourceValue(DATASOURCE_STATE);
459 if (!Double.isNaN(lastValue)) {
460 HistoricItem rrd4jItem = new RRD4jItem(itemName, mapToState(lastValue, item, unit),
461 ZonedDateTime.ofInstant(Instant.ofEpochMilli(db.getLastArchiveUpdateTime() * 1000),
462 ZoneId.systemDefault()));
463 return List.of(rrd4jItem);
471 throw new UnsupportedOperationException(
472 "rrd4j does not allow querys without a begin date, unless order is descending and a single value is requested");
475 start = filterBeginDate.toInstant().getEpochSecond();
478 // do not call method {@link RrdDb#createFetchRequest(ConsolFun, long, long, long)} if start > end to avoid
479 // an IAE to be thrown
481 logger.debug("Could not query rrd4j database for item '{}': start ({}) > end ({})", itemName, start,
486 FetchRequest request = db.createFetchRequest(getConsolidationFunction(db), start, end, 1);
487 FetchData result = request.fetchData();
489 List<HistoricItem> items = new ArrayList<>();
490 long ts = result.getFirstTimestamp();
491 long step = result.getRowCount() > 1 ? result.getStep() : 0;
492 for (double value : result.getValues(DATASOURCE_STATE)) {
493 if (!Double.isNaN(value) && (((ts >= start) && (ts <= end)) || (start == end))) {
494 RRD4jItem rrd4jItem = new RRD4jItem(itemName, mapToState(value, item, unit),
495 ZonedDateTime.ofInstant(Instant.ofEpochSecond(ts), ZoneId.systemDefault()));
496 items.add(rrd4jItem);
501 } catch (IOException e) {
502 logger.warn("Could not query rrd4j database for item '{}': {}", itemName, e.getMessage());
507 } catch (IOException e) {
508 logger.debug("Error closing rrd4j database: {}", e.getMessage());
514 public Set<PersistenceItemInfo> getItemInfo() {
518 protected synchronized @Nullable RrdDb getDB(String alias, boolean createFileIfAbsent) {
520 Path path = getDatabasePath(alias);
522 Builder builder = RrdDb.getBuilder();
523 builder.setPool(DATABASE_POOL);
525 if (Files.exists(path)) {
526 // recreate the RrdDb instance from the file
527 builder.setPath(path.toString());
528 db = builder.build();
529 } else if (createFileIfAbsent) {
530 if (!Files.exists(DB_FOLDER)) {
531 Files.createDirectories(DB_FOLDER);
533 RrdDef rrdDef = getRrdDef(alias, path);
534 if (rrdDef != null) {
535 // create a new database file
536 builder.setRrdDef(rrdDef);
537 db = builder.build();
540 "Did not create rrd4j database for item '{}' since no rrd definition could be determined. This is likely due to an unsupported item type.",
544 } catch (IOException e) {
545 logger.error("Could not create rrd4j database file '{}': {}", path, e.getMessage());
546 } catch (RejectedExecutionException e) {
547 // this happens if the system is shut down
548 logger.debug("Could not create rrd4j database file '{}': {}", path, e.getMessage());
553 private @Nullable RrdDefConfig getRrdDefConfig(String itemName) {
554 RrdDefConfig useRdc = null;
555 for (Map.Entry<String, RrdDefConfig> e : rrdDefs.entrySet()) {
556 // try to find special config
557 RrdDefConfig rdc = e.getValue();
558 if (rdc.appliesTo(itemName)) {
563 if (useRdc == null) { // not defined, use defaults
565 Item item = itemRegistry.getItem(itemName);
566 if (!isSupportedItemType(item)) {
569 if (item instanceof NumberItem) {
570 NumberItem numberItem = (NumberItem) item;
571 useRdc = numberItem.getDimension() != null ? rrdDefs.get(DEFAULT_QUANTIFIABLE)
572 : rrdDefs.get(DEFAULT_NUMERIC);
574 useRdc = rrdDefs.get(DEFAULT_OTHER);
576 } catch (ItemNotFoundException e) {
577 logger.debug("Could not find item '{}' in registry", itemName);
581 logger.trace("Using rrd definition '{}' for item '{}'.", useRdc, itemName);
585 private @Nullable RrdDef getRrdDef(String itemName, Path path) {
586 RrdDef rrdDef = new RrdDef(path.toString());
587 RrdDefConfig useRdc = getRrdDefConfig(itemName);
588 if (useRdc != null) {
589 rrdDef.setStep(useRdc.step);
590 rrdDef.setStartTime(System.currentTimeMillis() / 1000 - useRdc.step);
591 rrdDef.addDatasource(DATASOURCE_STATE, useRdc.dsType, useRdc.heartbeat, useRdc.min, useRdc.max);
592 for (RrdArchiveDef rad : useRdc.archives) {
593 rrdDef.addArchive(rad.fcn, rad.xff, rad.steps, rad.rows);
601 public ConsolFun getConsolidationFunction(RrdDb db) {
603 return db.getRrdDef().getArcDefs()[0].getConsolFun();
604 } catch (IOException e) {
605 return ConsolFun.MAX;
609 @SuppressWarnings({ "unchecked", "rawtypes" })
610 private State mapToState(double value, @Nullable Item item, @Nullable Unit unit) {
611 if (item instanceof GroupItem) {
612 item = ((GroupItem) item).getBaseItem();
615 if (item instanceof SwitchItem && !(item instanceof DimmerItem)) {
616 return value == 0.0d ? OnOffType.OFF : OnOffType.ON;
617 } else if (item instanceof ContactItem) {
618 return value == 0.0d ? OpenClosedType.CLOSED : OpenClosedType.OPEN;
619 } else if (item instanceof DimmerItem || item instanceof RollershutterItem || item instanceof ColorItem) {
620 // make sure Items that need PercentTypes instead of DecimalTypes do receive the right information
621 return new PercentType((int) Math.round(value * 100));
622 } else if (item instanceof NumberItem) {
624 return new QuantityType(value, unit);
627 return new DecimalType(value);
630 private boolean isSupportedItemType(Item item) {
631 if (item instanceof GroupItem) {
632 final Item baseItem = ((GroupItem) item).getBaseItem();
633 if (baseItem != null) {
638 return SUPPORTED_TYPES.contains(ItemUtil.getMainItemType(item.getType()));
641 public List<String> getRrdFiles() {
642 try (Stream<Path> stream = Files.list(DB_FOLDER)) {
643 return stream.filter(file -> !Files.isDirectory(file) && file.toFile().getName().endsWith(".rrd"))
644 .map(file -> file.toFile().getName()).collect(Collectors.toList());
645 } catch (IOException e) {
650 private static class RrdArchiveDef {
651 public @Nullable ConsolFun fcn;
653 public int steps, rows;
656 public String toString() {
657 StringBuilder sb = new StringBuilder(" " + fcn);
658 sb.append(" xff = ").append(xff);
659 sb.append(" steps = ").append(steps);
660 sb.append(" rows = ").append(rows);
661 return sb.toString();
665 private class RrdDefConfig {
667 public @Nullable DsType dsType;
668 public int heartbeat, step;
669 public double min, max;
670 public List<RrdArchiveDef> archives;
671 public List<String> itemNames;
673 private boolean isInitialized;
675 public RrdDefConfig(String name) {
677 archives = new ArrayList<>();
678 itemNames = new ArrayList<>();
679 isInitialized = false;
682 public void setDef(String defString) {
683 String[] opts = defString.split(",");
684 if (opts.length != 5) { // check if correct number of parameters
685 logger.warn("invalid number of parameters {}: {}", name, defString);
689 if ("ABSOLUTE".equals(opts[0])) { // dsType
690 dsType = DsType.ABSOLUTE;
691 } else if ("COUNTER".equals(opts[0])) {
692 dsType = DsType.COUNTER;
693 } else if ("DERIVE".equals(opts[0])) {
694 dsType = DsType.DERIVE;
695 } else if ("GAUGE".equals(opts[0])) {
696 dsType = DsType.GAUGE;
698 logger.warn("{}: dsType {} not supported", name, opts[0]);
701 heartbeat = Integer.parseInt(opts[1]);
703 if ("U".equals(opts[2])) {
706 min = Double.parseDouble(opts[2]);
709 if ("U".equals(opts[3])) {
712 max = Double.parseDouble(opts[3]);
715 step = Integer.parseInt(opts[4]);
717 isInitialized = true; // successfully initialized
722 public void addArchives(String archivesString) {
723 String[] splitArchives = archivesString.split(":");
724 for (String archiveString : splitArchives) {
725 String[] opts = archiveString.split(",");
726 if (opts.length != 4) { // check if correct number of parameters
727 logger.warn("invalid number of parameters {}: {}", name, archiveString);
730 RrdArchiveDef arc = new RrdArchiveDef();
732 if ("AVERAGE".equals(opts[0])) {
733 arc.fcn = ConsolFun.AVERAGE;
734 } else if ("MIN".equals(opts[0])) {
735 arc.fcn = ConsolFun.MIN;
736 } else if ("MAX".equals(opts[0])) {
737 arc.fcn = ConsolFun.MAX;
738 } else if ("LAST".equals(opts[0])) {
739 arc.fcn = ConsolFun.LAST;
740 } else if ("FIRST".equals(opts[0])) {
741 arc.fcn = ConsolFun.FIRST;
742 } else if ("TOTAL".equals(opts[0])) {
743 arc.fcn = ConsolFun.TOTAL;
745 logger.warn("{}: consolidation function {} not supported", name, opts[0]);
747 arc.xff = Double.parseDouble(opts[1]);
748 arc.steps = Integer.parseInt(opts[2]);
749 arc.rows = Integer.parseInt(opts[3]);
754 public void addItems(String itemsString) {
755 Collections.addAll(itemNames, itemsString.split(","));
758 public boolean appliesTo(String item) {
759 return itemNames.contains(item);
762 public boolean isValid() { // a valid configuration must be initialized
763 // and contain at least one function
764 return isInitialized && !archives.isEmpty();
768 public String toString() {
769 StringBuilder sb = new StringBuilder(name);
770 sb.append(" = ").append(dsType);
771 sb.append(" heartbeat = ").append(heartbeat);
772 sb.append(" min/max = ").append(min).append("/").append(max);
773 sb.append(" step = ").append(step);
774 sb.append(" ").append(archives.size()).append(" archives(s) = [");
775 for (RrdArchiveDef arc : archives) {
776 sb.append(arc.toString());
779 sb.append(itemNames.size()).append(" items(s) = [");
780 for (String item : itemNames) {
781 sb.append(item).append(" ");
784 return sb.toString();
789 public List<PersistenceStrategy> getDefaultStrategies() {
790 return List.of(PersistenceStrategy.Globals.RESTORE, PersistenceStrategy.Globals.CHANGE,
791 new PersistenceCronStrategy("everyMinute", "0 * * * * ?"));