From cbce9ff5aaf678f5fd0000e2d7932b3fad7476a6 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Fri, 23 Aug 2019 10:55:14 +0300 Subject: [PATCH] Ping table to Geolocations tab (server page) --- .../queries/objects/GeoInfoQueries.java | 8 +-- .../access/queries/objects/PingQueries.java | 58 +++++++++++++++++++ .../plan/system/json/JSONFactory.java | 23 ++++++-- .../webserver/pages/json/RootJSONHandler.java | 2 + .../resources/assets/plan/web/js/pingTable.js | 32 ++++++++++ .../assets/plan/web/js/sessionAccordion.js | 9 +-- .../resources/assets/plan/web/server.html | 17 ++---- 7 files changed, 126 insertions(+), 23 deletions(-) create mode 100644 Plan/common/src/main/resources/assets/plan/web/js/pingTable.js diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/objects/GeoInfoQueries.java b/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/objects/GeoInfoQueries.java index d38fcfee7..fa46efb3e 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/objects/GeoInfoQueries.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/objects/GeoInfoQueries.java @@ -161,19 +161,19 @@ public class GeoInfoQueries { } public static Query> serverGeolocationCounts(UUID serverUUID) { - String subQuery1 = SELECT + + String selectGeolocations = SELECT + GeoInfoTable.USER_UUID + ", " + GeoInfoTable.GEOLOCATION + ", " + GeoInfoTable.LAST_USED + FROM + GeoInfoTable.TABLE_NAME; - String subQuery2 = SELECT + + String selectLatestGeolocationDate = SELECT + GeoInfoTable.USER_UUID + ", " + "MAX(" + GeoInfoTable.LAST_USED + ") as m" + FROM + GeoInfoTable.TABLE_NAME + GROUP_BY + GeoInfoTable.USER_UUID; String sql = SELECT + GeoInfoTable.GEOLOCATION + ", COUNT(1) as c FROM (" + - "(" + subQuery1 + ") AS q1" + - INNER_JOIN + "(" + subQuery2 + ") AS q2 ON q1.uuid = q2.uuid" + + "(" + selectGeolocations + ") AS q1" + + INNER_JOIN + "(" + selectLatestGeolocationDate + ") AS q2 ON q1.uuid = q2.uuid" + INNER_JOIN + UserInfoTable.TABLE_NAME + " u on u." + UserInfoTable.USER_UUID + "=q1.uuid)" + WHERE + GeoInfoTable.LAST_USED + "=m" + AND + "u." + UserInfoTable.SERVER_UUID + "=?" + diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/objects/PingQueries.java b/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/objects/PingQueries.java index 7bc8bd8f4..424ea3e33 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/objects/PingQueries.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/objects/PingQueries.java @@ -21,6 +21,7 @@ import com.djrapitops.plan.data.container.Ping; import com.djrapitops.plan.db.access.Query; import com.djrapitops.plan.db.access.QueryAllStatement; import com.djrapitops.plan.db.access.QueryStatement; +import com.djrapitops.plan.db.sql.tables.GeoInfoTable; import com.djrapitops.plan.db.sql.tables.PingTable; import java.sql.PreparedStatement; @@ -186,4 +187,61 @@ public class PingQueries { } }; } + + public static Query> fetchPingDataOfServerByGeolocation(UUID serverUUID) { + String selectPingOfServer = SELECT + + PingTable.MAX_PING + ", " + + PingTable.MIN_PING + ", " + + PingTable.AVG_PING + ", " + + PingTable.USER_UUID + ", " + + PingTable.SERVER_UUID + + FROM + PingTable.TABLE_NAME; + + String selectGeolocations = SELECT + + GeoInfoTable.USER_UUID + ", " + + GeoInfoTable.GEOLOCATION + ", " + + GeoInfoTable.LAST_USED + + FROM + GeoInfoTable.TABLE_NAME; + String selectLatestGeolocationDate = SELECT + + GeoInfoTable.USER_UUID + ", " + + "MAX(" + GeoInfoTable.LAST_USED + ") as m" + + FROM + GeoInfoTable.TABLE_NAME + + GROUP_BY + GeoInfoTable.USER_UUID; + + String selectPingByGeolocation = SELECT + GeoInfoTable.GEOLOCATION + + ", MIN(" + PingTable.MIN_PING + ") as minPing" + + ", MAX(" + PingTable.MAX_PING + ") as maxPing" + + ", AVG(" + PingTable.AVG_PING + ") as avgPing" + + FROM + "(" + + "(" + selectGeolocations + ") AS q1" + + INNER_JOIN + "(" + selectLatestGeolocationDate + ") AS q2 ON q1.uuid = q2.uuid" + + INNER_JOIN + '(' + selectPingOfServer + ") sp on sp." + PingTable.USER_UUID + "=q1.uuid)" + + WHERE + GeoInfoTable.LAST_USED + "=m" + + AND + "sp." + PingTable.SERVER_UUID + "=?" + + GROUP_BY + GeoInfoTable.GEOLOCATION; + + return new QueryStatement>(selectPingByGeolocation) { + @Override + public void prepare(PreparedStatement statement) throws SQLException { + statement.setString(1, serverUUID.toString()); + } + + @Override + public Map processResults(ResultSet set) throws SQLException { + // TreeMap to sort alphabetically + Map pingByGeolocation = new TreeMap<>(); + while (set.next()) { + Ping ping = new Ping( + 0L, + serverUUID, + set.getInt("minPing"), + set.getInt("maxPing"), + set.getInt("avgPing") + ); + pingByGeolocation.put(set.getString(GeoInfoTable.GEOLOCATION), ping); + } + return pingByGeolocation; + } + }; + } } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/system/json/JSONFactory.java b/Plan/common/src/main/java/com/djrapitops/plan/system/json/JSONFactory.java index 0d711e820..4e589189d 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/system/json/JSONFactory.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/system/json/JSONFactory.java @@ -16,6 +16,7 @@ */ package com.djrapitops.plan.system.json; +import com.djrapitops.plan.data.container.Ping; import com.djrapitops.plan.data.container.PlayerKill; import com.djrapitops.plan.data.container.Session; import com.djrapitops.plan.data.container.TPS; @@ -26,10 +27,7 @@ import com.djrapitops.plan.data.store.objects.DateObj; import com.djrapitops.plan.db.Database; import com.djrapitops.plan.db.access.queries.analysis.PlayerCountQueries; import com.djrapitops.plan.db.access.queries.containers.ServerPlayersTableContainersQuery; -import com.djrapitops.plan.db.access.queries.objects.KillQueries; -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.TPSQueries; +import com.djrapitops.plan.db.access.queries.objects.*; import com.djrapitops.plan.extension.implementation.storage.queries.ExtensionServerPlayerDataTableQuery; import com.djrapitops.plan.system.database.DBSystem; import com.djrapitops.plan.system.info.server.Server; @@ -159,4 +157,21 @@ public class JSONFactory { }); return servers; } + + public List> pingPerGeolocation(UUID serverUUID) { + Map pingByGeolocation = dbSystem.getDatabase().query(PingQueries.fetchPingDataOfServerByGeolocation(serverUUID)); + List> tableEntries = new ArrayList<>(); + for (Map.Entry entry : pingByGeolocation.entrySet()) { + String geolocation = entry.getKey(); + Ping ping = entry.getValue(); + + Map tableEntry = new HashMap<>(); + tableEntry.put("country", geolocation); + tableEntry.put("avg_ping", ping.getAverage()); + tableEntry.put("min_ping", ping.getMin()); + tableEntry.put("max_ping", ping.getMax()); + tableEntries.add(tableEntry); + } + return tableEntries; + } } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/system/webserver/pages/json/RootJSONHandler.java b/Plan/common/src/main/java/com/djrapitops/plan/system/webserver/pages/json/RootJSONHandler.java index e5c87d008..6efc194be 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/system/webserver/pages/json/RootJSONHandler.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/system/webserver/pages/json/RootJSONHandler.java @@ -41,6 +41,7 @@ public class RootJSONHandler extends TreePageHandler { public RootJSONHandler( ResponseFactory responseFactory, Identifiers identifiers, + JSONFactory jsonFactory, GraphsJSONHandler graphsJSONHandler, SessionsJSONHandler sessionsJSONHandler, PlayersTableJSONHandler playersTableJSONHandler, @@ -62,6 +63,7 @@ public class RootJSONHandler extends TreePageHandler { registerPage("players", playersTableJSONHandler); registerPage("sessions", sessionsJSONHandler); registerPage("kills", playerKillsJSONHandler); + registerPage("pingTable", jsonFactory::pingPerGeolocation); registerPage("graph", graphsJSONHandler); registerPage("extensions", pluginTabJSONParser); diff --git a/Plan/common/src/main/resources/assets/plan/web/js/pingTable.js b/Plan/common/src/main/resources/assets/plan/web/js/pingTable.js new file mode 100644 index 000000000..fcf26bb1b --- /dev/null +++ b/Plan/common/src/main/resources/assets/plan/web/js/pingTable.js @@ -0,0 +1,32 @@ +function loadPingTable(json, error) { + pingTable = $("#geolocations").find("#data_ping_table").find("tbody"); + + if (error) { + pingTable.append('Error: ' + error + '---'); + return; + } + + var countries = json; + + if (!countries.length) { + pingTable.append('No Ping Data---'); + return; + } + + var tableHtml = ''; + + for (var i = 0; i < countries.length; i++) { + var country = countries[i]; + tableHtml += createPingTableRow(country); + } + + pingTable.append(tableHtml); +} + +function createPingTableRow(entry) { + return '' + entry.country + + '' + entry.avg_ping + + '' + entry.min_ping + + '' + entry.max_ping + + '' +} \ No newline at end of file diff --git a/Plan/common/src/main/resources/assets/plan/web/js/sessionAccordion.js b/Plan/common/src/main/resources/assets/plan/web/js/sessionAccordion.js index 282176bbf..050737d84 100644 --- a/Plan/common/src/main/resources/assets/plan/web/js/sessionAccordion.js +++ b/Plan/common/src/main/resources/assets/plan/web/js/sessionAccordion.js @@ -1,15 +1,16 @@ function loadSessionAccordion(json, error) { + sessionTable = $("#sessions-overview").find("#tableAccordion").find("tbody"); + if (error) { - $('#sessions-overview').addClass('forbidden'); // TODO Figure out 403 + sessionTable.append('Error: ' + error + '---'); return; } - sessionTable = $("#sessions-overview").find("#tableAccordion").find("tbody"); - var sessions = json.sessions; if (!sessions.length) { - sessionTable.append('No Sessions---') + sessionTable.append('No Sessions---'); + return; } // sessions_per_page can be undefined (-> NaN) or higher than amount of sessions. diff --git a/Plan/common/src/main/resources/assets/plan/web/server.html b/Plan/common/src/main/resources/assets/plan/web/server.html index 6ec5e05ae..9051eb820 100644 --- a/Plan/common/src/main/resources/assets/plan/web/server.html +++ b/Plan/common/src/main/resources/assets/plan/web/server.html @@ -895,7 +895,7 @@ -
+
@@ -939,23 +939,16 @@ Connection Information
- +
- + - - - - - - - - +
Country Average Ping Worst Ping Best Ping Worst Ping
Local Machine---
@@ -1365,6 +1358,7 @@ + @@ -1670,6 +1664,7 @@ } }); jsonRequest("../v1/kills?server=${serverName}", loadPlayerKills); + jsonRequest("../v1/pingTable?server=${serverName}", loadPingTable); $('.server-name').text('${serverName}'); $('.player-plugin-table').DataTable({