use of com.djrapitops.plan.delivery.domain.mutators.PlayersOnlineResolver in project Plan by plan-player-analytics.
the class OnlineActivityOverviewJSONCreator method createInsightsMap.
private Map<String, Object> createInsightsMap(ServerUUID serverUUID) {
Database db = dbSystem.getDatabase();
long now = System.currentTimeMillis();
long halfMonthAgo = now - TimeUnit.DAYS.toMillis(15L);
long monthAgo = now - TimeUnit.DAYS.toMillis(30L);
Map<String, Object> insights = new HashMap<>();
SessionsMutator sessions = new SessionsMutator(db.query(SessionQueries.fetchServerSessionsWithoutKillOrWorldData(monthAgo, now, serverUUID)));
List<TPS> tpsData = db.query(TPSQueries.fetchTPSDataOfServer(monthAgo, now, serverUUID));
Map<UUID, Long> registerDates = db.query(UserInfoQueries.fetchRegisterDates(monthAgo, now, serverUUID));
PlayersOnlineResolver playersOnlineResolver = new PlayersOnlineResolver(new TPSMutator(tpsData));
SessionsMutator firstSessions = sessions.filterBy(session -> {
long registered = registerDates.getOrDefault(session.getPlayerUUID(), -501L);
long start = session.getDate();
return Math.abs(registered - start) < 500L;
});
SessionsMutator firstSessionsBefore = firstSessions.filterSessionsBetween(monthAgo, halfMonthAgo);
SessionsMutator firstSessionsAfter = firstSessions.filterSessionsBetween(halfMonthAgo, now);
long avgFirstSessionLength = firstSessions.toAverageSessionLength();
long avgFirstSessionLengthBefore = firstSessionsBefore.toAverageSessionLength();
long avgFirstSessionLengthAfter = firstSessionsAfter.toAverageSessionLength();
insights.put("first_session_length_avg", timeAmountFormatter.apply(avgFirstSessionLength));
insights.put("first_session_length_trend", new Trend(avgFirstSessionLengthBefore, avgFirstSessionLengthAfter, false, timeAmountFormatter));
long medianFirstSessionLength = firstSessions.toMedianSessionLength();
long medianFirstSessionLengthBefore = firstSessionsBefore.toMedianSessionLength();
long medianFirstSessionLengthAfter = firstSessionsAfter.toMedianSessionLength();
insights.put("first_session_length_median", timeAmountFormatter.apply(medianFirstSessionLength));
insights.put("first_session_length_median_trend", new Trend(medianFirstSessionLengthBefore, medianFirstSessionLengthAfter, false, timeAmountFormatter));
int lonelyJoins = playersOnlineResolver.findLonelyJoins(sessions.toSessionStarts());
int loneJoinsBefore = playersOnlineResolver.findLonelyJoins(sessions.filterSessionsBetween(monthAgo, halfMonthAgo).toSessionStarts());
int loneJoinsAfter = playersOnlineResolver.findLonelyJoins(sessions.filterSessionsBetween(halfMonthAgo, now).toSessionStarts());
insights.put("lone_joins", lonelyJoins);
insights.put("lone_joins_trend", new Trend(loneJoinsBefore, loneJoinsAfter, true));
int newLonelyJoins = playersOnlineResolver.findLonelyJoins(firstSessions.toSessionStarts());
int newLoneJoinsBefore = playersOnlineResolver.findLonelyJoins(firstSessionsBefore.toSessionStarts());
int newLoneJoinsAfter = playersOnlineResolver.findLonelyJoins(firstSessionsAfter.toSessionStarts());
insights.put("lone_new_joins", newLonelyJoins);
insights.put("lone_new_joins_trend", new Trend(newLoneJoinsBefore, newLoneJoinsAfter, true));
double playersOnlineOnRegister = firstSessions.toAveragePlayersOnline(playersOnlineResolver);
double playersOnlineOnRegisterBefore = firstSessionsBefore.toAveragePlayersOnline(playersOnlineResolver);
double playersOnlineOnRegisterAfter = firstSessionsAfter.toAveragePlayersOnline(playersOnlineResolver);
insights.put("players_first_join_avg", decimalFormatter.apply(playersOnlineOnRegister));
insights.put("players_first_join_trend", new Trend(playersOnlineOnRegisterBefore, playersOnlineOnRegisterAfter, false, decimalFormatter));
return insights;
}
Aggregations