]> git.basschouten.com Git - openhab-addons.git/blob
bad9c4e078244c44b715a41c75515eecc8d19e77
[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.console;
14
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;
21
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;
37
38 /**
39  * The {@link RRD4jCommandExtension} is responsible for handling console commands
40  *
41  * @author Laurent Garnier - Initial contribution
42  */
43 @NonNullByDefault
44 @Component(service = ConsoleCommandExtension.class)
45 public class RRD4jCommandExtension extends AbstractConsoleCommandExtension implements ConsoleCommandCompleter {
46
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),
51             false);
52
53     private final PersistenceServiceRegistry persistenceServiceRegistry;
54     private final ItemRegistry itemRegistry;
55
56     @Activate
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;
62     }
63
64     @Override
65     public void execute(String[] args, Console console) {
66         RRD4jPersistenceService persistenceService = getPersistenceService();
67         if (persistenceService == null) {
68             console.println("No RRD4j persistence service installed:");
69             return;
70         }
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.");
77             return;
78         } else if (args.length == 1 && CMD_CHECK.equalsIgnoreCase(args[0])) {
79             checkAndClean(persistenceService, console, null, true);
80             return;
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);
83             return;
84         }
85         printUsage(console);
86     }
87
88     private @Nullable RRD4jPersistenceService getPersistenceService() {
89         for (PersistenceService persistenceService : persistenceServiceRegistry.getAll()) {
90             if (persistenceService instanceof RRD4jPersistenceService service) {
91                 return service;
92             }
93         }
94         return null;
95     }
96
97     private void checkAndClean(RRD4jPersistenceService persistenceService, Console console, @Nullable String itemName,
98             boolean checkOnly) {
99         List<String> filenames;
100         if (itemName != null) {
101             filenames = List.of(itemName + ".rrd");
102         } else {
103             filenames = persistenceService.getRrdFiles();
104             Collections.sort(filenames, Comparator.naturalOrder());
105         }
106
107         console.println((checkOnly ? "Checking" : "Cleaning") + " RRD files...");
108         int nb = 0;
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");
114             } else {
115                 boolean itemFound;
116                 try {
117                     itemRegistry.getItem(name);
118                     itemFound = true;
119                 } catch (ItemNotFoundException e) {
120                     itemFound = false;
121                 }
122                 if (itemFound) {
123                     continue;
124                 }
125                 if (checkOnly) {
126                     console.println("  - " + filename + ": no item found");
127                     nb++;
128                 } else if (path.toFile().delete()) {
129                     console.println("  - " + filename + ": file deleted");
130                     nb++;
131                 } else {
132                     console.println("  - " + filename + ": file deletion failed!");
133                 }
134             }
135         }
136         console.println(nb + " files " + (checkOnly ? "to delete." : "deleted."));
137     }
138
139     @Override
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"));
144     }
145
146     @Override
147     public @Nullable ConsoleCommandCompleter getCompleter() {
148         return this;
149     }
150
151     @Override
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,
163                             candidates);
164                 }
165             }
166         }
167         return false;
168     }
169 }