mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2024-12-25 10:37:51 +01:00
Added ping to player page, started work on a test database creator
This commit is contained in:
parent
2c9f89eb50
commit
a40c10f6bb
@ -18,6 +18,7 @@ import java.util.Objects;
|
||||
*/
|
||||
public class SQLiteDB extends SQLDB {
|
||||
|
||||
private final File databaseFile;
|
||||
private final String dbName;
|
||||
private Connection connection;
|
||||
private ITask connectionPingTask;
|
||||
@ -30,7 +31,12 @@ public class SQLiteDB extends SQLDB {
|
||||
}
|
||||
|
||||
public SQLiteDB(String dbName) {
|
||||
this.dbName = dbName;
|
||||
this(new File(PlanPlugin.getInstance().getDataFolder(), dbName + ".db"));
|
||||
}
|
||||
|
||||
public SQLiteDB(File databaseFile) {
|
||||
dbName = databaseFile.getName();
|
||||
this.databaseFile = databaseFile;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -39,14 +45,14 @@ public class SQLiteDB extends SQLDB {
|
||||
@Override
|
||||
public void setupDataSource() throws DBInitException {
|
||||
try {
|
||||
connection = getNewConnection(dbName);
|
||||
connection = getNewConnection(databaseFile);
|
||||
} catch (SQLException e) {
|
||||
throw new DBInitException(e);
|
||||
}
|
||||
startConnectionPingTask();
|
||||
}
|
||||
|
||||
public Connection getNewConnection(String dbName) throws SQLException {
|
||||
public Connection getNewConnection(File dbFile) throws SQLException {
|
||||
try {
|
||||
Class.forName("org.sqlite.JDBC");
|
||||
} catch (ClassNotFoundException e) {
|
||||
@ -54,7 +60,7 @@ public class SQLiteDB extends SQLDB {
|
||||
return null; // Should never happen.
|
||||
}
|
||||
|
||||
String dbFilePath = new File(PlanPlugin.getInstance().getDataFolder(), dbName + ".db").getAbsolutePath();
|
||||
String dbFilePath = dbFile.getAbsolutePath();
|
||||
|
||||
Connection newConnection = getConnectionFor(dbFilePath);
|
||||
Log.debug("SQLite " + dbName + ": Opened a new Connection");
|
||||
@ -88,7 +94,7 @@ public class SQLiteDB extends SQLDB {
|
||||
} catch (SQLException e) {
|
||||
Log.debug("Something went wrong during Ping task.");
|
||||
try {
|
||||
connection = getNewConnection(dbName);
|
||||
connection = getNewConnection(databaseFile);
|
||||
} catch (SQLException e1) {
|
||||
Log.toLog(this.getClass(), e1);
|
||||
Log.error("SQLite connection maintaining task had to be closed due to exception.");
|
||||
@ -123,7 +129,7 @@ public class SQLiteDB extends SQLDB {
|
||||
@Override
|
||||
public Connection getConnection() throws SQLException {
|
||||
if (connection == null) {
|
||||
connection = getNewConnection(dbName);
|
||||
connection = getNewConnection(databaseFile);
|
||||
}
|
||||
return connection;
|
||||
}
|
||||
|
@ -9,10 +9,7 @@ import com.djrapitops.plan.data.container.Session;
|
||||
import com.djrapitops.plan.data.store.containers.PerServerContainer;
|
||||
import com.djrapitops.plan.data.store.containers.PlayerContainer;
|
||||
import com.djrapitops.plan.data.store.keys.PlayerKeys;
|
||||
import com.djrapitops.plan.data.store.mutators.ActivityIndex;
|
||||
import com.djrapitops.plan.data.store.mutators.PerServerMutator;
|
||||
import com.djrapitops.plan.data.store.mutators.PvpInfoMutator;
|
||||
import com.djrapitops.plan.data.store.mutators.SessionsMutator;
|
||||
import com.djrapitops.plan.data.store.mutators.*;
|
||||
import com.djrapitops.plan.data.store.mutators.formatting.Formatter;
|
||||
import com.djrapitops.plan.data.store.mutators.formatting.Formatters;
|
||||
import com.djrapitops.plan.data.store.mutators.formatting.PlaceholderReplacer;
|
||||
@ -77,7 +74,7 @@ public class InspectPage implements Page {
|
||||
}
|
||||
}
|
||||
|
||||
public String parse(PlayerContainer container, UUID serverUUID, Map<UUID, String> serverNames) throws IOException {
|
||||
public String parse(PlayerContainer player, UUID serverUUID, Map<UUID, String> serverNames) throws IOException {
|
||||
long now = System.currentTimeMillis();
|
||||
|
||||
PlaceholderReplacer replacer = new PlaceholderReplacer();
|
||||
@ -92,20 +89,20 @@ public class InspectPage implements Page {
|
||||
Session session = activeSession.get();
|
||||
session.setSessionID(Integer.MAX_VALUE);
|
||||
online = true;
|
||||
container.putRawData(PlayerKeys.ACTIVE_SESSION, session);
|
||||
player.putRawData(PlayerKeys.ACTIVE_SESSION, session);
|
||||
}
|
||||
|
||||
String playerName = container.getValue(PlayerKeys.NAME).orElse("Unknown");
|
||||
int timesKicked = container.getValue(PlayerKeys.KICK_COUNT).orElse(0);
|
||||
String playerName = player.getValue(PlayerKeys.NAME).orElse("Unknown");
|
||||
int timesKicked = player.getValue(PlayerKeys.KICK_COUNT).orElse(0);
|
||||
|
||||
replacer.addAllPlaceholdersFrom(container, Formatters.yearLongValue(),
|
||||
replacer.addAllPlaceholdersFrom(player, Formatters.yearLongValue(),
|
||||
PlayerKeys.REGISTERED, PlayerKeys.LAST_SEEN
|
||||
);
|
||||
|
||||
replacer.put("playerName", playerName);
|
||||
replacer.put("kickCount", timesKicked);
|
||||
|
||||
PerServerContainer perServerContainer = container.getValue(PlayerKeys.PER_SERVER).orElse(new PerServerContainer());
|
||||
PerServerContainer perServerContainer = player.getValue(PlayerKeys.PER_SERVER).orElse(new PerServerContainer());
|
||||
PerServerMutator perServerMutator = new PerServerMutator(perServerContainer);
|
||||
|
||||
Map<UUID, WorldTimes> worldTimesPerServer = perServerMutator.worldTimesPerServer();
|
||||
@ -118,12 +115,17 @@ public class InspectPage implements Page {
|
||||
replacer.put("favoriteServer", favoriteServer);
|
||||
|
||||
replacer.put("tableBodyNicknames", new NicknameTable(
|
||||
container.getValue(PlayerKeys.NICKNAMES).orElse(new ArrayList<>()), serverNames)
|
||||
player.getValue(PlayerKeys.NICKNAMES).orElse(new ArrayList<>()), serverNames)
|
||||
.parseBody());
|
||||
replacer.put("tableBodyIPs", new GeoInfoTable(container.getValue(PlayerKeys.GEO_INFO).orElse(new ArrayList<>())).parseBody());
|
||||
replacer.put("tableBodyIPs", new GeoInfoTable(player.getValue(PlayerKeys.GEO_INFO).orElse(new ArrayList<>())).parseBody());
|
||||
|
||||
List<Session> allSessions = container.getValue(PlayerKeys.SESSIONS).orElse(new ArrayList<>());
|
||||
SessionsMutator sessionsMutator = SessionsMutator.forContainer(container);
|
||||
PingMutator pingMutator = PingMutator.forContainer(player);
|
||||
replacer.put("avgPing", FormatUtils.cutDecimals(pingMutator.average()) + " ms");
|
||||
replacer.put("minPing", pingMutator.min() + " ms");
|
||||
replacer.put("maxPing", pingMutator.max() + " ms");
|
||||
|
||||
List<Session> allSessions = player.getValue(PlayerKeys.SESSIONS).orElse(new ArrayList<>());
|
||||
SessionsMutator sessionsMutator = SessionsMutator.forContainer(player);
|
||||
allSessions.sort(new SessionStartComparator());
|
||||
|
||||
String sessionAccordionViewScript = "";
|
||||
@ -139,9 +141,9 @@ public class InspectPage implements Page {
|
||||
}
|
||||
}
|
||||
|
||||
ServerAccordion serverAccordion = new ServerAccordion(container, serverNames);
|
||||
ServerAccordion serverAccordion = new ServerAccordion(player, serverNames);
|
||||
|
||||
PlayerCalendar playerCalendar = new PlayerCalendar(container);
|
||||
PlayerCalendar playerCalendar = new PlayerCalendar(player);
|
||||
|
||||
replacer.put("calendarSeries", playerCalendar.toCalendarSeries());
|
||||
replacer.put("firstDay", 1);
|
||||
@ -160,7 +162,7 @@ public class InspectPage implements Page {
|
||||
sessionsAndPlaytime(replacer, sessionsMutator, daySessionsMutator, weekSessionsMutator, monthSessionsMutator);
|
||||
|
||||
String punchCardData = new PunchCardGraph(allSessions).toHighChartsSeries();
|
||||
WorldTimes worldTimes = container.getValue(PlayerKeys.WORLD_TIMES).orElse(new WorldTimes(new HashMap<>()));
|
||||
WorldTimes worldTimes = player.getValue(PlayerKeys.WORLD_TIMES).orElse(new WorldTimes(new HashMap<>()));
|
||||
|
||||
WorldPie worldPie = new WorldPie(worldTimes);
|
||||
|
||||
@ -171,15 +173,15 @@ public class InspectPage implements Page {
|
||||
|
||||
pvpAndPve(replacer, sessionsMutator, weekSessionsMutator, monthSessionsMutator);
|
||||
|
||||
ActivityIndex activityIndex = container.getActivityIndex(now);
|
||||
ActivityIndex activityIndex = player.getActivityIndex(now);
|
||||
|
||||
replacer.put("activityIndexNumber", activityIndex.getFormattedValue());
|
||||
replacer.put("activityIndexColor", activityIndex.getColor());
|
||||
replacer.put("activityIndex", activityIndex.getGroup());
|
||||
|
||||
replacer.put("playerStatus", HtmlStructure.playerStatus(online,
|
||||
container.getValue(PlayerKeys.BANNED).orElse(false),
|
||||
container.getValue(PlayerKeys.OPERATOR).orElse(false)));
|
||||
player.getValue(PlayerKeys.BANNED).orElse(false),
|
||||
player.getValue(PlayerKeys.OPERATOR).orElse(false)));
|
||||
|
||||
String serverName = serverNames.get(serverUUID);
|
||||
replacer.put("networkName",
|
||||
|
@ -310,8 +310,8 @@
|
||||
<p><i class="col-teal far fa-clock"></i> Longest Session <span
|
||||
class="pull-right">${sessionLengthLongest}</span>
|
||||
</p>
|
||||
<p><i class="col-teal far fa-clock"></i> Session Median <span
|
||||
class="pull-right">${sessionLengthMedian}</span>
|
||||
<p><i class="col-teal far fa-clock"></i> Session Median
|
||||
<span class="pull-right">${sessionLengthMedian}</span>
|
||||
</p>
|
||||
</div>
|
||||
<div class="col-xs-6 col-sm-6 col-md-6 col-lg-6">
|
||||
@ -321,8 +321,23 @@
|
||||
</p>
|
||||
<p>
|
||||
</p>
|
||||
<p><i class="col-green fa fa-server"></i> Favorite Server<span
|
||||
class="pull-right">${favoriteServer}</span>
|
||||
<p>
|
||||
<i class="col-green fa fa-server"></i> Favorite Server
|
||||
<span class="pull-right">${favoriteServer}</span>
|
||||
</p>
|
||||
<p>
|
||||
</p>
|
||||
<p>
|
||||
<i class="col-amber fa fa-signal"></i> Average Ping
|
||||
<span class="pull-right">${avgPing}</span>
|
||||
</p>
|
||||
<p>
|
||||
<i class="col-amber fa fa-signal"></i> Best Ping
|
||||
<span class="pull-right">${minPing}</span>
|
||||
</p>
|
||||
<p>
|
||||
<i class="col-amber fa fa-signal"></i> Worst Ping
|
||||
<span class="pull-right">${maxPing}</span>
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -65,11 +65,13 @@ public class SQLiteTest {
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpClass() throws Exception {
|
||||
new TestDatabaseCreator();
|
||||
|
||||
System.out.println("--- Test Class Setup ---");
|
||||
SystemMockUtil mockUtil = SystemMockUtil.setUp(temporaryFolder.getRoot())
|
||||
.enableConfigSystem();
|
||||
db = new SQLiteDB();
|
||||
SystemMockUtil.setUp(temporaryFolder.getRoot())
|
||||
.enableConfigSystem()
|
||||
.enableDatabaseSystem(db)
|
||||
mockUtil.enableDatabaseSystem(db)
|
||||
.enableServerInfoSystem();
|
||||
StaticHolder.saveInstance(SQLDB.class, Plan.class);
|
||||
StaticHolder.saveInstance(SQLiteTest.class, Plan.class);
|
||||
@ -757,7 +759,7 @@ public class SQLiteTest {
|
||||
System.out.println("Running backup..");
|
||||
db.backup().backup(backup);
|
||||
System.out.println("Backup Complete!");
|
||||
|
||||
|
||||
UserInfoTable userInfoTable = backup.getUserInfoTable();
|
||||
UsersTable usersTable = backup.getUsersTable();
|
||||
SessionsTable sessionsTable = backup.getSessionsTable();
|
||||
|
115
Plan/src/test/java/utilities/TestDatabaseCreator.java
Normal file
115
Plan/src/test/java/utilities/TestDatabaseCreator.java
Normal file
@ -0,0 +1,115 @@
|
||||
package utilities;
|
||||
|
||||
import com.djrapitops.plan.api.exceptions.database.DBInitException;
|
||||
import com.djrapitops.plan.data.container.Session;
|
||||
import com.djrapitops.plan.data.time.GMTimes;
|
||||
import com.djrapitops.plan.data.time.WorldTimes;
|
||||
import com.djrapitops.plan.system.database.databases.sql.SQLDB;
|
||||
import com.djrapitops.plan.system.database.databases.sql.SQLiteDB;
|
||||
import com.djrapitops.plan.system.info.server.Server;
|
||||
import com.djrapitops.plugin.api.TimeAmount;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class TestDatabaseCreator {
|
||||
|
||||
private static final List<UUID> SERVER_UUIDS = Arrays.stream(new String[]{
|
||||
"116aa03d-b9fd-4947-8983-f2ec3aa780e5",
|
||||
"a31b0867-aac0-4faf-b7b6-4eee6b66a51d",
|
||||
"b80b2e89-6274-4921-9546-e5d8185e0f4e",
|
||||
"459458ec-25da-4a46-b319-3ee11121f6a1",
|
||||
"14043fad-b743-4069-b260-97626631465d",
|
||||
"56520211-9b5c-460b-99ec-bdd77747d3c3",
|
||||
"facd2eb1-4eba-45e9-ab05-0947dca82bdd",
|
||||
"20988493-1da0-4a53-80b5-60d4a8936b50",
|
||||
"f9cc0853-73c1-44d1-8989-19c93977302d",
|
||||
"840b0c0e-a65c-4269-8d5c-d3e1de349557"
|
||||
}).map(UUID::fromString).collect(Collectors.toList());
|
||||
private static final String[] gms = GMTimes.getGMKeyArray();
|
||||
private final SQLDB db;
|
||||
private final Random r;
|
||||
private Map<UUID, List<String>> worlds;
|
||||
|
||||
public TestDatabaseCreator() throws DBInitException {
|
||||
File testDB = new File("src/test/resources/testDB.db".replace("/", File.separator));
|
||||
|
||||
boolean oldDB = testDB.exists();
|
||||
|
||||
db = new SQLiteDB(testDB);
|
||||
db.init();
|
||||
|
||||
r = new Random();
|
||||
|
||||
if (oldDB) {
|
||||
return;
|
||||
}
|
||||
|
||||
fillDatabase();
|
||||
}
|
||||
|
||||
private void fillDatabase() {
|
||||
addServers();
|
||||
addWorlds();
|
||||
addUsers();
|
||||
}
|
||||
|
||||
private void addWorlds() {
|
||||
worlds = new HashMap<>();
|
||||
|
||||
for (UUID serverUuid : SERVER_UUIDS) {
|
||||
for (int i = 0; i < r.nextInt(10); i++) {
|
||||
List<String> worldNames = worlds.getOrDefault(serverUuid, new ArrayList<>());
|
||||
worldNames.add(RandomData.randomString(50));
|
||||
worlds.put(serverUuid, worldNames);
|
||||
}
|
||||
}
|
||||
|
||||
for (Map.Entry<UUID, List<String>> entry : worlds.entrySet()) {
|
||||
db.getWorldTable().saveWorlds(entry.getValue(), entry.getKey());
|
||||
}
|
||||
}
|
||||
|
||||
private void addUsers() {
|
||||
for (int i = 0; i < 100000; i++) {
|
||||
UUID uuid = UUID.randomUUID();
|
||||
long registered = Math.abs(r.nextLong());
|
||||
String name = uuid.toString().split("-", 2)[0];
|
||||
db.save().registerNewUser(uuid, registered, name);
|
||||
}
|
||||
}
|
||||
|
||||
private void addSession(UUID uuid, long date) {
|
||||
UUID serverUUID = SERVER_UUIDS.get(r.nextInt(SERVER_UUIDS.size()));
|
||||
List<String> worldNames = worlds.get(serverUUID);
|
||||
String world = worldNames.get(r.nextInt(worldNames.size()));
|
||||
String gm = gms[r.nextInt(gms.length)];
|
||||
|
||||
long end = date + (long) r.nextInt((int) TimeAmount.DAY.ms());
|
||||
|
||||
Session session = new Session(-1, uuid, serverUUID,
|
||||
date, end,
|
||||
r.nextInt(100), // mobs
|
||||
r.nextInt(50), // deaths
|
||||
r.nextInt((int) (end - date)) // afk
|
||||
);
|
||||
|
||||
WorldTimes worldTimes = new WorldTimes(new HashMap<>());
|
||||
|
||||
}
|
||||
|
||||
private void addServers() {
|
||||
for (UUID serverUuid : SERVER_UUIDS) {
|
||||
Server server = new Server(
|
||||
-1,
|
||||
serverUuid,
|
||||
serverUuid.toString().split("-", 2)[0],
|
||||
"address",
|
||||
100
|
||||
);
|
||||
db.getServerTable().saveCurrentServerInfo(server);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user