Search in sources :

Example 31 with TextStringBuilder

use of org.apache.commons.text.TextStringBuilder in project Plan by plan-player-analytics.

the class UserInfoQueries method uuidsOfRegisteredBetween.

public static Query<Set<UUID>> uuidsOfRegisteredBetween(long after, long before, List<ServerUUID> serverUUIDs) {
    String sql = SELECT + DISTINCT + UserInfoTable.USER_UUID + FROM + UserInfoTable.TABLE_NAME + WHERE + UserInfoTable.REGISTERED + ">=?" + AND + UserInfoTable.REGISTERED + "<=?" + AND + UserInfoTable.SERVER_UUID + " IN ('" + new TextStringBuilder().appendWithSeparators(serverUUIDs, "','") + "')";
    return new QueryStatement<Set<UUID>>(sql) {

        @Override
        public void prepare(PreparedStatement statement) throws SQLException {
            statement.setLong(1, after);
            statement.setLong(2, before);
        }

        @Override
        public Set<UUID> processResults(ResultSet set) throws SQLException {
            Set<UUID> uuids = new HashSet<>();
            while (set.next()) {
                uuids.add(UUID.fromString(set.getString(UsersTable.USER_UUID)));
            }
            return uuids;
        }
    };
}
Also used : ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) QueryStatement(com.djrapitops.plan.storage.database.queries.QueryStatement) ServerUUID(com.djrapitops.plan.identification.ServerUUID) TextStringBuilder(org.apache.commons.text.TextStringBuilder)

Example 32 with TextStringBuilder

use of org.apache.commons.text.TextStringBuilder 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)

Example 33 with TextStringBuilder

use of org.apache.commons.text.TextStringBuilder in project Plan by plan-player-analytics.

the class NetworkActivityIndexQueries method selectActivityIndexSQL.

public static String selectActivityIndexSQL(Collection<ServerUUID> onServers) {
    String selectActivePlaytimeSQL = SELECT + "ux." + UsersTable.USER_UUID + ",COALESCE(active_playtime,0) AS active_playtime" + FROM + UsersTable.TABLE_NAME + " ux" + LEFT_JOIN + '(' + SELECT + SessionsTable.USER_UUID + ",SUM(" + SessionsTable.SESSION_END + '-' + SessionsTable.SESSION_START + '-' + SessionsTable.AFK_TIME + ") as active_playtime" + FROM + SessionsTable.TABLE_NAME + WHERE + SessionsTable.SESSION_END + ">=?" + AND + SessionsTable.SESSION_START + "<=?" + (onServers.isEmpty() ? "" : AND + SessionsTable.SERVER_UUID + " IN ('" + new TextStringBuilder().appendWithSeparators(onServers, "','") + "')") + GROUP_BY + SessionsTable.USER_UUID + ") sx on sx.uuid=ux.uuid";
    String selectThreeWeeks = selectActivePlaytimeSQL + UNION_ALL + selectActivePlaytimeSQL + UNION_ALL + selectActivePlaytimeSQL;
    return SELECT + "5.0 - 5.0 * AVG(1.0 / (?/2.0 * (q1.active_playtime*1.0/?) +1.0)) as activity_index," + "q1." + SessionsTable.USER_UUID + FROM + '(' + selectThreeWeeks + ") q1" + GROUP_BY + "q1." + SessionsTable.USER_UUID;
}
Also used : TextStringBuilder(org.apache.commons.text.TextStringBuilder)

Example 34 with TextStringBuilder

use of org.apache.commons.text.TextStringBuilder in project Plan by plan-player-analytics.

the class SessionQueries method summaryOfPlayers.

public static Query<Map<String, Long>> summaryOfPlayers(Set<UUID> playerUUIDs, List<ServerUUID> serverUUIDs, long after, long before) {
    String selectAggregates = SELECT + "SUM(" + SessionsTable.SESSION_END + '-' + SessionsTable.SESSION_START + ") as playtime," + "SUM(" + SessionsTable.SESSION_END + '-' + SessionsTable.SESSION_START + '-' + SessionsTable.AFK_TIME + ") as active_playtime," + "COUNT(1) as session_count" + FROM + SessionsTable.TABLE_NAME + WHERE + SessionsTable.SESSION_START + ">?" + AND + SessionsTable.SESSION_END + "<?" + AND + SessionsTable.USER_UUID + " IN ('" + new TextStringBuilder().appendWithSeparators(playerUUIDs, "','").build() + "')" + (serverUUIDs.isEmpty() ? "" : AND + SessionsTable.SERVER_UUID + " IN ('" + new TextStringBuilder().appendWithSeparators(serverUUIDs, "','") + "')");
    return new QueryStatement<Map<String, Long>>(selectAggregates) {

        @Override
        public void prepare(PreparedStatement statement) throws SQLException {
            statement.setLong(1, after);
            statement.setLong(2, before);
        }

        @Override
        public Map<String, Long> processResults(ResultSet set) throws SQLException {
            if (set.next()) {
                long sessionCount = set.getLong("session_count");
                long playtime = set.getLong("playtime");
                long activePlaytime = set.getLong("active_playtime");
                int playerCount = playerUUIDs.size();
                return Maps.builder(String.class, Long.class).put("total_playtime", playtime).put("average_playtime", playerCount != 0 ? playtime / playerCount : -1L).put("total_afk_playtime", playtime - activePlaytime).put("average_afk_playtime", playerCount != 0 ? (playtime - activePlaytime) / playerCount : -1L).put("total_active_playtime", activePlaytime).put("average_active_playtime", playerCount != 0 ? activePlaytime / playerCount : -1L).put("total_sessions", sessionCount).put("average_sessions", playerCount != 0 ? sessionCount / playerCount : -1L).put("average_session_length", sessionCount != 0 ? playtime / sessionCount : -1L).build();
            } else {
                return Collections.emptyMap();
            }
        }
    };
}
Also used : ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) QueryStatement(com.djrapitops.plan.storage.database.queries.QueryStatement) TextStringBuilder(org.apache.commons.text.TextStringBuilder)

Example 35 with TextStringBuilder

use of org.apache.commons.text.TextStringBuilder in project azure-kusto-java by Azure.

the class IngestionProperties method validate.

/**
 * Validate the minimum non-empty values needed for data ingestion and mappings.
 */
void validate() throws IngestionClientException {
    Ensure.stringIsNotBlank(databaseName, "databaseName");
    Ensure.stringIsNotBlank(tableName, "tableName");
    Ensure.argIsNotNull(reportMethod, "reportMethod");
    String mappingReference = ingestionMapping.getIngestionMappingReference();
    IngestionMapping.IngestionMappingKind ingestionMappingKind = ingestionMapping.getIngestionMappingKind();
    TextStringBuilder message = new TextStringBuilder();
    if ((ingestionMapping.getColumnMappings() == null) && StringUtils.isBlank(mappingReference)) {
        if (ingestionMappingKind != null) {
            message.appendln("IngestionMappingKind was defined ('%s'), so a mapping must be defined as well.", ingestionMappingKind);
        }
    } else {
        // a mapping was provided
        if (dataFormat.getIngestionMappingKind() != null && !dataFormat.getIngestionMappingKind().equals(ingestionMappingKind)) {
            message.appendln("Wrong ingestion mapping for format '%s'; mapping kind should be '%s', but was '%s'.", dataFormat.getKustoValue(), dataFormat.getIngestionMappingKind().getKustoValue(), ingestionMappingKind != null ? ingestionMappingKind.getKustoValue() : "null");
        }
        if (ingestionMapping.getColumnMappings() != null) {
            if (StringUtils.isNotBlank(mappingReference)) {
                message.appendln("Both mapping reference '%s' and column mappings were defined.", mappingReference);
            }
            if (ingestionMappingKind != null) {
                for (ColumnMapping column : ingestionMapping.getColumnMappings()) {
                    if (!column.isValid(ingestionMappingKind)) {
                        message.appendln("Column mapping '%s' is invalid.", column.getColumnName());
                    }
                }
            }
        }
    }
    if (!message.isEmpty()) {
        String messageStr = message.build();
        log.error(messageStr);
        throw new IngestionClientException(messageStr);
    }
}
Also used : IngestionClientException(com.microsoft.azure.kusto.ingest.exceptions.IngestionClientException) TextStringBuilder(org.apache.commons.text.TextStringBuilder)

Aggregations

TextStringBuilder (org.apache.commons.text.TextStringBuilder)37 PreparedStatement (java.sql.PreparedStatement)4 ResultSet (java.sql.ResultSet)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Issue (org.apache.gobblin.runtime.troubleshooter.Issue)4 QueryStatement (com.djrapitops.plan.storage.database.queries.QueryStatement)3 Nullable (javax.annotation.Nullable)3 StringTokenizer (org.apache.commons.text.StringTokenizer)3 Test (org.junit.jupiter.api.Test)3 ServerUUID (com.djrapitops.plan.identification.ServerUUID)2 com.haulmont.cuba.core.global (com.haulmont.cuba.core.global)2 SecurityJpqlGenerator (com.haulmont.cuba.core.global.filter.SecurityJpqlGenerator)2 ConditionsTree (com.haulmont.cuba.gui.components.filter.ConditionsTree)2 FakeFilterSupport (com.haulmont.cuba.gui.components.filter.FakeFilterSupport)2 FilterParser (com.haulmont.cuba.gui.components.filter.FilterParser)2 FilterEditor (com.haulmont.cuba.gui.components.filter.edit.FilterEditor)2 FilterEntity (com.haulmont.cuba.security.entity.FilterEntity)2 ComponentBuilder (net.md_5.bungee.api.chat.ComponentBuilder)2 HoverEvent (net.md_5.bungee.api.chat.HoverEvent)2