Made Ping store min, max, average to make it more useful

This commit is contained in:
Rsl1122 2018-07-16 11:25:46 +03:00
parent c9b4cae8e6
commit ab768c3f4b
6 changed files with 114 additions and 25 deletions

View File

@ -4,20 +4,34 @@ import com.djrapitops.plan.data.store.objects.DateObj;
import java.util.UUID;
public class Ping extends DateObj<Integer> {
public class Ping extends DateObj<Double> {
private UUID serverUUID;
private final UUID serverUUID;
private final double average;
private final int min;
private final int max;
public Ping(long date, Integer value, UUID serverUUID) {
super(date, value);
public Ping(long date, UUID serverUUID, int min, int max, double average) {
super(date, average);
this.serverUUID = serverUUID;
}
public Ping(long date, Integer value) {
super(date, value);
this.average = average;
this.min = min;
this.max = max;
}
public UUID getServerUUID() {
return serverUUID;
}
public double getAverage() {
return average;
}
public int getMin() {
return min;
}
public int getMax() {
return max;
}
}

View File

@ -6,6 +6,7 @@ import com.djrapitops.plan.data.store.keys.CommonKeys;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public class PingMutator {
@ -18,4 +19,22 @@ public class PingMutator {
public static PingMutator forContainer(DataContainer container) {
return new PingMutator(container.getValue(CommonKeys.PING).orElse(new ArrayList<>()));
}
public Optional<Ping> max() {
Ping maxPing = null;
int max = 0;
for (Ping ping : pings) {
Integer value = ping.getMax();
if (value > max) {
max = value;
maxPing = ping;
}
}
return Optional.ofNullable(maxPing);
}
public double average() {
return pings.stream().mapToDouble(Ping::getAverage).average().orElse(-1);
}
}

View File

@ -1,6 +1,7 @@
package com.djrapitops.plan.data.store.mutators;
import com.djrapitops.plan.data.container.GeoInfo;
import com.djrapitops.plan.data.container.Ping;
import com.djrapitops.plan.data.container.Session;
import com.djrapitops.plan.data.store.containers.DataContainer;
import com.djrapitops.plan.data.store.containers.PlayerContainer;
@ -108,6 +109,25 @@ public class PlayersMutator {
return geolocations;
}
public Map<String, List<Ping>> getPingPerCountry(UUID serverUUID) {
Map<String, List<Ping>> pingPerCountry = new HashMap<>();
for (PlayerContainer player : players) {
Optional<GeoInfo> mostRecent = GeoInfoMutator.forContainer(player).mostRecent();
if (!mostRecent.isPresent()) {
continue;
}
List<Ping> pings = player.getValue(PlayerKeys.PING).orElse(new ArrayList<>());
String country = mostRecent.get().getGeolocation();
List<Ping> countryPings = pingPerCountry.getOrDefault(country, new ArrayList<>());
pings.stream()
.filter(ping -> ping.getServerUUID().equals(serverUUID))
.forEach(countryPings::add);
pingPerCountry.put(country, countryPings);
}
return pingPerCountry;
}
public TreeMap<Long, Map<String, Set<UUID>>> toActivityDataMap(long date) {
TreeMap<Long, Map<String, Set<UUID>>> activityData = new TreeMap<>();
for (long time = date; time >= date - TimeAmount.MONTH.ms() * 2L; time -= TimeAmount.WEEK.ms()) {

View File

@ -30,7 +30,9 @@ public class PingTable extends UserIDTable {
Col.USER_ID + ", " +
Col.SERVER_ID + ", " +
Col.DATE + ", " +
Col.MAX_PING +
Col.MIN_PING + ", " +
Col.MAX_PING + ", " +
Col.AVG_PING +
") VALUES (" +
usersTable.statementSelectID + ", " +
serverTable.statementSelectServerID + ", ?, ?)";
@ -44,6 +46,8 @@ public class PingTable extends UserIDTable {
.column(Col.SERVER_ID, Sql.INT).notNull()
.column(Col.DATE, Sql.LONG).notNull()
.column(Col.MAX_PING, Sql.INT).notNull()
.column(Col.MIN_PING, Sql.INT).notNull()
.column(Col.AVG_PING, Sql.DOUBLE).notNull()
.primaryKey(usingMySQL, Col.ID)
.foreignKey(Col.USER_ID, usersTable.getTableName(), UsersTable.Col.ID)
.foreignKey(Col.SERVER_ID, ServerTable.TABLE_NAME, ServerTable.Col.SERVER_ID)
@ -70,7 +74,9 @@ public class PingTable extends UserIDTable {
statement.setString(1, uuid.toString());
statement.setString(2, ServerInfo.getServerUUID().toString());
statement.setLong(3, ping.getDate());
statement.setInt(4, ping.getValue());
statement.setInt(4, ping.getMin());
statement.setInt(5, ping.getMax());
statement.setDouble(6, ping.getAverage());
}
});
}
@ -92,9 +98,13 @@ public class PingTable extends UserIDTable {
while (set.next()) {
pings.add(new Ping(
set.getLong(Col.DATE.get()),
set.getInt(Col.MAX_PING.get()),
serverUUIDs.get(set.getInt(Col.SERVER_ID.get()))));
set.getLong(Col.DATE.get()),
serverUUIDs.get(set.getInt(Col.SERVER_ID.get())),
set.getInt(Col.MIN_PING.get()),
set.getInt(Col.MAX_PING.get()),
set.getDouble(Col.AVG_PING.get())
)
);
}
return pings;
@ -110,6 +120,8 @@ public class PingTable extends UserIDTable {
String sql = "SELECT " +
Col.DATE + ", " +
Col.MAX_PING + ", " +
Col.MIN_PING + ", " +
Col.AVG_PING + ", " +
usersUUIDColumn + ", " +
serverUUIDColumn +
" FROM " + tableName +
@ -124,10 +136,15 @@ public class PingTable extends UserIDTable {
UUID uuid = UUID.fromString(set.getString("uuid"));
UUID serverUUID = UUID.fromString(set.getString("s_uuid"));
long date = set.getLong(Col.DATE.get());
double avgPing = set.getDouble(Col.AVG_PING.get());
int minPing = set.getInt(Col.MIN_PING.get());
int maxPing = set.getInt(Col.MAX_PING.get());
List<Ping> pings = userPings.getOrDefault(uuid, new ArrayList<>());
pings.add(new Ping(date, maxPing, serverUUID));
pings.add(new Ping(date, serverUUID,
minPing,
maxPing,
avgPing));
userPings.put(uuid, pings);
}
@ -146,12 +163,16 @@ public class PingTable extends UserIDTable {
for (Ping ping : pings) {
UUID serverUUID = ping.getServerUUID();
long date = ping.getDate();
int maxPing = ping.getValue();
int minPing = ping.getMin();
int maxPing = ping.getMax();
double avgPing = ping.getAverage();
statement.setString(1, uuid.toString());
statement.setString(2, serverUUID.toString());
statement.setLong(3, date);
statement.setInt(4, maxPing);
statement.setInt(4, minPing);
statement.setInt(5, maxPing);
statement.setDouble(6, avgPing);
statement.addBatch();
}
}
@ -164,7 +185,9 @@ public class PingTable extends UserIDTable {
USER_ID(UserIDTable.Col.USER_ID.get()),
SERVER_ID("server_id"),
DATE("date"),
MAX_PING("max_ping");
MAX_PING("max_ping"),
AVG_PING("avg_ping"),
MIN_PING("min_ping");
private final String name;

View File

@ -15,7 +15,7 @@ import java.util.OptionalInt;
import java.util.UUID;
/**
* Processes 60s average of a Ping list.
* Processes 60s values of a Ping list.
* <p>
* Ping list contains 30 values as ping is updated every 2 seconds.
*
@ -24,16 +24,16 @@ import java.util.UUID;
public class PingInsertProcessor implements CriticalRunnable {
private final UUID uuid;
private final List<Ping> pingList;
private final List<DateObj<Integer>> pingList;
public PingInsertProcessor(UUID uuid, List<Ping> pingList) {
public PingInsertProcessor(UUID uuid, List<DateObj<Integer>> pingList) {
this.uuid = uuid;
this.pingList = pingList;
}
@Override
public void run() {
List<Ping> history = pingList;
List<DateObj<Integer>> history = pingList;
long lastDate = history.get(history.size() - 1).getDate();
OptionalInt max = history.stream()
.mapToInt(DateObj::getValue)
@ -44,9 +44,22 @@ public class PingInsertProcessor implements CriticalRunnable {
return;
}
int minValue = history.stream()
.mapToInt(DateObj::getValue)
.filter(i -> i != -1)
.min().orElse(-1);
double avgValue = history.stream()
.mapToInt(DateObj::getValue)
.filter(i -> i != -1)
.average().orElse(-1);
int maxValue = max.getAsInt();
Ping ping = new Ping(lastDate, maxValue, ServerInfo.getServerUUID());
Ping ping = new Ping(lastDate, ServerInfo.getServerUUID(),
minValue,
maxValue,
avgValue);
Database.getActive().save().ping(uuid, ping);
}

View File

@ -23,7 +23,7 @@
*/
package com.djrapitops.plan.system.tasks.server;
import com.djrapitops.plan.data.container.Ping;
import com.djrapitops.plan.data.store.objects.DateObj;
import com.djrapitops.plan.system.processing.Processing;
import com.djrapitops.plan.system.processing.processors.player.PingInsertProcessor;
import com.djrapitops.plan.utilities.java.Reflection;
@ -86,7 +86,7 @@ public class PingCountTimer extends AbsRunnable implements Listener {
pingField = localPing;
}
private final Map<UUID, List<Ping>> playerHistory = new HashMap<>();
private final Map<UUID, List<DateObj<Integer>>> playerHistory = new HashMap<>();
private static boolean isPingMethodAvailable() {
try {
@ -106,7 +106,7 @@ public class PingCountTimer extends AbsRunnable implements Listener {
Player player = Bukkit.getPlayer(uuid);
if (player != null) {
int ping = getPing(player);
history.add(new Ping(time, ping));
history.add(new DateObj<>(time, ping));
if (history.size() >= 30) {
Processing.submit(new PingInsertProcessor(uuid, new ArrayList<>(history)));
history.clear();