mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2024-09-07 04:18:26 +02:00
Optimized few CommonQueries
This commit is contained in:
parent
4bf38456b3
commit
5a87dfa7e8
@ -493,6 +493,32 @@ public class SessionQueries {
|
||||
};
|
||||
}
|
||||
|
||||
public static Query<Map<UUID, Long>> playtimeOfPlayer(long after, long before, UUID playerUUID) {
|
||||
String sql = SELECT + SessionsTable.SERVER_UUID + ",SUM(" + SessionsTable.SESSION_END + '-' + SessionsTable.SESSION_START + ") as playtime" +
|
||||
FROM + SessionsTable.TABLE_NAME +
|
||||
WHERE + SessionsTable.USER_UUID + "=?" +
|
||||
AND + SessionsTable.SESSION_END + ">=?" +
|
||||
AND + SessionsTable.SESSION_START + "<=?" +
|
||||
GROUP_BY + SessionsTable.SERVER_UUID;
|
||||
return new QueryStatement<Map<UUID, Long>>(sql) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setString(1, playerUUID.toString());
|
||||
statement.setLong(2, after);
|
||||
statement.setLong(3, before);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<UUID, Long> processResults(ResultSet set) throws SQLException {
|
||||
Map<UUID, Long> playtimeOfPlayer = new HashMap<>();
|
||||
while (set.next()) {
|
||||
playtimeOfPlayer.put(UUID.fromString(set.getString(SessionsTable.SERVER_UUID)), set.getLong("playtime"));
|
||||
}
|
||||
return playtimeOfPlayer;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static Query<Long> playtime(long after, long before) {
|
||||
String sql = SELECT + "SUM(" + SessionsTable.SESSION_END + '-' + SessionsTable.SESSION_START + ") as playtime" +
|
||||
FROM + SessionsTable.TABLE_NAME +
|
||||
@ -765,4 +791,23 @@ public class SessionQueries {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static Query<Long> lastSeen(UUID playerUUID, UUID serverUUID) {
|
||||
String sql = SELECT + "MAX(" + SessionsTable.SESSION_END + ") as last_seen" +
|
||||
FROM + SessionsTable.TABLE_NAME +
|
||||
WHERE + SessionsTable.USER_UUID + "=?" +
|
||||
AND + SessionsTable.SERVER_UUID + "=?";
|
||||
return new QueryStatement<Long>(sql) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setString(1, playerUUID.toString());
|
||||
statement.setString(2, serverUUID.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long processResults(ResultSet set) throws SQLException {
|
||||
return set.next() ? set.getLong("last_seen") : 0;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
@ -16,12 +16,10 @@
|
||||
*/
|
||||
package com.djrapitops.plan.query;
|
||||
|
||||
import com.djrapitops.plan.data.store.containers.PlayerContainer;
|
||||
import com.djrapitops.plan.data.store.mutators.SessionsMutator;
|
||||
import com.djrapitops.plan.db.DBType;
|
||||
import com.djrapitops.plan.db.Database;
|
||||
import com.djrapitops.plan.db.access.queries.containers.ContainerFetchQueries;
|
||||
import com.djrapitops.plan.db.access.queries.objects.ServerQueries;
|
||||
import com.djrapitops.plan.db.access.queries.objects.SessionQueries;
|
||||
import com.djrapitops.plan.db.access.queries.objects.UserIdentifierQueries;
|
||||
import com.djrapitops.plan.db.access.queries.schema.H2SchemaQueries;
|
||||
import com.djrapitops.plan.db.access.queries.schema.MySQLSchemaQueries;
|
||||
@ -41,21 +39,12 @@ public class CommonQueriesImplementation implements CommonQueries {
|
||||
|
||||
@Override
|
||||
public long fetchPlaytime(UUID playerUUID, UUID serverUUID, long after, long before) {
|
||||
// TODO Replace with single query later
|
||||
PlayerContainer player = db.query(ContainerFetchQueries.fetchPlayerContainer(playerUUID));
|
||||
return SessionsMutator.forContainer(player)
|
||||
.filterSessionsBetween(after, before)
|
||||
.filterPlayedOnServer(serverUUID)
|
||||
.toPlaytime();
|
||||
return db.query(SessionQueries.playtimeOfPlayer(after, before, playerUUID)).getOrDefault(serverUUID, 0L);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long fetchLastSeen(UUID playerUUID, UUID serverUUID) {
|
||||
// TODO Replace with single query later
|
||||
PlayerContainer player = db.query(ContainerFetchQueries.fetchPlayerContainer(playerUUID));
|
||||
return SessionsMutator.forContainer(player)
|
||||
.filterPlayedOnServer(serverUUID)
|
||||
.toLastSeen();
|
||||
return db.query(SessionQueries.lastSeen(playerUUID, serverUUID));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
Reference in New Issue
Block a user