Implemented Ping graph endpoint

This commit is contained in:
Rsl1122 2019-07-13 11:28:59 +03:00
parent 524e598664
commit 2d5890ff69
7 changed files with 109 additions and 9 deletions

View File

@ -143,4 +143,47 @@ public class PingQueries {
}
};
}
public static Query<List<Ping>> fetchPingDataOfServer(long after, long before, UUID serverUUID) {
String sql = SELECT +
PingTable.DATE + ", " +
PingTable.MAX_PING + ", " +
PingTable.MIN_PING + ", " +
PingTable.AVG_PING + ", " +
PingTable.USER_UUID + ", " +
PingTable.SERVER_UUID +
FROM + PingTable.TABLE_NAME +
WHERE + PingTable.SERVER_UUID + "=?" +
AND + PingTable.DATE + ">=?" +
AND + PingTable.DATE + "<=?";
return new QueryStatement<List<Ping>>(sql, 1000) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, serverUUID.toString());
statement.setLong(2, after);
statement.setLong(3, before);
}
@Override
public List<Ping> processResults(ResultSet set) throws SQLException {
List<Ping> pings = new ArrayList<>();
while (set.next()) {
UUID uuid = UUID.fromString(set.getString(PingTable.USER_UUID));
UUID serverUUID = UUID.fromString(set.getString(PingTable.SERVER_UUID));
long date = set.getLong(PingTable.DATE);
double avgPing = set.getDouble(PingTable.AVG_PING);
int minPing = set.getInt(PingTable.MIN_PING);
int maxPing = set.getInt(PingTable.MAX_PING);
pings.add(new Ping(date, serverUUID,
minPing,
maxPing,
avgPing));
}
return pings;
}
};
}
}

View File

@ -16,25 +16,21 @@
*/
package com.djrapitops.plan.system.json;
import com.djrapitops.plan.data.store.mutators.MutatorFunctions;
import com.djrapitops.plan.data.store.mutators.PlayersMutator;
import com.djrapitops.plan.data.store.mutators.SessionsMutator;
import com.djrapitops.plan.data.store.mutators.TPSMutator;
import com.djrapitops.plan.data.container.Ping;
import com.djrapitops.plan.data.store.mutators.*;
import com.djrapitops.plan.data.store.objects.DateMap;
import com.djrapitops.plan.data.time.WorldTimes;
import com.djrapitops.plan.db.Database;
import com.djrapitops.plan.db.access.queries.analysis.ActivityIndexQueries;
import com.djrapitops.plan.db.access.queries.containers.ServerPlayerContainersQuery;
import com.djrapitops.plan.db.access.queries.objects.GeoInfoQueries;
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.WorldTimesQueries;
import com.djrapitops.plan.db.access.queries.objects.*;
import com.djrapitops.plan.system.database.DBSystem;
import com.djrapitops.plan.system.settings.config.PlanConfig;
import com.djrapitops.plan.system.settings.paths.TimeSettings;
import com.djrapitops.plan.utilities.html.graphs.Graphs;
import com.djrapitops.plan.utilities.html.graphs.bar.BarGraph;
import com.djrapitops.plan.utilities.html.graphs.line.LineGraphFactory;
import com.djrapitops.plan.utilities.html.graphs.line.PingGraph;
import com.djrapitops.plan.utilities.html.graphs.pie.Pie;
import com.djrapitops.plan.utilities.html.graphs.pie.WorldPie;
import com.djrapitops.plan.utilities.html.graphs.special.WorldMap;
@ -161,4 +157,16 @@ public class GraphJSONParser {
dataMap.put("geolocation_bar_series", geolocationBarGraph.getBars());
return dataMap;
}
public String pingGraphsJSON(UUID serverUUID) {
Database db = dbSystem.getDatabase();
long now = System.currentTimeMillis();
List<Ping> pings = db.query(PingQueries.fetchPingDataOfServer(now - TimeUnit.DAYS.toMillis(180L), now, serverUUID));
PingGraph pingGraph = graphs.line().pingGraph(new PingMutator(pings).mutateToByMinutePings().all());// TODO Optimize in query
return "{\"min_ping_series\":" + pingGraph.getMinGraph().toHighChartsSeries() +
",\"avg_ping_series\":" + pingGraph.getAvgGraph().toHighChartsSeries() +
",\"max_ping_series\":" + pingGraph.getMaxGraph().toHighChartsSeries() + '}';
}
}

View File

@ -74,6 +74,8 @@ public class GraphsJSONHandler implements PageHandler {
return new JSONResponse<>(graphJSON.activityGraphsJSONAsMap(serverUUID));
case "geolocation":
return new JSONResponse<>(graphJSON.geolocationGraphsJSONAsMap(serverUUID));
case "ping":
return new JSONResponse(graphJSON.pingGraphsJSON(serverUUID));
default:
throw new BadRequestException("unknown 'type' parameter: " + type);
}

View File

@ -63,4 +63,16 @@ public class PingGraph {
public String toAvgSeries() {
return avgGraph.toHighChartsSeries();
}
public LineGraph getMaxGraph() {
return maxGraph;
}
public LineGraph getMinGraph() {
return minGraph;
}
public LineGraph getAvgGraph() {
return avgGraph;
}
}

View File

@ -62,4 +62,8 @@ public class Point {
"x=" + x + ", " +
"y=" + y + '}';
}
public double[] toArray() {
return new double[]{x, y};
}
}

View File

@ -1623,6 +1623,36 @@
$('#diskGraph').text("Failed to load graph data: " + error);
}
});
jsonRequest("../v1/graph?type=ping&serverName=${serverName}", function (json, error) {
if (json) {
var series = {
avgPing: {
name: s.name.avgPing,
type: s.type.spline,
tooltip: s.tooltip.twoDecimals,
data: json.avg_ping_series,
color: v.colors.avgPing
},
maxPing: {
name: s.name.maxPing,
type: s.type.spline,
tooltip: s.tooltip.zeroDecimals,
data: json.max_ping_series,
color: v.colors.maxPing
},
minPing: {
name: s.name.minPing,
type: s.type.spline,
tooltip: s.tooltip.zeroDecimals,
data: json.min_ping_series,
color: v.colors.minPing
}
};
lineChart('pingGraph', [series.avgPing, series.maxPing, series.minPing]);
} else if (error) {
$('#pingGraph').text("Failed to load graph data: " + error);
}
});
jsonRequest("../v1/graph?type=worldPie&serverName=${serverName}", function (json, error) {
if (data) {

View File

@ -88,4 +88,5 @@ Type | Description
`calendar` | Calendar data points for each day there is data for. Last 2 years.
`worldPie` | World Pie data of all sessions combined
`activity` | Activity stack graph and pie graph data
`geolocation` | World Map and bar graph data. World Map uses iso-a3 specification of Country codes and Bar graph uses country names.
`geolocation` | World Map and bar graph data. World Map uses iso-a3 specification of Country codes and Bar graph uses country names.
`ping` | Min, Avg, Max ping graph data.