PR for 4.0.0 (Fuzzlemann) (14) (#285) (Finalizes importer)

* Finalized Importer

* Test method for ManageImportCommand (runs OfflinePlayerImporter all the time, temporary)
Fix Importer
This commit is contained in:
Fuzzlemann 2017-09-10 14:07:23 +02:00 committed by Rsl1122
parent 25c2e8a1bf
commit 4a20d4417a
8 changed files with 118 additions and 29 deletions

View File

@ -43,6 +43,7 @@ import main.java.com.djrapitops.plan.systems.info.InformationManager;
import main.java.com.djrapitops.plan.systems.info.server.ServerInfoManager;
import main.java.com.djrapitops.plan.systems.listeners.*;
import main.java.com.djrapitops.plan.systems.processing.Processor;
import main.java.com.djrapitops.plan.systems.processing.importing.importers.OfflinePlayerImporter;
import main.java.com.djrapitops.plan.systems.queue.ProcessingQueue;
import main.java.com.djrapitops.plan.systems.tasks.TPSCountTimer;
import main.java.com.djrapitops.plan.systems.webserver.PageCache;
@ -192,6 +193,8 @@ public class Plan extends BukkitPlugin<Plan> implements IPlan {
hookHandler = new HookHandler(this);
Benchmark.stop("Enable", "Hook to 3rd party plugins");
ImporterManager.registerImporter(new OfflinePlayerImporter());
BStats bStats = new BStats(this);
bStats.registerMetrics();

View File

@ -3,12 +3,17 @@ package main.java.com.djrapitops.plan.command.commands.manage;
import com.djrapitops.plugin.command.CommandType;
import com.djrapitops.plugin.command.ISender;
import com.djrapitops.plugin.command.SubCommand;
import com.djrapitops.plugin.task.AbsRunnable;
import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.Permissions;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.locale.Locale;
import main.java.com.djrapitops.plan.locale.Msg;
import main.java.com.djrapitops.plan.systems.info.ImporterManager;
import main.java.com.djrapitops.plan.utilities.Check;
import java.sql.SQLException;
/**
* This manage subcommand is used to import data from 3rd party plugins.
* <p>
@ -47,7 +52,23 @@ public class ManageImportCommand extends SubCommand {
if (!Check.isTrue(args.length >= 1, Locale.get(Msg.CMD_FAIL_REQ_ONE_ARG) + " " + this.getArguments(), sender)) {
return true;
}
// TODO Rewrite Importer
runImport("offlineimporter");
return true;
}
private void runImport(String importer) {
plugin.getRunnableFactory().createNew("Import", new AbsRunnable() {
@Override
public void run() {
try {
ImporterManager.getImporter(importer).processImport();
} catch (SQLException e) {
Log.toLog(this.getClass().getName(), e);
} finally {
this.cancel();
}
}
}).runTaskAsynchronously();
}
}

View File

@ -146,7 +146,7 @@ public class TPSTable extends Table {
statement.setInt(1, p);
// More than 2 Months ago.
long fiveWeeks = TimeAmount.MONTH.ms() * 2L;
statement.setLong(1, MiscUtils.getTime() - fiveWeeks);
statement.setLong(2, MiscUtils.getTime() - fiveWeeks);
statement.execute();
commit(statement.getConnection());

View File

@ -47,8 +47,6 @@ public class PlanGamemodeChangeListener implements Listener {
String worldName = p.getWorld().getName();
Optional<Session> cachedSession = plugin.getDataCache().getCachedSession(uuid);
cachedSession.ifPresent(session -> {
session.changeState(worldName, gameMode, time);
});
cachedSession.ifPresent(session -> session.changeState(worldName, gameMode, time));
}
}

View File

@ -29,8 +29,6 @@ public class PlanWorldChangeListener implements Listener {
long time = MiscUtils.getTime();
Optional<Session> cachedSession = plugin.getDataCache().getCachedSession(uuid);
cachedSession.ifPresent(session -> {
session.changeState(worldName, gameMode, time);
});
cachedSession.ifPresent(session -> session.changeState(worldName, gameMode, time));
}
}

View File

@ -44,13 +44,33 @@ public abstract class Importer {
Benchmark.start(benchmarkName);
Benchmark.start(serverBenchmarkName);
processServerData();
Benchmark.stop(serverBenchmarkName);
ExecutorService service = Executors.newCachedThreadPool();
Benchmark.start(userDataBenchmarkName);
processUserData();
Benchmark.stop(userDataBenchmarkName);
new ImportExecutorHelper() {
@Override
void execute() throws SQLException {
Benchmark.start(serverBenchmarkName);
processServerData();
Benchmark.stop(serverBenchmarkName);
}
}.submit(service);
new ImportExecutorHelper() {
@Override
void execute() throws SQLException {
Benchmark.start(userDataBenchmarkName);
processUserData();
Benchmark.stop(userDataBenchmarkName);
}
}.submit(service);
service.shutdown();
try {
service.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
Log.toLog(this.getClass().getName(), e);
}
Benchmark.stop(benchmarkName);
}
@ -58,6 +78,7 @@ public abstract class Importer {
private void processServerData() throws SQLException {
String benchmarkName = "Processing Server Data";
String getDataBenchmarkName = "Getting Server Data";
String insertDataIntoDatabaseBenchmarkName = "Insert Server Data into Database";
Benchmark.start(benchmarkName);
Benchmark.start(getDataBenchmarkName);
@ -75,21 +96,47 @@ public abstract class Importer {
UUID uuid = plan.getServerInfoManager().getServerUUID();
Database db = plan.getDB();
db.getTpsTable().insertAllTPS(ImmutableMap.of(uuid, serverImportData.getTpsData()));
db.getCommandUseTable().insertCommandUsage(ImmutableMap.of(uuid, serverImportData.getCommandUsages()));
ExecutorService service = Executors.newCachedThreadPool();
Benchmark.start(benchmarkName);
Benchmark.start(insertDataIntoDatabaseBenchmarkName);
new ImportExecutorHelper() {
@Override
void execute() throws SQLException {
db.getTpsTable().insertAllTPS(ImmutableMap.of(uuid, serverImportData.getTpsData()));
}
}.submit(service);
new ImportExecutorHelper() {
@Override
void execute() throws SQLException {
db.getCommandUseTable().insertCommandUsage(ImmutableMap.of(uuid, serverImportData.getCommandUsages()));
}
}.submit(service);
service.shutdown();
try {
service.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
Log.toLog(this.getClass().getName(), e);
}
Benchmark.stop(insertDataIntoDatabaseBenchmarkName);
Benchmark.stop(benchmarkName);
}
private void processUserData() throws SQLException {
String benchmarkName = "Processing User Data";
String getDataBenchmarkName = "Getting User Data";
String insertDataIntoCollectionsBenchmarkName = "Insert User Data into Collections";
String insertDataIntoDatabaseBenchmarkName = "Insert User Data into Database";
Benchmark.start(benchmarkName);
Benchmark.start(getDataBenchmarkName);
List<UserImportData> userImportData = getUserImportData();
Benchmark.stop(getDataBenchmarkName);
if (Verify.isEmpty(userImportData)) {
@ -106,10 +153,14 @@ public abstract class Importer {
Database db = plan.getDB();
Set<UUID> existingUUIDs = db.getSavedUUIDs();
Set<UUID> existingUserInfoTableUUIDs = db.getUserInfoTable().getSavedUUIDs().get(serverUUID);
Benchmark.start(insertDataIntoCollectionsBenchmarkName);
Map<UUID, UserInfo> users = new Hashtable<>();
List<UserInfo> userInfo = new Vector<>();
Map<UUID, List<String>> nickNames = new Hashtable<>();
Map<UUID, Session> sessions = new Hashtable<>();
Map<UUID, List<Session>> sessions = new Hashtable<>();
Map<UUID, Map<String, String>> ips = new Hashtable<>();
Map<UUID, Integer> timesKicked = new Hashtable<>();
@ -118,24 +169,31 @@ public abstract class Importer {
UserInfo info = toUserInfo(data);
if (!existingUUIDs.contains(uuid)) {
users.put(uuid, info);
}
if (!existingUserInfoTableUUIDs.contains(uuid)) {
userInfo.add(info);
}
users.put(uuid, info);
nickNames.put(uuid, data.getNicknames());
ips.put(uuid, convertIPs(data));
timesKicked.put(uuid, data.getTimesKicked());
sessions.put(uuid, toSession(data));
sessions.put(uuid, Collections.singletonList(toSession(data)));
});
Benchmark.stop(insertDataIntoCollectionsBenchmarkName);
ExecutorService service = Executors.newCachedThreadPool();
Benchmark.start(insertDataIntoDatabaseBenchmarkName);
db.getUsersTable().insertUsers(users);
new ImportExecutorHelper() {
@Override
void execute() throws SQLException {
// TODO db.getSessionsTable().insertSessions(ImmutableMap.of(serverUUID, sessions));
db.getSessionsTable().insertSessions(ImmutableMap.of(serverUUID, sessions), true);
}
}.submit(service);
@ -144,7 +202,7 @@ public abstract class Importer {
void execute() throws SQLException {
db.getUsersTable().updateKicked(timesKicked);
}
}.submit(service);
}.submit(service);
new ImportExecutorHelper() {
@Override
@ -172,9 +230,11 @@ public abstract class Importer {
try {
service.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
Log.toLog(this.getClass().getName(), e);
}
Benchmark.stop(insertDataIntoDatabaseBenchmarkName);
Benchmark.stop(benchmarkName);
}

View File

@ -7,9 +7,11 @@ package main.java.com.djrapitops.plan.systems.processing.importing.importers;
import main.java.com.djrapitops.plan.systems.processing.importing.ServerImportData;
import main.java.com.djrapitops.plan.systems.processing.importing.UserImportData;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.Vector;
/**
@ -32,13 +34,23 @@ public class OfflinePlayerImporter extends Importer {
public List<UserImportData> getUserImportData() {
List<UserImportData> dataList = new Vector<>();
Set<OfflinePlayer> operators = Bukkit.getOperators();
Set<OfflinePlayer> banned = Bukkit.getBannedPlayers();
Arrays.stream(Bukkit.getOfflinePlayers()).parallel().forEach(player -> {
UserImportData.UserImportDataBuilder builder = UserImportData.builder();
builder.name(player.getName())
.uuid(player.getUniqueId())
.op(player.isOp())
.registered(player.getFirstPlayed())
.banned(player.isBanned());
.registered(player.getFirstPlayed());
if (operators.contains(player)) {
builder.op();
}
if (banned.contains(player)) {
builder.banned();
}
dataList.add(builder.build());
});

View File

@ -219,9 +219,6 @@ public class TestInit {
return configuration;
}
/**
* @return
*/
public Plan getPlanMock() {
return planMock;
}