07f228864a9e94eaeac6f70f5b01dad4c05894f1
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / ui / stdin / StatsCommand.java
1 /*
2  * XdccDownloader - StatsCommand.java - Copyright © 2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.xdcc.ui.stdin;
19
20 import static java.lang.String.format;
21 import static java.util.Collections.emptyList;
22
23 import java.io.IOException;
24 import java.io.Writer;
25 import java.util.Collection;
26 import java.util.List;
27 import java.util.StringJoiner;
28
29 import net.pterodactylus.xdcc.core.Core;
30 import net.pterodactylus.xdcc.data.ConnectedNetwork;
31
32 /**
33  * Command that outputs a short statistic of what is going on.
34  *
35  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36  */
37 public class StatsCommand implements Command {
38
39         private final Core core;
40
41         public StatsCommand(Core core) {
42                 this.core = core;
43         }
44
45         @Override
46         public String getName() {
47                 return "stats";
48         }
49
50         @Override
51         public Collection<String> getAliases() {
52                 return emptyList();
53         }
54
55         @Override
56         public State execute(State state, List<String> parameters, Writer outputWriter) throws IOException {
57                 int totalBotCount = 0;
58                 int totalPackCount = 0;
59                 for (ConnectedNetwork network : core.connectedNetworks()) {
60                         dumpNetworkStats(outputWriter, network);
61                         totalBotCount += network.getBotCount();
62                         totalPackCount += network.getPackCount();
63                 }
64                 outputWriter.write(format("Total: %d bots, %d packs.\n", totalBotCount, totalPackCount));
65                 return null;
66         }
67
68         private void dumpNetworkStats(Writer outputWriter, ConnectedNetwork network) throws IOException {
69                 outputWriter.write(format("Connected to %s via %s:%d.\n", network.getNetwork().name(), network.getHostname(), network.getPort(), network.getNickname()));
70                 StringJoiner joinedChannels = new StringJoiner(", ");
71                 network.getChannels().stream().forEach(
72                                 (channel) -> joinedChannels.add(channel));
73                 StringJoiner forceJoinedChannels = new StringJoiner(", ");
74                 network.getForcedChannels().stream().forEach((channel) -> forceJoinedChannels.add(channel));
75                 outputWriter.write(format("  Joined %s, force-joined %s.\n",
76                                 joinedChannels, forceJoinedChannels));
77                 outputWriter.write(format("  %d bots serving %d packs.\n", network.getBotCount(), network.getPackCount()));
78         }
79
80 }