Search in sources :

Example 21 with FinishedSession

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

the class ActivityIndexQueriesTest method activityIndexCalculationsMatchWithMissingData.

@RepeatedTest(value = 3, name = "Activity Index calculations match with missing data {currentRepetition}/{totalRepetitions}")
default void activityIndexCalculationsMatchWithMissingData() {
    long keepAfter = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(7L);
    storeSessions(session -> session.getDate() >= keepAfter && session.getEnd() >= keepAfter);
    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)

Example 22 with FinishedSession

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

the class ActivityIndexQueriesTest method networkActivityIndexCalculationsMatch.

@RepeatedTest(value = 3, name = "Network Activity Index calculations match {currentRepetition}/{totalRepetitions}")
default void networkActivityIndexCalculationsMatch() {
    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 NetworkTablePlayersQuery(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(activePlaytime(weekAgo, date));
        Long dbW2 = db().query(activePlaytime(twoWeeksAgo, weekAgo));
        Long dbW3 = db().query(activePlaytime(threeWeeksAgo, twoWeeksAgo));
        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) NetworkTablePlayersQuery(com.djrapitops.plan.storage.database.queries.objects.playertable.NetworkTablePlayersQuery) ActivityIndex(com.djrapitops.plan.delivery.domain.mutators.ActivityIndex) TablePlayer(com.djrapitops.plan.delivery.domain.TablePlayer) FinishedSession(com.djrapitops.plan.gathering.domain.FinishedSession) SessionsMutator(com.djrapitops.plan.delivery.domain.mutators.SessionsMutator) RepeatedTest(org.junit.jupiter.api.RepeatedTest)

Example 23 with FinishedSession

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

the class JoinAddressQueriesTest method latestJoinAddressIsUpdatedUponSecondSession.

@Test
default void latestJoinAddressIsUpdatedUponSecondSession() {
    joinAddressCanBeUnknown();
    FinishedSession session = RandomData.randomSession(serverUUID(), worlds, playerUUID, player2UUID);
    String expectedAddress = TestConstants.GET_PLAYER_HOSTNAME.get();
    session.getExtraData().put(JoinAddress.class, new JoinAddress(expectedAddress));
    db().executeTransaction(new StoreSessionTransaction(session));
    Map<String, Integer> expected = Map.of(expectedAddress, 1);
    Map<String, Integer> result = db().query(JoinAddressQueries.latestJoinAddresses());
    assertEquals(expected, result);
}
Also used : StoreSessionTransaction(com.djrapitops.plan.storage.database.transactions.events.StoreSessionTransaction) FinishedSession(com.djrapitops.plan.gathering.domain.FinishedSession) JoinAddress(com.djrapitops.plan.gathering.domain.event.JoinAddress) Test(org.junit.jupiter.api.Test)

Example 24 with FinishedSession

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

the class JoinAddressQueriesTest method joinAddressUpdateIsUniquePerServer.

@Test
default void joinAddressUpdateIsUniquePerServer() {
    joinAddressCanBeUnknown();
    db().executeTransaction(TestData.storeServers());
    db().executeTransaction(new WorldNameStoreTransaction(TestConstants.SERVER_TWO_UUID, worlds[0]));
    db().executeTransaction(new WorldNameStoreTransaction(TestConstants.SERVER_TWO_UUID, worlds[1]));
    FinishedSession session = RandomData.randomSession(TestConstants.SERVER_TWO_UUID, worlds, playerUUID, player2UUID);
    String expectedAddress = TestConstants.GET_PLAYER_HOSTNAME.get();
    session.getExtraData().put(JoinAddress.class, new JoinAddress(expectedAddress));
    db().executeTransaction(new StoreSessionTransaction(session));
    Map<String, Integer> expected1 = Map.of(JoinAddressTable.DEFAULT_VALUE_FOR_LOOKUP, 1);
    Map<String, Integer> result1 = db().query(JoinAddressQueries.latestJoinAddresses(serverUUID()));
    assertEquals(expected1, result1);
    Map<String, Integer> expected2 = Map.of(expectedAddress, 1);
    Map<String, Integer> result2 = db().query(JoinAddressQueries.latestJoinAddresses(TestConstants.SERVER_TWO_UUID));
    assertEquals(expected2, result2);
}
Also used : StoreSessionTransaction(com.djrapitops.plan.storage.database.transactions.events.StoreSessionTransaction) FinishedSession(com.djrapitops.plan.gathering.domain.FinishedSession) WorldNameStoreTransaction(com.djrapitops.plan.storage.database.transactions.events.WorldNameStoreTransaction) JoinAddress(com.djrapitops.plan.gathering.domain.event.JoinAddress) Test(org.junit.jupiter.api.Test)

Example 25 with FinishedSession

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

the class DatabaseTest method playerContainerSupportsAllPlayerKeys.

@Test
default void playerContainerSupportsAllPlayerKeys() throws IllegalAccessException {
    saveUserOne();
    saveUserTwo();
    saveTwoWorlds();
    FinishedSession session = RandomData.randomSession(serverUUID(), worlds, playerUUID, player2UUID);
    db().executeTransaction(new StoreSessionTransaction(session));
    db().executeTransaction(new NicknameStoreTransaction(playerUUID, RandomData.randomNickname(serverUUID()), (uuid, name) -> false));
    saveGeoInfo(playerUUID, new GeoInfo("TestLoc", RandomData.randomTime()));
    assertTrue(db().query(PlayerFetchQueries.isPlayerRegistered(playerUUID)));
    db().executeTransaction(new PingStoreTransaction(playerUUID, serverUUID(), RandomData.randomIntDateObjects()));
    PlayerContainer playerContainer = db().query(ContainerFetchQueries.fetchPlayerContainer(playerUUID));
    // Active sessions are added after fetching
    playerContainer.putRawData(PlayerKeys.ACTIVE_SESSION, RandomData.randomUnfinishedSession(serverUUID(), worlds, playerUUID));
    List<String> unsupported = new ArrayList<>();
    List<Key> keys = FieldFetcher.getPublicStaticFields(PlayerKeys.class, Key.class);
    for (Key<?> key : keys) {
        if (!playerContainer.supports(key)) {
            unsupported.add(key.getKeyName());
        }
    }
    assertTrue(unsupported.isEmpty(), () -> "Some keys are not supported by PlayerContainer: PlayerKeys." + unsupported.toString());
}
Also used : java.util(java.util) PlanConfig(com.djrapitops.plan.settings.config.PlanConfig) ServerTablePlayersQuery(com.djrapitops.plan.storage.database.queries.objects.playertable.ServerTablePlayersQuery) UserInfoTable(com.djrapitops.plan.storage.database.sql.tables.UserInfoTable) ServerUUID(com.djrapitops.plan.identification.ServerUUID) Query(com.djrapitops.plan.storage.database.queries.Query) LocalDateTime(java.time.LocalDateTime) TablePlayer(com.djrapitops.plan.delivery.domain.TablePlayer) FieldFetcher(utilities.FieldFetcher) GeoInfo(com.djrapitops.plan.gathering.domain.GeoInfo) SQLException(java.sql.SQLException) NetworkTablePlayersQuery(com.djrapitops.plan.storage.database.queries.objects.playertable.NetworkTablePlayersQuery) ResultSet(java.sql.ResultSet) BaseUser(com.djrapitops.plan.gathering.domain.BaseUser) PlayerKeys(com.djrapitops.plan.delivery.domain.keys.PlayerKeys) ActiveSession(com.djrapitops.plan.gathering.domain.ActiveSession) ServerAggregateQueries(com.djrapitops.plan.storage.database.queries.ServerAggregateQueries) StoreConfigTransaction(com.djrapitops.plan.storage.database.transactions.StoreConfigTransaction) ZoneOffset(java.time.ZoneOffset) UsersTable(com.djrapitops.plan.storage.database.sql.tables.UsersTable) TestConstants(utilities.TestConstants) FormatSettings(com.djrapitops.plan.settings.config.paths.FormatSettings) Transaction(com.djrapitops.plan.storage.database.transactions.Transaction) ContainerFetchQueries(com.djrapitops.plan.storage.database.queries.containers.ContainerFetchQueries) FinishedSession(com.djrapitops.plan.gathering.domain.FinishedSession) PlayerFetchQueries(com.djrapitops.plan.storage.database.queries.PlayerFetchQueries) QueryAllStatement(com.djrapitops.plan.storage.database.queries.QueryAllStatement) QuerySvc(com.djrapitops.plan.query.QuerySvc) Sql(com.djrapitops.plan.storage.database.sql.building.Sql) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) Key(com.djrapitops.plan.delivery.domain.keys.Key) DBCleanTask(com.djrapitops.plan.storage.upkeep.DBCleanTask) Locale(com.djrapitops.plan.settings.locale.Locale) com.djrapitops.plan.storage.database.transactions.events(com.djrapitops.plan.storage.database.transactions.events) Assertions(org.junit.jupiter.api.Assertions) RandomData(utilities.RandomData) PlayerContainer(com.djrapitops.plan.delivery.domain.container.PlayerContainer) TestPluginLogger(utilities.TestPluginLogger) com.djrapitops.plan.storage.database.queries.objects(com.djrapitops.plan.storage.database.queries.objects) RemovePlayerTransaction(com.djrapitops.plan.storage.database.transactions.commands.RemovePlayerTransaction) CreateIndexTransaction(com.djrapitops.plan.storage.database.transactions.init.CreateIndexTransaction) RegisterDateMinimizationPatch(com.djrapitops.plan.storage.database.transactions.patches.RegisterDateMinimizationPatch) Nickname(com.djrapitops.plan.delivery.domain.Nickname) Config(com.djrapitops.plan.settings.config.Config) PlayerContainer(com.djrapitops.plan.delivery.domain.container.PlayerContainer) GeoInfo(com.djrapitops.plan.gathering.domain.GeoInfo) FinishedSession(com.djrapitops.plan.gathering.domain.FinishedSession) Key(com.djrapitops.plan.delivery.domain.keys.Key) Test(org.junit.jupiter.api.Test)

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