Add network session placeholders

Affects issues:
- Close #2267
This commit is contained in:
Aurora Lahtela 2024-01-27 13:34:49 +02:00
parent 465af8e803
commit 01ce503c77
6 changed files with 293 additions and 94 deletions

View File

@ -215,7 +215,9 @@ public class ServerPlaceHolders implements Placeholders {
private void registerDynamicCategoryPlaceholders(PlanPlaceholders placeholders, Database database) { private void registerDynamicCategoryPlaceholders(PlanPlaceholders placeholders, Database database) {
List<TopCategoryQuery<Long>> queries = new ArrayList<>(); List<TopCategoryQuery<Long>> queries = new ArrayList<>();
queries.addAll(createCategoryQueriesForAllTimespans("playtime", (index, timespan, parameters) -> TopListQueries.fetchNthTop10PlaytimePlayerOn(getServerUUID(parameters), index, System.currentTimeMillis() - timespan, System.currentTimeMillis()))); queries.addAll(createCategoryQueriesForAllTimespans("playtime", (index, timespan, parameters) -> TopListQueries.fetchNthTop10PlaytimePlayerOn(getServerUUID(parameters), index, System.currentTimeMillis() - timespan, System.currentTimeMillis())));
queries.addAll(createCategoryQueriesForAllTimespans("network_playtime", (index, timespan, parameters) -> TopListQueries.fetchNthTop10PlaytimePlayerOn(null, index, System.currentTimeMillis() - timespan, System.currentTimeMillis())));
queries.addAll(createCategoryQueriesForAllTimespans("active_playtime", (index, timespan, parameters) -> TopListQueries.fetchNthTop10ActivePlaytimePlayerOn(getServerUUID(parameters), index, System.currentTimeMillis() - timespan, System.currentTimeMillis()))); queries.addAll(createCategoryQueriesForAllTimespans("active_playtime", (index, timespan, parameters) -> TopListQueries.fetchNthTop10ActivePlaytimePlayerOn(getServerUUID(parameters), index, System.currentTimeMillis() - timespan, System.currentTimeMillis())));
queries.addAll(createCategoryQueriesForAllTimespans("network_active_playtime", (index, timespan, parameters) -> TopListQueries.fetchNthTop10ActivePlaytimePlayerOn(null, index, System.currentTimeMillis() - timespan, System.currentTimeMillis())));
queries.addAll(createCategoryQueriesForAllTimespans("player_kills", (index, timespan, parameters) -> TopListQueries.fetchNthTop10PlayerKillCountOn(getServerUUID(parameters), index, System.currentTimeMillis() - timespan, System.currentTimeMillis()))); queries.addAll(createCategoryQueriesForAllTimespans("player_kills", (index, timespan, parameters) -> TopListQueries.fetchNthTop10PlayerKillCountOn(getServerUUID(parameters), index, System.currentTimeMillis() - timespan, System.currentTimeMillis())));
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {

View File

@ -52,6 +52,11 @@ public class SessionPlaceHolders implements Placeholders {
private final ServerInfo serverInfo; private final ServerInfo serverInfo;
private final Formatters formatters; private final Formatters formatters;
private Formatter<Long> timeAmount;
private Formatter<DateHolder> year;
private Formatter<Double> decimals;
private Database database;
@Inject @Inject
public SessionPlaceHolders( public SessionPlaceHolders(
PlanConfig config, PlanConfig config,
@ -71,67 +76,149 @@ public class SessionPlaceHolders implements Placeholders {
return timeAmount.apply(sessionCount != 0 ? playtime / sessionCount : playtime); return timeAmount.apply(sessionCount != 0 ? playtime / sessionCount : playtime);
} }
private static String getPlaytime(Database database, long after, long before, Formatter<Long> timeAmount) {
Long playtime = database.query(SessionQueries.playtime(after, before));
Long sessionCount = database.query(SessionQueries.sessionCount(after, before));
return timeAmount.apply(sessionCount != 0 ? playtime / sessionCount : playtime);
}
@Override @Override
public void register( public void register(
PlanPlaceholders placeholders PlanPlaceholders placeholders
) { ) {
int tzOffsetMs = config.getTimeZone().getOffset(System.currentTimeMillis()); int tzOffsetMs = config.getTimeZone().getOffset(System.currentTimeMillis());
Formatter<Long> timeAmount = formatters.timeAmount(); timeAmount = formatters.timeAmount();
Formatter<DateHolder> year = formatters.year(); year = formatters.year();
Formatter<Double> decimals = formatters.decimals(); decimals = formatters.decimals();
Database database = dbSystem.getDatabase(); database = dbSystem.getDatabase();
placeholders.registerStatic("sessions_play_time_total", registerServerPlaytime(placeholders);
parameters -> timeAmount.apply(database.query(SessionQueries.playtime(0L, now(), getServerUUID(parameters))))); registerNetworkPlaytime(placeholders);
placeholders.registerStatic("sessions_play_time_total_raw", registerServerActivePlaytime(placeholders);
parameters -> database.query(SessionQueries.playtime(0L, now(), getServerUUID(parameters)))); registerNetworkActivePlaytime(placeholders);
placeholders.registerStatic("sessions_play_time_day", registerServerAfkTime(placeholders);
parameters -> timeAmount.apply(database.query(SessionQueries.playtime(dayAgo(), now(), getServerUUID(parameters))))); registerNetworkAfkTime(placeholders);
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", registerServerPve(placeholders);
parameters -> timeAmount.apply(database.query(SessionQueries.activePlaytime(0L, now(), getServerUUID(parameters))))); registerSessionLength(placeholders);
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", registerServerUniquePlayers(placeholders);
parameters -> timeAmount.apply(database.query(SessionQueries.afkTime(0L, now(), getServerUUID(parameters))))); registerNetworkUniquePlayers(placeholders);
placeholders.registerStatic("sessions_afk_time_total_raw", registerAverageUniquePlayer(placeholders, tzOffsetMs);
parameters -> database.query(SessionQueries.afkTime(0L, now(), getServerUUID(parameters)))); registerNewPlayer(placeholders);
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))));
registerPing(placeholders);
registerServerPeakCounts(placeholders);
}
private void registerServerPeakCounts(PlanPlaceholders placeholders) {
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("-"));
}
private void registerPing(PlanPlaceholders placeholders) {
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("network_ping_total",
parameters -> decimals.apply(database.query(PingQueries.averagePing(0L, now()))) + " ms");
placeholders.registerStatic("network_ping_day",
parameters -> decimals.apply(database.query(PingQueries.averagePing(dayAgo(), now()))) + " ms");
placeholders.registerStatic("network_ping_week",
parameters -> decimals.apply(database.query(PingQueries.averagePing(weekAgo(), now()))) + " ms");
placeholders.registerStatic("network_ping_month",
parameters -> decimals.apply(database.query(PingQueries.averagePing(monthAgo(), now()))) + " ms");
}
private void registerNewPlayer(PlanPlaceholders placeholders) {
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("network_sessions_new_players_day",
parameters -> database.query(PlayerCountQueries.newPlayerCount(dayAgo(), now())));
placeholders.registerStatic("network_sessions_new_players_week",
parameters -> database.query(PlayerCountQueries.newPlayerCount(weekAgo(), now())));
placeholders.registerStatic("network_sessions_new_players_month",
parameters -> database.query(PlayerCountQueries.newPlayerCount(monthAgo(), now())));
}
private void registerAverageUniquePlayer(PlanPlaceholders placeholders, int tzOffsetMs) {
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("network_sessions_average_unique_players_total",
parameters -> database.query(PlayerCountQueries.averageUniquePlayerCount(0L, now(), tzOffsetMs)));
placeholders.registerStatic("network_sessions_average_unique_players_day",
parameters -> database.query(PlayerCountQueries.averageUniquePlayerCount(dayAgo(), now(), tzOffsetMs)));
placeholders.registerStatic("network_sessions_average_unique_players_week",
parameters -> database.query(PlayerCountQueries.averageUniquePlayerCount(weekAgo(), now(), tzOffsetMs)));
placeholders.registerStatic("network_sessions_average_unique_players_month",
parameters -> database.query(PlayerCountQueries.averageUniquePlayerCount(monthAgo(), now(), tzOffsetMs)));
}
private void registerSessionLength(PlanPlaceholders placeholders) {
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("network_sessions_average_session_length_total",
parameters -> getPlaytime(database, 0L, now(), timeAmount));
placeholders.registerStatic("network_sessions_average_session_length_day",
parameters -> getPlaytime(database, dayAgo(), now(), timeAmount));
placeholders.registerStatic("network_sessions_average_session_length_week",
parameters -> getPlaytime(database, weekAgo(), now(), timeAmount));
placeholders.registerStatic("network_sessions_average_session_length_month",
parameters -> getPlaytime(database, monthAgo(), now(), timeAmount));
}
private void registerNetworkUniquePlayers(PlanPlaceholders placeholders) {
PlanPlaceholders.StaticPlaceholderLoader networkUniquePlayers = parameters -> database.query(PlayerCountQueries.newPlayerCount(0L, now()));
placeholders.registerStatic("network_sessions_unique_players_total", networkUniquePlayers);
placeholders.registerStatic("network_sessions_new_players_total", networkUniquePlayers);
placeholders.registerStatic("network_sessions_unique_players_day",
parameters -> database.query(PlayerCountQueries.uniquePlayerCount(dayAgo(), now())));
placeholders.registerStatic("network_sessions_unique_players_today",
parameters -> {
NavigableMap<Long, Integer> playerCounts = database.query(PlayerCountQueries.uniquePlayerCounts(dayAgo(), now(), config.getTimeZone().getOffset(now())));
return playerCounts.isEmpty() ? 0 : playerCounts.lastEntry().getValue();
});
placeholders.registerStatic("network_sessions_unique_players_week",
parameters -> database.query(PlayerCountQueries.uniquePlayerCount(weekAgo(), now())));
placeholders.registerStatic("network_sessions_unique_players_month",
parameters -> database.query(PlayerCountQueries.uniquePlayerCount(monthAgo(), now())));
}
private void registerServerUniquePlayers(PlanPlaceholders placeholders) {
PlanPlaceholders.StaticPlaceholderLoader uniquePlayers = parameters -> database.query(PlayerCountQueries.newPlayerCount(0L, 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_unique_players_total", uniquePlayers);
placeholders.registerStatic("sessions_new_players_total", uniquePlayers); placeholders.registerStatic("sessions_new_players_total", uniquePlayers);
@ -147,7 +234,9 @@ public class SessionPlaceHolders implements Placeholders {
parameters -> database.query(PlayerCountQueries.uniquePlayerCount(weekAgo(), now(), getServerUUID(parameters)))); parameters -> database.query(PlayerCountQueries.uniquePlayerCount(weekAgo(), now(), getServerUUID(parameters))));
placeholders.registerStatic("sessions_unique_players_month", placeholders.registerStatic("sessions_unique_players_month",
parameters -> database.query(PlayerCountQueries.uniquePlayerCount(monthAgo(), now(), getServerUUID(parameters)))); parameters -> database.query(PlayerCountQueries.uniquePlayerCount(monthAgo(), now(), getServerUUID(parameters))));
}
private void registerServerPve(PlanPlaceholders placeholders) {
placeholders.registerStatic("sessions_players_death_total", placeholders.registerStatic("sessions_players_death_total",
parameters -> database.query(KillQueries.deathCount(0L, now(), getServerUUID(parameters)))); parameters -> database.query(KillQueries.deathCount(0L, now(), getServerUUID(parameters))));
placeholders.registerStatic("sessions_players_death_day", placeholders.registerStatic("sessions_players_death_day",
@ -174,50 +263,120 @@ public class SessionPlaceHolders implements Placeholders {
parameters -> database.query(KillQueries.mobKillCount(weekAgo(), now(), getServerUUID(parameters)))); parameters -> database.query(KillQueries.mobKillCount(weekAgo(), now(), getServerUUID(parameters))));
placeholders.registerStatic("sessions_mob_kill_month", placeholders.registerStatic("sessions_mob_kill_month",
parameters -> database.query(KillQueries.mobKillCount(monthAgo(), now(), getServerUUID(parameters)))); parameters -> database.query(KillQueries.mobKillCount(monthAgo(), now(), getServerUUID(parameters))));
}
placeholders.registerStatic("sessions_average_session_length_total", private void registerNetworkAfkTime(PlanPlaceholders placeholders) {
parameters -> getPlaytime(database, 0L, now(), getServerUUID(parameters), timeAmount)); placeholders.registerStatic("network_sessions_afk_time_total",
placeholders.registerStatic("sessions_average_session_length_day", parameters -> timeAmount.apply(database.query(SessionQueries.afkTime(0L, now()))));
parameters -> getPlaytime(database, dayAgo(), now(), getServerUUID(parameters), timeAmount)); placeholders.registerStatic("network_sessions_afk_time_total_raw",
placeholders.registerStatic("sessions_average_session_length_week", parameters -> database.query(SessionQueries.afkTime(0L, now())));
parameters -> getPlaytime(database, weekAgo(), now(), getServerUUID(parameters), timeAmount)); placeholders.registerStatic("network_sessions_afk_time_day",
placeholders.registerStatic("sessions_average_session_length_month", parameters -> timeAmount.apply(database.query(SessionQueries.afkTime(dayAgo(), now()))));
parameters -> getPlaytime(database, monthAgo(), now(), getServerUUID(parameters), timeAmount)); placeholders.registerStatic("network_sessions_afk_time_day_raw",
parameters -> database.query(SessionQueries.afkTime(dayAgo(), now())));
placeholders.registerStatic("network_sessions_afk_time_week",
parameters -> timeAmount.apply(database.query(SessionQueries.afkTime(weekAgo(), now()))));
placeholders.registerStatic("network_sessions_afk_time_week_raw",
parameters -> database.query(SessionQueries.afkTime(weekAgo(), now())));
placeholders.registerStatic("network_sessions_afk_time_month",
parameters -> timeAmount.apply(database.query(SessionQueries.afkTime(monthAgo(), now()))));
placeholders.registerStatic("network_sessions_afk_time_month_raw",
parameters -> database.query(SessionQueries.afkTime(monthAgo(), now())));
}
placeholders.registerStatic("sessions_average_unique_players_total", private void registerServerAfkTime(PlanPlaceholders placeholders) {
parameters -> database.query(PlayerCountQueries.averageUniquePlayerCount(0L, now(), tzOffsetMs, getServerUUID(parameters)))); placeholders.registerStatic("sessions_afk_time_total",
placeholders.registerStatic("sessions_average_unique_players_day", parameters -> timeAmount.apply(database.query(SessionQueries.afkTime(0L, now(), getServerUUID(parameters)))));
parameters -> database.query(PlayerCountQueries.averageUniquePlayerCount(dayAgo(), now(), tzOffsetMs, getServerUUID(parameters)))); placeholders.registerStatic("sessions_afk_time_total_raw",
placeholders.registerStatic("sessions_average_unique_players_week", parameters -> database.query(SessionQueries.afkTime(0L, now(), getServerUUID(parameters))));
parameters -> database.query(PlayerCountQueries.averageUniquePlayerCount(weekAgo(), now(), tzOffsetMs, getServerUUID(parameters)))); placeholders.registerStatic("sessions_afk_time_day",
placeholders.registerStatic("sessions_average_unique_players_month", parameters -> timeAmount.apply(database.query(SessionQueries.afkTime(dayAgo(), now(), getServerUUID(parameters)))));
parameters -> database.query(PlayerCountQueries.averageUniquePlayerCount(monthAgo(), now(), tzOffsetMs, 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))));
}
placeholders.registerStatic("sessions_new_players_day", private void registerNetworkActivePlaytime(PlanPlaceholders placeholders) {
parameters -> database.query(PlayerCountQueries.newPlayerCount(dayAgo(), now(), getServerUUID(parameters)))); placeholders.registerStatic("network_sessions_active_time_total",
placeholders.registerStatic("sessions_new_players_week", parameters -> timeAmount.apply(database.query(SessionQueries.activePlaytime(0L, now()))));
parameters -> database.query(PlayerCountQueries.newPlayerCount(weekAgo(), now(), getServerUUID(parameters)))); placeholders.registerStatic("network_sessions_active_time_total_raw",
placeholders.registerStatic("sessions_new_players_month", parameters -> database.query(SessionQueries.activePlaytime(0L, now())));
parameters -> database.query(PlayerCountQueries.newPlayerCount(monthAgo(), now(), getServerUUID(parameters)))); placeholders.registerStatic("network_sessions_active_time_day",
parameters -> timeAmount.apply(database.query(SessionQueries.activePlaytime(dayAgo(), now()))));
placeholders.registerStatic("network_sessions_active_time_day_raw",
parameters -> database.query(SessionQueries.activePlaytime(dayAgo(), now())));
placeholders.registerStatic("network_sessions_active_time_week",
parameters -> timeAmount.apply(database.query(SessionQueries.activePlaytime(weekAgo(), now()))));
placeholders.registerStatic("network_sessions_active_time_week_raw",
parameters -> database.query(SessionQueries.activePlaytime(weekAgo(), now())));
placeholders.registerStatic("network_sessions_active_time_month",
parameters -> timeAmount.apply(database.query(SessionQueries.activePlaytime(monthAgo(), now()))));
placeholders.registerStatic("network_sessions_active_time_month_raw",
parameters -> database.query(SessionQueries.activePlaytime(monthAgo(), now())));
}
placeholders.registerStatic("ping_total", private void registerServerActivePlaytime(PlanPlaceholders placeholders) {
parameters -> decimals.apply(database.query(PingQueries.averagePing(0L, now(), getServerUUID(parameters)))) + " ms"); placeholders.registerStatic("sessions_active_time_total",
placeholders.registerStatic("ping_day", parameters -> timeAmount.apply(database.query(SessionQueries.activePlaytime(0L, now(), getServerUUID(parameters)))));
parameters -> decimals.apply(database.query(PingQueries.averagePing(dayAgo(), now(), getServerUUID(parameters)))) + " ms"); placeholders.registerStatic("sessions_active_time_total_raw",
placeholders.registerStatic("ping_week", parameters -> database.query(SessionQueries.activePlaytime(0L, now(), getServerUUID(parameters))));
parameters -> decimals.apply(database.query(PingQueries.averagePing(weekAgo(), now(), getServerUUID(parameters)))) + " ms"); placeholders.registerStatic("sessions_active_time_day",
placeholders.registerStatic("ping_month", parameters -> timeAmount.apply(database.query(SessionQueries.activePlaytime(dayAgo(), now(), getServerUUID(parameters)))));
parameters -> decimals.apply(database.query(PingQueries.averagePing(monthAgo(), now(), getServerUUID(parameters)))) + " ms"); 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_peak_count", private void registerNetworkPlaytime(PlanPlaceholders placeholders) {
parameters -> database.query(TPSQueries.fetchAllTimePeakPlayerCount(getServerUUID(parameters))).map(DateObj::getValue).orElse(0)); placeholders.registerStatic("network_sessions_play_time_total",
placeholders.registerStatic("sessions_peak_date", parameters -> timeAmount.apply(database.query(SessionQueries.playtime(0L, now()))));
parameters -> database.query(TPSQueries.fetchAllTimePeakPlayerCount(getServerUUID(parameters))).map(year).orElse("-")); placeholders.registerStatic("network_sessions_play_time_total_raw",
parameters -> database.query(SessionQueries.playtime(0L, now())));
placeholders.registerStatic("network_sessions_play_time_day",
parameters -> timeAmount.apply(database.query(SessionQueries.playtime(dayAgo(), now()))));
placeholders.registerStatic("network_sessions_play_time_day_raw",
parameters -> database.query(SessionQueries.playtime(dayAgo(), now())));
placeholders.registerStatic("network_sessions_play_time_week",
parameters -> timeAmount.apply(database.query(SessionQueries.playtime(weekAgo(), now()))));
placeholders.registerStatic("network_sessions_play_time_week_raw",
parameters -> database.query(SessionQueries.playtime(weekAgo(), now())));
placeholders.registerStatic("network_sessions_play_time_month",
parameters -> timeAmount.apply(database.query(SessionQueries.playtime(monthAgo(), now()))));
placeholders.registerStatic("network_sessions_play_time_month_raw",
parameters -> database.query(SessionQueries.playtime(monthAgo(), now())));
}
placeholders.registerStatic("sessions_recent_peak_count", private void registerServerPlaytime(PlanPlaceholders placeholders) {
parameters -> database.query(TPSQueries.fetchPeakPlayerCount(getServerUUID(parameters), now() - TimeUnit.DAYS.toMillis(2L))).map(DateObj::getValue).orElse(0)); placeholders.registerStatic("sessions_play_time_total",
placeholders.registerStatic("sessions_recent_peak_date", parameters -> timeAmount.apply(database.query(SessionQueries.playtime(0L, now(), getServerUUID(parameters)))));
parameters -> database.query(TPSQueries.fetchPeakPlayerCount(getServerUUID(parameters), now() - TimeUnit.DAYS.toMillis(2L))).map(year).orElse("-")); 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))));
} }
private ServerUUID getServerUUID(@Untrusted Arguments parameters) { private ServerUUID getServerUUID(@Untrusted Arguments parameters) {

View File

@ -225,6 +225,26 @@ public class PlayerCountQueries {
}; };
} }
public static Query<Integer> averageUniquePlayerCount(long after, long before, long timeZoneOffset) {
return database -> {
Sql sql = database.getSql();
String selectUniquePlayersPerDay = SELECT +
sql.dateToEpochSecond(sql.dateToDayStamp(sql.epochSecondToDate('(' + SessionsTable.SESSION_START + "+?)/1000"))) +
"*1000 as date," +
"COUNT(DISTINCT " + SessionsTable.USER_ID + ") as " + PLAYER_COUNT +
FROM + SessionsTable.TABLE_NAME +
WHERE + SessionsTable.SESSION_END + "<=?" +
AND + SessionsTable.SESSION_START + ">=?" +
GROUP_BY + "date";
String selectAverage = SELECT + "AVG(" + PLAYER_COUNT + ") as average" + FROM + '(' + selectUniquePlayersPerDay + ") q1";
return database.queryOptional(selectAverage,
set -> (int) set.getDouble("average"),
timeZoneOffset, before, after)
.orElse(0);
};
}
public static Query<Integer> newPlayerCount(long after, long before, ServerUUID serverUUID) { public static Query<Integer> newPlayerCount(long after, long before, ServerUUID serverUUID) {
String sql = SELECT + "COUNT(1) as " + PLAYER_COUNT + String sql = SELECT + "COUNT(1) as " + PLAYER_COUNT +
FROM + UserInfoTable.TABLE_NAME + FROM + UserInfoTable.TABLE_NAME +

View File

@ -40,7 +40,7 @@ public class TopListQueries {
"SUM(" + SessionsTable.SESSION_END + '-' + SessionsTable.SESSION_START + ") as playtime" + "SUM(" + SessionsTable.SESSION_END + '-' + SessionsTable.SESSION_START + ") as playtime" +
FROM + SessionsTable.TABLE_NAME + " s" + FROM + SessionsTable.TABLE_NAME + " s" +
INNER_JOIN + UsersTable.TABLE_NAME + " u on u." + UsersTable.ID + "=s." + SessionsTable.USER_ID + INNER_JOIN + UsersTable.TABLE_NAME + " u on u." + UsersTable.ID + "=s." + SessionsTable.USER_ID +
WHERE + SessionsTable.SERVER_ID + "=" + ServerTable.SELECT_SERVER_ID + WHERE + "(? IS NULL OR " + SessionsTable.SERVER_ID + "=" + ServerTable.SELECT_SERVER_ID + ')' +
AND + SessionsTable.SESSION_START + ">?" + AND + SessionsTable.SESSION_START + ">?" +
AND + SessionsTable.SESSION_END + "<?" + AND + SessionsTable.SESSION_END + "<?" +
GROUP_BY + UsersTable.USER_NAME + GROUP_BY + UsersTable.USER_NAME +
@ -49,7 +49,7 @@ public class TopListQueries {
OFFSET + "?"; OFFSET + "?";
return db -> db.queryOptional(sql, set -> new TopListEntry<>(set.getString(UsersTable.USER_NAME), set.getLong("playtime")), return db -> db.queryOptional(sql, set -> new TopListEntry<>(set.getString(UsersTable.USER_NAME), set.getLong("playtime")),
serverUUID, after, before, n); serverUUID, serverUUID, after, before, n);
} }
public static Query<Optional<TopListEntry<Long>>> fetchNthTop10ActivePlaytimePlayerOn(ServerUUID serverUUID, int n, long after, long before) { public static Query<Optional<TopListEntry<Long>>> fetchNthTop10ActivePlaytimePlayerOn(ServerUUID serverUUID, int n, long after, long before) {
@ -58,7 +58,7 @@ public class TopListQueries {
"SUM(" + SessionsTable.SESSION_END + '-' + SessionsTable.SESSION_START + '-' + SessionsTable.AFK_TIME + ") as active_playtime" + "SUM(" + SessionsTable.SESSION_END + '-' + SessionsTable.SESSION_START + '-' + SessionsTable.AFK_TIME + ") as active_playtime" +
FROM + SessionsTable.TABLE_NAME + " s" + FROM + SessionsTable.TABLE_NAME + " s" +
INNER_JOIN + UsersTable.TABLE_NAME + " u on u." + UsersTable.ID + "=s." + SessionsTable.USER_ID + INNER_JOIN + UsersTable.TABLE_NAME + " u on u." + UsersTable.ID + "=s." + SessionsTable.USER_ID +
WHERE + SessionsTable.SERVER_ID + "=" + ServerTable.SELECT_SERVER_ID + WHERE + "(? IS NULL OR " + SessionsTable.SERVER_ID + "=" + ServerTable.SELECT_SERVER_ID + ')' +
AND + SessionsTable.SESSION_START + ">?" + AND + SessionsTable.SESSION_START + ">?" +
AND + SessionsTable.SESSION_END + "<?" + AND + SessionsTable.SESSION_END + "<?" +
GROUP_BY + UsersTable.USER_NAME + GROUP_BY + UsersTable.USER_NAME +
@ -67,7 +67,7 @@ public class TopListQueries {
OFFSET + "?"; OFFSET + "?";
return db -> db.queryOptional(sql, set -> new TopListEntry<>(set.getString(UsersTable.USER_NAME), set.getLong("active_playtime")), return db -> db.queryOptional(sql, set -> new TopListEntry<>(set.getString(UsersTable.USER_NAME), set.getLong("active_playtime")),
serverUUID, after, before, n); serverUUID, serverUUID, after, before, n);
} }
public static Query<Optional<TopListEntry<Long>>> fetchNthTop10PlayerKillCountOn(ServerUUID serverUUID, int n, long after, long before) { public static Query<Optional<TopListEntry<Long>>> fetchNthTop10PlayerKillCountOn(ServerUUID serverUUID, int n, long after, long before) {

View File

@ -260,4 +260,12 @@ public class PingQueries {
} }
}; };
} }
public static Query<Double> averagePing(long after, long before) {
String sql = SELECT + "AVG(" + PingTable.AVG_PING + ") as average" + FROM + PingTable.TABLE_NAME +
WHERE + PingTable.SERVER_ID + "=" + ServerTable.SELECT_SERVER_ID +
AND + PingTable.DATE + ">=?" +
AND + PingTable.DATE + "<=?";
return db -> db.queryOptional(sql, set -> set.getDouble("average"), after, before).orElse(-1.0);
}
} }

View File

@ -917,6 +917,16 @@ public class SessionQueries {
}; };
} }
public static Query<Long> activePlaytime(long after, long before) {
String sql = SELECT + "SUM(" + SessionsTable.SESSION_END + '-' + SessionsTable.SESSION_START + '-' + SessionsTable.AFK_TIME +
") as playtime" +
FROM + SessionsTable.TABLE_NAME +
WHERE + SessionsTable.SESSION_END + ">=?" +
AND + SessionsTable.SESSION_START + "<=?";
return db -> db.queryOptional(sql, set -> set.getLong("playtime"), after, before)
.orElse(0L);
}
public static Query<Set<Integer>> userIdsOfPlayedBetween(long after, long before, List<ServerUUID> serverUUIDs) { public static Query<Set<Integer>> userIdsOfPlayedBetween(long after, long before, List<ServerUUID> serverUUIDs) {
String selectServerIds = SELECT + ServerTable.ID + String selectServerIds = SELECT + ServerTable.ID +
FROM + ServerTable.TABLE_NAME + FROM + ServerTable.TABLE_NAME +