Search in sources :

Example 1 with ServerStatsType

use of com.sx4.bot.entities.info.ServerStatsType in project Sx4 by sx4-discord-bot.

the class ServerStatsManager method toData.

public synchronized List<Document> toData(Date time) {
    List<Document> list = new ArrayList<>();
    TLongSet guildIds = this.counter.keySet();
    guildIds.forEach(guildId -> {
        Map<ServerStatsType, Integer> guild = this.counter.get(guildId);
        Document data = new Document("guildId", guildId).append("time", time);
        Set<ServerStatsType> types = guild.keySet();
        for (ServerStatsType type : types) {
            data.append(type.getField(), guild.get(type));
        }
        list.add(data);
        return true;
    });
    return list;
}
Also used : TLongSet(gnu.trove.set.TLongSet) ServerStatsType(com.sx4.bot.entities.info.ServerStatsType) Document(org.bson.Document)

Example 2 with ServerStatsType

use of com.sx4.bot.entities.info.ServerStatsType in project Sx4 by sx4-discord-bot.

the class ServerStatsCommand method onCommand.

public void onCommand(Sx4CommandEvent event, @Argument(value = "duration", endless = true, nullDefault = true) Duration duration, @Option(value = "live", description = "Adds the live counting data to the total") boolean live, @Option(value = "reset", description = "Gets the stats since 00:00 UTC") boolean reset) {
    if (duration != null && (duration.toHours() < 1 || duration.toHours() > 168)) {
        event.replyFailure("Time frame cannot be less than 1 hour or more than 7 days").queue();
        return;
    }
    List<Document> data = event.getMongo().getServerStats(Filters.eq("guildId", event.getGuild().getIdLong()), MongoDatabase.EMPTY_DOCUMENT).into(new ArrayList<>());
    if (data.isEmpty()) {
        event.replyFailure("There has been no data recorded for this server yet").queue();
        return;
    }
    Date lastUpdate = event.getBot().getServerStatsManager().getLastUpdate();
    OffsetDateTime currentHour = OffsetDateTime.now(ZoneOffset.UTC).withMinute(0).withSecond(0).withNano(0);
    if (reset) {
        duration = Duration.between(currentHour.withHour(0), currentHour);
    }
    Map<Long, Map<ServerStatsType, Integer>> map = new HashMap<>();
    if (duration == null) {
        Map<ServerStatsType, Integer> defaultStatsDay = new HashMap<>();
        defaultStatsDay.put(ServerStatsType.JOINS, 0);
        defaultStatsDay.put(ServerStatsType.MESSAGES, 0);
        map.put(24L, defaultStatsDay);
        Map<ServerStatsType, Integer> defaultStatsWeek = new HashMap<>();
        defaultStatsWeek.put(ServerStatsType.JOINS, 0);
        defaultStatsWeek.put(ServerStatsType.MESSAGES, 0);
        map.put(168L, defaultStatsWeek);
    } else {
        Map<ServerStatsType, Integer> defaultStats = new HashMap<>();
        defaultStats.put(ServerStatsType.JOINS, 0);
        defaultStats.put(ServerStatsType.MESSAGES, 0);
        map.put(duration.toHours(), defaultStats);
    }
    for (Document stats : data) {
        Date time = stats.getDate("time");
        Duration difference = Duration.between(time.toInstant(), currentHour);
        int joins = stats.getInteger("joins", 0), messages = stats.getInteger("messages", 0);
        if (duration != null && difference.toHours() <= duration.toHours()) {
            this.compute(map, duration.toHours(), joins, messages);
        }
        if (duration == null && difference.toHours() <= 24) {
            this.compute(map, 24, joins, messages);
        }
        if (duration == null && difference.toDays() <= 7) {
            this.compute(map, 168, joins, messages);
        }
        lastUpdate = lastUpdate == null || lastUpdate.getTime() < time.getTime() ? time : lastUpdate;
    }
    EmbedBuilder embed = new EmbedBuilder().setAuthor("Server Stats", null, event.getGuild().getIconUrl()).setFooter("Updated every hour").setTimestamp(lastUpdate.toInstant());
    ServerStatsManager manager = event.getBot().getServerStatsManager();
    for (long key : map.keySet()) {
        Map<ServerStatsType, Integer> stats = map.get(key);
        for (ServerStatsType type : stats.keySet()) {
            embed.addField(StringUtility.title(type.getField()) + " (" + TimeUtility.LONG_TIME_FORMATTER.parse(Duration.of(key, ChronoUnit.HOURS)) + ")", String.format("%,d", stats.get(type) + (live ? manager.getCounter(event.getGuild().getIdLong(), type) : 0)), false);
        }
    }
    event.reply(embed.build()).queue();
}
Also used : ServerStatsType(com.sx4.bot.entities.info.ServerStatsType) Duration(java.time.Duration) ServerStatsManager(com.sx4.bot.managers.ServerStatsManager) Document(org.bson.Document) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) OffsetDateTime(java.time.OffsetDateTime)

Aggregations

ServerStatsType (com.sx4.bot.entities.info.ServerStatsType)2 Document (org.bson.Document)2 ServerStatsManager (com.sx4.bot.managers.ServerStatsManager)1 TLongSet (gnu.trove.set.TLongSet)1 Duration (java.time.Duration)1 OffsetDateTime (java.time.OffsetDateTime)1 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)1