Solved some new code smells

- Return empty set instead of null FilterQuery#getSetParameters
- DateRangeFilter constructor is protected
- String constant JSONFileStorage
- String constant VersionChecker
- String constants FLOOR(, MIN(, MAX( TPSQueries
This commit is contained in:
Risto Lahtela 2021-01-30 19:17:59 +02:00
parent 76dc1a98a7
commit 1c71922b04
6 changed files with 46 additions and 35 deletions

View File

@ -20,10 +20,7 @@ import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.*;
/**
* Represents parameters for a single {@link Filter} parsed from the query json.
@ -54,7 +51,7 @@ public class FilterQuery {
}
public Set<String> getSetParameters() {
if (parameters == null) return null;
if (parameters == null) return Collections.emptySet();
return parameters.keySet();
}
}

View File

@ -32,7 +32,7 @@ public abstract class DateRangeFilter implements Filter {
private final DBSystem dbSystem;
private final SimpleDateFormat dateFormat;
public DateRangeFilter(DBSystem dbSystem) {
protected DateRangeFilter(DBSystem dbSystem) {
this.dbSystem = dbSystem;
this.dateFormat = new SimpleDateFormat("dd/MM/yyyy kk:mm");
}

View File

@ -47,32 +47,32 @@ public class TPSQueries {
public static Query<List<TPS>> fetchTPSDataOfServer(UUID serverUUID) {
return db -> {
String selectLowestResolution = SELECT +
"MIN(t." + DATE + ") as " + DATE + ',' +
"MIN(t." + TPS + ") as " + TPS + ',' +
"MAX(t." + PLAYERS_ONLINE + ") as " + PLAYERS_ONLINE + ',' +
"MAX(t." + RAM_USAGE + ") as " + RAM_USAGE + ',' +
"MAX(t." + CPU_USAGE + ") as " + CPU_USAGE + ',' +
"MAX(t." + ENTITIES + ") as " + ENTITIES + ',' +
"MAX(t." + CHUNKS + ") as " + CHUNKS + ',' +
"MAX(t." + FREE_DISK + ") as " + FREE_DISK +
min("t." + DATE) + " as " + DATE + ',' +
min("t." + TPS) + " as " + TPS + ',' +
max("t." + PLAYERS_ONLINE) + " as " + PLAYERS_ONLINE + ',' +
max("t." + RAM_USAGE) + " as " + RAM_USAGE + ',' +
max("t." + CPU_USAGE) + " as " + CPU_USAGE + ',' +
max("t." + ENTITIES) + " as " + ENTITIES + ',' +
max("t." + CHUNKS) + " as " + CHUNKS + ',' +
max("t." + FREE_DISK) + " as " + FREE_DISK +
FROM + TABLE_NAME + " t" +
WHERE + SERVER_ID + "=" + ServerTable.STATEMENT_SELECT_SERVER_ID +
AND + DATE + "<?" +
GROUP_BY + "FLOOR(" + DATE + "/?)";
GROUP_BY + floor(DATE + "/?");
String selectLowerResolution = SELECT +
"MIN(t." + DATE + ") as " + DATE + ',' +
"MIN(t." + TPS + ") as " + TPS + ',' +
"MAX(t." + PLAYERS_ONLINE + ") as " + PLAYERS_ONLINE + ',' +
"MAX(t." + RAM_USAGE + ") as " + RAM_USAGE + ',' +
"MAX(t." + CPU_USAGE + ") as " + CPU_USAGE + ',' +
"MAX(t." + ENTITIES + ") as " + ENTITIES + ',' +
"MAX(t." + CHUNKS + ") as " + CHUNKS + ',' +
"MAX(t." + FREE_DISK + ") as " + FREE_DISK +
min("t." + DATE) + " as " + DATE + ',' +
min("t." + TPS) + " as " + TPS + ',' +
max("t." + PLAYERS_ONLINE) + " as " + PLAYERS_ONLINE + ',' +
max("t." + RAM_USAGE) + " as " + RAM_USAGE + ',' +
max("t." + CPU_USAGE) + " as " + CPU_USAGE + ',' +
max("t." + ENTITIES) + " as " + ENTITIES + ',' +
max("t." + CHUNKS) + " as " + CHUNKS + ',' +
max("t." + FREE_DISK) + " as " + FREE_DISK +
FROM + TABLE_NAME + " t" +
WHERE + SERVER_ID + "=" + ServerTable.STATEMENT_SELECT_SERVER_ID +
AND + DATE + ">=?" +
AND + DATE + "<?" +
GROUP_BY + "FLOOR(" + DATE + "/?)";
GROUP_BY + floor(DATE + "/?");
String selectNormalResolution = SELECT +
DATE + ',' + TPS + ',' + PLAYERS_ONLINE + ',' +
RAM_USAGE + ',' + CPU_USAGE + ',' + ENTITIES + ',' + CHUNKS + ',' + FREE_DISK +
@ -155,11 +155,11 @@ public class TPSQueries {
}
public static Query<List<DateObj<Integer>>> fetchQueryPreviewPlayersOnline(UUID serverUUID) {
String sql = SELECT + "MIN(" + DATE + ") as " + DATE + ',' +
"MAX(" + PLAYERS_ONLINE + ") as " + PLAYERS_ONLINE +
String sql = SELECT + min(DATE) + " as " + DATE + ',' +
max(PLAYERS_ONLINE) + " as " + PLAYERS_ONLINE +
FROM + TABLE_NAME +
WHERE + SERVER_ID + "=" + ServerTable.STATEMENT_SELECT_SERVER_ID +
GROUP_BY + "FLOOR(" + DATE + "/?)";
GROUP_BY + floor(DATE + "/?");
return new QueryStatement<List<DateObj<Integer>>>(sql) {
@Override

View File

@ -49,10 +49,21 @@ public abstract class Sql {
public static final String IS_NULL = " IS NULL";
public static final String IS_NOT_NULL = " IS NOT NULL";
private static final String FLOOR = "FLOOR(";
private static final String MIN = "MIN(";
private static final String MAX = "MAX(";
private static final String VARCHAR = "varchar(";
public static String varchar(int length) {
return "varchar(" + length + ')';
return VARCHAR + length + ')';
}
public static String floor(String expression) {return FLOOR + expression + ')';}
public static String min(String expression) {return MIN + expression + ')';}
public static String max(String expression) {return MAX + expression + ')';}
/**
* Turn day of week to epoch ms.
* <p>

View File

@ -44,7 +44,9 @@ public class JSONFileStorage implements JSONStorage {
private final PluginLogger logger;
private final Path jsonDirectory;
private final Pattern timestampRegex = Pattern.compile(".*-([0-9]*).json");
private final String jsonFileExtension = ".json";
@Inject
public JSONFileStorage(PlanFiles files, PluginLogger logger) {
@ -55,7 +57,7 @@ public class JSONFileStorage implements JSONStorage {
@Override
public StoredJSON storeJson(String identifier, String json, long timestamp) {
Path writingTo = jsonDirectory.resolve(identifier + '-' + timestamp + ".json");
Path writingTo = jsonDirectory.resolve(identifier + '-' + timestamp + jsonFileExtension);
try {
Files.createDirectories(jsonDirectory);
Files.write(writingTo, json.getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE);
@ -71,7 +73,7 @@ public class JSONFileStorage implements JSONStorage {
if (stored == null) return Optional.empty();
for (File file : stored) {
String fileName = file.getName();
if (fileName.endsWith(".json") && fileName.startsWith(identifier)) {
if (fileName.endsWith(jsonFileExtension) && fileName.startsWith(identifier)) {
return Optional.ofNullable(readStoredJSON(file));
}
}
@ -99,7 +101,7 @@ public class JSONFileStorage implements JSONStorage {
@Override
public Optional<StoredJSON> fetchExactJson(String identifier, long timestamp) {
File found = jsonDirectory.resolve(identifier + "-" + timestamp + ".json").toFile();
File found = jsonDirectory.resolve(identifier + "-" + timestamp + jsonFileExtension).toFile();
if (!found.exists()) return Optional.empty();
return Optional.ofNullable(readStoredJSON(found));
}
@ -120,7 +122,7 @@ public class JSONFileStorage implements JSONStorage {
for (File file : stored) {
try {
String fileName = file.getName();
if (fileName.endsWith(".json") && fileName.startsWith(identifier)) {
if (fileName.endsWith(jsonFileExtension) && fileName.startsWith(identifier)) {
Matcher timestampMatch = timestampRegex.matcher(fileName);
if (timestampMatch.find() && timestampComparator.test(timestampMatch, timestamp)) {
return Optional.ofNullable(readStoredJSON(file));

View File

@ -51,6 +51,7 @@ public class VersionChecker implements SubSystem {
private final PluginLogger logger;
private final RunnableFactory runnableFactory;
private final ErrorLogger errorLogger;
private final String downloadIconHtml = "<i class=\"fa fa-fw fa-download\"></i> ";
private VersionInfo newVersionAvailable;
@ -131,7 +132,7 @@ public class VersionChecker implements SubSystem {
"font-size: 0.95rem;" : "";
return "<button class=\"btn bg-white col-plan\" style=\"" + reduceFontSize +
"\" data-target=\"#updateModal\" data-toggle=\"modal\" type=\"button\">" +
"<i class=\"fa fa-fw fa-download\"></i> " + locale.getString(PluginLang.VERSION_UPDATE) + ": " + v.getVersion().getVersionString() +
downloadIconHtml + locale.getString(PluginLang.VERSION_UPDATE) + ": " + v.getVersion().getVersionString() +
"</button>";
}
);
@ -147,7 +148,7 @@ public class VersionChecker implements SubSystem {
return getNewVersionAvailable()
.map(v -> "<div class=\"modal-header\">" +
"<h5 class=\"modal-title\" id=\"updateModalLabel\">" +
"<i class=\"fa fa-fw fa-download\"></i> " + locale.getString(PluginLang.VERSION_UPDATE_AVAILABLE, v.getVersion().getVersionString()) +
downloadIconHtml + locale.getString(PluginLang.VERSION_UPDATE_AVAILABLE, v.getVersion().getVersionString()) +
"</h5><button aria-label=\"Close\" class=\"close\" data-dismiss=\"modal\" type=\"button\"><span aria-hidden=\"true\">&times;</span></button>" +
"</div>" + // Close modal-header
"<div class=\"modal-body\">" +
@ -156,7 +157,7 @@ public class VersionChecker implements SubSystem {
"<a class=\"btn col-plan\" href=\"" + v.getChangeLogUrl() + "\" rel=\"noopener noreferrer\" target=\"_blank\">" +
"<i class=\"fa fa-fw fa-list\"></i> " + locale.getString(PluginLang.VERSION_CHANGE_LOG) + "</a>" +
"<a class=\"btn col-plan\" href=\"" + v.getDownloadUrl() + "\" rel=\"noopener noreferrer\" target=\"_blank\">" +
"<i class=\"fa fa-fw fa-download\"></i> " + locale.getString(PluginLang.VERSION_DOWNLOAD, v.getVersion().getVersionString()) + "</a>" +
downloadIconHtml + locale.getString(PluginLang.VERSION_DOWNLOAD, v.getVersion().getVersionString()) + "</a>" +
"</div>") // Close modal-body
.orElse("<div class=\"modal-header\">" +
"<h5 class=\"modal-title\" id=\"updateModalLabel\">" +