Search in sources :

Example 1 with Formatter

use of com.djrapitops.plan.delivery.formatting.Formatter in project Plan by plan-player-analytics.

the class JSONFactory method serversAsJSONMaps.

public Map<String, Object> serversAsJSONMaps() {
    Database db = dbSystem.getDatabase();
    long now = System.currentTimeMillis();
    long weekAgo = now - TimeUnit.DAYS.toMillis(7L);
    Formatter<Long> year = formatters.yearLong();
    Formatter<Double> decimals = formatters.decimals();
    Formatter<Long> timeAmount = formatters.timeAmount();
    Map<ServerUUID, Server> serverInformation = db.query(ServerQueries.fetchPlanServerInformation());
    ServerUUID proxyUUID = serverInformation.values().stream().filter(Server::isProxy).findFirst().map(Server::getUuid).orElse(null);
    Map<ServerUUID, List<TPS>> tpsData = db.query(TPSQueries.fetchTPSDataOfAllServersBut(weekAgo, now, proxyUUID));
    Map<ServerUUID, Integer> totalPlayerCounts = db.query(PlayerCountQueries.newPlayerCounts(0, now));
    Map<ServerUUID, Integer> newPlayerCounts = db.query(PlayerCountQueries.newPlayerCounts(weekAgo, now));
    Map<ServerUUID, Integer> uniquePlayerCounts = db.query(PlayerCountQueries.uniquePlayerCounts(weekAgo, now));
    List<Map<String, Object>> servers = new ArrayList<>();
    serverInformation.entrySet().stream().sorted(Comparator.comparing(entry -> entry.getValue().getIdentifiableName().toLowerCase())).filter(entry -> entry.getValue().isNotProxy()).forEach(entry -> {
        ServerUUID serverUUID = entry.getKey();
        Map<String, Object> server = new HashMap<>();
        server.put("name", entry.getValue().getIdentifiableName());
        Optional<DateObj<Integer>> recentPeak = db.query(TPSQueries.fetchPeakPlayerCount(serverUUID, now - TimeUnit.DAYS.toMillis(2L)));
        Optional<DateObj<Integer>> allTimePeak = db.query(TPSQueries.fetchAllTimePeakPlayerCount(serverUUID));
        server.put("last_peak_date", recentPeak.map(DateObj::getDate).map(year).orElse("-"));
        server.put("best_peak_date", allTimePeak.map(DateObj::getDate).map(year).orElse("-"));
        server.put("last_peak_players", recentPeak.map(DateObj::getValue).orElse(0));
        server.put("best_peak_players", allTimePeak.map(DateObj::getValue).orElse(0));
        TPSMutator tpsMonth = new TPSMutator(tpsData.getOrDefault(serverUUID, Collections.emptyList()));
        server.put("playersOnline", tpsMonth.all().stream().map(tps -> new double[] { tps.getDate(), tps.getPlayers() }).toArray(double[][]::new));
        server.put("players", totalPlayerCounts.getOrDefault(serverUUID, 0));
        server.put("new_players", newPlayerCounts.getOrDefault(serverUUID, 0));
        server.put("unique_players", uniquePlayerCounts.getOrDefault(serverUUID, 0));
        TPSMutator tpsWeek = tpsMonth.filterDataBetween(weekAgo, now);
        double averageTPS = tpsWeek.averageTPS();
        server.put("avg_tps", averageTPS != -1 ? decimals.apply(averageTPS) : locale.get(HtmlLang.UNIT_NO_DATA).toString());
        server.put("low_tps_spikes", tpsWeek.lowTpsSpikeCount(config.get(DisplaySettings.GRAPH_TPS_THRESHOLD_MED)));
        server.put("downtime", timeAmount.apply(tpsWeek.serverDownTime()));
        server.put("current_uptime", serverUptimeCalculator.getServerUptimeMillis(serverUUID).map(timeAmount).orElse(locale.getString(GenericLang.UNAVAILABLE)));
        Optional<TPS> online = tpsWeek.getLast();
        server.put("online", online.map(point -> point.getDate() >= now - TimeUnit.MINUTES.toMillis(3L) ? point.getPlayers() : "Possibly offline").orElse(locale.get(HtmlLang.UNIT_NO_DATA).toString()));
        servers.add(server);
    });
    return Collections.singletonMap("servers", servers);
}
Also used : HtmlLang(com.djrapitops.plan.settings.locale.lang.HtmlLang) java.util(java.util) PlanConfig(com.djrapitops.plan.settings.config.PlanConfig) ServerTablePlayersQuery(com.djrapitops.plan.storage.database.queries.objects.playertable.ServerTablePlayersQuery) ServerUUID(com.djrapitops.plan.identification.ServerUUID) TPSMutator(com.djrapitops.plan.delivery.domain.mutators.TPSMutator) Singleton(javax.inject.Singleton) Database(com.djrapitops.plan.storage.database.Database) DBSystem(com.djrapitops.plan.storage.database.DBSystem) Inject(javax.inject.Inject) Graphs(com.djrapitops.plan.delivery.rendering.json.graphs.Graphs) TimeSettings(com.djrapitops.plan.settings.config.paths.TimeSettings) NetworkTablePlayersQuery(com.djrapitops.plan.storage.database.queries.objects.playertable.NetworkTablePlayersQuery) DateObj(com.djrapitops.plan.delivery.domain.DateObj) ServerUptimeCalculator(com.djrapitops.plan.gathering.ServerUptimeCalculator) ServerDto(com.djrapitops.plan.delivery.domain.datatransfer.ServerDto) ExtensionTabData(com.djrapitops.plan.extension.implementation.results.ExtensionTabData) SessionCache(com.djrapitops.plan.gathering.cache.SessionCache) Maps(com.djrapitops.plan.utilities.java.Maps) SessionsMutator(com.djrapitops.plan.delivery.domain.mutators.SessionsMutator) com.djrapitops.plan.gathering.domain(com.djrapitops.plan.gathering.domain) ServerInfo(com.djrapitops.plan.identification.ServerInfo) GenericLang(com.djrapitops.plan.settings.locale.lang.GenericLang) ExtensionServerTableDataQuery(com.djrapitops.plan.extension.implementation.storage.queries.ExtensionServerTableDataQuery) PlayerKillMutator(com.djrapitops.plan.delivery.domain.mutators.PlayerKillMutator) Formatters(com.djrapitops.plan.delivery.formatting.Formatters) SessionStartComparator(com.djrapitops.plan.utilities.comparators.SessionStartComparator) Collectors(java.util.stream.Collectors) TimeUnit(java.util.concurrent.TimeUnit) Server(com.djrapitops.plan.identification.Server) Locale(com.djrapitops.plan.settings.locale.Locale) com.djrapitops.plan.storage.database.queries.objects(com.djrapitops.plan.storage.database.queries.objects) DisplaySettings(com.djrapitops.plan.settings.config.paths.DisplaySettings) Formatter(com.djrapitops.plan.delivery.formatting.Formatter) PlayerCountQueries(com.djrapitops.plan.storage.database.queries.analysis.PlayerCountQueries) Server(com.djrapitops.plan.identification.Server) Database(com.djrapitops.plan.storage.database.Database) ServerUUID(com.djrapitops.plan.identification.ServerUUID) DateObj(com.djrapitops.plan.delivery.domain.DateObj) TPSMutator(com.djrapitops.plan.delivery.domain.mutators.TPSMutator)

Example 2 with Formatter

use of com.djrapitops.plan.delivery.formatting.Formatter in project Plan by plan-player-analytics.

the class SessionPlaceHolders method register.

@Override
public void register(PlanPlaceholders placeholders) {
    int tzOffsetMs = config.getTimeZone().getOffset(System.currentTimeMillis());
    Formatter<Long> timeAmount = formatters.timeAmount();
    Formatter<DateHolder> year = formatters.year();
    Formatter<Double> decimals = formatters.decimals();
    Database database = dbSystem.getDatabase();
    placeholders.registerStatic("sessions_play_time_total", parameters -> timeAmount.apply(database.query(SessionQueries.playtime(0L, now(), getServerUUID(parameters)))));
    placeholders.registerStatic("sessions_play_time_total_raw", parameters -> database.query(SessionQueries.playtime(0L, now(), getServerUUID(parameters))));
    placeholders.registerStatic("sessions_play_time_day", parameters -> timeAmount.apply(database.query(SessionQueries.playtime(dayAgo(), now(), getServerUUID(parameters)))));
    placeholders.registerStatic("sessions_play_time_day_raw", parameters -> database.query(SessionQueries.playtime(dayAgo(), now(), getServerUUID(parameters))));
    placeholders.registerStatic("sessions_play_time_week", parameters -> timeAmount.apply(database.query(SessionQueries.playtime(weekAgo(), now(), getServerUUID(parameters)))));
    placeholders.registerStatic("sessions_play_time_week_raw", parameters -> database.query(SessionQueries.playtime(weekAgo(), now(), getServerUUID(parameters))));
    placeholders.registerStatic("sessions_play_time_month", parameters -> timeAmount.apply(database.query(SessionQueries.playtime(monthAgo(), now(), getServerUUID(parameters)))));
    placeholders.registerStatic("sessions_play_time_month_raw", parameters -> database.query(SessionQueries.playtime(monthAgo(), now(), getServerUUID(parameters))));
    placeholders.registerStatic("sessions_active_time_total", parameters -> timeAmount.apply(database.query(SessionQueries.activePlaytime(0L, now(), getServerUUID(parameters)))));
    placeholders.registerStatic("sessions_active_time_total_raw", parameters -> database.query(SessionQueries.activePlaytime(0L, now(), getServerUUID(parameters))));
    placeholders.registerStatic("sessions_active_time_day", parameters -> timeAmount.apply(database.query(SessionQueries.activePlaytime(dayAgo(), now(), getServerUUID(parameters)))));
    placeholders.registerStatic("sessions_active_time_day_raw", parameters -> database.query(SessionQueries.activePlaytime(dayAgo(), now(), getServerUUID(parameters))));
    placeholders.registerStatic("sessions_active_time_week", parameters -> timeAmount.apply(database.query(SessionQueries.activePlaytime(weekAgo(), now(), getServerUUID(parameters)))));
    placeholders.registerStatic("sessions_active_time_week_raw", parameters -> database.query(SessionQueries.activePlaytime(weekAgo(), now(), getServerUUID(parameters))));
    placeholders.registerStatic("sessions_active_time_month", parameters -> timeAmount.apply(database.query(SessionQueries.activePlaytime(monthAgo(), now(), getServerUUID(parameters)))));
    placeholders.registerStatic("sessions_active_time_month_raw", parameters -> database.query(SessionQueries.activePlaytime(monthAgo(), now(), getServerUUID(parameters))));
    placeholders.registerStatic("sessions_afk_time_total", parameters -> timeAmount.apply(database.query(SessionQueries.afkTime(0L, now(), getServerUUID(parameters)))));
    placeholders.registerStatic("sessions_afk_time_total_raw", parameters -> database.query(SessionQueries.afkTime(0L, now(), getServerUUID(parameters))));
    placeholders.registerStatic("sessions_afk_time_day", parameters -> timeAmount.apply(database.query(SessionQueries.afkTime(dayAgo(), now(), getServerUUID(parameters)))));
    placeholders.registerStatic("sessions_afk_time_day_raw", parameters -> database.query(SessionQueries.afkTime(dayAgo(), now(), getServerUUID(parameters))));
    placeholders.registerStatic("sessions_afk_time_week", parameters -> timeAmount.apply(database.query(SessionQueries.afkTime(weekAgo(), now(), getServerUUID(parameters)))));
    placeholders.registerStatic("sessions_afk_time_week_raw", parameters -> database.query(SessionQueries.afkTime(weekAgo(), now(), getServerUUID(parameters))));
    placeholders.registerStatic("sessions_afk_time_month", parameters -> timeAmount.apply(database.query(SessionQueries.afkTime(monthAgo(), now(), getServerUUID(parameters)))));
    placeholders.registerStatic("sessions_afk_time_month_raw", parameters -> database.query(SessionQueries.afkTime(monthAgo(), now(), getServerUUID(parameters))));
    PlanPlaceholders.StaticPlaceholderLoader uniquePlayers = parameters -> database.query(PlayerCountQueries.newPlayerCount(0L, now(), getServerUUID(parameters)));
    placeholders.registerStatic("sessions_unique_players_total", uniquePlayers);
    placeholders.registerStatic("sessions_new_players_total", uniquePlayers);
    placeholders.registerStatic("sessions_unique_players_day", parameters -> database.query(PlayerCountQueries.uniquePlayerCount(dayAgo(), now(), getServerUUID(parameters))));
    placeholders.registerStatic("sessions_unique_players_today", parameters -> database.query(PlayerCountQueries.uniquePlayerCounts(dayAgo(), now(), config.getTimeZone().getOffset(now()), getServerUUID(parameters))).lastEntry().getValue());
    placeholders.registerStatic("sessions_unique_players_week", parameters -> database.query(PlayerCountQueries.uniquePlayerCount(weekAgo(), now(), getServerUUID(parameters))));
    placeholders.registerStatic("sessions_unique_players_month", parameters -> database.query(PlayerCountQueries.uniquePlayerCount(monthAgo(), now(), getServerUUID(parameters))));
    placeholders.registerStatic("sessions_players_death_total", parameters -> database.query(KillQueries.deathCount(0L, now(), getServerUUID(parameters))));
    placeholders.registerStatic("sessions_players_death_day", parameters -> database.query(KillQueries.deathCount(dayAgo(), now(), getServerUUID(parameters))));
    placeholders.registerStatic("sessions_players_death_week", parameters -> database.query(KillQueries.deathCount(weekAgo(), now(), getServerUUID(parameters))));
    placeholders.registerStatic("sessions_players_death_month", parameters -> database.query(KillQueries.deathCount(monthAgo(), now(), getServerUUID(parameters))));
    placeholders.registerStatic("sessions_players_kill_total", parameters -> database.query(KillQueries.playerKillCount(0L, now(), getServerUUID(parameters))));
    placeholders.registerStatic("sessions_players_kill_day", parameters -> database.query(KillQueries.playerKillCount(dayAgo(), now(), getServerUUID(parameters))));
    placeholders.registerStatic("sessions_players_kill_week", parameters -> database.query(KillQueries.playerKillCount(weekAgo(), now(), getServerUUID(parameters))));
    placeholders.registerStatic("sessions_players_kill_month", parameters -> database.query(KillQueries.playerKillCount(monthAgo(), now(), getServerUUID(parameters))));
    placeholders.registerStatic("sessions_mob_kill_total", parameters -> database.query(KillQueries.mobKillCount(0L, now(), getServerUUID(parameters))));
    placeholders.registerStatic("sessions_mob_kill_day", parameters -> database.query(KillQueries.mobKillCount(dayAgo(), now(), getServerUUID(parameters))));
    placeholders.registerStatic("sessions_mob_kill_week", parameters -> database.query(KillQueries.mobKillCount(weekAgo(), now(), getServerUUID(parameters))));
    placeholders.registerStatic("sessions_mob_kill_month", parameters -> database.query(KillQueries.mobKillCount(monthAgo(), now(), getServerUUID(parameters))));
    placeholders.registerStatic("sessions_average_session_length_total", parameters -> getPlaytime(database, 0L, now(), getServerUUID(parameters), timeAmount));
    placeholders.registerStatic("sessions_average_session_length_day", parameters -> getPlaytime(database, dayAgo(), now(), getServerUUID(parameters), timeAmount));
    placeholders.registerStatic("sessions_average_session_length_week", parameters -> getPlaytime(database, weekAgo(), now(), getServerUUID(parameters), timeAmount));
    placeholders.registerStatic("sessions_average_session_length_month", parameters -> getPlaytime(database, monthAgo(), now(), getServerUUID(parameters), timeAmount));
    placeholders.registerStatic("sessions_average_unique_players_total", parameters -> database.query(PlayerCountQueries.averageUniquePlayerCount(0L, now(), tzOffsetMs, getServerUUID(parameters))));
    placeholders.registerStatic("sessions_average_unique_players_day", parameters -> database.query(PlayerCountQueries.averageUniquePlayerCount(dayAgo(), now(), tzOffsetMs, getServerUUID(parameters))));
    placeholders.registerStatic("sessions_average_unique_players_week", parameters -> database.query(PlayerCountQueries.averageUniquePlayerCount(weekAgo(), now(), tzOffsetMs, getServerUUID(parameters))));
    placeholders.registerStatic("sessions_average_unique_players_month", parameters -> database.query(PlayerCountQueries.averageUniquePlayerCount(monthAgo(), now(), tzOffsetMs, getServerUUID(parameters))));
    placeholders.registerStatic("sessions_new_players_day", parameters -> database.query(PlayerCountQueries.newPlayerCount(dayAgo(), now(), getServerUUID(parameters))));
    placeholders.registerStatic("sessions_new_players_week", parameters -> database.query(PlayerCountQueries.newPlayerCount(weekAgo(), now(), getServerUUID(parameters))));
    placeholders.registerStatic("sessions_new_players_month", parameters -> database.query(PlayerCountQueries.newPlayerCount(monthAgo(), now(), getServerUUID(parameters))));
    placeholders.registerStatic("ping_total", parameters -> decimals.apply(database.query(PingQueries.averagePing(0L, now(), getServerUUID(parameters)))) + " ms");
    placeholders.registerStatic("ping_day", parameters -> decimals.apply(database.query(PingQueries.averagePing(dayAgo(), now(), getServerUUID(parameters)))) + " ms");
    placeholders.registerStatic("ping_week", parameters -> decimals.apply(database.query(PingQueries.averagePing(weekAgo(), now(), getServerUUID(parameters)))) + " ms");
    placeholders.registerStatic("ping_month", parameters -> decimals.apply(database.query(PingQueries.averagePing(monthAgo(), now(), getServerUUID(parameters)))) + " ms");
    placeholders.registerStatic("sessions_peak_count", parameters -> database.query(TPSQueries.fetchAllTimePeakPlayerCount(getServerUUID(parameters))).map(DateObj::getValue).orElse(0));
    placeholders.registerStatic("sessions_peak_date", parameters -> database.query(TPSQueries.fetchAllTimePeakPlayerCount(getServerUUID(parameters))).map(year).orElse("-"));
    placeholders.registerStatic("sessions_recent_peak_count", parameters -> database.query(TPSQueries.fetchPeakPlayerCount(getServerUUID(parameters), now() - TimeUnit.DAYS.toMillis(2L))).map(DateObj::getValue).orElse(0));
    placeholders.registerStatic("sessions_recent_peak_date", parameters -> database.query(TPSQueries.fetchPeakPlayerCount(getServerUUID(parameters), now() - TimeUnit.DAYS.toMillis(2L))).map(year).orElse("-"));
}
Also used : PlanConfig(com.djrapitops.plan.settings.config.PlanConfig) ServerInfo(com.djrapitops.plan.identification.ServerInfo) ServerUUID(com.djrapitops.plan.identification.ServerUUID) Formatters(com.djrapitops.plan.delivery.formatting.Formatters) Singleton(javax.inject.Singleton) Database(com.djrapitops.plan.storage.database.Database) DBSystem(com.djrapitops.plan.storage.database.DBSystem) Inject(javax.inject.Inject) TimeUnit(java.util.concurrent.TimeUnit) DateHolder(com.djrapitops.plan.delivery.domain.DateHolder) DateObj(com.djrapitops.plan.delivery.domain.DateObj) Server(com.djrapitops.plan.identification.Server) MiscUtils(com.djrapitops.plan.utilities.MiscUtils) Optional(java.util.Optional) Arguments(com.djrapitops.plan.commands.use.Arguments) com.djrapitops.plan.storage.database.queries.objects(com.djrapitops.plan.storage.database.queries.objects) Formatter(com.djrapitops.plan.delivery.formatting.Formatter) PlayerCountQueries(com.djrapitops.plan.storage.database.queries.analysis.PlayerCountQueries) DateHolder(com.djrapitops.plan.delivery.domain.DateHolder) DateObj(com.djrapitops.plan.delivery.domain.DateObj) Database(com.djrapitops.plan.storage.database.Database)

Aggregations

DateObj (com.djrapitops.plan.delivery.domain.DateObj)2 Formatter (com.djrapitops.plan.delivery.formatting.Formatter)2 Formatters (com.djrapitops.plan.delivery.formatting.Formatters)2 Server (com.djrapitops.plan.identification.Server)2 ServerInfo (com.djrapitops.plan.identification.ServerInfo)2 ServerUUID (com.djrapitops.plan.identification.ServerUUID)2 PlanConfig (com.djrapitops.plan.settings.config.PlanConfig)2 DBSystem (com.djrapitops.plan.storage.database.DBSystem)2 Database (com.djrapitops.plan.storage.database.Database)2 PlayerCountQueries (com.djrapitops.plan.storage.database.queries.analysis.PlayerCountQueries)2 com.djrapitops.plan.storage.database.queries.objects (com.djrapitops.plan.storage.database.queries.objects)2 TimeUnit (java.util.concurrent.TimeUnit)2 Inject (javax.inject.Inject)2 Singleton (javax.inject.Singleton)2 Arguments (com.djrapitops.plan.commands.use.Arguments)1 DateHolder (com.djrapitops.plan.delivery.domain.DateHolder)1 ServerDto (com.djrapitops.plan.delivery.domain.datatransfer.ServerDto)1 PlayerKillMutator (com.djrapitops.plan.delivery.domain.mutators.PlayerKillMutator)1 SessionsMutator (com.djrapitops.plan.delivery.domain.mutators.SessionsMutator)1 TPSMutator (com.djrapitops.plan.delivery.domain.mutators.TPSMutator)1