From 6023b9e88cd2c5ab2f4a266c08e83afe84c8aa8c Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Fri, 25 Jan 2019 20:21:08 +0200 Subject: [PATCH] Split ServerContainerQuery and NetworkContainerQuery into classes --- .../db/sql/queries/ContainerFetchQueries.java | 111 +++--------------- .../containers/NetworkContainerQuery.java | 79 +++++++++++++ .../containers/ServerContainerQuery.java | 107 +++++++++++++++++ 3 files changed, 204 insertions(+), 93 deletions(-) create mode 100644 Plan/common/src/main/java/com/djrapitops/plan/db/sql/queries/containers/NetworkContainerQuery.java create mode 100644 Plan/common/src/main/java/com/djrapitops/plan/db/sql/queries/containers/ServerContainerQuery.java diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/queries/ContainerFetchQueries.java b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/queries/ContainerFetchQueries.java index bc4d2dce9..408ec21ae 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/queries/ContainerFetchQueries.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/queries/ContainerFetchQueries.java @@ -16,24 +16,13 @@ */ package com.djrapitops.plan.db.sql.queries; -import com.djrapitops.plan.data.container.Session; -import com.djrapitops.plan.data.container.TPS; import com.djrapitops.plan.data.store.containers.NetworkContainer; import com.djrapitops.plan.data.store.containers.ServerContainer; -import com.djrapitops.plan.data.store.keys.NetworkKeys; -import com.djrapitops.plan.data.store.keys.ServerKeys; -import com.djrapitops.plan.data.store.mutators.PlayersMutator; -import com.djrapitops.plan.data.store.mutators.SessionsMutator; -import com.djrapitops.plan.data.store.objects.DateObj; import com.djrapitops.plan.db.access.Query; -import com.djrapitops.plan.system.cache.SessionCache; -import com.djrapitops.plan.system.info.server.Server; +import com.djrapitops.plan.db.sql.queries.containers.NetworkContainerQuery; +import com.djrapitops.plan.db.sql.queries.containers.ServerContainerQuery; -import java.util.List; -import java.util.Optional; import java.util.UUID; -import java.util.concurrent.TimeUnit; -import java.util.stream.Collectors; /** * Static method class for queries that return some kind of {@link com.djrapitops.plan.data.store.containers.DataContainer}. @@ -46,90 +35,26 @@ public class ContainerFetchQueries { /* Static method class */ } + /** + * Used to get a NetworkContainer, some limitations apply to values returned by DataContainer keys. + * + * @return a new NetworkContainer. + * @see com.djrapitops.plan.db.sql.queries.containers.NetworkContainerQuery + */ public static Query fetchNetworkContainer() { - return db -> { - ServerContainer bungeeContainer = db.query(getBungeeServerContainer()); - NetworkContainer networkContainer = db.getNetworkContainerFactory().forBungeeContainer(bungeeContainer); - networkContainer.putCachingSupplier(NetworkKeys.BUKKIT_SERVERS, () -> - db.query(LargeFetchQueries.fetchPlanServerInformation()).values() - .stream().filter(Server::isNotProxy).collect(Collectors.toSet()) - ); - return networkContainer; - }; - } - - private static Query getBungeeServerContainer() { - return db -> { - Optional proxyInformation = db.query(OptionalFetchQueries.proxyServerInformation()); - if (!proxyInformation.isPresent()) { - return new ServerContainer(); - } - - UUID serverUUID = proxyInformation.get().getUuid(); - ServerContainer container = db.query(getServerContainer(serverUUID)); - container.putCachingSupplier(ServerKeys.PLAYERS, db.fetch()::getAllPlayerContainers); - container.putCachingSupplier(ServerKeys.TPS, db.getTpsTable()::getNetworkOnlineData); - container.putSupplier(ServerKeys.WORLD_TIMES, null); // Additional Session information not supported - container.putSupplier(ServerKeys.PLAYER_KILLS, null); - container.putSupplier(ServerKeys.PLAYER_KILL_COUNT, null); - - return container; - }; + return new NetworkContainerQuery(); } + /** + * Used to get a ServerContainer, some limitations apply to values returned by DataContainer keys. + *

+ * + * @param serverUUID UUID of the Server. + * @return a new ServerContainer. + * @see com.djrapitops.plan.db.sql.queries.containers.ServerContainerQuery + */ public static Query getServerContainer(UUID serverUUID) { - return db -> { - ServerContainer container = new ServerContainer(); - - Optional serverInfo = db.getServerTable().getServerInfo(serverUUID); - if (!serverInfo.isPresent()) { - return container; - } - - container.putRawData(ServerKeys.SERVER_UUID, serverUUID); - container.putRawData(ServerKeys.NAME, serverInfo.get().getName()); - container.putCachingSupplier(ServerKeys.PLAYERS, () -> db.fetch().getPlayerContainers(serverUUID)); - container.putSupplier(ServerKeys.PLAYER_COUNT, () -> container.getUnsafe(ServerKeys.PLAYERS).size()); - - container.putCachingSupplier(ServerKeys.TPS, () -> db.getTpsTable().getTPSData(serverUUID)); - container.putCachingSupplier(ServerKeys.PING, () -> PlayersMutator.forContainer(container).pings()); - container.putCachingSupplier(ServerKeys.ALL_TIME_PEAK_PLAYERS, () -> { - Optional allTimePeak = db.getTpsTable().getAllTimePeak(serverUUID); - if (allTimePeak.isPresent()) { - TPS peak = allTimePeak.get(); - return new DateObj<>(peak.getDate(), peak.getPlayers()); - } - return null; - }); - container.putCachingSupplier(ServerKeys.RECENT_PEAK_PLAYERS, () -> { - long twoDaysAgo = System.currentTimeMillis() - (TimeUnit.DAYS.toMillis(2L)); - Optional lastPeak = db.getTpsTable().getPeakPlayerCount(serverUUID, twoDaysAgo); - if (lastPeak.isPresent()) { - TPS peak = lastPeak.get(); - return new DateObj<>(peak.getDate(), peak.getPlayers()); - } - return null; - }); - - container.putCachingSupplier(ServerKeys.COMMAND_USAGE, () -> db.getCommandUseTable().getCommandUse(serverUUID)); - container.putCachingSupplier(ServerKeys.WORLD_TIMES, () -> db.getWorldTimesTable().getWorldTimesOfServer(serverUUID)); - - // Calculating getters - container.putCachingSupplier(ServerKeys.OPERATORS, () -> PlayersMutator.forContainer(container).operators()); - container.putCachingSupplier(ServerKeys.SESSIONS, () -> { - List sessions = PlayersMutator.forContainer(container).getSessions(); - if (serverUUID.equals(serverInfo.get().getUuid())) { - sessions.addAll(SessionCache.getActiveSessions().values()); - } - return sessions; - }); - container.putCachingSupplier(ServerKeys.PLAYER_KILLS, () -> SessionsMutator.forContainer(container).toPlayerKillList()); - container.putCachingSupplier(ServerKeys.PLAYER_KILL_COUNT, () -> container.getUnsafe(ServerKeys.PLAYER_KILLS).size()); - container.putCachingSupplier(ServerKeys.MOB_KILL_COUNT, () -> SessionsMutator.forContainer(container).toMobKillCount()); - container.putCachingSupplier(ServerKeys.DEATH_COUNT, () -> SessionsMutator.forContainer(container).toDeathCount()); - - return container; - }; + return new ServerContainerQuery(serverUUID); } } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/queries/containers/NetworkContainerQuery.java b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/queries/containers/NetworkContainerQuery.java new file mode 100644 index 000000000..654a190b0 --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/queries/containers/NetworkContainerQuery.java @@ -0,0 +1,79 @@ +/* + * 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.sql.queries.containers; + +import com.djrapitops.plan.data.store.containers.NetworkContainer; +import com.djrapitops.plan.data.store.containers.ServerContainer; +import com.djrapitops.plan.data.store.keys.NetworkKeys; +import com.djrapitops.plan.data.store.keys.ServerKeys; +import com.djrapitops.plan.db.SQLDB; +import com.djrapitops.plan.db.access.Query; +import com.djrapitops.plan.db.sql.queries.ContainerFetchQueries; +import com.djrapitops.plan.db.sql.queries.LargeFetchQueries; +import com.djrapitops.plan.db.sql.queries.OptionalFetchQueries; +import com.djrapitops.plan.system.info.server.Server; + +import java.util.Optional; +import java.util.UUID; +import java.util.stream.Collectors; + +/** + * Used to get a NetworkContainer, some limitations apply to values returned by DataContainer keys. + *

+ * Limitations: + * - Bungee ServerContainer does not support: ServerKeys WORLD_TIMES, PLAYER_KILLS, PLAYER_DEATHS, PLAYER_KILL_COUNT + * - Bungee ServerContainer ServerKeys.TPS only contains playersOnline values + * - NetworkKeys.PLAYERS PlayerContainers: + * - do not support: PlayerKeys WORLD_TIMES, PLAYER_KILLS, PLAYER_DEATHS, PLAYER_KILL_COUNT + * - PlayerKeys.PER_SERVER does not support: PerServerKeys WORLD_TIMES, PLAYER_KILLS, PLAYER_DEATHS, PLAYER_KILL_COUNT + *

+ * Blocking methods are not called until DataContainer getter methods are called. + * + * @author Rsl1122 + */ +public class NetworkContainerQuery implements Query { + + private static Query getBungeeServerContainer() { + return db -> { + Optional proxyInformation = db.query(OptionalFetchQueries.proxyServerInformation()); + if (!proxyInformation.isPresent()) { + return new ServerContainer(); + } + + UUID serverUUID = proxyInformation.get().getUuid(); + ServerContainer container = db.query(ContainerFetchQueries.getServerContainer(serverUUID)); + container.putCachingSupplier(ServerKeys.PLAYERS, db.fetch()::getAllPlayerContainers); + container.putCachingSupplier(ServerKeys.TPS, db.getTpsTable()::getNetworkOnlineData); + container.putSupplier(ServerKeys.WORLD_TIMES, null); // Additional Session information not supported + container.putSupplier(ServerKeys.PLAYER_KILLS, null); + container.putSupplier(ServerKeys.PLAYER_KILL_COUNT, null); + + return container; + }; + } + + @Override + public NetworkContainer executeQuery(SQLDB db) { + ServerContainer bungeeContainer = db.query(getBungeeServerContainer()); + NetworkContainer networkContainer = db.getNetworkContainerFactory().forBungeeContainer(bungeeContainer); + networkContainer.putCachingSupplier(NetworkKeys.BUKKIT_SERVERS, () -> + db.query(LargeFetchQueries.fetchPlanServerInformation()).values() + .stream().filter(Server::isNotProxy).collect(Collectors.toSet()) + ); + return networkContainer; + } +} \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/queries/containers/ServerContainerQuery.java b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/queries/containers/ServerContainerQuery.java new file mode 100644 index 000000000..ef91f421f --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/queries/containers/ServerContainerQuery.java @@ -0,0 +1,107 @@ +/* + * 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.sql.queries.containers; + +import com.djrapitops.plan.data.container.Session; +import com.djrapitops.plan.data.container.TPS; +import com.djrapitops.plan.data.store.containers.ServerContainer; +import com.djrapitops.plan.data.store.keys.ServerKeys; +import com.djrapitops.plan.data.store.mutators.PlayersMutator; +import com.djrapitops.plan.data.store.mutators.SessionsMutator; +import com.djrapitops.plan.data.store.objects.DateObj; +import com.djrapitops.plan.db.SQLDB; +import com.djrapitops.plan.db.access.Query; +import com.djrapitops.plan.system.cache.SessionCache; +import com.djrapitops.plan.system.info.server.Server; + +import java.util.List; +import java.util.Optional; +import java.util.UUID; +import java.util.concurrent.TimeUnit; + +/** + * Used to get a ServerContainer, some limitations apply to values returned by DataContainer keys. + *

+ * Limitations: + * - ServerKeys.PLAYERS PlayerContainers PlayerKeys.PER_SERVER only contains information about the queried server. + *

+ * Blocking methods are not called until DataContainer getter methods are called. + * + * @author Rsl1122 + */ +public class ServerContainerQuery implements Query { + + private final UUID serverUUID; + + public ServerContainerQuery(UUID serverUUID) { + this.serverUUID = serverUUID; + } + + @Override + public ServerContainer executeQuery(SQLDB db) { + ServerContainer container = new ServerContainer(); + + Optional serverInfo = db.getServerTable().getServerInfo(serverUUID); + if (!serverInfo.isPresent()) { + return container; + } + + container.putRawData(ServerKeys.SERVER_UUID, serverUUID); + container.putRawData(ServerKeys.NAME, serverInfo.get().getName()); + container.putCachingSupplier(ServerKeys.PLAYERS, () -> db.fetch().getPlayerContainers(serverUUID)); + container.putSupplier(ServerKeys.PLAYER_COUNT, () -> container.getUnsafe(ServerKeys.PLAYERS).size()); + + container.putCachingSupplier(ServerKeys.TPS, () -> db.getTpsTable().getTPSData(serverUUID)); + container.putCachingSupplier(ServerKeys.PING, () -> PlayersMutator.forContainer(container).pings()); + container.putCachingSupplier(ServerKeys.ALL_TIME_PEAK_PLAYERS, () -> { + Optional allTimePeak = db.getTpsTable().getAllTimePeak(serverUUID); + if (allTimePeak.isPresent()) { + TPS peak = allTimePeak.get(); + return new DateObj<>(peak.getDate(), peak.getPlayers()); + } + return null; + }); + container.putCachingSupplier(ServerKeys.RECENT_PEAK_PLAYERS, () -> { + long twoDaysAgo = System.currentTimeMillis() - (TimeUnit.DAYS.toMillis(2L)); + Optional lastPeak = db.getTpsTable().getPeakPlayerCount(serverUUID, twoDaysAgo); + if (lastPeak.isPresent()) { + TPS peak = lastPeak.get(); + return new DateObj<>(peak.getDate(), peak.getPlayers()); + } + return null; + }); + + container.putCachingSupplier(ServerKeys.COMMAND_USAGE, () -> db.getCommandUseTable().getCommandUse(serverUUID)); + container.putCachingSupplier(ServerKeys.WORLD_TIMES, () -> db.getWorldTimesTable().getWorldTimesOfServer(serverUUID)); + + // Calculating getters + container.putCachingSupplier(ServerKeys.OPERATORS, () -> PlayersMutator.forContainer(container).operators()); + container.putCachingSupplier(ServerKeys.SESSIONS, () -> { + List sessions = PlayersMutator.forContainer(container).getSessions(); + if (serverUUID.equals(serverInfo.get().getUuid())) { + sessions.addAll(SessionCache.getActiveSessions().values()); + } + return sessions; + }); + container.putCachingSupplier(ServerKeys.PLAYER_KILLS, () -> SessionsMutator.forContainer(container).toPlayerKillList()); + container.putCachingSupplier(ServerKeys.PLAYER_KILL_COUNT, () -> container.getUnsafe(ServerKeys.PLAYER_KILLS).size()); + container.putCachingSupplier(ServerKeys.MOB_KILL_COUNT, () -> SessionsMutator.forContainer(container).toMobKillCount()); + container.putCachingSupplier(ServerKeys.DEATH_COUNT, () -> SessionsMutator.forContainer(container).toDeathCount()); + + return container; + } +} \ No newline at end of file