🎨 Replace download command with Kotlin version
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / ui / stdin / UptimeCommand.java
1 package net.pterodactylus.xdcc.ui.stdin;
2
3 import java.io.IOException;
4 import java.io.Writer;
5 import java.time.Duration;
6 import java.time.OffsetDateTime;
7 import java.time.format.DateTimeFormatter;
8 import java.util.Collection;
9 import java.util.Collections;
10 import java.util.List;
11 import java.util.stream.Collectors;
12 import java.util.stream.Stream;
13
14 import net.pterodactylus.xdcc.core.Core;
15 import net.pterodactylus.xdcc.data.ConnectedNetwork;
16
17 /**
18  * {@link Command} that writes uptime information.
19  *
20  * @author <a href="mailto:bombe@pterodactylus.net">David â€˜Bombe’ Roden</a>
21  */
22 public class UptimeCommand implements Command {
23
24         private final DateTimeFormatter formatter = DateTimeFormatter.RFC_1123_DATE_TIME;
25         private final Core core;
26
27         public UptimeCommand(Core core) {
28                 this.core = core;
29         }
30
31         @Override
32         public String getName() {
33                 return "uptime";
34         }
35
36         @Override
37         public Collection<String> getAliases() {
38                 return Collections.emptySet();
39         }
40
41         @Override
42         public State execute(State state, List<String> parameters, Writer outputWriter)
43         throws IOException {
44                 OffsetDateTime now = OffsetDateTime.now();
45                 outputWriter.write(String.format("Core running since %s, %s ago.\n",
46                                 formatter.format(core.getUptime().subtractFrom(now)),
47                                 formatTime(core.getUptime())));
48                 for (ConnectedNetwork network : core.connectedNetworks()) {
49                         outputWriter.write(String.format("%s connected since %s, %s ago.\n",
50                                         network.getNetwork().name(),
51                                         formatter.format(network.getUptime().subtractFrom(now)),
52                                         formatTime(network.getUptime())));
53                 }
54                 return state;
55         }
56
57         private String formatTime(Duration uptime) {
58                 long uptimeSeconds = uptime.getSeconds();
59                 long seconds = uptimeSeconds % 60;
60                 long minutes = (uptimeSeconds / 60) % 60;
61                 long hours = (uptimeSeconds / (60 * 60)) % 24;
62                 long days = (uptimeSeconds / (60 * 60 * 24)) % 7;
63                 long weeks = (uptimeSeconds / (60 * 60 * 24 * 7));
64                 String uptimeString = Stream
65                                 .of(weeks + "w", days + "d", hours + "h", minutes + "m", seconds + "s")
66                                 .filter(v -> !v.startsWith("0"))
67                                 .collect(Collectors.joining(" "));
68                 return uptimeString.isEmpty() ? "0s" : uptimeString;
69         }
70
71 }