use of com.djrapitops.plan.delivery.domain.DateHolder in project Plan by plan-player-analytics.
the class PingMutator method addPingToSessions.
public void addPingToSessions(List<FinishedSession> sessions) {
if (sessions.isEmpty())
return;
Comparator<DateHolder> comparator = new DateHolderOldestComparator();
sessions.sort(comparator);
pings.sort(comparator);
Map<ServerUUID, SortedMap<Long, Ping>> pingByServer = sortByServers(pings);
Map<ServerUUID, List<FinishedSession>> sessionsByServer = SessionsMutator.sortByServers(sessions);
for (Map.Entry<ServerUUID, SortedMap<Long, Ping>> entry : pingByServer.entrySet()) {
ServerUUID serverUUID = entry.getKey();
SortedMap<Long, Ping> pingOfServer = entry.getValue();
if (pingOfServer.isEmpty())
continue;
List<FinishedSession> sessionsOfServer = sessionsByServer.getOrDefault(serverUUID, Collections.emptyList());
double pingCount = 0.0;
int pingEntries = 0;
for (FinishedSession session : sessionsOfServer) {
long start = session.getDate();
long end = session.getEnd();
if (end < start)
continue;
// Calculate average ping for each session with a section of the Ping map
SortedMap<Long, Ping> duringSession = pingOfServer.subMap(start, end);
for (Ping ping : duringSession.values()) {
pingCount += ping.getAverage();
pingEntries++;
}
if (pingEntries != 0) {
session.getExtraData().put(AveragePing.class, new AveragePing(pingCount / pingEntries));
}
pingCount = 0.0;
pingEntries = 0;
}
}
}
use of com.djrapitops.plan.delivery.domain.DateHolder in project Plan by plan-player-analytics.
the class SessionQueries method extractDataFromSessionSelectStatement.
private static List<FinishedSession> extractDataFromSessionSelectStatement(ResultSet set) throws SQLException {
// Server UUID - Player UUID - Session Start - Session
Map<ServerUUID, Map<UUID, SortedMap<Long, FinishedSession>>> byServer = new HashMap<>();
// Utilities
String[] gms = GMTimes.getGMKeyArray();
Comparator<DateHolder> mostRecentFirst = new DateHolderRecentComparator();
// Descending order, most recent first.
Comparator<Long> longRecentComparator = (one, two) -> Long.compare(two, one);
while (set.next()) {
ServerUUID serverUUID = ServerUUID.fromString(set.getString(SessionsTable.SERVER_UUID));
Map<UUID, SortedMap<Long, FinishedSession>> serverSessions = byServer.computeIfAbsent(serverUUID, Maps::create);
UUID playerUUID = UUID.fromString(set.getString(SessionsTable.USER_UUID));
SortedMap<Long, FinishedSession> playerSessions = serverSessions.computeIfAbsent(playerUUID, key -> new TreeMap<>(longRecentComparator));
long sessionStart = set.getLong(SessionsTable.SESSION_START);
// id, uuid, serverUUID, sessionStart, sessionEnd, mobKills, deaths, afkTime
FinishedSession session = playerSessions.getOrDefault(sessionStart, new FinishedSession(playerUUID, serverUUID, sessionStart, set.getLong(SessionsTable.SESSION_END), set.getLong(SessionsTable.AFK_TIME), new DataMap()));
DataMap extraData = session.getExtraData();
extraData.put(FinishedSession.Id.class, new FinishedSession.Id(set.getInt(SessionsTable.ID)));
extraData.put(MobKillCounter.class, new MobKillCounter(set.getInt(SessionsTable.MOB_KILLS)));
extraData.put(DeathCounter.class, new DeathCounter(set.getInt(SessionsTable.DEATHS)));
Optional<WorldTimes> existingWorldTimes = extraData.get(WorldTimes.class);
Optional<PlayerKills> existingPlayerKills = extraData.get(PlayerKills.class);
WorldTimes worldTimes = existingWorldTimes.orElseGet(WorldTimes::new);
String worldName = set.getString(WorldTable.NAME);
if (!worldTimes.contains(worldName)) {
Map<String, Long> gmMap = new HashMap<>();
gmMap.put(gms[0], set.getLong(WorldTimesTable.SURVIVAL));
gmMap.put(gms[1], set.getLong(WorldTimesTable.CREATIVE));
gmMap.put(gms[2], set.getLong(WorldTimesTable.ADVENTURE));
gmMap.put(gms[3], set.getLong(WorldTimesTable.SPECTATOR));
GMTimes gmTimes = new GMTimes(gmMap);
worldTimes.setGMTimesForWorld(worldName, gmTimes);
}
if (!existingWorldTimes.isPresent())
extraData.put(WorldTimes.class, worldTimes);
ServerName serverName = new ServerName(Server.getIdentifiableName(set.getString("server_name"), set.getInt("server_id")));
extraData.put(ServerName.class, serverName);
PlayerKills playerKills = existingPlayerKills.orElseGet(PlayerKills::new);
String victimName = set.getString("victim_name");
if (victimName != null) {
PlayerKill.Killer killer = new PlayerKill.Killer(UUID.fromString(set.getString(KillsTable.KILLER_UUID)), set.getString("killer_name"));
PlayerKill.Victim victim = new PlayerKill.Victim(UUID.fromString(set.getString(KillsTable.VICTIM_UUID)), victimName);
ServerIdentifier serverIdentifier = new ServerIdentifier(serverUUID, serverName);
String weapon = set.getString(KillsTable.WEAPON);
long date = set.getLong(KillsTable.DATE);
PlayerKill newKill = new PlayerKill(killer, victim, serverIdentifier, weapon, date);
if (!playerKills.contains(newKill)) {
playerKills.add(newKill);
}
}
if (!existingPlayerKills.isPresent())
extraData.put(PlayerKills.class, playerKills);
extraData.put(PlayerName.class, new PlayerName(set.getString("name")));
session.setAsFirstSessionIfMatches(set.getLong("registered"));
playerSessions.put(sessionStart, session);
}
return byServer.values().stream().map(Map::values).flatMap(Collection::stream).map(SortedMap::values).flatMap(Collection::stream).sorted(// Disorder arises
mostRecentFirst).collect(Collectors.toList());
}
use of com.djrapitops.plan.delivery.domain.DateHolder in project Plan by plan-player-analytics.
the class DataUtilityCommands method sendInGameMessages.
private void sendInGameMessages(CMDSender sender, PlayerContainer player) {
long now = System.currentTimeMillis();
com.djrapitops.plan.delivery.formatting.Formatter<DateHolder> timestamp = formatters.year();
Formatter<Long> length = formatters.timeAmount();
String playerName = player.getValue(PlayerKeys.NAME).orElse(locale.getString(GenericLang.UNKNOWN));
ActivityIndex activityIndex = player.getActivityIndex(now, config.get(TimeSettings.ACTIVE_PLAY_THRESHOLD));
Long registered = player.getValue(PlayerKeys.REGISTERED).orElse(0L);
Long lastSeen = player.getValue(PlayerKeys.LAST_SEEN).orElse(0L);
List<GeoInfo> geoInfo = player.getValue(PlayerKeys.GEO_INFO).orElse(new ArrayList<>());
Optional<GeoInfo> mostRecentGeoInfo = new GeoInfoMutator(geoInfo).mostRecent();
String geolocation = mostRecentGeoInfo.isPresent() ? mostRecentGeoInfo.get().getGeolocation() : "-";
SessionsMutator sessionsMutator = SessionsMutator.forContainer(player);
String table = locale.getString(CommandLang.HEADER_INSPECT, playerName) + '\n' + locale.getString(CommandLang.INGAME_ACTIVITY_INDEX, activityIndex.getFormattedValue(formatters.decimals()), activityIndex.getGroup()) + '\n' + locale.getString(CommandLang.INGAME_REGISTERED, timestamp.apply(() -> registered)) + '\n' + locale.getString(CommandLang.INGAME_LAST_SEEN, timestamp.apply(() -> lastSeen)) + '\n' + locale.getString(CommandLang.INGAME_GEOLOCATION, geolocation) + '\n' + locale.getString(CommandLang.INGAME_TIMES_KICKED, player.getValue(PlayerKeys.KICK_COUNT).orElse(0)) + '\n' + '\n' + locale.getString(CommandLang.INGAME_PLAYTIME, length.apply(sessionsMutator.toPlaytime())) + '\n' + locale.getString(CommandLang.INGAME_ACTIVE_PLAYTIME, length.apply(sessionsMutator.toActivePlaytime())) + '\n' + locale.getString(CommandLang.INGAME_AFK_PLAYTIME, length.apply(sessionsMutator.toAfkTime())) + '\n' + locale.getString(CommandLang.INGAME_LONGEST_SESSION, length.apply(sessionsMutator.toLongestSessionLength())) + '\n' + '\n' + locale.getString(CommandLang.INGAME_PLAYER_KILLS, sessionsMutator.toPlayerKillCount()) + '\n' + locale.getString(CommandLang.INGAME_MOB_KILLS, sessionsMutator.toMobKillCount()) + '\n' + locale.getString(CommandLang.INGAME_DEATHS, sessionsMutator.toDeathCount());
sender.send(sender.getFormatter().table(table, ": "));
}
use of com.djrapitops.plan.delivery.domain.DateHolder 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("-"));
}
Aggregations