Optimized few CommonQueries

This commit is contained in:
Rsl1122 2019-08-25 18:56:09 +03:00
parent 4bf38456b3
commit 5a87dfa7e8
2 changed files with 48 additions and 14 deletions

View File

@ -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) { public static Query<Long> playtime(long after, long before) {
String sql = SELECT + "SUM(" + SessionsTable.SESSION_END + '-' + SessionsTable.SESSION_START + ") as playtime" + String sql = SELECT + "SUM(" + SessionsTable.SESSION_END + '-' + SessionsTable.SESSION_START + ") as playtime" +
FROM + SessionsTable.TABLE_NAME + 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;
}
};
}
} }

View File

@ -16,12 +16,10 @@
*/ */
package com.djrapitops.plan.query; 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.DBType;
import com.djrapitops.plan.db.Database; 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.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.objects.UserIdentifierQueries;
import com.djrapitops.plan.db.access.queries.schema.H2SchemaQueries; import com.djrapitops.plan.db.access.queries.schema.H2SchemaQueries;
import com.djrapitops.plan.db.access.queries.schema.MySQLSchemaQueries; import com.djrapitops.plan.db.access.queries.schema.MySQLSchemaQueries;
@ -41,21 +39,12 @@ public class CommonQueriesImplementation implements CommonQueries {
@Override @Override
public long fetchPlaytime(UUID playerUUID, UUID serverUUID, long after, long before) { public long fetchPlaytime(UUID playerUUID, UUID serverUUID, long after, long before) {
// TODO Replace with single query later return db.query(SessionQueries.playtimeOfPlayer(after, before, playerUUID)).getOrDefault(serverUUID, 0L);
PlayerContainer player = db.query(ContainerFetchQueries.fetchPlayerContainer(playerUUID));
return SessionsMutator.forContainer(player)
.filterSessionsBetween(after, before)
.filterPlayedOnServer(serverUUID)
.toPlaytime();
} }
@Override @Override
public long fetchLastSeen(UUID playerUUID, UUID serverUUID) { public long fetchLastSeen(UUID playerUUID, UUID serverUUID) {
// TODO Replace with single query later return db.query(SessionQueries.lastSeen(playerUUID, serverUUID));
PlayerContainer player = db.query(ContainerFetchQueries.fetchPlayerContainer(playerUUID));
return SessionsMutator.forContainer(player)
.filterPlayedOnServer(serverUUID)
.toLastSeen();
} }
@Override @Override