Search in sources :

Example 1 with ActivityIndex

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

the class NetworkActivityIndexQueries method activityIndexForNewPlayers.

public static Query<Collection<ActivityIndex>> activityIndexForNewPlayers(long after, long before, Long threshold) {
    String selectNewUUIDs = SELECT + UsersTable.USER_UUID + FROM + UsersTable.TABLE_NAME + WHERE + UsersTable.REGISTERED + "<=?" + AND + UsersTable.REGISTERED + ">=?";
    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);
            setSelectActivityIndexSQLParameters(statement, 3, threshold, 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) PreparedStatement(java.sql.PreparedStatement) QueryStatement(com.djrapitops.plan.storage.database.queries.QueryStatement)

Example 2 with ActivityIndex

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

the class NetworkActivityIndexQueries method averageActivityIndexForRetainedPlayers.

public static Query<ActivityIndex> averageActivityIndexForRetainedPlayers(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" + 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);
            // 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 3 with ActivityIndex

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

the class PlayersTableJSONCreator method addPlayerData.

private void addPlayerData(Map<String, Object> dataJson, TablePlayer player) {
    String name = player.getName().orElse(player.getPlayerUUID().toString());
    String url = (playersPage ? "./player/" : "../player/") + Html.encodeToURL(player.getPlayerUUID().toString());
    int loginTimes = player.getSessionCount().orElse(0);
    long activePlaytime = player.getActivePlaytime().orElse(-1L);
    long registered = player.getRegistered().orElse(-1L);
    long lastSeen = player.getLastSeen().orElse(-1L);
    ActivityIndex activityIndex = player.getCurrentActivityIndex().orElseGet(() -> new ActivityIndex(0.0, 0));
    boolean isBanned = player.isBanned();
    String activityString = activityIndex.getFormattedValue(decimalFormatter) + (isBanned ? " (<b>" + locale.get(HtmlLang.LABEL_BANNED) + "</b>)" : " (" + activityIndex.getGroup() + ")");
    String geolocation = player.getGeolocation().orElse("-");
    Html link = openPlayerPageInNewTab ? Html.LINK_EXTERNAL : Html.LINK;
    putDataEntry(dataJson, link.create(url, StringUtils.replace(StringEscapeUtils.escapeHtml4(name), "\\", "\\\\")), "name");
    putDataEntry(dataJson, activityIndex.getValue(), activityString, "index");
    putDataEntry(dataJson, activePlaytime, numberFormatters.get(FormatType.TIME_MILLISECONDS).apply(activePlaytime), "activePlaytime");
    putDataEntry(dataJson, loginTimes, "sessions");
    putDataEntry(dataJson, registered, numberFormatters.get(FormatType.DATE_YEAR).apply(registered), "registered");
    putDataEntry(dataJson, lastSeen, numberFormatters.get(FormatType.DATE_YEAR).apply(lastSeen), "seen");
    putDataEntry(dataJson, geolocation, "geolocation");
}
Also used : ActivityIndex(com.djrapitops.plan.delivery.domain.mutators.ActivityIndex) Html(com.djrapitops.plan.delivery.rendering.html.Html)

Example 4 with ActivityIndex

use of com.djrapitops.plan.delivery.domain.mutators.ActivityIndex 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, ": "));
}
Also used : DateHolder(com.djrapitops.plan.delivery.domain.DateHolder) ActivityIndex(com.djrapitops.plan.delivery.domain.mutators.ActivityIndex) GeoInfo(com.djrapitops.plan.gathering.domain.GeoInfo) GeoInfoMutator(com.djrapitops.plan.delivery.domain.mutators.GeoInfoMutator) SessionsMutator(com.djrapitops.plan.delivery.domain.mutators.SessionsMutator)

Example 5 with ActivityIndex

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

the class QueryTablePlayersQuery method executeQuery.

@Override
public List<TablePlayer> executeQuery(SQLDB db) {
    String uuidsInSet = " IN ('" + new TextStringBuilder().appendWithSeparators(playerUUIDs, "','").build() + "')";
    String selectGeolocations = SELECT + DISTINCT + GeoInfoTable.USER_UUID + ", " + GeoInfoTable.GEOLOCATION + ", " + GeoInfoTable.LAST_USED + FROM + GeoInfoTable.TABLE_NAME;
    String selectLatestGeolocationDate = SELECT + GeoInfoTable.USER_UUID + ", " + "MAX(" + GeoInfoTable.LAST_USED + ") as last_used_g" + FROM + GeoInfoTable.TABLE_NAME + GROUP_BY + GeoInfoTable.USER_UUID;
    String selectLatestGeolocations = SELECT + "g1." + GeoInfoTable.GEOLOCATION + ',' + "g1." + GeoInfoTable.USER_UUID + FROM + "(" + selectGeolocations + ") AS g1" + INNER_JOIN + "(" + selectLatestGeolocationDate + ") AS g2 ON g1.uuid = g2.uuid" + WHERE + GeoInfoTable.LAST_USED + "=last_used_g";
    String selectSessionData = SELECT + "s." + SessionsTable.USER_UUID + ',' + "MAX(" + SessionsTable.SESSION_END + ") as last_seen," + "COUNT(1) as count," + "SUM(" + SessionsTable.SESSION_END + '-' + SessionsTable.SESSION_START + '-' + SessionsTable.AFK_TIME + ") as active_playtime" + FROM + SessionsTable.TABLE_NAME + " s" + WHERE + "s." + SessionsTable.SESSION_START + ">=?" + AND + "s." + SessionsTable.SESSION_END + "<=?" + AND + "s." + SessionsTable.USER_UUID + uuidsInSet + (serverUUIDs.isEmpty() ? "" : AND + "s." + SessionsTable.SERVER_UUID + " IN ('" + new TextStringBuilder().appendWithSeparators(serverUUIDs, "','") + "')") + GROUP_BY + "s." + SessionsTable.USER_UUID;
    String selectBanned = SELECT + DISTINCT + "ub." + UserInfoTable.USER_UUID + FROM + UserInfoTable.TABLE_NAME + " ub" + WHERE + UserInfoTable.BANNED + "=?" + AND + UserInfoTable.USER_UUID + uuidsInSet + (serverUUIDs.isEmpty() ? "" : AND + UserInfoTable.SERVER_UUID + " IN ('" + new TextStringBuilder().appendWithSeparators(serverUUIDs, "','") + "')");
    String selectBaseUsers = SELECT + "u." + UsersTable.USER_UUID + ',' + "u." + UsersTable.USER_NAME + ',' + "u." + UsersTable.REGISTERED + ',' + "ban." + UserInfoTable.USER_UUID + " as banned," + "geo." + GeoInfoTable.GEOLOCATION + ',' + "ses.last_seen," + "ses.count," + "ses.active_playtime," + "act.activity_index" + FROM + UsersTable.TABLE_NAME + " u" + LEFT_JOIN + '(' + selectBanned + ") ban on ban." + UserInfoTable.USER_UUID + "=u." + UsersTable.USER_UUID + LEFT_JOIN + '(' + selectLatestGeolocations + ") geo on geo." + GeoInfoTable.USER_UUID + "=u." + UsersTable.USER_UUID + LEFT_JOIN + '(' + selectSessionData + ") ses on ses." + SessionsTable.USER_UUID + "=u." + UsersTable.USER_UUID + LEFT_JOIN + '(' + NetworkActivityIndexQueries.selectActivityIndexSQL() + ") act on u." + SessionsTable.USER_UUID + "=act." + UserInfoTable.USER_UUID + WHERE + "u." + UserInfoTable.USER_UUID + uuidsInSet + ORDER_BY + "ses.last_seen DESC";
    return db.query(new QueryStatement<List<TablePlayer>>(selectBaseUsers, 1000) {

        @Override
        public void prepare(PreparedStatement statement) throws SQLException {
            statement.setBoolean(1, true);
            statement.setLong(2, afterDate);
            statement.setLong(3, beforeDate);
            NetworkActivityIndexQueries.setSelectActivityIndexSQLParameters(statement, 4, activeMsThreshold, beforeDate);
        }

        @Override
        public List<TablePlayer> processResults(ResultSet set) throws SQLException {
            List<TablePlayer> players = new ArrayList<>();
            while (set.next()) {
                TablePlayer.Builder player = TablePlayer.builder().uuid(UUID.fromString(set.getString(UsersTable.USER_UUID))).name(set.getString(UsersTable.USER_NAME)).geolocation(set.getString(GeoInfoTable.GEOLOCATION)).registered(set.getLong(UsersTable.REGISTERED)).lastSeen(set.getLong("last_seen")).sessionCount(set.getInt("count")).activePlaytime(set.getLong("active_playtime")).activityIndex(new ActivityIndex(set.getDouble("activity_index"), beforeDate));
                if (set.getString("banned") != null) {
                    player.banned();
                }
                players.add(player.build());
            }
            return players;
        }
    });
}
Also used : SQLException(java.sql.SQLException) TextStringBuilder(org.apache.commons.text.TextStringBuilder) ActivityIndex(com.djrapitops.plan.delivery.domain.mutators.ActivityIndex) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List) PreparedStatement(java.sql.PreparedStatement) TextStringBuilder(org.apache.commons.text.TextStringBuilder)

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