package net.pterodactylus.xdcc.ui.stdin;
+import static java.lang.String.format;
import static java.util.Collections.emptyList;
import java.io.IOException;
import java.io.Writer;
import java.util.Collection;
import java.util.List;
-import java.util.Set;
+import java.util.StringJoiner;
import net.pterodactylus.xdcc.core.Core;
-import net.pterodactylus.xdcc.data.Bot;
-import net.pterodactylus.xdcc.data.Pack;
-
-import com.google.common.collect.Sets;
+import net.pterodactylus.xdcc.data.ConnectedNetwork;
/**
* Command that outputs a short statistic of what is going on.
*/
public class StatsCommand implements Command {
- /** The core to operate on. */
private final Core core;
- /**
- * Creates a new stats command.
- *
- * @param core
- * The core to operate on
- */
public StatsCommand(Core core) {
this.core = core;
}
- //
- // COMMAND METHODS
- //
-
@Override
public String getName() {
return "stats";
@Override
public State execute(State state, List<String> parameters, Writer outputWriter) throws IOException {
- int configuredChannelsCount = core.channels().size();
- int joinedChannelsCount = core.joinedChannels().size();
- int extraChannelsCount = core.extraChannels().size();
- Collection<Bot> bots = core.bots();
- Set<String> packNames = Sets.newHashSet();
- int packsCount = 0;
- for (Bot bot : bots) {
- packsCount += bot.packs().size();
- for (Pack pack : bot) {
- packNames.add(pack.name());
+ int totalBotCount = 0;
+ int totalPackCount = 0;
+ for (ConnectedNetwork network : core.connectedNetworks()) {
+ dumpNetworkStats(outputWriter, network);
+ totalBotCount += network.getBotCount();
+ totalPackCount += network.getPackCount();
+ }
+ outputWriter.write(format("Total: %d bots, %d packs.\n", totalBotCount, totalPackCount));
+ return state;
+ }
+
+ private void dumpNetworkStats(Writer outputWriter,
+ ConnectedNetwork network) throws IOException {
+ outputWriter.write(format("Connected to %s via %s:%d.\n",
+ network.getNetwork().name(), network.getHostname(),
+ network.getPort(), network.getNickname()));
+ String joinedChannels = joinChannelNames(network.getChannels());
+ String forcedChannels = joinChannelNames(network.getForcedChannels());
+ if (forcedChannels.isEmpty()) {
+ if (!joinedChannels.isEmpty()) {
+ outputWriter.write(format(" Joined %s.\n", joinedChannels));
+ }
+ } else {
+ if (joinedChannels.isEmpty()) {
+ outputWriter.write(
+ format(" Force-joined %s.\n", forcedChannels));
+ } else {
+ outputWriter.write(format(" Joined %s, force-joined %s.\n",
+ joinedChannels, forcedChannels));
}
}
+ outputWriter.write(
+ format(" %d bots serving %d packs.\n", network.getBotCount(),
+ network.getPackCount()));
+ }
- outputWriter.write(String.format("%d channels (%d joined, %d extra), %d bots offering %d packs (%d unique).\n", configuredChannelsCount, joinedChannelsCount, extraChannelsCount, bots.size(), packsCount, packNames.size()));
- outputWriter.flush();
- return state;
+ private String joinChannelNames(Collection<String> channels) {
+ StringJoiner joinedChannels = new StringJoiner(", ");
+ channels.stream().forEach((channel) -> joinedChannels.add(channel));
+ return joinedChannels.toString();
}
}