Fixed new sonar smells

- Reduced constructor parameters in Exporter
- Removed Throwable#printStackTrace calls
- Cleaned up SessionsMutator#toJSONMaps a little
- Replaced uses of getString/Number/ methods of PlanConfig with PlanConfig#get
This commit is contained in:
Rsl1122 2020-01-09 17:46:05 +02:00
parent a5219dd901
commit 8dcfd3ad9a
17 changed files with 59 additions and 138 deletions

View File

@ -19,7 +19,6 @@ package com.djrapitops.plan;
import com.djrapitops.plan.commands.PlanCommand;
import com.djrapitops.plan.gathering.ServerShutdownSave;
import com.djrapitops.plan.modules.APFModule;
import com.djrapitops.plan.modules.FilesModule;
import com.djrapitops.plan.modules.SystemObjectProvidingModule;
import com.djrapitops.plan.modules.bukkit.BukkitPlanModule;
import com.djrapitops.plan.modules.bukkit.BukkitServerPropertiesModule;
@ -39,7 +38,7 @@ import javax.inject.Singleton;
BukkitPlanModule.class,
SystemObjectProvidingModule.class,
APFModule.class,
FilesModule.class,
BukkitServerPropertiesModule.class,
BukkitSuperClassBindingModule.class
})

View File

@ -18,7 +18,6 @@ package com.djrapitops.plan;
import com.djrapitops.plan.commands.PlanProxyCommand;
import com.djrapitops.plan.modules.APFModule;
import com.djrapitops.plan.modules.FilesModule;
import com.djrapitops.plan.modules.ProxySuperClassBindingModule;
import com.djrapitops.plan.modules.SystemObjectProvidingModule;
import com.djrapitops.plan.modules.bungee.BungeeCommandModule;
@ -41,7 +40,7 @@ import javax.inject.Singleton;
BungeeCommandModule.class,
SystemObjectProvidingModule.class,
APFModule.class,
FilesModule.class,
ProxySuperClassBindingModule.class,
BungeeSuperClassBindingModule.class,
BungeeServerPropertiesModule.class

View File

@ -196,7 +196,7 @@ public class SessionsMutator {
private Predicate<Session> getBetweenPredicate(long after, long before) {
return session -> {
Long start = session.getUnsafe(SessionKeys.START);
Long end = session.getValue(SessionKeys.END).orElse(System.currentTimeMillis());
long end = session.getValue(SessionKeys.END).orElse(System.currentTimeMillis());
return (after <= start && start <= before) || (after <= end && end <= before);
};
}
@ -265,12 +265,16 @@ public class SessionsMutator {
) {
return Lists.map(sessions, session -> {
Map<String, Object> sessionMap = new HashMap<>();
sessionMap.put("player_name", session.getValue(SessionKeys.NAME).orElse(session.getUnsafe(SessionKeys.UUID).toString()));
sessionMap.put("player_url_name", Html.encodeToURL((String) sessionMap.get("player_name")));
sessionMap.put("player_uuid", session.getUnsafe(SessionKeys.UUID).toString());
sessionMap.put("server_name", session.getValue(SessionKeys.SERVER_NAME).orElse(session.getUnsafe(SessionKeys.SERVER_UUID).toString()));
sessionMap.put("server_url_name", Html.encodeToURL((String) sessionMap.get("server_name")));
sessionMap.put("server_uuid", session.getUnsafe(SessionKeys.SERVER_UUID).toString());
String playerUUID = session.getUnsafe(SessionKeys.UUID).toString();
String serverUUID = session.getUnsafe(SessionKeys.SERVER_UUID).toString();
String playerName = session.getValue(SessionKeys.NAME).orElse(playerUUID);
String serverName = session.getValue(SessionKeys.SERVER_NAME).orElse(serverUUID);
sessionMap.put("player_name", playerName);
sessionMap.put("player_url_name", Html.encodeToURL(playerName));
sessionMap.put("player_uuid", playerUUID);
sessionMap.put("server_name", serverName);
sessionMap.put("server_url_name", Html.encodeToURL(serverName));
sessionMap.put("server_uuid", serverUUID);
sessionMap.put("name", nameFunction.apply(sessionMap));
sessionMap.put("start", session.getValue(SessionKeys.START).map(formatters.yearLong()).orElse("-") +
(session.supports(SessionKeys.END) ? "" : " (Online)"));

View File

@ -22,14 +22,11 @@ import com.djrapitops.plan.exceptions.connection.NotFoundException;
import com.djrapitops.plan.identification.Server;
import com.djrapitops.plan.settings.config.PlanConfig;
import com.djrapitops.plan.settings.config.paths.ExportSettings;
import com.djrapitops.plan.storage.file.PlanFiles;
import com.djrapitops.plugin.logging.console.PluginLogger;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
@ -42,61 +39,34 @@ import java.util.UUID;
@Singleton
public class Exporter extends FileExporter {
private final PlanFiles files;
private final PlanConfig config;
private final PlayerJSONExporter playerJSONExporter;
private final PlayerPageExporter playerPageExporter;
private final PlayersPageExporter playersPageExporter;
private final ServerPageExporter serverPageExporter;
private final NetworkPageExporter networkPageExporter;
private final PluginLogger logger;
private final Set<UUID> failedServers;
@Inject
public Exporter(
PlanFiles files,
PlanConfig config,
PlayerJSONExporter playerJSONExporter,
PlayerPageExporter playerPageExporter,
PlayersPageExporter playersPageExporter,
ServerPageExporter serverPageExporter,
NetworkPageExporter networkPageExporter,
PluginLogger logger
NetworkPageExporter networkPageExporter
) {
this.files = files;
this.config = config;
this.playerJSONExporter = playerJSONExporter;
this.playerPageExporter = playerPageExporter;
this.playersPageExporter = playersPageExporter;
this.serverPageExporter = serverPageExporter;
this.networkPageExporter = networkPageExporter;
this.logger = logger;
failedServers = new HashSet<>();
}
private Path getPageExportDirectory() {
Path exportDirectory = Paths.get(config.get(ExportSettings.HTML_EXPORT_PATH));
Path webDirectory = files.getDataDirectory().resolve("web");
if (exportDirectory.toAbsolutePath().equals(webDirectory.toAbsolutePath())) {
logger.warn("'" + ExportSettings.HTML_EXPORT_PATH.getPath() + "' can not be '/Plan/web/' directory, using '/Plan/Analysis Results' as fallback.");
exportDirectory = files.getDataDirectory().resolve("Analysis Results");
}
return exportDirectory.isAbsolute()
? exportDirectory
: files.getDataDirectory().resolve(exportDirectory);
}
private Path getJSONExportDirectory() {
Path exportDirectory = Paths.get(config.get(ExportSettings.JSON_EXPORT_PATH));
return exportDirectory.isAbsolute()
? exportDirectory
: files.getDataDirectory().resolve(exportDirectory);
}
/**
* Export a page of a server.
*
@ -109,7 +79,7 @@ public class Exporter extends FileExporter {
if (failedServers.contains(serverUUID) || config.isFalse(ExportSettings.SERVER_PAGE)) return false;
try {
Path toDirectory = getPageExportDirectory();
Path toDirectory = config.getPageExportPath();
if (server.isProxy()) {
networkPageExporter.export(toDirectory, server);
} else {
@ -127,7 +97,7 @@ public class Exporter extends FileExporter {
if (failedServers.contains(serverUUID) || config.isFalse(ExportSettings.SERVER_JSON)) return false;
try {
Path toDirectory = getJSONExportDirectory().resolve(toFileName(server.getName()));
Path toDirectory = config.getJSONExportPath().resolve(toFileName(server.getName()));
if (server.isProxy()) {
networkPageExporter.exportJSON(toDirectory, server);
} else {
@ -149,7 +119,7 @@ public class Exporter extends FileExporter {
* @throws ExportException If the export failed due to IO, NotFound or GenerationException.
*/
public boolean exportPlayerPage(UUID playerUUID, String playerName) throws ExportException {
Path toDirectory = getPageExportDirectory();
Path toDirectory = config.getPageExportPath();
if (config.isFalse(ExportSettings.PLAYER_PAGES)) return false;
try {
@ -161,7 +131,7 @@ public class Exporter extends FileExporter {
}
public boolean exportPlayersPage() throws ExportException {
Path toDirectory = getPageExportDirectory();
Path toDirectory = config.getPageExportPath();
if (config.isFalse(ExportSettings.PLAYERS_PAGE)) return false;
try {
@ -181,7 +151,7 @@ public class Exporter extends FileExporter {
* @throws ExportException If the export failed due to IOException.
*/
public boolean exportPlayerJSON(UUID playerUUID, String playerName) throws ExportException {
Path toDirectory = getJSONExportDirectory();
Path toDirectory = config.getJSONExportPath();
if (config.isFalse(ExportSettings.PLAYER_JSON)) return false;
try {

View File

@ -205,7 +205,7 @@ public class JSONFactory {
TPSMutator tpsWeek = tpsMonth.filterDataBetween(weekAgo, now);
double averageTPS = tpsWeek.averageTPS();
server.put("avg_tps", averageTPS != -1 ? decimals.apply(averageTPS) : locale.get(HtmlLang.UNIT_NO_DATA).toString());
server.put("low_tps_spikes", tpsWeek.lowTpsSpikeCount(config.getNumber(DisplaySettings.GRAPH_TPS_THRESHOLD_MED)));
server.put("low_tps_spikes", tpsWeek.lowTpsSpikeCount(config.get(DisplaySettings.GRAPH_TPS_THRESHOLD_MED)));
server.put("downtime", timeAmount.apply(tpsWeek.serverDownTime()));
Optional<TPS> online = tpsWeek.getLast();

View File

@ -193,7 +193,7 @@ public class OnlineActivityOverviewJSONCreator implements ServerTabJSONCreator<M
TPSMutator tpsMutator = new TPSMutator(db.query(TPSQueries.fetchTPSDataOfServer(monthAgo, now, serverUUID)));
numbers.put("average_tps", decimalFormatter.apply(tpsMutator.averageTPS()));
numbers.put("low_tps_spikes", tpsMutator.lowTpsSpikeCount(config.getNumber(DisplaySettings.GRAPH_TPS_THRESHOLD_MED)));
numbers.put("low_tps_spikes", tpsMutator.lowTpsSpikeCount(config.get(DisplaySettings.GRAPH_TPS_THRESHOLD_MED)));
numbers.put("downtime", timeAmountFormatter.apply(tpsMutator.serverDownTime()));
return numbers;

View File

@ -112,7 +112,7 @@ public class ServerOverviewJSONCreator implements ServerTabJSONCreator<Map<Strin
TPSMutator tpsMutator = new TPSMutator(db.query(TPSQueries.fetchTPSDataOfServer(weekAgo, now, serverUUID)));
double averageTPS = tpsMutator.averageTPS();
sevenDays.put("average_tps", averageTPS != -1 ? decimals.apply(averageTPS) : locale.get(GenericLang.UNAVAILABLE).toString());
sevenDays.put("low_tps_spikes", tpsMutator.lowTpsSpikeCount(config.getNumber(DisplaySettings.GRAPH_TPS_THRESHOLD_MED)));
sevenDays.put("low_tps_spikes", tpsMutator.lowTpsSpikeCount(config.get(DisplaySettings.GRAPH_TPS_THRESHOLD_MED)));
sevenDays.put("downtime", timeAmount.apply(tpsMutator.serverDownTime()));
return sevenDays;

View File

@ -110,7 +110,7 @@ public class RequestHandler implements HttpHandler {
responseHeaders.set("WWW-Authenticate", response.getHeader("WWW-Authenticate").orElse("Basic realm=\"Plan WebUser (/plan register)\""));
}
responseHeaders.set("Access-Control-Allow-Origin", config.getString(WebserverSettings.CORS_ALLOW_ORIGIN));
responseHeaders.set("Access-Control-Allow-Origin", config.get(WebserverSettings.CORS_ALLOW_ORIGIN));
responseHeaders.set("Access-Control-Allow-Methods", "GET, OPTIONS");
response.setResponseHeaders(responseHeaders);
response.send(exchange, locale, theme);

View File

@ -46,7 +46,6 @@ public interface Geolocator {
InetAddress inetAddress = InetAddress.getByName(address);
return getCountry(inetAddress);
} catch (UnknownHostException e) {
e.printStackTrace();
return Optional.empty();
}
}

View File

@ -67,7 +67,6 @@ public class IP2CGeolocator implements Geolocator {
try {
return readIPFromURL(address);
} catch (IOException e) {
e.printStackTrace();
return Optional.empty();
}
}

View File

@ -1,42 +0,0 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.modules;
import com.djrapitops.plan.storage.file.PlanFiles;
import dagger.Module;
import dagger.Provides;
import javax.inject.Named;
import javax.inject.Singleton;
import java.io.File;
/**
* Dagger Module for the Plan files.
*
* @author Rsl1122
*/
@Module
public class FilesModule {
@Provides
@Named("configFile")
@Singleton
File provideConfigFile(PlanFiles files) {
return files.getConfigFile();
}
}

View File

@ -93,7 +93,7 @@ public abstract class ConfigSystem implements SubSystem {
}
public void checkWrongTimeZone() {
String timeZone = config.getString(FormatSettings.TIMEZONE);
String timeZone = config.get(FormatSettings.TIMEZONE);
Optional<TimeZone> foundTZ = TimeZoneUtility.parseTimeZone(timeZone);
if (!foundTZ.isPresent()) {
logger.warn("Config: " + FormatSettings.TIMEZONE.getPath() + " has invalid value '" + timeZone + "', using GMT+0");

View File

@ -16,17 +16,18 @@
*/
package com.djrapitops.plan.settings.config;
import com.djrapitops.plan.settings.config.paths.ExportSettings;
import com.djrapitops.plan.settings.config.paths.FormatSettings;
import com.djrapitops.plan.settings.config.paths.key.Setting;
import com.djrapitops.plan.storage.file.PlanFiles;
import com.djrapitops.plugin.logging.console.PluginLogger;
import com.djrapitops.plugin.utilities.Verify;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.ZoneId;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.TimeZone;
@ -40,22 +41,23 @@ import java.util.concurrent.TimeUnit;
@Singleton
public class PlanConfig extends Config {
private final PlanFiles files;
private final ExtensionSettings extensionSettings;
private final WorldAliasSettings worldAliasSettings;
private final PluginLogger logger;
@Inject
public PlanConfig(
@Named("configFile") File file,
PlanFiles files,
WorldAliasSettings worldAliasSettings,
PluginLogger logger
) {
super(file);
super(files.getConfigFile());
this.files = files;
this.extensionSettings = new ExtensionSettings(this);
this.worldAliasSettings = worldAliasSettings;
this.logger = logger;
extensionSettings = new ExtensionSettings(this);
}
public <T> T get(Setting<T> setting) {
@ -83,38 +85,12 @@ public class PlanConfig extends Config {
return !isTrue(setting);
}
/**
* If the settings is a String, this method should be used.
*
* @return String value of the config setting.
*/
public String getString(Setting<String> setting) {
return get(setting);
}
/**
* If the settings is a number, this method should be used.
*
* @return Integer value of the config setting
*/
public int getNumber(Setting<Integer> setting) {
return get(setting);
}
public List<String> getStringList(Setting<List<String>> setting) {
return get(setting);
}
public ConfigNode getConfigNode(Setting<ConfigNode> setting) {
return get(setting);
}
public <T> void set(Setting<T> setting, T value) {
set(setting.getPath(), value);
}
public TimeZone getTimeZone() {
String timeZone = getString(FormatSettings.TIMEZONE);
String timeZone = get(FormatSettings.TIMEZONE);
Optional<TimeZone> foundTZ = TimeZoneUtility.parseTimeZone(timeZone);
return foundTZ.orElse(TimeZone.getTimeZone(ZoneId.of("UTC")));
}
@ -125,6 +101,27 @@ public class PlanConfig extends Config {
return -offset / hourMs;
}
public Path getPageExportPath() {
Path exportDirectory = Paths.get(get(ExportSettings.HTML_EXPORT_PATH));
Path webDirectory = files.getDataDirectory().resolve("web");
if (exportDirectory.toAbsolutePath().equals(webDirectory.toAbsolutePath())) {
logger.warn("'" + ExportSettings.HTML_EXPORT_PATH.getPath() + "' can not be '/Plan/web/' directory, using '/Plan/Analysis Results' as fallback.");
exportDirectory = files.getDataDirectory().resolve("Analysis Results");
}
return exportDirectory.isAbsolute()
? exportDirectory
: files.getDataDirectory().resolve(exportDirectory);
}
public Path getJSONExportPath() {
Path exportDirectory = Paths.get(get(ExportSettings.JSON_EXPORT_PATH));
return exportDirectory.isAbsolute()
? exportDirectory
: files.getDataDirectory().resolve(exportDirectory);
}
public ExtensionSettings getExtensionSettings() {
return extensionSettings;
}

View File

@ -20,7 +20,6 @@ import com.djrapitops.plan.PlanPlugin;
import com.djrapitops.plan.PlanSystem;
import com.djrapitops.plan.commands.PlanCommand;
import com.djrapitops.plan.modules.APFModule;
import com.djrapitops.plan.modules.FilesModule;
import com.djrapitops.plan.modules.SystemObjectProvidingModule;
import dagger.BindsInstance;
import dagger.Component;
@ -37,7 +36,7 @@ import javax.inject.Singleton;
PlanPluginModule.class,
SystemObjectProvidingModule.class,
APFModule.class,
FilesModule.class,
PluginServerPropertiesModule.class,
PluginSuperClassBindingModule.class
})

View File

@ -19,7 +19,6 @@ package com.djrapitops.plan;
import com.djrapitops.plan.commands.PlanCommand;
import com.djrapitops.plan.gathering.ServerShutdownSave;
import com.djrapitops.plan.modules.APFModule;
import com.djrapitops.plan.modules.FilesModule;
import com.djrapitops.plan.modules.SystemObjectProvidingModule;
import com.djrapitops.plan.modules.nukkit.NukkitPlanModule;
import com.djrapitops.plan.modules.nukkit.NukkitServerPropertiesModule;
@ -39,7 +38,7 @@ import javax.inject.Singleton;
NukkitPlanModule.class,
SystemObjectProvidingModule.class,
APFModule.class,
FilesModule.class,
NukkitServerPropertiesModule.class,
NukkitSuperClassBindingModule.class
})

View File

@ -19,7 +19,6 @@ package com.djrapitops.plan;
import com.djrapitops.plan.commands.PlanCommand;
import com.djrapitops.plan.gathering.ServerShutdownSave;
import com.djrapitops.plan.modules.APFModule;
import com.djrapitops.plan.modules.FilesModule;
import com.djrapitops.plan.modules.SystemObjectProvidingModule;
import com.djrapitops.plan.modules.sponge.SpongePlanModule;
import com.djrapitops.plan.modules.sponge.SpongeServerPropertiesModule;
@ -39,7 +38,7 @@ import javax.inject.Singleton;
SpongePlanModule.class,
SystemObjectProvidingModule.class,
APFModule.class,
FilesModule.class,
SpongeSuperClassBindingModule.class,
SpongeServerPropertiesModule.class
})

View File

@ -18,7 +18,6 @@ package com.djrapitops.plan;
import com.djrapitops.plan.commands.PlanProxyCommand;
import com.djrapitops.plan.modules.APFModule;
import com.djrapitops.plan.modules.FilesModule;
import com.djrapitops.plan.modules.ProxySuperClassBindingModule;
import com.djrapitops.plan.modules.SystemObjectProvidingModule;
import com.djrapitops.plan.modules.velocity.VelocityCommandModule;
@ -41,7 +40,7 @@ import javax.inject.Singleton;
VelocityCommandModule.class,
SystemObjectProvidingModule.class,
APFModule.class,
FilesModule.class,
ProxySuperClassBindingModule.class,
VelocitySuperClassBindingModule.class,
VelocityServerPropertiesModule.class