Search in sources :

Example 16 with FinishedSession

use of com.djrapitops.plan.gathering.domain.FinishedSession in project Plan by plan-player-analytics.

the class JSErrorRegressionTest method savePlayerData.

private static void savePlayerData() {
    DBSystem dbSystem = planSystem.getDatabaseSystem();
    Database database = dbSystem.getDatabase();
    UUID uuid = TestConstants.PLAYER_ONE_UUID;
    database.executeTransaction(new PlayerRegisterTransaction(uuid, RandomData::randomTime, TestConstants.PLAYER_ONE_NAME));
    FinishedSession session = new FinishedSession(uuid, serverUUID, 1000L, 11000L, 500L, new DataMap());
    database.executeTransaction(new WorldNameStoreTransaction(serverUUID, "world"));
    database.executeTransaction(new StoreSessionTransaction(session));
}
Also used : StoreSessionTransaction(com.djrapitops.plan.storage.database.transactions.events.StoreSessionTransaction) DBSystem(com.djrapitops.plan.storage.database.DBSystem) FinishedSession(com.djrapitops.plan.gathering.domain.FinishedSession) Database(com.djrapitops.plan.storage.database.Database) WorldNameStoreTransaction(com.djrapitops.plan.storage.database.transactions.events.WorldNameStoreTransaction) PlayerRegisterTransaction(com.djrapitops.plan.storage.database.transactions.events.PlayerRegisterTransaction) ServerUUID(com.djrapitops.plan.identification.ServerUUID) DataMap(com.djrapitops.plan.gathering.domain.DataMap)

Example 17 with FinishedSession

use of com.djrapitops.plan.gathering.domain.FinishedSession in project Plan by plan-player-analytics.

the class ShutdownSaveTest method endedSessionsHaveSameEndTime.

@Test
void endedSessionsHaveSameEndTime() {
    for (int i = 0; i < 100; i++) {
        UUID playerUUID = UUID.randomUUID();
        ActiveSession session = RandomData.randomUnfinishedSession(TestConstants.SERVER_UUID, new String[] { "w1", "w2" }, playerUUID);
        sessionCache.cacheSession(playerUUID, session);
    }
    long endTime = System.currentTimeMillis();
    Collection<ActiveSession> activeSessions = SessionCache.getActiveSessions();
    for (FinishedSession session : underTest.finishSessions(activeSessions, endTime)) {
        assertEquals(endTime, session.getEnd(), "One of the sessions had differing end time");
    }
}
Also used : ActiveSession(com.djrapitops.plan.gathering.domain.ActiveSession) FinishedSession(com.djrapitops.plan.gathering.domain.FinishedSession) ServerUUID(com.djrapitops.plan.identification.ServerUUID) UUID(java.util.UUID) Test(org.junit.jupiter.api.Test)

Example 18 with FinishedSession

use of com.djrapitops.plan.gathering.domain.FinishedSession in project Plan by plan-player-analytics.

the class SessionCacheTest method sessionsAreRemovedFromCacheOnEnd.

@Test
void sessionsAreRemovedFromCacheOnEnd() {
    Optional<FinishedSession> ended = new SessionCache().endSession(uuid, System.currentTimeMillis());
    assertTrue(ended.isPresent());
    for (ActiveSession session : SessionCache.getActiveSessions()) {
        fail("Session was still in cache: " + session);
    }
}
Also used : ActiveSession(com.djrapitops.plan.gathering.domain.ActiveSession) FinishedSession(com.djrapitops.plan.gathering.domain.FinishedSession) Test(org.junit.jupiter.api.Test)

Example 19 with FinishedSession

use of com.djrapitops.plan.gathering.domain.FinishedSession in project Plan by plan-player-analytics.

the class PlayerContainerQuery method executeQuery.

@Override
public PlayerContainer executeQuery(SQLDB db) {
    PlayerContainer container = new PlayerContainer();
    container.putRawData(PlayerKeys.UUID, uuid);
    Key<BaseUser> baseUserKey = new Key<>(BaseUser.class, "BASE_USER");
    container.putSupplier(baseUserKey, () -> db.query(BaseUserQueries.fetchBaseUserOfPlayer(uuid)).orElse(null));
    container.putSupplier(PlayerKeys.REGISTERED, () -> container.getValue(baseUserKey).map(BaseUser::getRegistered).orElse(null));
    container.putSupplier(PlayerKeys.NAME, () -> container.getValue(baseUserKey).map(BaseUser::getName).orElse(null));
    container.putSupplier(PlayerKeys.KICK_COUNT, () -> container.getValue(baseUserKey).map(BaseUser::getTimesKicked).orElse(null));
    container.putCachingSupplier(PlayerKeys.GEO_INFO, () -> db.query(GeoInfoQueries.fetchPlayerGeoInformation(uuid)));
    container.putCachingSupplier(PlayerKeys.PING, () -> db.query(PingQueries.fetchPingDataOfPlayer(uuid)));
    container.putCachingSupplier(PlayerKeys.NICKNAMES, () -> db.query(NicknameQueries.fetchNicknameDataOfPlayer(uuid)));
    container.putCachingSupplier(PlayerKeys.PER_SERVER, () -> db.query(new PerServerContainerQuery(uuid)));
    container.putSupplier(PlayerKeys.BANNED, () -> new PerServerMutator(container.getValue(PlayerKeys.PER_SERVER).orElse(new PerServerContainer())).isBanned());
    container.putSupplier(PlayerKeys.OPERATOR, () -> new PerServerMutator(container.getValue(PlayerKeys.PER_SERVER).orElse(new PerServerContainer())).isOperator());
    container.putCachingSupplier(PlayerKeys.SESSIONS, () -> {
        List<FinishedSession> sessions = new PerServerMutator(container.getValue(PlayerKeys.PER_SERVER).orElse(new PerServerContainer())).flatMapSessions();
        container.getValue(PlayerKeys.ACTIVE_SESSION).map(ActiveSession::toFinishedSessionFromStillActive).ifPresent(sessions::add);
        return sessions;
    });
    container.putCachingSupplier(PlayerKeys.WORLD_TIMES, () -> {
        WorldTimes worldTimes = db.query(WorldTimesQueries.fetchPlayerTotalWorldTimes(uuid));
        container.getValue(PlayerKeys.ACTIVE_SESSION).ifPresent(session -> worldTimes.add(session.getExtraData(WorldTimes.class).orElseGet(WorldTimes::new)));
        return worldTimes;
    });
    container.putSupplier(PlayerKeys.LAST_SEEN, () -> SessionsMutator.forContainer(container).toLastSeen());
    container.putSupplier(PlayerKeys.PLAYER_KILLS, () -> db.query(KillQueries.fetchPlayerKillsOfPlayer(uuid)));
    container.putSupplier(PlayerKeys.PLAYER_DEATHS_KILLS, () -> db.query(KillQueries.fetchPlayerDeathsOfPlayer(uuid)));
    container.putSupplier(PlayerKeys.PLAYER_KILL_COUNT, () -> container.getValue(PlayerKeys.PLAYER_KILLS).map(Collection::size).orElse(0));
    container.putSupplier(PlayerKeys.MOB_KILL_COUNT, () -> SessionsMutator.forContainer(container).toMobKillCount());
    container.putSupplier(PlayerKeys.DEATH_COUNT, () -> SessionsMutator.forContainer(container).toDeathCount());
    SessionCache.getCachedSession(uuid).ifPresent(session -> container.putRawData(PlayerKeys.ACTIVE_SESSION, session));
    return container;
}
Also used : PerServerContainer(com.djrapitops.plan.delivery.domain.container.PerServerContainer) PlayerContainer(com.djrapitops.plan.delivery.domain.container.PlayerContainer) BaseUser(com.djrapitops.plan.gathering.domain.BaseUser) FinishedSession(com.djrapitops.plan.gathering.domain.FinishedSession) PerServerMutator(com.djrapitops.plan.delivery.domain.mutators.PerServerMutator) Collection(java.util.Collection) WorldTimes(com.djrapitops.plan.gathering.domain.WorldTimes) Key(com.djrapitops.plan.delivery.domain.keys.Key)

Example 20 with FinishedSession

use of com.djrapitops.plan.gathering.domain.FinishedSession in project Plan by plan-player-analytics.

the class ActivityIndexQueriesTest method activityIndexCalculationsMatch.

// @Test
// default void activityIndexCoalesceSanityCheck() {
// storeSessions();
// Map<String, Integer> groupings = db().query(
// ActivityIndexQueries.fetchActivityIndexGroupingsOn(System.currentTimeMillis(), serverUUID(), TimeUnit.HOURS.toMillis(2L))
// );
// Map<String, Integer> expected = Collections.singletonMap(ActivityIndex.getDefaultGroups()[4], 1); // Inactive
// assertEquals(expected, groupings);
// }
@RepeatedTest(value = 3, name = "Activity Index calculations match {currentRepetition}/{totalRepetitions}")
default void activityIndexCalculationsMatch() {
    storeSessions(session -> true);
    long date = System.currentTimeMillis();
    long playtimeThreshold = TimeUnit.HOURS.toMillis(5L);
    List<FinishedSession> sessions = db().query(SessionQueries.fetchSessionsOfPlayer(playerUUID)).values().stream().flatMap(Collection::stream).collect(Collectors.toList());
    ActivityIndex javaCalculation = new ActivityIndex(sessions, date, playtimeThreshold);
    List<TablePlayer> players = db().query(new ServerTablePlayersQuery(serverUUID(), date, playtimeThreshold, 5));
    Optional<TablePlayer> found = players.stream().filter(tp -> playerUUID.equals(tp.getPlayerUUID())).findFirst();
    assertTrue(found.isPresent());
    Optional<ActivityIndex> currentActivityIndex = found.get().getCurrentActivityIndex();
    assertTrue(currentActivityIndex.isPresent());
    assertEquals(javaCalculation.getValue(), currentActivityIndex.get().getValue(), 0.001, () -> {
        StringBuilder errorMsg = new StringBuilder("Activity indexes did not match\n");
        long week = TimeUnit.DAYS.toMillis(7L);
        long weekAgo = date - week;
        long twoWeeksAgo = date - 2L * week;
        long threeWeeksAgo = date - 3L * week;
        SessionsMutator mutator = new SessionsMutator(sessions);
        SessionsMutator w1 = mutator.filterSessionsBetween(weekAgo, date);
        SessionsMutator w2 = mutator.filterSessionsBetween(twoWeeksAgo, weekAgo);
        SessionsMutator w3 = mutator.filterSessionsBetween(threeWeeksAgo, twoWeeksAgo);
        Long dbW1 = db().query(SessionQueries.activePlaytime(weekAgo, date, serverUUID()));
        Long dbW2 = db().query(SessionQueries.activePlaytime(twoWeeksAgo, weekAgo, serverUUID()));
        Long dbW3 = db().query(SessionQueries.activePlaytime(threeWeeksAgo, twoWeeksAgo, serverUUID()));
        errorMsg.append("Java calculation playtimes: ").append(w1.toActivePlaytime()).append("ms,").append(w2.toActivePlaytime()).append("ms,").append(w3.toActivePlaytime()).append("ms\n").append("DB calculation playtimes: ").append(dbW1).append("ms,").append(dbW2).append("ms,").append(dbW3).append("ms");
        return errorMsg.toString();
    });
}
Also used : RepeatedTest(org.junit.jupiter.api.RepeatedTest) ServerTablePlayersQuery(com.djrapitops.plan.storage.database.queries.objects.playertable.ServerTablePlayersQuery) SessionsTable(com.djrapitops.plan.storage.database.sql.tables.SessionsTable) TablePlayer(com.djrapitops.plan.delivery.domain.TablePlayer) PlayerServerRegisterTransaction(com.djrapitops.plan.storage.database.transactions.events.PlayerServerRegisterTransaction) SQLException(java.sql.SQLException) NetworkTablePlayersQuery(com.djrapitops.plan.storage.database.queries.objects.playertable.NetworkTablePlayersQuery) ResultSet(java.sql.ResultSet) DatabaseTestPreparer(com.djrapitops.plan.storage.database.DatabaseTestPreparer) UsersTable(com.djrapitops.plan.storage.database.sql.tables.UsersTable) TestConstants(utilities.TestConstants) SessionsMutator(com.djrapitops.plan.delivery.domain.mutators.SessionsMutator) ActivityIndexQueries(com.djrapitops.plan.storage.database.queries.analysis.ActivityIndexQueries) FinishedSession(com.djrapitops.plan.gathering.domain.FinishedSession) Predicate(java.util.function.Predicate) Collection(java.util.Collection) PreparedStatement(java.sql.PreparedStatement) Collectors(java.util.stream.Collectors) Sql(com.djrapitops.plan.storage.database.sql.building.Sql) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) ActivityIndex(com.djrapitops.plan.delivery.domain.mutators.ActivityIndex) Assertions(org.junit.jupiter.api.Assertions) RandomData(utilities.RandomData) Optional(java.util.Optional) SessionQueries(com.djrapitops.plan.storage.database.queries.objects.SessionQueries) WorldNameStoreTransaction(com.djrapitops.plan.storage.database.transactions.events.WorldNameStoreTransaction) StoreSessionTransaction(com.djrapitops.plan.storage.database.transactions.events.StoreSessionTransaction) ActivityIndex(com.djrapitops.plan.delivery.domain.mutators.ActivityIndex) TablePlayer(com.djrapitops.plan.delivery.domain.TablePlayer) ServerTablePlayersQuery(com.djrapitops.plan.storage.database.queries.objects.playertable.ServerTablePlayersQuery) FinishedSession(com.djrapitops.plan.gathering.domain.FinishedSession) SessionsMutator(com.djrapitops.plan.delivery.domain.mutators.SessionsMutator) RepeatedTest(org.junit.jupiter.api.RepeatedTest)

Aggregations

FinishedSession (com.djrapitops.plan.gathering.domain.FinishedSession)32 Test (org.junit.jupiter.api.Test)22 ServerUUID (com.djrapitops.plan.identification.ServerUUID)13 RepeatedTest (org.junit.jupiter.api.RepeatedTest)11 StoreSessionTransaction (com.djrapitops.plan.storage.database.transactions.events.StoreSessionTransaction)10 WorldNameStoreTransaction (com.djrapitops.plan.storage.database.transactions.events.WorldNameStoreTransaction)8 ActiveSession (com.djrapitops.plan.gathering.domain.ActiveSession)6 RandomData (utilities.RandomData)6 TestConstants (utilities.TestConstants)6 TablePlayer (com.djrapitops.plan.delivery.domain.TablePlayer)5 List (java.util.List)5 WorldTimes (com.djrapitops.plan.gathering.domain.WorldTimes)4 NetworkTablePlayersQuery (com.djrapitops.plan.storage.database.queries.objects.playertable.NetworkTablePlayersQuery)4 ServerTablePlayersQuery (com.djrapitops.plan.storage.database.queries.objects.playertable.ServerTablePlayersQuery)4 Sql (com.djrapitops.plan.storage.database.sql.building.Sql)4 UsersTable (com.djrapitops.plan.storage.database.sql.tables.UsersTable)4 ResultSet (java.sql.ResultSet)4 SQLException (java.sql.SQLException)4 Collection (java.util.Collection)4 TimeUnit (java.util.concurrent.TimeUnit)4