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.console;
15 import java.nio.file.Files;
16 import java.nio.file.Path;
17 import java.util.Collections;
18 import java.util.Comparator;
19 import java.util.List;
20 import java.util.stream.Collectors;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.core.io.console.Console;
25 import org.openhab.core.io.console.ConsoleCommandCompleter;
26 import org.openhab.core.io.console.StringsCompleter;
27 import org.openhab.core.io.console.extensions.AbstractConsoleCommandExtension;
28 import org.openhab.core.io.console.extensions.ConsoleCommandExtension;
29 import org.openhab.core.items.ItemNotFoundException;
30 import org.openhab.core.items.ItemRegistry;
31 import org.openhab.core.persistence.PersistenceService;
32 import org.openhab.core.persistence.PersistenceServiceRegistry;
33 import org.openhab.persistence.rrd4j.internal.RRD4jPersistenceService;
34 import org.osgi.service.component.annotations.Activate;
35 import org.osgi.service.component.annotations.Component;
36 import org.osgi.service.component.annotations.Reference;
39 * The {@link RRD4jCommandExtension} is responsible for handling console commands
41 * @author Laurent Garnier - Initial contribution
44 @Component(service = ConsoleCommandExtension.class)
45 public class RRD4jCommandExtension extends AbstractConsoleCommandExtension implements ConsoleCommandCompleter {
47 private static final String CMD_LIST = "list";
48 private static final String CMD_CHECK = "check";
49 private static final String CMD_CLEAN = "clean";
50 private static final StringsCompleter CMD_COMPLETER = new StringsCompleter(List.of(CMD_LIST, CMD_CHECK, CMD_CLEAN),
53 private final PersistenceServiceRegistry persistenceServiceRegistry;
54 private final ItemRegistry itemRegistry;
57 public RRD4jCommandExtension(final @Reference PersistenceServiceRegistry persistenceServiceRegistry,
58 final @Reference ItemRegistry itemRegistry) {
59 super(RRD4jPersistenceService.SERVICE_ID, "Interact with the RRD4j persistence service.");
60 this.persistenceServiceRegistry = persistenceServiceRegistry;
61 this.itemRegistry = itemRegistry;
65 public void execute(String[] args, Console console) {
66 RRD4jPersistenceService persistenceService = getPersistenceService();
67 if (persistenceService == null) {
68 console.println("No RRD4j persistence service installed:");
71 if (args.length == 1 && CMD_LIST.equalsIgnoreCase(args[0])) {
72 List<String> filenames = persistenceService.getRrdFiles();
73 Collections.sort(filenames, Comparator.naturalOrder());
74 console.println("Existing RRD files...");
75 filenames.forEach(filename -> console.println(" - " + filename));
76 console.println(filenames.size() + " files found.");
78 } else if (args.length == 1 && CMD_CHECK.equalsIgnoreCase(args[0])) {
79 checkAndClean(persistenceService, console, null, true);
81 } else if (args.length >= 1 && args.length <= 2 && CMD_CLEAN.equalsIgnoreCase(args[0])) {
82 checkAndClean(persistenceService, console, args.length == 2 ? args[1] : null, false);
88 private @Nullable RRD4jPersistenceService getPersistenceService() {
89 for (PersistenceService persistenceService : persistenceServiceRegistry.getAll()) {
90 if (persistenceService instanceof RRD4jPersistenceService service) {
97 private void checkAndClean(RRD4jPersistenceService persistenceService, Console console, @Nullable String itemName,
99 List<String> filenames;
100 if (itemName != null) {
101 filenames = List.of(itemName + ".rrd");
103 filenames = persistenceService.getRrdFiles();
104 Collections.sort(filenames, Comparator.naturalOrder());
107 console.println((checkOnly ? "Checking" : "Cleaning") + " RRD files...");
109 for (String filename : filenames) {
110 String name = filename.substring(0, filename.lastIndexOf(".rrd"));
111 Path path = RRD4jPersistenceService.getDatabasePath(name);
112 if (!Files.exists(path)) {
113 console.println(" - " + filename + ": file not found");
117 itemRegistry.getItem(name);
119 } catch (ItemNotFoundException e) {
126 console.println(" - " + filename + ": no item found");
128 } else if (path.toFile().delete()) {
129 console.println(" - " + filename + ": file deleted");
132 console.println(" - " + filename + ": file deletion failed!");
136 console.println(nb + " files " + (checkOnly ? "to delete." : "deleted."));
140 public List<String> getUsages() {
141 return List.of(buildCommandUsage(CMD_LIST, "list Round Robin Database files"),
142 buildCommandUsage(CMD_CHECK, "check for RRD files without existing item"),
143 buildCommandUsage(CMD_CLEAN + " [<itemName>]", "delete RRD files without existing item"));
147 public @Nullable ConsoleCommandCompleter getCompleter() {
152 public boolean complete(String[] args, int cursorArgumentIndex, int cursorPosition, List<String> candidates) {
153 if (cursorArgumentIndex <= 0) {
154 return CMD_COMPLETER.complete(args, cursorArgumentIndex, cursorPosition, candidates);
155 } else if (cursorArgumentIndex == 1) {
156 if (CMD_CLEAN.equalsIgnoreCase(args[0])) {
157 RRD4jPersistenceService persistenceService = getPersistenceService();
158 if (persistenceService != null) {
159 List<String> filenames = persistenceService.getRrdFiles().stream()
160 .map(filename -> filename.substring(0, filename.lastIndexOf(".rrd")))
161 .collect(Collectors.toList());
162 return new StringsCompleter(filenames, true).complete(args, cursorArgumentIndex, cursorPosition,