Search in sources :

Example 6 with ActivityIndex

use of com.djrapitops.plan.delivery.domain.mutators.ActivityIndex in project Plan by plan-player-analytics.

the class ActivityIndexQueries method averageActivityIndexForRetainedPlayers.

public static Query<ActivityIndex> averageActivityIndexForRetainedPlayers(long after, long before, ServerUUID serverUUID, Long threshold) {
    String selectNewUUIDs = SELECT + UserInfoTable.USER_UUID + FROM + UserInfoTable.TABLE_NAME + WHERE + UserInfoTable.REGISTERED + "<=?" + AND + UserInfoTable.REGISTERED + ">=?" + AND + UserInfoTable.SERVER_UUID + "=?";
    String selectUniqueUUIDs = SELECT + "DISTINCT " + SessionsTable.USER_UUID + FROM + SessionsTable.TABLE_NAME + WHERE + SessionsTable.SESSION_START + ">=?" + AND + SessionsTable.SESSION_END + "<=?" + AND + SessionsTable.SERVER_UUID + "=?";
    String sql = SELECT + "AVG(activity_index) as average" + FROM + '(' + selectNewUUIDs + ") n" + INNER_JOIN + '(' + selectUniqueUUIDs + ") u on n." + SessionsTable.USER_UUID + "=u." + SessionsTable.USER_UUID + INNER_JOIN + '(' + selectActivityIndexSQL() + ") a on n." + SessionsTable.USER_UUID + "=a." + SessionsTable.USER_UUID;
    return new QueryStatement<ActivityIndex>(sql) {

        @Override
        public void prepare(PreparedStatement statement) throws SQLException {
            statement.setLong(1, before);
            statement.setLong(2, after);
            statement.setString(3, serverUUID.toString());
            // Have played in the last half of the time frame
            long half = before - (before - after) / 2;
            statement.setLong(4, half);
            statement.setLong(5, before);
            statement.setString(6, serverUUID.toString());
            setSelectActivityIndexSQLParameters(statement, 7, threshold, serverUUID, before);
        }

        @Override
        public ActivityIndex processResults(ResultSet set) throws SQLException {
            return set.next() ? new ActivityIndex(set.getDouble("average"), before) : new ActivityIndex(0.0, before);
        }
    };
}
Also used : ActivityIndex(com.djrapitops.plan.delivery.domain.mutators.ActivityIndex) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) QueryStatement(com.djrapitops.plan.storage.database.queries.QueryStatement)

Example 7 with ActivityIndex

use of com.djrapitops.plan.delivery.domain.mutators.ActivityIndex in project Plan by plan-player-analytics.

the class ActivityIndexQueries method activityIndexForNewPlayers.

public static Query<Collection<ActivityIndex>> activityIndexForNewPlayers(long after, long before, ServerUUID serverUUID, Long threshold) {
    String selectNewUUIDs = SELECT + UserInfoTable.USER_UUID + FROM + UserInfoTable.TABLE_NAME + WHERE + UserInfoTable.REGISTERED + "<=?" + AND + UserInfoTable.REGISTERED + ">=?" + AND + UserInfoTable.SERVER_UUID + "=?";
    String sql = SELECT + "activity_index" + FROM + '(' + selectNewUUIDs + ") n" + INNER_JOIN + '(' + selectActivityIndexSQL() + ") a on n." + SessionsTable.USER_UUID + "=a." + SessionsTable.USER_UUID;
    return new QueryStatement<Collection<ActivityIndex>>(sql) {

        @Override
        public void prepare(PreparedStatement statement) throws SQLException {
            statement.setLong(1, before);
            statement.setLong(2, after);
            statement.setString(3, serverUUID.toString());
            setSelectActivityIndexSQLParameters(statement, 4, threshold, serverUUID, before);
        }

        @Override
        public Collection<ActivityIndex> processResults(ResultSet set) throws SQLException {
            Collection<ActivityIndex> indexes = new ArrayList<>();
            while (set.next()) {
                indexes.add(new ActivityIndex(set.getDouble("activity_index"), before));
            }
            return indexes;
        }
    };
}
Also used : ActivityIndex(com.djrapitops.plan.delivery.domain.mutators.ActivityIndex) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) QueryStatement(com.djrapitops.plan.storage.database.queries.QueryStatement)

Example 8 with ActivityIndex

use of com.djrapitops.plan.delivery.domain.mutators.ActivityIndex in project Plan by plan-player-analytics.

the class ActivityIndexQueries method averageActivityIndexForNonRetainedPlayers.

public static Query<ActivityIndex> averageActivityIndexForNonRetainedPlayers(long after, long before, ServerUUID serverUUID, Long threshold) {
    String selectNewUUIDs = SELECT + UserInfoTable.USER_UUID + FROM + UserInfoTable.TABLE_NAME + WHERE + UserInfoTable.REGISTERED + "<=?" + AND + UserInfoTable.REGISTERED + ">=?" + AND + UserInfoTable.SERVER_UUID + "=?";
    String selectUniqueUUIDs = SELECT + "DISTINCT " + SessionsTable.USER_UUID + FROM + SessionsTable.TABLE_NAME + WHERE + SessionsTable.SESSION_START + ">=?" + AND + SessionsTable.SESSION_END + "<=?" + AND + SessionsTable.SERVER_UUID + "=?";
    String sql = SELECT + "AVG(activity_index) as average" + FROM + '(' + selectNewUUIDs + ") n" + LEFT_JOIN + '(' + selectUniqueUUIDs + ") u on n." + SessionsTable.USER_UUID + "=u." + SessionsTable.USER_UUID + INNER_JOIN + '(' + selectActivityIndexSQL() + ") a on n." + SessionsTable.USER_UUID + "=a." + SessionsTable.USER_UUID + WHERE + "n." + SessionsTable.USER_UUID + IS_NULL;
    return new QueryStatement<ActivityIndex>(sql) {

        @Override
        public void prepare(PreparedStatement statement) throws SQLException {
            statement.setLong(1, before);
            statement.setLong(2, after);
            statement.setString(3, serverUUID.toString());
            // Have played in the last half of the time frame
            long half = before - (before - after) / 2;
            statement.setLong(4, half);
            statement.setLong(5, before);
            statement.setString(6, serverUUID.toString());
            setSelectActivityIndexSQLParameters(statement, 7, threshold, serverUUID, before);
        }

        @Override
        public ActivityIndex processResults(ResultSet set) throws SQLException {
            return set.next() ? new ActivityIndex(set.getDouble("average"), before) : new ActivityIndex(0.0, before);
        }
    };
}
Also used : ActivityIndex(com.djrapitops.plan.delivery.domain.mutators.ActivityIndex) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) QueryStatement(com.djrapitops.plan.storage.database.queries.QueryStatement)

Example 9 with ActivityIndex

use of com.djrapitops.plan.delivery.domain.mutators.ActivityIndex in project Plan by plan-player-analytics.

the class NetworkActivityIndexQueries method averageActivityIndexForNonRetainedPlayers.

public static Query<ActivityIndex> averageActivityIndexForNonRetainedPlayers(long after, long before, Long threshold) {
    String selectNewUUIDs = SELECT + UsersTable.USER_UUID + FROM + UsersTable.TABLE_NAME + WHERE + UsersTable.REGISTERED + "<=?" + AND + UsersTable.REGISTERED + ">=?";
    String selectUniqueUUIDs = SELECT + "DISTINCT " + SessionsTable.USER_UUID + FROM + SessionsTable.TABLE_NAME + WHERE + SessionsTable.SESSION_START + ">=?" + AND + SessionsTable.SESSION_END + "<=?";
    String sql = SELECT + "AVG(activity_index) as average" + FROM + '(' + selectNewUUIDs + ") n" + LEFT_JOIN + '(' + selectUniqueUUIDs + ") u on n." + SessionsTable.USER_UUID + "=u." + SessionsTable.USER_UUID + INNER_JOIN + '(' + selectActivityIndexSQL() + ") a on n." + SessionsTable.USER_UUID + "=a." + SessionsTable.USER_UUID + WHERE + "n." + SessionsTable.USER_UUID + IS_NULL;
    return new QueryStatement<ActivityIndex>(sql) {

        @Override
        public void prepare(PreparedStatement statement) throws SQLException {
            statement.setLong(1, before);
            statement.setLong(2, after);
            // Have played in the last half of the time frame
            long half = before - (before - after) / 2;
            statement.setLong(3, half);
            statement.setLong(4, before);
            setSelectActivityIndexSQLParameters(statement, 5, threshold, before);
        }

        @Override
        public ActivityIndex processResults(ResultSet set) throws SQLException {
            return set.next() ? new ActivityIndex(set.getDouble("average"), before) : new ActivityIndex(0.0, before);
        }
    };
}
Also used : ActivityIndex(com.djrapitops.plan.delivery.domain.mutators.ActivityIndex) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) QueryStatement(com.djrapitops.plan.storage.database.queries.QueryStatement)

Example 10 with ActivityIndex

use of com.djrapitops.plan.delivery.domain.mutators.ActivityIndex 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) 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)

Aggregations

ActivityIndex (com.djrapitops.plan.delivery.domain.mutators.ActivityIndex)12 PreparedStatement (java.sql.PreparedStatement)10 ResultSet (java.sql.ResultSet)10 QueryStatement (com.djrapitops.plan.storage.database.queries.QueryStatement)6 SessionsMutator (com.djrapitops.plan.delivery.domain.mutators.SessionsMutator)4 SQLException (java.sql.SQLException)4 List (java.util.List)4 TablePlayer (com.djrapitops.plan.delivery.domain.TablePlayer)3 FinishedSession (com.djrapitops.plan.gathering.domain.FinishedSession)3 DatabaseTestPreparer (com.djrapitops.plan.storage.database.DatabaseTestPreparer)3 ActivityIndexQueries (com.djrapitops.plan.storage.database.queries.analysis.ActivityIndexQueries)3 SessionQueries (com.djrapitops.plan.storage.database.queries.objects.SessionQueries)3 NetworkTablePlayersQuery (com.djrapitops.plan.storage.database.queries.objects.playertable.NetworkTablePlayersQuery)3 ServerTablePlayersQuery (com.djrapitops.plan.storage.database.queries.objects.playertable.ServerTablePlayersQuery)3 Sql (com.djrapitops.plan.storage.database.sql.building.Sql)3 SessionsTable (com.djrapitops.plan.storage.database.sql.tables.SessionsTable)3 UsersTable (com.djrapitops.plan.storage.database.sql.tables.UsersTable)3 PlayerServerRegisterTransaction (com.djrapitops.plan.storage.database.transactions.events.PlayerServerRegisterTransaction)3 WorldNameStoreTransaction (com.djrapitops.plan.storage.database.transactions.events.WorldNameStoreTransaction)3 Collection (java.util.Collection)3