Fixed AnalyzeCommand

This commit is contained in:
Rsl1122 2018-01-24 18:53:00 +02:00
parent d5fcb1edf0
commit 9b0d42587b
11 changed files with 304 additions and 97 deletions

View File

@ -1,12 +1,20 @@
package com.djrapitops.plan.command.commands;
import com.djrapitops.plan.Plan;
import com.djrapitops.plan.api.exceptions.connection.WebException;
import com.djrapitops.plan.api.exceptions.database.DBException;
import com.djrapitops.plan.settings.locale.Locale;
import com.djrapitops.plan.settings.locale.Msg;
import com.djrapitops.plan.system.database.databases.Database;
import com.djrapitops.plan.system.info.InfoSystem;
import com.djrapitops.plan.system.info.connection.ConnectionSystem;
import com.djrapitops.plan.system.info.request.GenerateAnalysisPageRequest;
import com.djrapitops.plan.system.info.server.Server;
import com.djrapitops.plan.system.info.server.ServerInfo;
import com.djrapitops.plan.system.processing.processors.Processor;
import com.djrapitops.plan.system.settings.Permissions;
import com.djrapitops.plan.systems.info.InformationManager;
import com.djrapitops.plan.system.webserver.WebServerSystem;
import com.djrapitops.plan.utilities.analysis.Analysis;
import com.djrapitops.plugin.api.utility.log.Log;
import com.djrapitops.plugin.command.CommandType;
import com.djrapitops.plugin.command.CommandUtils;
@ -14,12 +22,9 @@ import com.djrapitops.plugin.command.ISender;
import com.djrapitops.plugin.command.SubCommand;
import com.djrapitops.plugin.task.AbsRunnable;
import com.djrapitops.plugin.task.RunnableFactory;
import com.djrapitops.plugin.utilities.Verify;
import org.bukkit.ChatColor;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
@ -31,9 +36,6 @@ import java.util.UUID;
*/
public class AnalyzeCommand extends SubCommand {
private final Plan plugin;
private final InformationManager infoManager;
/**
* Subcommand Constructor.
*
@ -45,34 +47,6 @@ public class AnalyzeCommand extends SubCommand {
Permissions.ANALYZE.getPermission(),
Locale.get(Msg.CMD_USG_ANALYZE).parse(),
"[ServerName or ID]");
this.plugin = plugin;
infoManager = plugin.getInfoManager();
}
public static void sendAnalysisMessage(Collection<ISender> senders, UUID serverUUID) throws DBException {
if (Verify.isEmpty(senders)) {
return;
}
Plan plugin = Plan.getInstance();
Optional<String> serverName = plugin.getDB().fetch().getServerName(serverUUID);
serverName.ifPresent(name -> {
String target = "/server/" + name;
String url = plugin.getInfoManager().getLinkTo(target);
String message = Locale.get(Msg.CMD_INFO_LINK).toString();
for (ISender sender : senders) {
sender.sendMessage(Locale.get(Msg.CMD_HEADER_ANALYZE).toString());
// Link
boolean console = !CommandUtils.isPlayer(sender);
if (console) {
sender.sendMessage(message + url);
} else {
sender.sendMessage(message);
sender.sendLink(" ", Locale.get(Msg.CMD_INFO_CLICK_ME).toString(), url);
}
sender.sendMessage(Locale.get(Msg.CMD_CONSTANT_FOOTER).toString());
}
});
}
@Override
@ -82,39 +56,52 @@ public class AnalyzeCommand extends SubCommand {
@Override
public boolean onCommand(ISender sender, String commandLabel, String[] args) {
UUID serverUUID = Plan.getServerUUID();
if (args.length >= 1 && plugin.getInfoManager().isUsingAnotherWebServer()) {
try {
List<Server> bukkitServers = plugin.getDB().getServerTable().getBukkitServers();
Optional<Server> server = bukkitServers.stream().filter(info -> {
StringBuilder idBuilder = new StringBuilder(args[0]);
if (args.length > 1) {
for (int i = 1; i < args.length; i++) {
idBuilder.append(" ").append(args[i]);
}
}
String serverIdentifier = idBuilder.toString();
return Integer.toString(info.getId()).equals(serverIdentifier) || info.getName().equalsIgnoreCase(serverIdentifier);
}).findFirst();
if (server.isPresent()) {
serverUUID = server.get().getUuid();
}
} catch (SQLException e) {
Log.toLog(this.getClass().getName(), e);
return true;
}
}
updateCache(sender, serverUUID);
sender.sendMessage(Locale.get(Msg.CMD_INFO_FETCH_DATA).toString());
if (plugin.getInfoManager().isAuthRequired() && CommandUtils.isPlayer(sender)) {
new Processor<ISender>(sender) {
@Override
public void process() {
try {
Server server = getServer(args).orElseGet(ServerInfo::getServer);
UUID serverUUID = server.getUuid();
if (!ServerInfo.getServerUUID().equals(serverUUID) || !Analysis.isAnalysisBeingRun()) {
InfoSystem.getInstance().sendRequest(new GenerateAnalysisPageRequest(serverUUID));
}
sendWebUserNotificationIfNecessary(sender);
sendLink(server, sender);
} catch (DBException | WebException e) {
// TODO Exception handling
sender.sendMessage(ChatColor.RED + " Error occurred: " + e.toString());
Log.toLog(this.getClass().getName(), e);
}
}
}.queue();
return true;
}
private void sendLink(Server server, ISender sender) {
String target = "/server/" + server.getName();
String url = ConnectionSystem.getAddress() + target;
String message = Locale.get(Msg.CMD_INFO_LINK).toString();
sender.sendMessage(Locale.get(Msg.CMD_HEADER_ANALYZE).toString());
// Link
boolean console = !CommandUtils.isPlayer(sender);
if (console) {
sender.sendMessage(message + url);
} else {
sender.sendMessage(message);
sender.sendLink(" ", Locale.get(Msg.CMD_INFO_CLICK_ME).toString(), url);
}
sender.sendMessage(Locale.get(Msg.CMD_CONSTANT_FOOTER).toString());
}
private void sendWebUserNotificationIfNecessary(ISender sender) {
if (WebServerSystem.getInstance().getWebServer().isAuthRequired() && CommandUtils.isPlayer(sender)) {
RunnableFactory.createNew(new AbsRunnable("WebUser exist check task") {
@Override
public void run() {
try {
boolean senderHasWebUser = plugin.getDB().getSecurityTable().userExists(sender.getName());
boolean senderHasWebUser = Database.getActive().check().doesWebUserExists(sender.getName());
if (!senderHasWebUser) {
sender.sendMessage(ChatColor.YELLOW + "[Plan] You might not have a web user, use /plan register <password>");
}
@ -126,11 +113,31 @@ public class AnalyzeCommand extends SubCommand {
}
}).runTaskAsynchronously();
}
return true;
}
private void updateCache(ISender sender, UUID serverUUID) {
infoManager.addAnalysisNotification(sender, serverUUID);
infoManager.refreshAnalysis(serverUUID);
private Optional<Server> getServer(String[] args) throws DBException {
if (args.length >= 1 && ConnectionSystem.getInstance().isServerAvailable()) {
Map<UUID, Server> bukkitServers = Database.getActive().fetch().getBukkitServers();
String serverIdentifier = getGivenIdentifier(args);
for (Map.Entry<UUID, Server> entry : bukkitServers.entrySet()) {
Server server = entry.getValue();
if (Integer.toString(server.getId()).equals(serverIdentifier)
|| server.getName().equalsIgnoreCase(serverIdentifier)) {
return Optional.of(server);
}
}
}
return Optional.empty();
}
private String getGivenIdentifier(String[] args) {
StringBuilder idBuilder = new StringBuilder(args[0]);
if (args.length > 1) {
for (int i = 1; i < args.length; i++) {
idBuilder.append(" ").append(args[i]);
}
}
return idBuilder.toString();
}
}

View File

@ -7,6 +7,7 @@ package com.djrapitops.plan.system.database.databases.operation;
import com.djrapitops.plan.api.exceptions.database.DBException;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
/**
@ -36,7 +37,7 @@ public interface TransferOperations {
Map<UUID, String> getEncodedServerHtml() throws DBException;
UUID getServerPlayerIsOnline(UUID playerUUID) throws DBException;
Optional<UUID> getServerPlayerIsOnlineOn(UUID playerUUID) throws DBException;
Map<UUID, String> getEncodedPlayerPluginsTabs(UUID playerUUID) throws DBException;
}

View File

@ -10,6 +10,7 @@ import com.djrapitops.plan.system.database.databases.sql.SQLDB;
import java.sql.SQLException;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
/**
@ -87,8 +88,12 @@ public class SQLTransferOps extends SQLOps implements TransferOperations {
}
@Override
public UUID getServerPlayerIsOnline(UUID playerUUID) {
return null; // TODO
public Optional<UUID> getServerPlayerIsOnlineOn(UUID playerUUID) throws DBException {
try {
return transferTable.getServerPlayerIsOnline(playerUUID);
} catch (SQLException e) {
throw SQLErrorUtil.getExceptionFor(e);
}
}
@Override

View File

@ -23,6 +23,7 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
/**
@ -75,6 +76,31 @@ public class TransferTable extends Table {
);
}
public void clean() throws SQLException {
String sql = "DELETE FROM " + tableName +
" WHERE " + columnExpiry + " < ?" +
" AND " + columnInfoType + " != ?";
execute(new ExecStatement(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setLong(1, MiscUtils.getTime() + TimeAmount.MINUTE.ms());
statement.setString(2, "onlineStatus");
}
});
sql = "DELETE FROM " + tableName +
" WHERE " + columnSenderID + " = " + serverTable.statementSelectServerID +
" AND " + columnInfoType + " != ?";
execute(new ExecStatement(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, ServerInfo.getServerUUID().toString());
statement.setString(2, "onlineStatus");
}
});
}
public void storePlayerHtml(UUID player, String encodedHtml) throws SQLException {
execute(new ExecStatement(insertStatement) {
@Override
@ -195,4 +221,43 @@ public class TransferTable extends Table {
}
});
}
public Optional<UUID> getServerPlayerIsOnline(UUID playerUUID) throws SQLException {
String serverIDColumn = serverTable + "." + serverTable.getColumnID();
String serverUUIDColumn = serverTable + "." + serverTable.getColumnUUID() + " as s_uuid";
String sql = "SELECT " +
serverUUIDColumn +
" FROM " + tableName +
" JOIN " + serverTable + " on " + serverIDColumn + "=" + columnSenderID +
" WHERE " + columnExtraVariables + "=?" +
" ORDER BY " + columnExpiry + " LIMIT 1";
return query(new QueryStatement<Optional<UUID>>(sql, 1) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, playerUUID.toString());
}
@Override
public Optional<UUID> processResults(ResultSet set) throws SQLException {
if (set.next()) {
return Optional.of(UUID.fromString(set.getString(columnExtraVariables)));
}
return Optional.empty();
}
});
}
public void storePlayerOnlineOnThisServer(UUID playerUUID) throws SQLException {
execute(new ExecStatement(insertStatement) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, ServerInfo.getServerUUID().toString());
statement.setLong(2, MiscUtils.getTime() + TimeAmount.MINUTE.ms());
statement.setString(3, "onlineStatus");
statement.setString(4, playerUUID.toString());
statement.setString(5, null);
}
});
}
}

View File

@ -5,13 +5,9 @@
package com.djrapitops.plan.system.info.connection;
import com.djrapitops.plan.api.exceptions.connection.NoServersException;
import com.djrapitops.plan.api.exceptions.connection.UnsupportedTransferDatabaseException;
import com.djrapitops.plan.api.exceptions.database.DBException;
import com.djrapitops.plan.system.database.databases.Database;
import com.djrapitops.plan.system.info.request.CacheRequest;
import com.djrapitops.plan.system.info.request.GenerateAnalysisPageRequest;
import com.djrapitops.plan.system.info.request.GenerateInspectPageRequest;
import com.djrapitops.plan.system.info.request.InfoRequest;
import com.djrapitops.plan.system.info.request.*;
import com.djrapitops.plan.system.info.server.Server;
import com.djrapitops.plan.system.info.server.ServerInfo;
import com.djrapitops.plan.utilities.MiscUtils;
@ -20,6 +16,7 @@ import com.djrapitops.plugin.api.utility.log.Log;
import com.djrapitops.plugin.task.AbsRunnable;
import com.djrapitops.plugin.task.RunnableFactory;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
@ -31,12 +28,14 @@ import java.util.UUID;
*/
public class BukkitConnectionSystem extends ConnectionSystem {
private long latestServerMapRefresh = 0;
private long latestServerMapRefresh;
private Server mainServer;
private Map<UUID, Server> servers;
public BukkitConnectionSystem() {
servers = new HashMap<>();
latestServerMapRefresh = 0;
}
private void refreshServerMap() {
@ -68,8 +67,10 @@ public class BukkitConnectionSystem extends ConnectionSystem {
UUID serverUUID = ((GenerateAnalysisPageRequest) infoRequest).getServerUUID();
server = servers.get(serverUUID);
} else if (infoRequest instanceof GenerateInspectPageRequest) {
UUID serverUUID = getServerWherePlayerIsOnline((GenerateInspectPageRequest) infoRequest);
server = servers.getOrDefault(serverUUID, ServerInfo.getServer());
Optional<UUID> serverUUID = getServerWherePlayerIsOnline((GenerateInspectPageRequest) infoRequest);
if (serverUUID.isPresent()) {
server = servers.getOrDefault(serverUUID.get(), ServerInfo.getServer());
}
}
if (server == null) {
throw new NoServersException("Proper server is not available to process requests.");
@ -77,16 +78,14 @@ public class BukkitConnectionSystem extends ConnectionSystem {
return server;
}
private UUID getServerWherePlayerIsOnline(GenerateInspectPageRequest infoRequest) {
UUID playerUUID = infoRequest.getPlayerUUID();
try {
return Database.getActive().transfer().getServerPlayerIsOnline(playerUUID);
} catch (UnsupportedTransferDatabaseException e) {
/* Do nothing */
} catch (DBException e) {
Log.toLog(this.getClass().getName(), e);
@Override
public void sendWideInfoRequest(WideRequest infoRequest) throws NoServersException {
if (servers.isEmpty()) {
throw new NoServersException("No Servers Available to make process request.");
}
for (Server server : servers.values()) {
WebExceptionLogger.logIfOccurs(this.getClass(), () -> sendInfoRequest(infoRequest, server));
}
return null;
}
@Override

View File

@ -4,10 +4,103 @@
*/
package com.djrapitops.plan.system.info.connection;
import com.djrapitops.plan.api.exceptions.connection.NoServersException;
import com.djrapitops.plan.api.exceptions.database.DBException;
import com.djrapitops.plan.system.database.databases.Database;
import com.djrapitops.plan.system.info.request.*;
import com.djrapitops.plan.system.info.server.Server;
import com.djrapitops.plan.system.info.server.ServerInfo;
import com.djrapitops.plan.system.webserver.WebServerSystem;
import com.djrapitops.plan.utilities.MiscUtils;
import com.djrapitops.plugin.api.TimeAmount;
import com.djrapitops.plugin.api.utility.log.Log;
import com.djrapitops.plugin.task.AbsRunnable;
import com.djrapitops.plugin.task.RunnableFactory;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
/**
* ConnectionSystem for Bungee.
*
* @author Rsl1122
*/
public class BungeeConnectionSystem extends ConnectionSystem {
private long latestServerMapRefresh;
private Map<UUID, Server> servers;
public BungeeConnectionSystem() {
servers = new HashMap<>();
latestServerMapRefresh = 0;
}
private void refreshServerMap() {
if (latestServerMapRefresh < MiscUtils.getTime() - TimeAmount.MINUTE.ms() * 2L) {
try {
servers = Database.getActive().fetch().getBukkitServers();
latestServerMapRefresh = MiscUtils.getTime();
} catch (DBException e) {
Log.toLog(this.getClass().getName(), e);
}
}
}
@Override
protected Server selectServerForRequest(InfoRequest infoRequest) throws NoServersException {
Server server = null;
if (infoRequest instanceof CacheRequest) {
throw new NoServersException("Bungee should not send Cache requests.");
} else if (infoRequest instanceof GenerateAnalysisPageRequest) {
UUID serverUUID = ((GenerateAnalysisPageRequest) infoRequest).getServerUUID();
server = servers.get(serverUUID);
} else if (infoRequest instanceof GenerateInspectPageRequest) {
Optional<UUID> serverUUID = getServerWherePlayerIsOnline((GenerateInspectPageRequest) infoRequest);
if (serverUUID.isPresent()) {
server = servers.getOrDefault(serverUUID.get(), ServerInfo.getServer());
}
}
if (server == null) {
throw new NoServersException("Proper server is not available to process requests.");
}
return server;
}
@Override
public void sendWideInfoRequest(WideRequest infoRequest) throws NoServersException {
if (servers.isEmpty()) {
throw new NoServersException("No Servers Available to make process request.");
}
for (Server server : servers.values()) {
WebExceptionLogger.logIfOccurs(this.getClass(), () -> sendInfoRequest(infoRequest, server));
}
}
@Override
public boolean isServerAvailable() {
return true;
}
@Override
public String getMainAddress() {
return WebServerSystem.getInstance().getWebServer().getAccessAddress();
}
@Override
public void enable() {
refreshServerMap();
RunnableFactory.createNew("Server List Update Task", new AbsRunnable() {
@Override
public void run() {
refreshServerMap();
}
}).runTaskTimerAsynchronously(TimeAmount.SECOND.ticks() * 30L, TimeAmount.MINUTE.ticks() * 5L);
}
@Override
public void disable() {
}
}

View File

@ -5,16 +5,21 @@
package com.djrapitops.plan.system.info.connection;
import com.djrapitops.plan.api.exceptions.connection.NoServersException;
import com.djrapitops.plan.api.exceptions.connection.UnsupportedTransferDatabaseException;
import com.djrapitops.plan.api.exceptions.connection.WebException;
import com.djrapitops.plan.api.exceptions.database.DBException;
import com.djrapitops.plan.system.SubSystem;
import com.djrapitops.plan.system.database.databases.Database;
import com.djrapitops.plan.system.info.InfoSystem;
import com.djrapitops.plan.system.info.request.*;
import com.djrapitops.plan.system.info.server.Server;
import com.djrapitops.plan.system.info.server.ServerInfo;
import com.djrapitops.plan.utilities.NullCheck;
import com.djrapitops.plugin.api.utility.log.Log;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
/**
@ -63,9 +68,17 @@ public abstract class ConnectionSystem implements SubSystem {
protected abstract Server selectServerForRequest(InfoRequest infoRequest) throws NoServersException;
public static String getAddress() {
return getInstance().getMainAddress();
}
public void sendInfoRequest(InfoRequest infoRequest) throws WebException {
Server server = selectServerForRequest(infoRequest);
new ConnectionOut(server, ServerInfo.getServerUUID(), infoRequest).sendRequest();
sendInfoRequest(infoRequest, server);
}
public void sendInfoRequest(InfoRequest infoRequest, Server toServer) throws WebException {
new ConnectionOut(toServer, ServerInfo.getServerUUID(), infoRequest).sendRequest();
}
public ConnectionLog getConnectionLog() {
@ -75,4 +88,18 @@ public abstract class ConnectionSystem implements SubSystem {
public abstract boolean isServerAvailable();
public abstract String getMainAddress();
public abstract void sendWideInfoRequest(WideRequest infoRequest) throws NoServersException;
protected Optional<UUID> getServerWherePlayerIsOnline(GenerateInspectPageRequest infoRequest) {
UUID playerUUID = infoRequest.getPlayerUUID();
try {
return Database.getActive().transfer().getServerPlayerIsOnlineOn(playerUUID);
} catch (UnsupportedTransferDatabaseException e) {
/* Do nothing */
} catch (DBException e) {
Log.toLog(this.getClass().getName(), e);
}
return Optional.empty();
}
}

View File

@ -23,7 +23,7 @@ import java.util.UUID;
*
* @author Rsl1122
*/
public class GenerateInspectPluginsTabRequest extends InfoRequestWithVariables implements GenerateRequest {
public class GenerateInspectPluginsTabRequest extends InfoRequestWithVariables implements GenerateRequest, WideRequest {
private final UUID playerUUID;

View File

@ -0,0 +1,13 @@
/*
* Licence is provided in the jar as license.yml also here:
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
*/
package com.djrapitops.plan.system.info.request;
/**
* InfoRequest that should be relayed to all Bukkit servers.
*
* @author Rsl1122
*/
public interface WideRequest extends InfoRequest {
}

View File

@ -10,7 +10,6 @@ import com.djrapitops.plan.api.exceptions.connection.ConnectionFailException;
import com.djrapitops.plan.api.exceptions.connection.NotFoundException;
import com.djrapitops.plan.api.exceptions.connection.WebException;
import com.djrapitops.plan.api.exceptions.connection.WebFailException;
import com.djrapitops.plan.command.commands.AnalyzeCommand;
import com.djrapitops.plan.data.AnalysisData;
import com.djrapitops.plan.settings.theme.Theme;
import com.djrapitops.plan.system.settings.Settings;
@ -32,7 +31,6 @@ import com.djrapitops.plan.utilities.analysis.Analysis;
import com.djrapitops.plan.utilities.file.export.HtmlExport;
import com.djrapitops.plugin.api.utility.log.Log;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Optional;
import java.util.UUID;
@ -200,11 +198,6 @@ public class BukkitInformationManager extends InformationManager {
@Override
public void analysisReady(UUID serverUUID) {
try {
AnalyzeCommand.sendAnalysisMessage(analysisNotification.get(serverUUID), serverUUID);
} catch (SQLException e) {
Log.toLog(this.getClass().getName(), e);
}
analysisNotification.getOrDefault(serverUUID, new HashSet<>()).clear();
}

View File

@ -181,4 +181,8 @@ public class Analysis {
Benchmark.stop("Analysis", "3rd party Analysis");
return containers;
}
public static boolean isAnalysisBeingRun() {
return serverProfile != null;
}
}