Updated db IPTable to include last_used & bumped schema version to 13. #422

This commit is contained in:
Rsl1122 2017-11-20 15:16:37 +02:00
parent 86c4e36176
commit f77eb460cc
10 changed files with 113 additions and 113 deletions

View File

@ -40,13 +40,12 @@ public class GeoInfo {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
GeoInfo geoInfo = (GeoInfo) o;
return lastUsed == geoInfo.lastUsed &&
Objects.equal(ip, geoInfo.ip) &&
return Objects.equal(ip, geoInfo.ip) &&
Objects.equal(geolocation, geoInfo.geolocation);
}
@Override
public int hashCode() {
return Objects.hashCode(ip, geolocation, lastUsed);
return Objects.hashCode(ip, geolocation);
}
}

View File

@ -116,7 +116,7 @@ public abstract class SQLDB extends Database {
if (newDatabase) {
Log.info("New Database created.");
setVersion(12);
setVersion(13);
}
int version = getVersion();
@ -143,6 +143,10 @@ public abstract class SQLDB extends Database {
ipsTable.alterTableV12();
setVersion(12);
}
if (version < 13) {
ipsTable.alterTableV13();
setVersion(13);
}
} catch (SQLException e) {
throw new DatabaseInitException("Failed to set-up Database", e);
}
@ -249,8 +253,7 @@ public abstract class SQLDB extends Database {
profile.setActions(actionsTable.getActions(uuid));
profile.setNicknames(nicknamesTable.getAllNicknames(uuid));
// TODO Add IPs as GeoInfo, requires IPTable changes
profile.setGeoInformation(ipsTable.getGeoInfo(uuid));
Map<UUID, List<Session>> sessions = sessionsTable.getSessions(uuid);
profile.setSessions(sessions);

View File

@ -2,6 +2,7 @@ package main.java.com.djrapitops.plan.database.tables;
import com.djrapitops.plugin.utilities.Verify;
import main.java.com.djrapitops.plan.api.exceptions.DBCreateTableException;
import main.java.com.djrapitops.plan.data.GeoInfo;
import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.processing.ExecStatement;
import main.java.com.djrapitops.plan.database.processing.QueryAllStatement;
@ -22,6 +23,7 @@ public class IPsTable extends UserIDTable {
private final String columnIP = "ip";
private final String columnGeolocation = "geolocation";
private final String columnLastUsed = "last_used";
private String insertStatement;
/**
@ -33,10 +35,11 @@ public class IPsTable extends UserIDTable {
insertStatement = "INSERT INTO " + tableName + " ("
+ columnUserID + ", "
+ columnIP + ", "
+ columnGeolocation
+ columnGeolocation + ", "
+ columnLastUsed
+ ") VALUES ("
+ usersTable.statementSelectID + ", "
+ "?, ?)";
+ "?, ?, ?)";
}
@Override
@ -45,6 +48,7 @@ public class IPsTable extends UserIDTable {
.column(columnUserID, Sql.INT).notNull()
.column(columnIP, Sql.varchar(39)).notNull()
.column(columnGeolocation, Sql.varchar(50)).notNull()
.column(columnLastUsed, Sql.LONG).notNull().defaultValue("0")
.foreignKey(columnUserID, usersTable.getTableName(), usersTable.getColumnID())
.toString()
);
@ -56,56 +60,69 @@ public class IPsTable extends UserIDTable {
}
}
/**
* @param uuid UUID of the user.
* @return Users's Login Geolocations.
* @throws SQLException when an error at retrieval happens
*/
public List<String> getGeolocations(UUID uuid) throws SQLException {
return getStringList(uuid, columnGeolocation);
public void alterTableV13() {
addColumns(columnLastUsed + " bigint NOT NULL DEFAULT 0");
}
public List<String> getIps(UUID uuid) throws SQLException {
return getStringList(uuid, columnIP);
}
private List<String> getStringList(UUID uuid, String column) throws SQLException {
String sql = "SELECT DISTINCT " + column + " FROM " + tableName +
public List<GeoInfo> getGeoInfo(UUID uuid) throws SQLException {
String sql = "SELECT DISTINCT * FROM " + tableName +
" WHERE " + columnUserID + "=" + usersTable.statementSelectID;
return query(new QueryStatement<List<String>>(sql, 100) {
return query(new QueryStatement<List<GeoInfo>>(sql, 100) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, uuid.toString());
}
@Override
public List<String> processResults(ResultSet set) throws SQLException {
List<String> stringList = new ArrayList<>();
public List<GeoInfo> processResults(ResultSet set) throws SQLException {
List<GeoInfo> geoInfo = new ArrayList<>();
while (set.next()) {
stringList.add(set.getString(column));
String ip = set.getString(columnIP);
String geolocation = set.getString(columnGeolocation);
long lastUsed = set.getLong(columnLastUsed);
geoInfo.add(new GeoInfo(ip, geolocation, lastUsed));
}
return stringList;
return geoInfo;
}
});
}
public void saveIP(UUID uuid, String ip, String geolocation) throws SQLException {
List<String> ips = getIps(uuid);
if (ips.contains(ip)) {
return;
public void saveGeoInfo(UUID uuid, GeoInfo info) throws SQLException {
List<GeoInfo> geoInfo = getGeoInfo(uuid);
if (geoInfo.contains(info)) {
updateGeoInfo(uuid, info);
}
insertIp(uuid, ip, geolocation);
insertGeoInfo(uuid, info);
}
private void insertIp(UUID uuid, String ip, String geolocation) throws SQLException {
private void insertGeoInfo(UUID uuid, GeoInfo info) throws SQLException {
execute(new ExecStatement(insertStatement) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, uuid.toString());
statement.setString(2, ip);
statement.setString(3, geolocation);
statement.setString(2, info.getIp());
statement.setString(3, info.getGeolocation());
statement.setLong(4, info.getLastUsed());
}
});
}
private void updateGeoInfo(UUID uuid, GeoInfo info) throws SQLException {
String sql = "UPDATE " + tableName + " SET "
+ columnLastUsed + "=?" +
" WHERE " + columnUserID + "=" + usersTable.statementSelectID +
" AND " + columnIP + "=?" +
" AND " + columnGeolocation + "=?";
execute(new ExecStatement(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setLong(1, info.getLastUsed());
statement.setString(2, uuid.toString());
statement.setString(3, info.getIp());
statement.setString(4, info.getGeolocation());
}
});
}
@ -131,62 +148,39 @@ public class IPsTable extends UserIDTable {
});
}
public Map<UUID, List<String>> getAllGeolocations() throws SQLException {
public Map<UUID, List<GeoInfo>> getAllGeoInfo() throws SQLException {
String usersIDColumn = usersTable + "." + usersTable.getColumnID();
String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid";
String sql = "SELECT " +
columnIP + ", " +
columnGeolocation + ", " +
columnLastUsed + ", " +
usersUUIDColumn +
" FROM " + tableName +
" JOIN " + usersTable + " on " + usersIDColumn + "=" + columnUserID;
return query(new QueryAllStatement<Map<UUID, List<String>>>(sql, 50000) {
return query(new QueryAllStatement<Map<UUID, List<GeoInfo>>>(sql, 50000) {
@Override
public Map<UUID, List<String>> processResults(ResultSet set) throws SQLException {
Map<UUID, List<String>> geoLocations = new HashMap<>();
public Map<UUID, List<GeoInfo>> processResults(ResultSet set) throws SQLException {
Map<UUID, List<GeoInfo>> geoLocations = new HashMap<>();
while (set.next()) {
UUID uuid = UUID.fromString(set.getString("uuid"));
List<String> userGeoLocs = geoLocations.getOrDefault(uuid, new ArrayList<>());
userGeoLocs.add(set.getString(columnGeolocation));
geoLocations.put(uuid, userGeoLocs);
List<GeoInfo> userGeoInfo = geoLocations.getOrDefault(uuid, new ArrayList<>());
String ip = set.getString(columnIP);
String geolocation = set.getString(columnGeolocation);
long lastUsed = set.getLong(columnLastUsed);
userGeoInfo.add(new GeoInfo(ip, geolocation, lastUsed));
geoLocations.put(uuid, userGeoInfo);
}
return geoLocations;
}
});
}
public Map<UUID, Map<String, String>> getAllIPsAndGeolocations() throws SQLException {
String usersIDColumn = usersTable + "." + usersTable.getColumnID();
String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid";
String sql = "SELECT " +
columnGeolocation + ", " +
columnIP + ", " +
usersUUIDColumn +
" FROM " + tableName +
" JOIN " + usersTable + " on " + usersIDColumn + "=" + columnUserID;
return query(new QueryAllStatement<Map<UUID, Map<String, String>>>(sql, 50000) {
@Override
public Map<UUID, Map<String, String>> processResults(ResultSet set) throws SQLException {
Map<UUID, Map<String, String>> map = new HashMap<>();
while (set.next()) {
UUID uuid = UUID.fromString(set.getString("uuid"));
Map<String, String> userMap = map.getOrDefault(uuid, new HashMap<>());
String geoLocation = set.getString(columnGeolocation);
String ip = set.getString(columnIP);
userMap.put(ip, geoLocation);
map.put(uuid, userMap);
}
return map;
}
});
}
public void insertIPsAndGeolocations(Map<UUID, Map<String, String>> allIPsAndGeolocations) throws SQLException {
public void insertAllGeoInfo(Map<UUID, List<GeoInfo>> allIPsAndGeolocations) throws SQLException {
if (Verify.isEmpty(allIPsAndGeolocations)) {
return;
}
@ -196,14 +190,16 @@ public class IPsTable extends UserIDTable {
public void prepare(PreparedStatement statement) throws SQLException {
// Every User
for (UUID uuid : allIPsAndGeolocations.keySet()) {
// Every IP & Geolocation
for (Map.Entry<String, String> entry : allIPsAndGeolocations.get(uuid).entrySet()) {
String ip = entry.getKey();
String geoLocation = entry.getValue();
// Every GeoInfo
for (GeoInfo info : allIPsAndGeolocations.get(uuid)) {
String ip = info.getIp();
String geoLocation = info.getGeolocation();
long lastUsed = info.getLastUsed();
statement.setString(1, uuid.toString());
statement.setString(2, ip);
statement.setString(3, geoLocation);
statement.setLong(4, lastUsed);
statement.addBatch();
}

View File

@ -107,8 +107,8 @@ public class BatchOperationTable extends Table {
if (toDB.equals(this)) {
return;
}
Log.debug("Batch Copy IPs & Geolocations");
toDB.getDb().getIpsTable().insertIPsAndGeolocations(db.getIpsTable().getAllIPsAndGeolocations());
Log.debug("Batch Copy IPs, Geolocations & Last used dates");
toDB.getDb().getIpsTable().insertAllGeoInfo(db.getIpsTable().getAllGeoInfo());
}
public void copyNicknames(BatchOperationTable toDB) throws SQLException {

View File

@ -88,13 +88,13 @@ public class InspectPageParser extends PageParser {
addValue("gmPieColors", Settings.THEME_GRAPH_GM_PIE.toString());
addValue("serverPieColors", Settings.THEME_GRAPH_SERVER_PREF_PIE.toString());
List<String> geolocations = db.getIpsTable().getGeolocations(uuid);
// List<String> geolocations = db.getIpsTable().getGeolocations(uuid);
List<String> nicknames = db.getNicknamesTable().getNicknames(uuid).stream()
.map(HtmlUtils::swapColorsToSpan)
.collect(Collectors.toList());
addValue("nicknames", HtmlStructure.createDotList(nicknames.toArray(new String[nicknames.size()])));
addValue("geolocations", HtmlStructure.createDotList(geolocations.toArray(new String[geolocations.size()])));
// addValue("geolocations", HtmlStructure.createDotList(geolocations.toArray(new String[geolocations.size()])));
Map<UUID, List<Session>> sessions = sessionsTable.getSessions(uuid);
List<Session> allSessions = sessions.values().stream()

View File

@ -102,7 +102,7 @@ public class PlanPlayerListener implements Listener {
cache.cacheSession(uuid, Session.start(time, world, gm));
plugin.addToProcessQueue(
new RegisterProcessor(uuid, player.getFirstPlayed(), time, playerName, playersOnline,
new IPUpdateProcessor(uuid, ip),
new IPUpdateProcessor(uuid, ip, time),
new NameProcessor(uuid, playerName, displayName)
),
new NetworkPageUpdateProcessor(plugin.getInfoManager())

View File

@ -9,6 +9,7 @@ import com.djrapitops.plugin.api.utility.log.Log;
import com.djrapitops.plugin.utilities.Verify;
import com.google.common.collect.ImmutableMap;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.data.GeoInfo;
import main.java.com.djrapitops.plan.data.Session;
import main.java.com.djrapitops.plan.data.UserInfo;
import main.java.com.djrapitops.plan.data.time.WorldTimes;
@ -17,6 +18,7 @@ import main.java.com.djrapitops.plan.systems.cache.GeolocationCache;
import main.java.com.djrapitops.plan.systems.processing.importing.ServerImportData;
import main.java.com.djrapitops.plan.systems.processing.importing.UserImportData;
import main.java.com.djrapitops.plan.systems.processing.importing.UserImportRefiner;
import main.java.com.djrapitops.plan.utilities.MiscUtils;
import java.sql.SQLException;
import java.util.*;
@ -161,7 +163,7 @@ public abstract class Importer {
List<UserInfo> userInfo = new Vector<>();
Map<UUID, List<String>> nickNames = new Hashtable<>();
Map<UUID, List<Session>> sessions = new Hashtable<>();
Map<UUID, Map<String, String>> ips = new Hashtable<>();
Map<UUID, List<GeoInfo>> geoInfo = new Hashtable<>();
Map<UUID, Integer> timesKicked = new Hashtable<>();
userImportData.parallelStream().forEach(data -> {
@ -177,7 +179,7 @@ public abstract class Importer {
}
nickNames.put(uuid, data.getNicknames());
ips.put(uuid, convertIPs(data));
geoInfo.put(uuid, convertGeoInfo(data));
timesKicked.put(uuid, data.getTimesKicked());
sessions.put(uuid, Collections.singletonList(toSession(data)));
});
@ -221,7 +223,7 @@ public abstract class Importer {
new ImportExecutorHelper() {
@Override
void execute() throws SQLException {
db.getIpsTable().insertIPsAndGeolocations(ips);
db.getIpsTable().insertAllGeoInfo(geoInfo);
}
}.submit(service);
@ -260,14 +262,14 @@ public abstract class Importer {
return session;
}
private Map<String, String> convertIPs(UserImportData userImportData) {
Map<String, String> convertedIPs;
List<String> ips = userImportData.getIps();
private List<GeoInfo> convertGeoInfo(UserImportData userImportData) {
long date = MiscUtils.getTime();
convertedIPs = ips.parallelStream()
.collect(Collectors.toMap(ip -> ip, GeolocationCache::getCountry, (a, b) -> b, HashMap::new));
return convertedIPs;
return userImportData.getIps().parallelStream()
.map(ip -> {
String geoLoc = GeolocationCache.getCountry(ip);
return new GeoInfo(ip, geoLoc, date);
}).collect(Collectors.toList());
}
private abstract class ImportExecutorHelper {

View File

@ -6,6 +6,7 @@ package main.java.com.djrapitops.plan.systems.processing.player;
import com.djrapitops.plugin.api.utility.log.Log;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.data.GeoInfo;
import main.java.com.djrapitops.plan.systems.cache.GeolocationCache;
import java.sql.SQLException;
@ -19,10 +20,12 @@ import java.util.UUID;
public class IPUpdateProcessor extends PlayerProcessor {
private final String ip;
private final long time;
public IPUpdateProcessor(UUID uuid, String ip) {
public IPUpdateProcessor(UUID uuid, String ip, long time) {
super(uuid);
this.ip = ip;
this.time = time;
}
@Override
@ -30,7 +33,7 @@ public class IPUpdateProcessor extends PlayerProcessor {
UUID uuid = getUUID();
String country = GeolocationCache.getCountry(ip);
try {
Plan.getInstance().getDB().getIpsTable().saveIP(uuid, ip, country);
Plan.getInstance().getDB().getIpsTable().saveGeoInfo(uuid, new GeoInfo(ip, country, time));
} catch (SQLException e) {
Log.toLog(this.getClass().getName(), e);
}

View File

@ -293,8 +293,8 @@ public class Analysis {
joinInfo.addSessions(sessions);
}
Map<UUID, List<String>> geolocations = db.getIpsTable().getAllGeolocations();
geolocPart.addGeoLocations(geolocations);
Map<UUID, List<GeoInfo>> geolocations = db.getIpsTable().getAllGeoInfo();
// geolocPart.addGeoLocations(geolocations);
analysisData.setPlayersTable(PlayersTableCreator.createTable(userInfo, joinInfo, geolocPart));

View File

@ -271,18 +271,18 @@ public class DatabaseTest {
String expectedIP = "1.2.3.4";
String expectedGeoLoc = "TestLocation";
long time = MiscUtils.getTime();
ipsTable.saveIP(uuid, expectedIP, expectedGeoLoc);
ipsTable.saveIP(uuid, expectedIP, expectedGeoLoc);
GeoInfo expected = new GeoInfo(expectedIP, expectedGeoLoc, time);
ipsTable.saveGeoInfo(uuid, expected);
ipsTable.saveGeoInfo(uuid, expected);
commitTest();
List<String> ips = ipsTable.getIps(uuid);
assertEquals(1, ips.size());
assertEquals(expectedIP, ips.get(0));
List<String> geolocations = ipsTable.getGeolocations(uuid);
assertEquals(1, geolocations.size());
assertEquals(expectedGeoLoc, geolocations.get(0));
List<GeoInfo> getInfo = ipsTable.getGeoInfo(uuid);
assertEquals(1, getInfo.size());
GeoInfo actual = getInfo.get(0);
assertEquals(expected, actual);
assertEquals(time, actual.getLastUsed());
Optional<String> result = ipsTable.getGeolocation(expectedIP);
@ -574,7 +574,7 @@ public class DatabaseTest {
sessionsTable.saveSession(uuid, session);
nicknamesTable.saveUserName(uuid, "TestNick");
ipsTable.saveIP(uuid, "1.2.3.4", "TestLoc");
ipsTable.saveGeoInfo(uuid, new GeoInfo("1.2.3.4", "TestLoc", 223456789L));
actionsTable.insertAction(uuid, new Action(1324L, Actions.FIRST_SESSION, "Add"));
assertTrue(usersTable.isRegistered(uuid));
@ -584,8 +584,7 @@ public class DatabaseTest {
assertFalse(usersTable.isRegistered(uuid));
assertFalse(userInfoTable.isRegistered(uuid));
assertTrue(nicknamesTable.getNicknames(uuid).isEmpty());
assertTrue(ipsTable.getGeolocations(uuid).isEmpty());
assertTrue(ipsTable.getIps(uuid).isEmpty());
assertTrue(ipsTable.getGeoInfo(uuid).isEmpty());
assertTrue(sessionsTable.getSessions(uuid).isEmpty());
assertTrue(actionsTable.getActions(uuid).isEmpty());
}
@ -610,8 +609,7 @@ public class DatabaseTest {
assertFalse(userInfoTable.isRegistered(uuid));
assertTrue(nicknamesTable.getNicknames(uuid).isEmpty());
assertTrue(ipsTable.getGeolocations(uuid).isEmpty());
assertTrue(ipsTable.getIps(uuid).isEmpty());
assertTrue(ipsTable.getGeoInfo(uuid).isEmpty());
assertTrue(sessionsTable.getSessions(uuid).isEmpty());
assertTrue(actionsTable.getActions(uuid).isEmpty());
assertTrue(db.getCommandUse().isEmpty());
@ -645,7 +643,7 @@ public class DatabaseTest {
sessionsTable.saveSession(uuid, session);
nicknamesTable.saveUserName(uuid, "TestNick");
ipsTable.saveIP(uuid, "1.2.3.4", "TestLoc");
ipsTable.saveGeoInfo(uuid, new GeoInfo("1.2.3.4", "TestLoc", 223456789L));
actionsTable.insertAction(uuid, new Action(1324L, Actions.FIRST_SESSION, "Add"));
assertTrue(usersTable.isRegistered(uuid));
@ -791,8 +789,7 @@ public class DatabaseTest {
assertTrue(userInfoTable.isRegistered(uuid));
assertFalse(nicknamesTable.getNicknames(uuid).isEmpty());
assertFalse(ipsTable.getGeolocations(uuid).isEmpty());
assertFalse(ipsTable.getIps(uuid).isEmpty());
assertFalse(ipsTable.getGeoInfo(uuid).isEmpty());
assertFalse(sessionsTable.getSessions(uuid).isEmpty());
assertFalse(actionsTable.getActions(uuid).isEmpty());
assertFalse(backup.getCommandUse().isEmpty());