diff --git a/src/main/java/com/diamondfire/helpbot/bot/HelpBotInstance.java b/src/main/java/com/diamondfire/helpbot/bot/HelpBotInstance.java index 699b064a..fe6f3a76 100644 --- a/src/main/java/com/diamondfire/helpbot/bot/HelpBotInstance.java +++ b/src/main/java/com/diamondfire/helpbot/bot/HelpBotInstance.java @@ -1,6 +1,5 @@ package com.diamondfire.helpbot.bot; -import com.diamondfire.helpbot.HelpBot; import com.diamondfire.helpbot.bot.command.CommandHandler; import com.diamondfire.helpbot.bot.command.impl.codeblock.*; import com.diamondfire.helpbot.bot.command.impl.other.*; @@ -110,7 +109,8 @@ public static void initialize() throws LoginException { new ExcusedStaffCommand(), new SupportBannedPlayersCommand(), new DiscussionMuteCommand(), - new NbsCommand() + new NbsCommand(), + new DailySessionsCommand() ); JDABuilder builder = JDABuilder.createDefault(config.getToken()) diff --git a/src/main/java/com/diamondfire/helpbot/bot/command/impl/stats/support/DailySessionsCommand.java b/src/main/java/com/diamondfire/helpbot/bot/command/impl/stats/support/DailySessionsCommand.java new file mode 100644 index 00000000..fb1bf540 --- /dev/null +++ b/src/main/java/com/diamondfire/helpbot/bot/command/impl/stats/support/DailySessionsCommand.java @@ -0,0 +1,99 @@ +package com.diamondfire.helpbot.bot.command.impl.stats.support; + +import com.diamondfire.helpbot.bot.command.argument.ArgumentSet; +import com.diamondfire.helpbot.bot.command.argument.impl.parsing.types.SingleArgumentContainer; +import com.diamondfire.helpbot.bot.command.argument.impl.types.DateArgument; +import com.diamondfire.helpbot.bot.command.help.*; +import com.diamondfire.helpbot.bot.command.impl.Command; +import com.diamondfire.helpbot.bot.command.permissions.Permission; +import com.diamondfire.helpbot.bot.events.CommandEvent; +import com.diamondfire.helpbot.sys.database.impl.DatabaseQuery; +import com.diamondfire.helpbot.sys.database.impl.queries.BasicQuery; +import com.diamondfire.helpbot.sys.graph.graphable.*; +import com.diamondfire.helpbot.sys.graph.impl.ChartGraphBuilder; +import com.diamondfire.helpbot.util.DateUtil; + +import java.sql.ResultSet; +import java.time.Instant; +import java.util.*; + + +public class DailySessionsCommand extends Command { + + @Override + public String getName() { + return "dailysessions"; + } + + @Override + public String[] getAliases() { + return new String[]{"ds"}; + } + + @Override + public HelpContext getHelpContext() { + return new HelpContext() + .description("Generates a graph of all sessions done in a specific day.") + .category(CommandCategory.SUPPORT) + .addArgument( + new HelpContextArgument() + .name("date") + .optional() + ); + } + + @Override + public ArgumentSet compileArguments() { + return new ArgumentSet() + .addArgument("date", + new SingleArgumentContainer<>(new DateArgument()).optional(null)); + + } + + @Override + public Permission getPermission() { + return Permission.SUPPORT; + } + + @Override + public void run(CommandEvent event) { + Date date; + if (event.getArgument("date") == null) { + date = Date.from(Instant.now()); + } else { + date = event.getArgument("date"); + } + + java.sql.Date sqlDate = DateUtil.toSqlDate(date); + + new DatabaseQuery() + .query(new BasicQuery("SELECT hour(time) AS time, COUNT(*) AS count\n" + + "FROM hypercube.support_sessions\n" + + "WHERE DATE(time) = ?\n" + + "GROUP BY hour(time)\n" + + "ORDER BY time", + (statement) -> statement.setDate(1, sqlDate)) + ) + .compile() + .run((query) -> { + Map, Integer> dates = new LinkedHashMap<>(); + ResultSet set = query.getResult(); + + for (int i = 0; i < 25; i++) { + boolean end = set.next(); + String timedisplay = i + ""; + if (i < 10) { + timedisplay = "0" + timedisplay; + } + dates.put(new StringEntry(timedisplay + ":00"), !end ? 0 : set.getInt("count")); + } + + event.getChannel().sendFile(new ChartGraphBuilder() + .setGraphName(String.format("Total sessions on %s", date)) + .createGraph(dates)).queue(); + + }); + + } + +} \ No newline at end of file