From db0a8359b93355ddc3cc4cda91535966e37a6692 Mon Sep 17 00:00:00 2001 From: Risto Lahtela <24460436+Rsl1122@users.noreply.github.com> Date: Wed, 3 Feb 2021 19:38:53 +0200 Subject: [PATCH] Fixed optimized performance graph issues with gap on Because of the new resolution reduction the gap algorithm was not advanced enough to differentiate between lower resolution and missing data. This was fixed by adding 3 different Gap strategies for the different resolutions --- .../delivery/domain/mutators/TPSMutator.java | 17 +++++--- .../json/graphs/GraphJSONCreator.java | 37 ++++++++++++++-- .../database/queries/objects/TPSQueries.java | 43 +++---------------- 3 files changed, 50 insertions(+), 47 deletions(-) diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/mutators/TPSMutator.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/mutators/TPSMutator.java index aa9ccf1b6..9d76f6bd3 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/mutators/TPSMutator.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/mutators/TPSMutator.java @@ -18,12 +18,14 @@ package com.djrapitops.plan.delivery.domain.mutators; import com.djrapitops.plan.delivery.domain.container.DataContainer; import com.djrapitops.plan.delivery.domain.keys.ServerKeys; +import com.djrapitops.plan.delivery.rendering.json.graphs.line.LineGraph; import com.djrapitops.plan.delivery.rendering.json.graphs.line.Point; import com.djrapitops.plan.gathering.domain.TPS; import com.djrapitops.plan.utilities.comparators.TPSComparator; import com.djrapitops.plan.utilities.java.Lists; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Optional; import java.util.concurrent.TimeUnit; @@ -250,28 +252,29 @@ public class TPSMutator { return Optional.of(tpsData.get(tpsData.size() - 1)); } - public Number[][] toArrays(boolean displayGaps) { + public List toArrays(LineGraph.GapStrategy gapStrategy) { List arrays = new ArrayList<>(); Long lastX = null; for (TPS tps : tpsData) { long date = tps.getDate(); - if (displayGaps && lastX != null && date - lastX > TimeUnit.MINUTES.toMillis(3L)) { - addMissingPoints(arrays, lastX, date); + if (gapStrategy.fillGaps && lastX != null && date - lastX > gapStrategy.acceptableGapMs) { + addMissingPoints(arrays, lastX, date, gapStrategy); } lastX = date; arrays.add(tps.toArray()); } - return arrays.toArray(new Number[0][]); + return arrays; } - private void addMissingPoints(List arrays, Long lastX, long date) { - long iterate = lastX + TimeUnit.MINUTES.toMillis(1L); + private void addMissingPoints(List arrays, Long lastX, long date, LineGraph.GapStrategy gapStrategy) { + long iterate = lastX + gapStrategy.diffToFirstGapPointMs; while (iterate < date) { Number[] entry = new Number[7]; + if (gapStrategy.fillWith != null) Arrays.fill(entry, gapStrategy.fillWith); entry[0] = iterate; arrays.add(entry); - iterate += TimeUnit.MINUTES.toMillis(30L); + iterate += gapStrategy.fillFrequencyMs; } } } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/json/graphs/GraphJSONCreator.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/json/graphs/GraphJSONCreator.java index 3986b6143..10996cac0 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/json/graphs/GraphJSONCreator.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/json/graphs/GraphJSONCreator.java @@ -79,9 +79,11 @@ public class GraphJSONCreator { } public String performanceGraphJSON(UUID serverUUID) { + long now = System.currentTimeMillis(); Database db = dbSystem.getDatabase(); LineGraphFactory lineGraphs = graphs.line(); - TPSMutator tpsMutator = new TPSMutator(db.query(TPSQueries.fetchTPSDataOfServer(serverUUID))); + long halfYearAgo = now - TimeUnit.DAYS.toMillis(180); + TPSMutator tpsMutator = new TPSMutator(db.query(TPSQueries.fetchTPSDataOfServer(halfYearAgo, now, serverUUID))); return '{' + "\"playersOnline\":" + lineGraphs.playersOnlineGraph(tpsMutator).toHighChartsSeries() + ",\"tps\":" + lineGraphs.tpsGraph(tpsMutator).toHighChartsSeries() + @@ -108,9 +110,38 @@ public class GraphJSONCreator { } public Map optimizedPerformanceGraphJSON(UUID serverUUID) { + long now = System.currentTimeMillis(); + long twoMonthsAgo = now - TimeUnit.DAYS.toMillis(60); + long monthAgo = now - TimeUnit.DAYS.toMillis(30); + + long lowestResolution = TimeUnit.MINUTES.toMillis(20); + long lowResolution = TimeUnit.MINUTES.toMillis(5); Database db = dbSystem.getDatabase(); - TPSMutator tpsMutator = new TPSMutator(db.query(TPSQueries.fetchTPSDataOfServer(serverUUID))); - Number[][] values = tpsMutator.toArrays(config.isTrue(DisplaySettings.GAPS_IN_GRAPH_DATA)); + TPSMutator lowestResolutionData = new TPSMutator(db.query(TPSQueries.fetchTPSDataOfServerInResolution(0, twoMonthsAgo, lowestResolution, serverUUID))); + TPSMutator lowResolutionData = new TPSMutator(db.query(TPSQueries.fetchTPSDataOfServerInResolution(twoMonthsAgo, monthAgo, lowResolution, serverUUID))); + TPSMutator highResolutionData = new TPSMutator(db.query(TPSQueries.fetchTPSDataOfServer(monthAgo, now, serverUUID))); + + List values = lowestResolutionData.toArrays(new LineGraph.GapStrategy( + config.isTrue(DisplaySettings.GAPS_IN_GRAPH_DATA), + lowestResolution + TimeUnit.MINUTES.toMillis(1), + TimeUnit.MINUTES.toMillis(1), + TimeUnit.MINUTES.toMillis(30), + null + )); + values.addAll(lowResolutionData.toArrays(new LineGraph.GapStrategy( + config.isTrue(DisplaySettings.GAPS_IN_GRAPH_DATA), + lowResolution + TimeUnit.MINUTES.toMillis(1), + TimeUnit.MINUTES.toMillis(1), + TimeUnit.MINUTES.toMillis(30), + null + ))); + values.addAll(highResolutionData.toArrays(new LineGraph.GapStrategy( + config.isTrue(DisplaySettings.GAPS_IN_GRAPH_DATA), + TimeUnit.MINUTES.toMillis(3), + TimeUnit.MINUTES.toMillis(1), + TimeUnit.MINUTES.toMillis(30), + null + ))); return Maps.builder(String.class, Object.class) .put("keys", new String[]{"date", "playersOnline", "tps", "cpu", "ram", "entities", "chunks", "disk"}) diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/objects/TPSQueries.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/objects/TPSQueries.java index 9b6dbf09f..84f762cd5 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/objects/TPSQueries.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/objects/TPSQueries.java @@ -44,22 +44,9 @@ public class TPSQueries { /* Static method class */ } - public static Query> fetchTPSDataOfServer(UUID serverUUID) { + public static Query> fetchTPSDataOfServerInResolution(long after, long before, long resolution, UUID serverUUID) { return db -> { - String selectLowestResolution = SELECT + - min("t." + DATE) + " as " + DATE + ',' + - min("t." + TPS) + " as " + TPS + ',' + - max("t." + PLAYERS_ONLINE) + " as " + PLAYERS_ONLINE + ',' + - max("t." + RAM_USAGE) + " as " + RAM_USAGE + ',' + - max("t." + CPU_USAGE) + " as " + CPU_USAGE + ',' + - max("t." + ENTITIES) + " as " + ENTITIES + ',' + - max("t." + CHUNKS) + " as " + CHUNKS + ',' + - max("t." + FREE_DISK) + " as " + FREE_DISK + - FROM + TABLE_NAME + " t" + - WHERE + SERVER_ID + "=" + ServerTable.STATEMENT_SELECT_SERVER_ID + - AND + DATE + "=?" + AND + DATE + "=?"; - - String sql = selectLowestResolution + - UNION + selectLowerResolution + - UNION + selectNormalResolution + + GROUP_BY + floor(DATE + "/?") + ORDER_BY + DATE; return db.query(new QueryStatement>(sql, 50000) { @Override public void prepare(PreparedStatement statement) throws SQLException { - long now = System.currentTimeMillis(); - long lowestResolution = TimeUnit.MINUTES.toMillis(20); - long lowResolution = TimeUnit.MINUTES.toMillis(5); statement.setString(1, serverUUID.toString()); - statement.setLong(2, now - TimeUnit.DAYS.toMillis(60)); - statement.setLong(3, lowestResolution); - statement.setString(4, serverUUID.toString()); - statement.setLong(5, now - TimeUnit.DAYS.toMillis(60)); - statement.setLong(6, now - TimeUnit.DAYS.toMillis(30)); - statement.setLong(7, lowResolution); - statement.setString(8, serverUUID.toString()); - statement.setLong(9, now - TimeUnit.DAYS.toMillis(30)); + statement.setLong(2, after); + statement.setLong(3, before); + statement.setLong(4, resolution); } @Override