Merge branch '4.0.0-BungeeCord-Support' of https://github.com/Rsl1122/Plan-PlayerAnalytics

This commit is contained in:
Fuzzlemann 2017-08-22 17:23:58 +02:00
commit 6f807c123d
7 changed files with 66 additions and 39 deletions

View File

@ -3,15 +3,12 @@ package main.java.com.djrapitops.plan.database.databases;
import com.djrapitops.plugin.task.AbsRunnable;
import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.data.KillData;
import main.java.com.djrapitops.plan.data.Session;
import main.java.com.djrapitops.plan.data.UserData;
import main.java.com.djrapitops.plan.database.Database;
import main.java.com.djrapitops.plan.database.tables.*;
import main.java.com.djrapitops.plan.utilities.Benchmark;
import main.java.com.djrapitops.plan.utilities.MiscUtils;
import java.net.InetAddress;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
@ -40,6 +37,7 @@ public abstract class SQLDB extends Database {
this.supportsModification = supportsModification;
usingMySQL = getName().equals("MySQL");
serverTable = new ServerTable(this, usingMySQL);
usersTable = new UsersTable(this, usingMySQL);
sessionsTable = new SessionsTable(this, usingMySQL);
killsTable = new KillsTable(this, usingMySQL);
@ -51,7 +49,6 @@ public abstract class SQLDB extends Database {
securityTable = new SecurityTable(this, usingMySQL);
worldTable = new WorldTable(this, usingMySQL);
worldTimesTable = new WorldTimesTable(this, usingMySQL);
serverTable = new ServerTable(this, usingMySQL);
startConnectionPingTask();
}
@ -186,7 +183,7 @@ public abstract class SQLDB extends Database {
*/
public Table[] getAllTables() {
return new Table[]{
usersTable, ipsTable,
serverTable, usersTable, ipsTable,
nicknamesTable, sessionsTable, killsTable,
commandUseTable, tpsTable, worldTable,
worldTimesTable, securityTable};
@ -200,7 +197,7 @@ public abstract class SQLDB extends Database {
ipsTable,
nicknamesTable, sessionsTable, killsTable,
worldTimesTable, worldTable, usersTable,
commandUseTable, tpsTable};
commandUseTable, tpsTable, serverTable};
}
/**
@ -357,30 +354,7 @@ public abstract class SQLDB extends Database {
if (data.isEmpty()) {
return data;
}
Map<Integer, UUID> idUuidRel = userIds.entrySet().stream().collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
List<Integer> ids = userIds.entrySet().stream().filter(e -> uuids.contains(e.getKey())).map(Map.Entry::getValue).collect(Collectors.toList());
Log.debug("Database", "Using IDs: " + ids.size());
Map<Integer, List<String>> nicknames = nicknamesTable.getNicknames(ids);
Map<Integer, Set<InetAddress>> ipList = ipsTable.getIPList(ids);
Map<Integer, List<KillData>> playerKills = killsTable.getPlayerKills(ids, idUuidRel);
Map<Integer, List<Session>> sessionData = sessionsTable.getSessionData(ids);
Map<Integer, Map<String, Long>> worldTimes = worldTimesTable.getWorldTimes(ids);
Log.debug("Database",
"Data found for:",
" UUIDs: " + uuids.size(),
" IDs: " + userIds.size(),
" UserData: " + data.size(),
" Nicknames: " + nicknames.size(),
" IPs: " + ipList.size(),
" Kills: " + playerKills.size(),
" Sessions: " + sessionData.size(),
" World Times: " + worldTimes.size()
);
for (UserData uData : data) {
// TODO add extra data
}
// TODO REWRITE
Benchmark.stop("Database", "Get UserData for " + uuidsCol.size());
setAvailable();

View File

@ -252,6 +252,6 @@ public class ServerTable extends Table {
}
public String getColumnID() {
return tableName + "." + columnServerID;
return columnServerID;
}
}

View File

@ -42,8 +42,10 @@ public class SessionsTable extends UserIDTable {
@Override
public boolean createTable() {
try {
execute(TableSqlParser.createTable(tableName)
.primaryKeyIDColumn(usingMySQL, columnServerID, Sql.LONG)
String serverTableName = serverTable.getTableName();
String serverTableID = serverTable.getColumnID();
String sql = TableSqlParser.createTable(this.tableName)
.primaryKeyIDColumn(usingMySQL, columnSessionID, Sql.LONG)
.column(columnUserID, Sql.INT).notNull()
.column(columnServerID, Sql.INT).notNull()
.column(columnSessionStart, Sql.LONG).notNull()
@ -51,10 +53,11 @@ public class SessionsTable extends UserIDTable {
.column(columnMobKills, Sql.INT).notNull()
.column(columnDeaths, Sql.INT).notNull()
.foreignKey(columnUserID, usersTable.getTableName(), usersTable.getColumnID())
.foreignKey(columnServerID, serverTable.getTableName(), serverTable.getColumnID())
.foreignKey(columnServerID, serverTableName, serverTableID)
.primaryKey(usingMySQL, columnSessionID)
.toString()
);
.toString();
System.out.println(sql);
execute(sql);
return true;
} catch (SQLException ex) {
Log.toLog(this.getClass().getName(), ex);
@ -132,4 +135,50 @@ public class SessionsTable extends UserIDTable {
close(set, statement);
}
}
public long getPlaytime(UUID uuid) throws SQLException {
return getPlaytime(uuid, Plan.getServerUUID());
}
public long getPlaytime(UUID uuid, UUID serverUUID) throws SQLException {
return getPlaytime(uuid, serverUUID, 0L);
}
public long getPlaytime(UUID uuid, UUID serverUUID, long afterDate) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement("SELECT FROM " + tableName + " "
+ "(SUM(" + columnSessionEnd + ") - SUM(" + columnSessionStart + ")) as playtime "
+ "WHERE " + columnSessionStart + ">? AND "
+ columnUserID + "=" + usersTable.statementSelectID + " AND "
+ columnServerID + "=" + serverTable.statementSelectServerID);
statement.setLong(1, afterDate);
statement.setString(2, uuid.toString());
statement.setString(3, serverUUID.toString());
set = statement.executeQuery();
if (set.next()) {
return set.getLong("playtime");
}
return 0;
} finally {
close(set, statement);
}
}
public Map<String, Long> getPlaytimeByServer(UUID uuid) throws SQLException {
Map<Integer, String> serverNames = serverTable.getServerNames();
Map<String, Long> playtimes = new HashMap<>();
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement("SELECT FROM " + tableName + " "
+ "(SUM(" + columnSessionEnd + ") - SUM(" + columnSessionStart + ")) as playtime "
+ "WHERE " + columnSessionStart + ">? AND "
+ columnUserID + "=" + usersTable.statementSelectID); // TODO CONTINUE
return playtimes;
} finally {
close(set, statement);
}
}
}

View File

@ -11,6 +11,7 @@ import main.java.com.djrapitops.plan.utilities.file.FileUtil;
import org.bukkit.plugin.java.JavaPlugin;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
@ -76,6 +77,7 @@ public class DatabaseCommitTest {
db.commit();
}
@Ignore("//TODO")
@Test
public void testCommitToDBFile() throws SQLException {
db.init();

View File

@ -146,6 +146,7 @@ public class DatabaseTest {
/**
* @throws SQLException
*/
@Ignore("")
@Test // TODO Rewrite
public void testRemoveAll() throws SQLException {
db.init();
@ -166,6 +167,7 @@ public class DatabaseTest {
/**
* @throws SQLException
*/
@Ignore("//TODO")
@Test
public void testSaveCommandUse() throws SQLException {
db.init();

View File

@ -123,8 +123,8 @@ public class AnalysisUtilsTest {
@Test
public void testTransformSessionDataToLengths() {
Collection<Session> data = new ArrayList<>();
data.add(new Session(0L, 5L, null, null, 0, 0));
data.add(new Session(0, 20L, null, null, 0, 0));
data.add(new Session(1, 0L, 5L, 0, 0));
data.add(new Session(1, 0, 20L, 0, 0));
List<Long> expResult = new ArrayList<>();
expResult.add(5L);
expResult.add(20L);

View File

@ -46,7 +46,7 @@ public class RandomData {
public static List<Session> randomSessions() {
List<Session> test = new ArrayList<>();
for (int i = 0; i < 20; i++) {
test.add(new Session(r.nextLong(), r.nextLong(), null, null, 0, 0));
test.add(new Session(1, r.nextLong(), r.nextLong(), 0, 0));
}
return test;
}