Refactored TPSTable#getPlayersOnlineForServers to a query

This commit is contained in:
Rsl1122 2019-02-16 14:09:01 +02:00
parent 6eafce05ba
commit ba41952bee
4 changed files with 47 additions and 54 deletions

View File

@ -25,6 +25,7 @@ import com.djrapitops.plan.data.store.mutators.TPSMutator;
import com.djrapitops.plan.data.store.mutators.health.NetworkHealthInformation;
import com.djrapitops.plan.db.Database;
import com.djrapitops.plan.db.access.queries.ServerAggregateQueries;
import com.djrapitops.plan.db.access.queries.objects.TPSQueries;
import com.djrapitops.plan.system.database.DBSystem;
import com.djrapitops.plan.system.info.server.Server;
import com.djrapitops.plan.system.info.server.properties.ServerProperties;
@ -99,9 +100,9 @@ public class NetworkContainer extends DynamicDataContainer {
}
private void addServerBoxes() {
putSupplier(NetworkKeys.NETWORK_PLAYER_ONLINE_DATA, () -> database.fetch().getPlayersOnlineForServers(
putSupplier(NetworkKeys.NETWORK_PLAYER_ONLINE_DATA, () -> database.query(TPSQueries.fetchPlayerOnlineDataOfServers(
getValue(NetworkKeys.BUKKIT_SERVERS).orElse(new ArrayList<>()))
);
));
putSupplier(NetworkKeys.SERVERS_TAB, () -> {
StringBuilder serverBoxes = new StringBuilder();
Map<Integer, List<TPS>> playersOnlineData = getValue(NetworkKeys.NETWORK_PLAYER_ONLINE_DATA).orElse(new HashMap<>());

View File

@ -19,17 +19,20 @@ package com.djrapitops.plan.db.access.queries.objects;
import com.djrapitops.plan.data.container.TPS;
import com.djrapitops.plan.data.container.builders.TPSBuilder;
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.parsing.Select;
import com.djrapitops.plan.db.sql.tables.ServerTable;
import com.djrapitops.plan.system.info.server.Server;
import com.djrapitops.plugin.api.TimeAmount;
import org.apache.commons.text.TextStringBuilder;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.*;
import static com.djrapitops.plan.db.sql.parsing.Sql.*;
import static com.djrapitops.plan.db.sql.tables.TPSTable.*;
/**
@ -77,4 +80,41 @@ public class TPSQueries {
};
}
public static Query<Map<Integer, List<TPS>>> fetchPlayerOnlineDataOfServers(Collection<Server> servers) {
if (servers.isEmpty()) {
return db -> new HashMap<>();
}
TextStringBuilder sql = new TextStringBuilder(SELECT);
sql.append(SERVER_ID).append(", ")
.append(DATE).append(", ")
.append(PLAYERS_ONLINE)
.append(FROM).append(TABLE_NAME)
.append(WHERE).append(DATE).append(">").append(System.currentTimeMillis() - TimeAmount.WEEK.toMillis(2L))
.append(AND).append("(");
sql.appendWithSeparators(servers.stream().map(server -> SERVER_ID + "=" + server.getId()).iterator(), " OR ");
sql.append(")");
return new QueryAllStatement<Map<Integer, List<TPS>>>(sql.toString(), 10000) {
@Override
public Map<Integer, List<TPS>> processResults(ResultSet set) throws SQLException {
Map<Integer, List<TPS>> map = new HashMap<>();
while (set.next()) {
int serverID = set.getInt(SERVER_ID);
int playersOnline = set.getInt(PLAYERS_ONLINE);
long date = set.getLong(DATE);
List<TPS> tpsList = map.getOrDefault(serverID, new ArrayList<>());
TPS tps = TPSBuilder.get().date(date)
.playersOnline(playersOnline)
.toTPS();
tpsList.add(tps);
map.put(serverID, tpsList);
}
return map;
}
};
}
}

View File

@ -16,20 +16,10 @@
*/
package com.djrapitops.plan.db.sql.tables;
import com.djrapitops.plan.data.container.TPS;
import com.djrapitops.plan.data.container.builders.TPSBuilder;
import com.djrapitops.plan.db.DBType;
import com.djrapitops.plan.db.SQLDB;
import com.djrapitops.plan.db.access.QueryAllStatement;
import com.djrapitops.plan.db.sql.parsing.CreateTableParser;
import com.djrapitops.plan.db.sql.parsing.Sql;
import com.djrapitops.plan.system.info.server.Server;
import com.djrapitops.plugin.api.TimeAmount;
import org.apache.commons.text.TextStringBuilder;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
/**
* Table that is in charge of storing TPS, Players Online and Performance data.
@ -84,42 +74,4 @@ public class TPSTable extends Table {
.foreignKey(SERVER_ID, ServerTable.TABLE_NAME, ServerTable.SERVER_ID)
.toString();
}
public Map<Integer, List<TPS>> getPlayersOnlineForServers(Collection<Server> servers) {
if (servers.isEmpty()) {
return new HashMap<>();
}
TextStringBuilder sql = new TextStringBuilder("SELECT ");
sql.append(SERVER_ID).append(", ")
.append(DATE).append(", ")
.append(PLAYERS_ONLINE)
.append(" FROM ").append(TABLE_NAME)
.append(" WHERE ")
.append(DATE).append(">").append(System.currentTimeMillis() - TimeAmount.WEEK.toMillis(2L))
.append(" AND (");
sql.appendWithSeparators(servers.stream().map(server -> SERVER_ID + "=" + server.getId()).iterator(), " OR ");
sql.append(")");
return query(new QueryAllStatement<Map<Integer, List<TPS>>>(sql.toString(), 10000) {
@Override
public Map<Integer, List<TPS>> processResults(ResultSet set) throws SQLException {
Map<Integer, List<TPS>> map = new HashMap<>();
while (set.next()) {
int serverID = set.getInt(SERVER_ID);
int playersOnline = set.getInt(PLAYERS_ONLINE);
long date = set.getLong(DATE);
List<TPS> tpsList = map.getOrDefault(serverID, new ArrayList<>());
TPS tps = TPSBuilder.get().date(date)
.playersOnline(playersOnline)
.toTPS();
tpsList.add(tps);
map.put(serverID, tpsList);
}
return map;
}
});
}
}

View File

@ -175,7 +175,7 @@ public class SQLFetchOps extends SQLOps implements FetchOperations {
@Override
public Map<Integer, List<TPS>> getPlayersOnlineForServers(Collection<Server> servers) {
return tpsTable.getPlayersOnlineForServers(servers);
return db.query(TPSQueries.fetchPlayerOnlineDataOfServers(servers));
}
@Override