From edb6a6eb724d7d7398b366826a9136b687169715 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Tue, 29 Jan 2019 11:26:56 +0200 Subject: [PATCH] Refactored WorldTimesTable#getWorldTimesOfUser to a query --- .../queries/PlayerAggregateQueries.java | 92 +++++++++++++++++++ .../queries/ServerAggregateQueries.java | 6 ++ .../containers/PlayerContainerQuery.java | 3 +- .../plan/db/sql/tables/WorldTimesTable.java | 42 --------- .../com/djrapitops/plan/db/CommonDBTest.java | 2 +- 5 files changed, 101 insertions(+), 44 deletions(-) create mode 100644 Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/PlayerAggregateQueries.java diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/PlayerAggregateQueries.java b/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/PlayerAggregateQueries.java new file mode 100644 index 000000000..9d47bb71d --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/PlayerAggregateQueries.java @@ -0,0 +1,92 @@ +/* + * This file is part of Player Analytics (Plan). + * + * Plan is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License v3 as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Plan is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Plan. If not, see . + */ +package com.djrapitops.plan.db.access.queries; + +import com.djrapitops.plan.data.time.GMTimes; +import com.djrapitops.plan.data.time.WorldTimes; +import com.djrapitops.plan.db.access.Query; +import com.djrapitops.plan.db.access.QueryStatement; +import com.djrapitops.plan.db.sql.tables.WorldTable; +import com.djrapitops.plan.db.sql.tables.WorldTimesTable; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +/** + * Static method class for queries that count how many entries of particular kinds there are for a player. + * + * @author Rsl1122 + */ +public class PlayerAggregateQueries { + + private PlayerAggregateQueries() { + /* Static method class */ + } + + /** + * Sum total playtime per world on all servers. + * + * @param playerUUID UUID of the player. + * @return WorldTimes with world name - playtime ms information. + */ + public static Query totalWorldTimes(UUID playerUUID) { + String worldIDColumn = WorldTable.TABLE_NAME + "." + WorldTable.ID; + String worldNameColumn = WorldTable.TABLE_NAME + "." + WorldTable.NAME + " as world"; + String sql = "SELECT " + + "SUM(" + WorldTimesTable.SURVIVAL + ") as survival, " + + "SUM(" + WorldTimesTable.CREATIVE + ") as creative, " + + "SUM(" + WorldTimesTable.ADVENTURE + ") as adventure, " + + "SUM(" + WorldTimesTable.SPECTATOR + ") as spectator, " + + worldNameColumn + + " FROM " + WorldTimesTable.TABLE_NAME + + " INNER JOIN " + WorldTable.TABLE_NAME + " on " + worldIDColumn + "=" + WorldTimesTable.WORLD_ID + + " WHERE " + WorldTimesTable.USER_UUID + "=?" + + " GROUP BY world"; + + return new QueryStatement(sql) { + @Override + public void prepare(PreparedStatement statement) throws SQLException { + statement.setString(1, playerUUID.toString()); + } + + @Override + public WorldTimes processResults(ResultSet set) throws SQLException { + String[] gms = GMTimes.getGMKeyArray(); + + WorldTimes worldTimes = new WorldTimes(new HashMap<>()); + while (set.next()) { + String worldName = set.getString("world"); + + Map gmMap = new HashMap<>(); + gmMap.put(gms[0], set.getLong("survival")); + gmMap.put(gms[1], set.getLong("creative")); + gmMap.put(gms[2], set.getLong("adventure")); + gmMap.put(gms[3], set.getLong("spectator")); + GMTimes gmTimes = new GMTimes(gmMap); + + worldTimes.setGMTimesForWorld(worldName, gmTimes); + } + return worldTimes; + } + }; + } + +} \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/ServerAggregateQueries.java b/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/ServerAggregateQueries.java index feab5eacb..22f77777b 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/ServerAggregateQueries.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/ServerAggregateQueries.java @@ -133,6 +133,12 @@ public class ServerAggregateQueries { }; } + /** + * Sum total playtime per world on a server. + * + * @param serverUUID Server UUID of the Plan server. + * @return WorldTimes with world name - playtime ms information. + */ public static Query totalWorldTimes(UUID serverUUID) { String worldIDColumn = WorldTable.TABLE_NAME + "." + WorldTable.ID; String worldNameColumn = WorldTable.TABLE_NAME + "." + WorldTable.NAME + " as world"; diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/containers/PlayerContainerQuery.java b/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/containers/PlayerContainerQuery.java index 1c641a007..d7e2b14db 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/containers/PlayerContainerQuery.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/containers/PlayerContainerQuery.java @@ -29,6 +29,7 @@ import com.djrapitops.plan.data.store.mutators.SessionsMutator; import com.djrapitops.plan.data.time.WorldTimes; import com.djrapitops.plan.db.SQLDB; import com.djrapitops.plan.db.access.Query; +import com.djrapitops.plan.db.access.queries.PlayerAggregateQueries; import java.util.HashMap; import java.util.List; @@ -72,7 +73,7 @@ public class PlayerContainerQuery implements Query { ); container.putCachingSupplier(PlayerKeys.WORLD_TIMES, () -> { - WorldTimes worldTimes = new PerServerMutator(container.getUnsafe(PlayerKeys.PER_SERVER)).flatMapWorldTimes(); + WorldTimes worldTimes = db.query(PlayerAggregateQueries.totalWorldTimes(uuid)); container.getValue(PlayerKeys.ACTIVE_SESSION).ifPresent(session -> worldTimes.add( session.getValue(SessionKeys.WORLD_TIMES).orElse(new WorldTimes(new HashMap<>()))) ); diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/WorldTimesTable.java b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/WorldTimesTable.java index eb01e926d..f0b9e3212 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/WorldTimesTable.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/WorldTimesTable.java @@ -173,48 +173,6 @@ public class WorldTimesTable extends Table { }); } - public WorldTimes getWorldTimesOfUser(UUID uuid) { - String worldIDColumn = WorldTable.TABLE_NAME + "." + WorldTable.ID; - String worldNameColumn = WorldTable.TABLE_NAME + "." + WorldTable.NAME + " as world_name"; - String sql = "SELECT " + - "SUM(" + SURVIVAL + ") as survival, " + - "SUM(" + CREATIVE + ") as creative, " + - "SUM(" + ADVENTURE + ") as adventure, " + - "SUM(" + SPECTATOR + ") as spectator, " + - worldNameColumn + - " FROM " + TABLE_NAME + - " INNER JOIN " + WorldTable.TABLE_NAME + " on " + worldIDColumn + "=" + WORLD_ID + - " WHERE " + USER_UUID + "=?" + - " GROUP BY " + WORLD_ID; - - return query(new QueryStatement(sql) { - @Override - public void prepare(PreparedStatement statement) throws SQLException { - statement.setString(1, uuid.toString()); - } - - @Override - public WorldTimes processResults(ResultSet set) throws SQLException { - String[] gms = GMTimes.getGMKeyArray(); - - WorldTimes worldTimes = new WorldTimes(new HashMap<>()); - while (set.next()) { - String worldName = set.getString("world_name"); - - Map gmMap = new HashMap<>(); - gmMap.put(gms[0], set.getLong("survival")); - gmMap.put(gms[1], set.getLong("creative")); - gmMap.put(gms[2], set.getLong("adventure")); - gmMap.put(gms[3], set.getLong("spectator")); - GMTimes gmTimes = new GMTimes(gmMap); - - worldTimes.setGMTimesForWorld(worldName, gmTimes); - } - return worldTimes; - } - }); - } - public Map getAllWorldTimesBySessionID() { String worldIDColumn = WorldTable.TABLE_NAME + "." + WorldTable.ID; String worldNameColumn = WorldTable.TABLE_NAME + "." + WorldTable.NAME + " as world_name"; diff --git a/Plan/common/src/test/java/com/djrapitops/plan/db/CommonDBTest.java b/Plan/common/src/test/java/com/djrapitops/plan/db/CommonDBTest.java index fec74a312..ed33abba1 100644 --- a/Plan/common/src/test/java/com/djrapitops/plan/db/CommonDBTest.java +++ b/Plan/common/src/test/java/com/djrapitops/plan/db/CommonDBTest.java @@ -776,7 +776,7 @@ public abstract class CommonDBTest { @Test public void testGetUserWorldTimes() { testSaveSessionsWorldTimes(); - WorldTimes worldTimesOfUser = db.getWorldTimesTable().getWorldTimesOfUser(playerUUID); + WorldTimes worldTimesOfUser = db.query(PlayerAggregateQueries.totalWorldTimes(playerUUID)); assertEquals(createWorldTimes(), worldTimesOfUser); }