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
This commit is contained in:
Risto Lahtela 2021-02-03 19:38:53 +02:00
parent 8f6c286d9c
commit db0a8359b9
3 changed files with 50 additions and 47 deletions

View File

@ -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<Number[]> toArrays(LineGraph.GapStrategy gapStrategy) {
List<Number[]> 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<Number[]> arrays, Long lastX, long date) {
long iterate = lastX + TimeUnit.MINUTES.toMillis(1L);
private void addMissingPoints(List<Number[]> 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;
}
}
}

View File

@ -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<String, Object> 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<Number[]> 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"})

View File

@ -44,22 +44,9 @@ public class TPSQueries {
/* Static method class */
}
public static Query<List<TPS>> fetchTPSDataOfServer(UUID serverUUID) {
public static Query<List<TPS>> 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 + "<?" +
GROUP_BY + floor(DATE + "/?");
String selectLowerResolution = SELECT +
String sql = SELECT +
min("t." + DATE) + " as " + DATE + ',' +
min("t." + TPS) + " as " + TPS + ',' +
max("t." + PLAYERS_ONLINE) + " as " + PLAYERS_ONLINE + ',' +
@ -72,34 +59,16 @@ public class TPSQueries {
WHERE + SERVER_ID + "=" + ServerTable.STATEMENT_SELECT_SERVER_ID +
AND + DATE + ">=?" +
AND + DATE + "<?" +
GROUP_BY + floor(DATE + "/?");
String selectNormalResolution = SELECT +
DATE + ',' + TPS + ',' + PLAYERS_ONLINE + ',' +
RAM_USAGE + ',' + CPU_USAGE + ',' + ENTITIES + ',' + CHUNKS + ',' + FREE_DISK +
FROM + TABLE_NAME +
WHERE + SERVER_ID + "=" + ServerTable.STATEMENT_SELECT_SERVER_ID +
AND + DATE + ">=?";
String sql = selectLowestResolution +
UNION + selectLowerResolution +
UNION + selectNormalResolution +
GROUP_BY + floor(DATE + "/?") +
ORDER_BY + DATE;
return db.query(new QueryStatement<List<TPS>>(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