Add uptime command
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / ui / stdin / UptimeCommand.java
diff --git a/src/main/java/net/pterodactylus/xdcc/ui/stdin/UptimeCommand.java b/src/main/java/net/pterodactylus/xdcc/ui/stdin/UptimeCommand.java
new file mode 100644 (file)
index 0000000..56188ec
--- /dev/null
@@ -0,0 +1,71 @@
+package net.pterodactylus.xdcc.ui.stdin;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.time.Duration;
+import java.time.OffsetDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import net.pterodactylus.xdcc.core.Core;
+import net.pterodactylus.xdcc.data.ConnectedNetwork;
+
+/**
+ * {@link Command} that writes uptime information.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class UptimeCommand implements Command {
+
+       private final DateTimeFormatter formatter = DateTimeFormatter.RFC_1123_DATE_TIME;
+       private final Core core;
+
+       public UptimeCommand(Core core) {
+               this.core = core;
+       }
+
+       @Override
+       public String getName() {
+               return "uptime";
+       }
+
+       @Override
+       public Collection<String> getAliases() {
+               return Collections.emptySet();
+       }
+
+       @Override
+       public State execute(State state, List<String> parameters, Writer outputWriter)
+       throws IOException {
+               OffsetDateTime now = OffsetDateTime.now();
+               outputWriter.write(String.format("Core running since %s, %s ago.\n",
+                               formatter.format(core.getUptime().subtractFrom(now)),
+                               formatTime(core.getUptime())));
+               for (ConnectedNetwork network : core.connectedNetworks()) {
+                       outputWriter.write(String.format("%s connected since %s, %s ago.\n",
+                                       network.getNetwork().name(),
+                                       formatter.format(network.getUptime().subtractFrom(now)),
+                                       formatTime(network.getUptime())));
+               }
+               return state;
+       }
+
+       private String formatTime(Duration uptime) {
+               long uptimeSeconds = uptime.getSeconds();
+               long seconds = uptimeSeconds % 60;
+               long minutes = (uptimeSeconds / 60) % 60;
+               long hours = (uptimeSeconds / (60 * 60)) % 24;
+               long days = (uptimeSeconds / (60 * 60 * 24)) % 7;
+               long weeks = (uptimeSeconds / (60 * 60 * 24 * 7));
+               String uptimeString = Stream
+                               .of(weeks + "w", days + "d", hours + "h", minutes + "m", seconds + "s")
+                               .filter(v -> !v.startsWith("0"))
+                               .collect(Collectors.joining(" "));
+               return uptimeString.isEmpty() ? "0s" : uptimeString;
+       }
+
+}