From c4963c117a5f305c53471dadad16981e766a4997 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Wed, 22 Nov 2017 22:01:51 +0200 Subject: [PATCH] ServerProfile get method to database, deprecated old analysis part system --- .../djrapitops/plan/data/ServerProfile.java | 7 +++ .../plan/data/analysis/ActivityPart.java | 1 + .../plan/data/analysis/CommandUsagePart.java | 1 + .../plan/data/analysis/GeolocationPart.java | 1 + .../plan/data/analysis/JoinInfoPart.java | 1 + .../plan/data/analysis/KillPart.java | 1 + .../plan/data/analysis/PlayerCountPart.java | 1 + .../plan/data/analysis/PlaytimePart.java | 1 + .../plan/data/analysis/TPSPart.java | 1 + .../plan/data/analysis/WorldPart.java | 1 + .../djrapitops/plan/database/Database.java | 3 + .../plan/database/databases/SQLDB.java | 63 ++++++++++++++++--- .../plan/database/tables/ActionsTable.java | 39 ++++++++++++ .../plan/database/tables/NicknamesTable.java | 1 + .../plan/database/tables/TPSTable.java | 6 +- 15 files changed, 120 insertions(+), 8 deletions(-) diff --git a/Plan/src/main/java/com/djrapitops/plan/data/ServerProfile.java b/Plan/src/main/java/com/djrapitops/plan/data/ServerProfile.java index 690778cb2..2667be1bc 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/ServerProfile.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/ServerProfile.java @@ -52,6 +52,13 @@ public class ServerProfile { players = new ArrayList<>(); tps = new ArrayList<>(); commandUsage = new HashMap<>(); + + allTimePeak = -1; + allTimePeakPlayers = -1; + lastPeakDate = -1; + lastPeakPlayers = -1; + playersOnline = -1; + playersMax = -1; } public List getPlayers() { diff --git a/Plan/src/main/java/com/djrapitops/plan/data/analysis/ActivityPart.java b/Plan/src/main/java/com/djrapitops/plan/data/analysis/ActivityPart.java index c19fbe86f..6efe16f2b 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/analysis/ActivityPart.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/analysis/ActivityPart.java @@ -38,6 +38,7 @@ import java.util.stream.Collectors; * @author Rsl1122 * @since 3.5.2 */ +@Deprecated public class ActivityPart extends RawData { private final PlayerCountPart playerCount; diff --git a/Plan/src/main/java/com/djrapitops/plan/data/analysis/CommandUsagePart.java b/Plan/src/main/java/com/djrapitops/plan/data/analysis/CommandUsagePart.java index af2cb2d74..319659b4a 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/analysis/CommandUsagePart.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/analysis/CommandUsagePart.java @@ -24,6 +24,7 @@ import java.util.Map; * @author Rsl1122 * @since 3.5.2 */ +@Deprecated public class CommandUsagePart extends RawData { private Map commandUsage; diff --git a/Plan/src/main/java/com/djrapitops/plan/data/analysis/GeolocationPart.java b/Plan/src/main/java/com/djrapitops/plan/data/analysis/GeolocationPart.java index f73003c18..7a06b0979 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/analysis/GeolocationPart.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/analysis/GeolocationPart.java @@ -20,6 +20,7 @@ import java.util.stream.Collectors; * @author Rsl1122 * @since 3.5.2 */ +@Deprecated public class GeolocationPart extends RawData { private final Map mostCommonGeolocations; diff --git a/Plan/src/main/java/com/djrapitops/plan/data/analysis/JoinInfoPart.java b/Plan/src/main/java/com/djrapitops/plan/data/analysis/JoinInfoPart.java index 312557c60..f6fdeb59f 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/analysis/JoinInfoPart.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/analysis/JoinInfoPart.java @@ -36,6 +36,7 @@ import java.util.stream.Collectors; * @author Rsl1122 * @since 3.5.2 */ +@Deprecated public class JoinInfoPart extends RawData { private final Map activeSessions; diff --git a/Plan/src/main/java/com/djrapitops/plan/data/analysis/KillPart.java b/Plan/src/main/java/com/djrapitops/plan/data/analysis/KillPart.java index ce5132102..71178bcea 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/analysis/KillPart.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/analysis/KillPart.java @@ -21,6 +21,7 @@ import java.util.UUID; * @author Rsl1122 * @since 3.5.2 */ +@Deprecated public class KillPart extends RawData { private final JoinInfoPart joinInfoPart; diff --git a/Plan/src/main/java/com/djrapitops/plan/data/analysis/PlayerCountPart.java b/Plan/src/main/java/com/djrapitops/plan/data/analysis/PlayerCountPart.java index 7b8727632..607f4c6cd 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/analysis/PlayerCountPart.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/analysis/PlayerCountPart.java @@ -18,6 +18,7 @@ import java.util.UUID; * @author Rsl1122 * @since 3.5.2 */ +@Deprecated public class PlayerCountPart extends RawData { private final Set uuids; diff --git a/Plan/src/main/java/com/djrapitops/plan/data/analysis/PlaytimePart.java b/Plan/src/main/java/com/djrapitops/plan/data/analysis/PlaytimePart.java index bdd6e20e9..651c1f88a 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/analysis/PlaytimePart.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/analysis/PlaytimePart.java @@ -13,6 +13,7 @@ import main.java.com.djrapitops.plan.utilities.FormatUtils; * @author Rsl1122 * @since 3.5.2 */ +@Deprecated public class PlaytimePart extends RawData { private long totalPlaytime; diff --git a/Plan/src/main/java/com/djrapitops/plan/data/analysis/TPSPart.java b/Plan/src/main/java/com/djrapitops/plan/data/analysis/TPSPart.java index b40d2a42e..e4fda3777 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/analysis/TPSPart.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/analysis/TPSPart.java @@ -49,6 +49,7 @@ import java.util.List; * @author Rsl1122 * @since 3.5.2 */ +@Deprecated public class TPSPart extends RawData { private final List tpsData; diff --git a/Plan/src/main/java/com/djrapitops/plan/data/analysis/WorldPart.java b/Plan/src/main/java/com/djrapitops/plan/data/analysis/WorldPart.java index 5d81c9079..399337c96 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/analysis/WorldPart.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/analysis/WorldPart.java @@ -21,6 +21,7 @@ import java.util.HashMap; * @author Rsl1122 * @since 3.6.0 */ +@Deprecated public class WorldPart extends RawData { private WorldTimes worldTimes; diff --git a/Plan/src/main/java/com/djrapitops/plan/database/Database.java b/Plan/src/main/java/com/djrapitops/plan/database/Database.java index 5c71bf29c..2ae48c4d0 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/Database.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/Database.java @@ -3,6 +3,7 @@ package main.java.com.djrapitops.plan.database; import main.java.com.djrapitops.plan.api.IPlan; import main.java.com.djrapitops.plan.api.exceptions.DatabaseInitException; import main.java.com.djrapitops.plan.data.PlayerProfile; +import main.java.com.djrapitops.plan.data.ServerProfile; import main.java.com.djrapitops.plan.database.tables.*; import org.apache.commons.lang3.StringUtils; @@ -268,4 +269,6 @@ public abstract class Database { } public abstract PlayerProfile getPlayerProfile(UUID uuid) throws SQLException; + + public abstract ServerProfile getServerProfile(UUID serverUUID) throws SQLException; } diff --git a/Plan/src/main/java/com/djrapitops/plan/database/databases/SQLDB.java b/Plan/src/main/java/com/djrapitops/plan/database/databases/SQLDB.java index bb3ff5188..b100eda3e 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/databases/SQLDB.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/databases/SQLDB.java @@ -8,20 +8,16 @@ import com.djrapitops.plugin.task.ITask; import com.djrapitops.plugin.task.RunnableFactory; import main.java.com.djrapitops.plan.api.IPlan; import main.java.com.djrapitops.plan.api.exceptions.DatabaseInitException; -import main.java.com.djrapitops.plan.data.PlayerProfile; -import main.java.com.djrapitops.plan.data.Session; -import main.java.com.djrapitops.plan.data.UserInfo; +import main.java.com.djrapitops.plan.data.*; import main.java.com.djrapitops.plan.database.Database; import main.java.com.djrapitops.plan.database.tables.*; import main.java.com.djrapitops.plan.database.tables.move.Version8TransferTable; +import main.java.com.djrapitops.plan.utilities.MiscUtils; import org.apache.commons.dbcp2.BasicDataSource; import java.sql.Connection; import java.sql.SQLException; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.UUID; +import java.util.*; /** * Class containing main logic for different data related save and load functionality. @@ -278,6 +274,59 @@ public abstract class SQLDB extends Database { } } + @Override + public ServerProfile getServerProfile(UUID serverUUID) throws SQLException { + ServerProfile profile = new ServerProfile(serverUUID); + + profile.setPlayers(getPlayers(serverUUID)); + profile.setTps(tpsTable.getTPSData(serverUUID)); + Optional allTimePeak = tpsTable.getAllTimePeak(serverUUID); + if (allTimePeak.isPresent()) { + TPS peak = allTimePeak.get(); + profile.setAllTimePeak(peak.getDate()); + profile.setAllTimePeakPlayers(peak.getPlayers()); + } + Optional lastPeak = tpsTable.getPeakPlayerCount(serverUUID, MiscUtils.getTime() - (TimeAmount.DAY.ms() * 2L)); + if (lastPeak.isPresent()) { + TPS peak = lastPeak.get(); + profile.setLastPeakDate(peak.getDate()); + profile.setLastPeakPlayers(peak.getPlayers()); + } + + profile.setCommandUsage(commandUseTable.getCommandUse(serverUUID)); + profile.setServerWorldtimes(worldTimesTable.getWorldTimesOfServer(serverUUID)); + + return profile; + } + + private List getPlayers(UUID serverUUID) throws SQLException { + List serverUserInfo = userInfoTable.getServerUserInfo(serverUUID); + Map timesKicked = usersTable.getAllTimesKicked(); + Map> actions = actionsTable.getServerActions(serverUUID); + Map> geoInfo = ipsTable.getAllGeoInfo(); + Map> sessions = sessionsTable.getSessionInfoOfServer(serverUUID); + + List players = new ArrayList<>(); + + for (UserInfo userInfo : serverUserInfo) { + UUID uuid = userInfo.getUuid(); + PlayerProfile profile = new PlayerProfile(uuid, userInfo.getName(), userInfo.getRegistered()); + profile.setTimesKicked(timesKicked.getOrDefault(uuid, 0)); + if (userInfo.isBanned()) { + profile.bannedOnServer(serverUUID); + } + if (userInfo.isOpped()) { + profile.oppedOnServer(serverUUID); + } + profile.setActions(actions.getOrDefault(uuid, new ArrayList<>())); + profile.setGeoInformation(geoInfo.getOrDefault(uuid, new ArrayList<>())); + profile.setSessions(serverUUID, sessions.getOrDefault(uuid, new ArrayList<>())); + + players.add(profile); + } + return players; + } + @Override public boolean wasSeenBefore(UUID uuid) { if (uuid == null) { diff --git a/Plan/src/main/java/com/djrapitops/plan/database/tables/ActionsTable.java b/Plan/src/main/java/com/djrapitops/plan/database/tables/ActionsTable.java index 25427d540..8b8021a30 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/tables/ActionsTable.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/tables/ActionsTable.java @@ -193,4 +193,43 @@ public class ActionsTable extends UserIDTable { } }); } + + public Map> getServerActions(UUID serverUUID) throws SQLException { + String usersIDColumn = usersTable + "." + usersTable.getColumnID(); + String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid"; + String sql = "SELECT " + + columnActionID + ", " + + columnDate + ", " + + columnAdditionalInfo + ", " + + usersUUIDColumn + + " FROM " + tableName + + " JOIN " + usersTable + " on " + usersIDColumn + "=" + columnUserID + + " WHERE " + serverTable.statementSelectServerID + "=" + columnServerID; + + return query(new QueryStatement>>(sql, 20000) { + @Override + public void prepare(PreparedStatement statement) throws SQLException { + statement.setString(1, serverUUID.toString()); + } + + @Override + public Map> processResults(ResultSet set) throws SQLException { + Map> map = new HashMap<>(); + while (set.next()) { + UUID uuid = UUID.fromString(set.getString("uuid")); + + List actions = map.getOrDefault(uuid, new ArrayList<>()); + + long date = set.getLong(columnDate); + Actions doneAction = Actions.getById(set.getInt(columnActionID)); + String additionalInfo = set.getString(columnAdditionalInfo); + + actions.add(new Action(date, doneAction, additionalInfo, -1)); + + map.put(uuid, actions); + } + return map; + } + }); + } } \ No newline at end of file diff --git a/Plan/src/main/java/com/djrapitops/plan/database/tables/NicknamesTable.java b/Plan/src/main/java/com/djrapitops/plan/database/tables/NicknamesTable.java index 5ecd67a04..d7780282a 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/tables/NicknamesTable.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/tables/NicknamesTable.java @@ -234,4 +234,5 @@ public class NicknamesTable extends UserIDTable { public String getColumnUserID() { return columnUserID; } + } diff --git a/Plan/src/main/java/com/djrapitops/plan/database/tables/TPSTable.java b/Plan/src/main/java/com/djrapitops/plan/database/tables/TPSTable.java index 25ea21197..2084dad3b 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/tables/TPSTable.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/tables/TPSTable.java @@ -76,6 +76,10 @@ public class TPSTable extends Table { * @return @throws SQLException */ public List getTPSData() throws SQLException { + return getTPSData(MiscUtils.getIPlan().getServerUuid()); + } + + public List getTPSData(UUID serverUUID) throws SQLException { String sql = Select.all(tableName) .where(columnServerID + "=" + serverTable.statementSelectServerID) .toString(); @@ -83,7 +87,7 @@ public class TPSTable extends Table { return query(new QueryStatement>(sql, 50000) { @Override public void prepare(PreparedStatement statement) throws SQLException { - statement.setString(1, MiscUtils.getIPlan().getServerUuid().toString()); + statement.setString(1, serverUUID.toString()); } @Override