mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-01-09 09:57:35 +01:00
[Debt] Removed FormatUtils class.
Added instance variables for different Formatter objects with a TODO to accompany the additions where dagger injection was not yet possible. Moved formatIP to GeoInfo Moved getStackTrace to PromptAuthorizationResponse Affected issues: none
This commit is contained in:
parent
8d36b08acb
commit
fac06e7d80
@ -6,11 +6,11 @@ package com.djrapitops.plan.data.container;
|
||||
|
||||
import com.djrapitops.plan.data.store.objects.DateHolder;
|
||||
import com.djrapitops.plan.data.store.objects.DateMap;
|
||||
import com.djrapitops.plan.utilities.FormatUtils;
|
||||
import com.djrapitops.plan.utilities.SHA256Hash;
|
||||
import com.google.common.base.Objects;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.net.Inet6Address;
|
||||
import java.net.InetAddress;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
@ -27,7 +27,7 @@ public class GeoInfo implements DateHolder, Serializable {
|
||||
private final long date;
|
||||
|
||||
public GeoInfo(InetAddress address, String geolocation, long lastUsed) throws NoSuchAlgorithmException {
|
||||
this(FormatUtils.formatIP(address), geolocation, lastUsed, new SHA256Hash(address.getHostAddress()).create());
|
||||
this(formatIP(address), geolocation, lastUsed, new SHA256Hash(address.getHostAddress()).create());
|
||||
}
|
||||
|
||||
public GeoInfo(String ip, String geolocation, long date, String ipHash) {
|
||||
@ -45,6 +45,42 @@ public class GeoInfo implements DateHolder, Serializable {
|
||||
return map;
|
||||
}
|
||||
|
||||
static String formatIP(InetAddress address) {
|
||||
String ip = address.getHostAddress();
|
||||
if ("localhost".equals(ip)) {
|
||||
return ip;
|
||||
}
|
||||
if (address instanceof Inet6Address) {
|
||||
StringBuilder b = new StringBuilder();
|
||||
int i = 0;
|
||||
for (String part : ip.split(":")) {
|
||||
if (i >= 3) {
|
||||
break;
|
||||
}
|
||||
|
||||
b.append(part).append(':');
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
return b.append("xx..").toString();
|
||||
} else {
|
||||
StringBuilder b = new StringBuilder();
|
||||
int i = 0;
|
||||
for (String part : ip.split("\\.")) {
|
||||
if (i >= 2) {
|
||||
break;
|
||||
}
|
||||
|
||||
b.append(part).append('.');
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
return b.append("xx.xx").toString();
|
||||
}
|
||||
}
|
||||
|
||||
public String getIp() {
|
||||
return ip;
|
||||
}
|
||||
|
@ -4,7 +4,7 @@ import com.djrapitops.plan.data.container.Session;
|
||||
import com.djrapitops.plan.data.store.containers.DataContainer;
|
||||
import com.djrapitops.plan.data.store.keys.PlayerKeys;
|
||||
import com.djrapitops.plan.system.settings.Settings;
|
||||
import com.djrapitops.plan.utilities.FormatUtils;
|
||||
import com.djrapitops.plan.utilities.formatting.Formatter;
|
||||
import com.djrapitops.plugin.api.TimeAmount;
|
||||
|
||||
import java.util.List;
|
||||
@ -110,7 +110,8 @@ public class ActivityIndex {
|
||||
}
|
||||
|
||||
public String getFormattedValue() {
|
||||
return FormatUtils.cutDecimals(value);
|
||||
Formatter<Double> decimalFormatter = null; // TODO Add as method parameter
|
||||
return decimalFormatter.apply(value);
|
||||
}
|
||||
|
||||
public String getGroup() {
|
||||
|
@ -21,6 +21,7 @@ public abstract class AbstractHealthInfo {
|
||||
|
||||
// TODO
|
||||
protected Formatter<Long> timeAmountFormatter;
|
||||
protected Formatter<Double> decimalFormatter;
|
||||
protected Formatter<Double> percentageFormatter;
|
||||
|
||||
public AbstractHealthInfo(long now, long monthAgo) {
|
||||
|
@ -11,7 +11,6 @@ import com.djrapitops.plan.data.store.mutators.PlayersMutator;
|
||||
import com.djrapitops.plan.data.store.mutators.PlayersOnlineResolver;
|
||||
import com.djrapitops.plan.data.store.mutators.TPSMutator;
|
||||
import com.djrapitops.plan.system.settings.Settings;
|
||||
import com.djrapitops.plan.utilities.FormatUtils;
|
||||
import com.djrapitops.plan.utilities.html.icon.Icons;
|
||||
import com.djrapitops.plugin.api.TimeAmount;
|
||||
|
||||
@ -66,10 +65,10 @@ public class HealthInformation extends AbstractHealthInfo {
|
||||
.average().orElse(0);
|
||||
if (avgOnlineOnRegister >= 1) {
|
||||
addNote(Icons.GREEN_THUMB + " New Players have players to play with when they join ("
|
||||
+ FormatUtils.cutDecimals(avgOnlineOnRegister) + " on average)");
|
||||
+ decimalFormatter.apply(avgOnlineOnRegister) + " on average)");
|
||||
} else {
|
||||
addNote(Icons.YELLOW_FLAG + " New Players may not have players to play with when they join ("
|
||||
+ FormatUtils.cutDecimals(avgOnlineOnRegister) + " on average)");
|
||||
+ decimalFormatter.apply(avgOnlineOnRegister) + " on average)");
|
||||
serverHealth -= 5;
|
||||
}
|
||||
|
||||
@ -106,7 +105,7 @@ public class HealthInformation extends AbstractHealthInfo {
|
||||
serverHealth *= 0.6;
|
||||
}
|
||||
avgLowThresholdString += " Average TPS was above Low Threshold "
|
||||
+ FormatUtils.cutDecimals(aboveThreshold * 100.0) + "% of the time";
|
||||
+ decimalFormatter.apply(aboveThreshold * 100.0) + "% of the time";
|
||||
|
||||
int threshold = Settings.THEME_GRAPH_TPS_THRESHOLD_MED.getNumber();
|
||||
if (tpsSpikeMonth <= 5) {
|
||||
|
@ -8,7 +8,6 @@ import com.djrapitops.plan.data.store.keys.NetworkKeys;
|
||||
import com.djrapitops.plan.data.store.mutators.PlayersMutator;
|
||||
import com.djrapitops.plan.data.store.mutators.SessionsMutator;
|
||||
import com.djrapitops.plan.system.info.server.Server;
|
||||
import com.djrapitops.plan.utilities.FormatUtils;
|
||||
import com.djrapitops.plan.utilities.html.icon.Icon;
|
||||
import com.djrapitops.plan.utilities.html.icon.Icons;
|
||||
|
||||
@ -82,7 +81,7 @@ public class NetworkHealthInformation extends AbstractHealthInfo {
|
||||
return subNote + (playersPerMonth >= average && playersPerMonth > 0 ? Icons.GREEN_PLUS : Icons.RED_MINUS) + " " +
|
||||
server.getName() + ": " + playersPerMonth;
|
||||
}).forEach(subNotes::append);
|
||||
addNote(icon + " " + FormatUtils.cutDecimals(average) + uniquePlayersNote + subNotes.toString());
|
||||
addNote(icon + " " + decimalFormatter.apply(average) + uniquePlayersNote + subNotes.toString());
|
||||
}
|
||||
|
||||
private void newPlayersNote(int serverCount, Key<Server> serverKey, List<DataContainer> perServerContainers) {
|
||||
@ -109,7 +108,7 @@ public class NetworkHealthInformation extends AbstractHealthInfo {
|
||||
return subNote + (playersPerMonth >= average && playersPerMonth > 0 ? Icons.GREEN_PLUS : Icons.RED_MINUS) + " " +
|
||||
server.getName() + ": " + playersPerMonth;
|
||||
}).forEach(subNotes::append);
|
||||
addNote(icon + " " + FormatUtils.cutDecimals(average) + newPlayersNote + subNotes.toString());
|
||||
addNote(icon + " " + decimalFormatter.apply(average) + newPlayersNote + subNotes.toString());
|
||||
}
|
||||
|
||||
private List<DataContainer> getPerServerContainers(PlayersMutator playersMutator, Collection<Server> servers, Key<Server> serverKey) {
|
||||
|
@ -3,9 +3,11 @@ package com.djrapitops.plan.system.webserver.response;
|
||||
import com.djrapitops.plan.api.exceptions.WebUserAuthException;
|
||||
import com.djrapitops.plan.system.webserver.auth.FailReason;
|
||||
import com.djrapitops.plan.system.webserver.response.errors.ErrorResponse;
|
||||
import com.djrapitops.plan.utilities.FormatUtils;
|
||||
import com.djrapitops.plan.utilities.html.icon.Icon;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Rsl1122
|
||||
* @since 3.5.2
|
||||
@ -41,7 +43,7 @@ public class PromptAuthorizationResponse extends ErrorResponse {
|
||||
|
||||
if (failReason == FailReason.ERROR) {
|
||||
StringBuilder errorBuilder = new StringBuilder("</p><pre>");
|
||||
for (String line : FormatUtils.getStackTrace(e.getCause())) {
|
||||
for (String line : getStackTrace(e.getCause())) {
|
||||
errorBuilder.append(line);
|
||||
}
|
||||
errorBuilder.append("</pre>");
|
||||
@ -53,4 +55,29 @@ public class PromptAuthorizationResponse extends ErrorResponse {
|
||||
response.replacePlaceholders();
|
||||
return response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets lines for stack trace recursively.
|
||||
*
|
||||
* @param throwable Throwable element
|
||||
* @return lines of stack trace.
|
||||
*/
|
||||
private static List<String> getStackTrace(Throwable throwable) {
|
||||
List<String> stackTrace = new ArrayList<>();
|
||||
stackTrace.add(throwable.toString());
|
||||
for (StackTraceElement element : throwable.getStackTrace()) {
|
||||
stackTrace.add(" " + element.toString());
|
||||
}
|
||||
|
||||
Throwable cause = throwable.getCause();
|
||||
if (cause != null) {
|
||||
List<String> causeTrace = getStackTrace(cause);
|
||||
if (!causeTrace.isEmpty()) {
|
||||
causeTrace.set(0, "Caused by: " + causeTrace.get(0));
|
||||
stackTrace.addAll(causeTrace);
|
||||
}
|
||||
}
|
||||
|
||||
return stackTrace;
|
||||
}
|
||||
}
|
||||
|
@ -1,172 +0,0 @@
|
||||
package com.djrapitops.plan.utilities;
|
||||
|
||||
import com.djrapitops.plan.system.settings.Settings;
|
||||
import com.djrapitops.plugin.api.TimeAmount;
|
||||
|
||||
import java.net.Inet6Address;
|
||||
import java.net.InetAddress;
|
||||
import java.text.DecimalFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
|
||||
/**
|
||||
* @author Rsl1122
|
||||
*/
|
||||
public class FormatUtils {
|
||||
|
||||
/**
|
||||
* Constructor used to hide the public constructor
|
||||
*/
|
||||
private FormatUtils() {
|
||||
throw new IllegalStateException("Utility class");
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static String formatTimeStampISO8601NoClock(long epochMs) {
|
||||
String format = "yyyy-MM-dd";
|
||||
|
||||
return format(epochMs, format);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static String formatTimeStampDay(long epochMs) {
|
||||
String format = "MMMMM d";
|
||||
|
||||
if (Settings.FORMAT_DATE_RECENT_DAYS.isTrue()) {
|
||||
format = replaceRecentDays(epochMs, format, "MMMMM");
|
||||
}
|
||||
|
||||
return format(epochMs, format);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static String formatTimeStampClock(long epochMs) {
|
||||
String format = Settings.FORMAT_DATE_CLOCK.toString();
|
||||
|
||||
return format(epochMs, format);
|
||||
}
|
||||
|
||||
private static String format(long epochMs, String format) {
|
||||
boolean useServerTime = Settings.USE_SERVER_TIME.isTrue();
|
||||
String locale = Settings.LOCALE.toString();
|
||||
Locale usedLocale = locale.equalsIgnoreCase("default") ? Locale.ENGLISH : Locale.forLanguageTag(locale);
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat(format, usedLocale);
|
||||
TimeZone timeZone = useServerTime ? TimeZone.getDefault() : TimeZone.getTimeZone("GMT");
|
||||
dateFormat.setTimeZone(timeZone);
|
||||
return dateFormat.format(epochMs);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static String formatTimeStampSecond(long epochMs) {
|
||||
String format = Settings.FORMAT_DATE_FULL.toString();
|
||||
|
||||
if (Settings.FORMAT_DATE_RECENT_DAYS.isTrue()) {
|
||||
format = replaceRecentDays(epochMs, format);
|
||||
}
|
||||
|
||||
return format(epochMs, format);
|
||||
}
|
||||
|
||||
private static String replaceRecentDays(long epochMs, String format) {
|
||||
return replaceRecentDays(epochMs, format, Settings.FORMAT_DATE_RECENT_DAYS_PATTERN.toString());
|
||||
}
|
||||
|
||||
private static String replaceRecentDays(long epochMs, String format, String pattern) {
|
||||
long now = System.currentTimeMillis();
|
||||
|
||||
long fromStartOfDay = now % TimeAmount.DAY.ms();
|
||||
if (epochMs > now - fromStartOfDay) {
|
||||
format = format.replace(pattern, "'Today'");
|
||||
} else if (epochMs > now - TimeAmount.DAY.ms() - fromStartOfDay) {
|
||||
format = format.replace(pattern, "'Yesterday'");
|
||||
} else if (epochMs > now - TimeAmount.DAY.ms() * 5L) {
|
||||
format = format.replace(pattern, "EEEE");
|
||||
}
|
||||
return format;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static String formatTimeStampYear(long epochMs) {
|
||||
String format = Settings.FORMAT_DATE_NO_SECONDS.toString();
|
||||
|
||||
if (Settings.FORMAT_DATE_RECENT_DAYS.isTrue()) {
|
||||
format = replaceRecentDays(epochMs, format);
|
||||
}
|
||||
return format(epochMs, format);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove extra decimals from the end of the double.
|
||||
*
|
||||
* @param d Double.
|
||||
* @return String format of the double.
|
||||
*/
|
||||
@Deprecated
|
||||
public static String cutDecimals(double d) {
|
||||
return new DecimalFormat(Settings.FORMAT_DECIMALS.toString()).format(d);
|
||||
}
|
||||
|
||||
public static String formatIP(InetAddress address) {
|
||||
String ip = address.getHostAddress();
|
||||
if ("localhost".equals(ip)) {
|
||||
return ip;
|
||||
}
|
||||
if (address instanceof Inet6Address) {
|
||||
StringBuilder b = new StringBuilder();
|
||||
int i = 0;
|
||||
for (String part : ip.split(":")) {
|
||||
if (i >= 3) {
|
||||
break;
|
||||
}
|
||||
|
||||
b.append(part).append(':');
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
return b.append("xx..").toString();
|
||||
} else {
|
||||
StringBuilder b = new StringBuilder();
|
||||
int i = 0;
|
||||
for (String part : ip.split("\\.")) {
|
||||
if (i >= 2) {
|
||||
break;
|
||||
}
|
||||
|
||||
b.append(part).append('.');
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
return b.append("xx.xx").toString();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets lines for stack trace recursively.
|
||||
*
|
||||
* @param throwable Throwable element
|
||||
* @return lines of stack trace.
|
||||
*/
|
||||
public static List<String> getStackTrace(Throwable throwable) {
|
||||
List<String> stackTrace = new ArrayList<>();
|
||||
stackTrace.add(throwable.toString());
|
||||
for (StackTraceElement element : throwable.getStackTrace()) {
|
||||
stackTrace.add(" " + element.toString());
|
||||
}
|
||||
|
||||
Throwable cause = throwable.getCause();
|
||||
if (cause != null) {
|
||||
List<String> causeTrace = getStackTrace(cause);
|
||||
if (!causeTrace.isEmpty()) {
|
||||
causeTrace.set(0, "Caused by: " + causeTrace.get(0));
|
||||
stackTrace.addAll(causeTrace);
|
||||
}
|
||||
}
|
||||
|
||||
return stackTrace;
|
||||
}
|
||||
}
|
@ -15,62 +15,93 @@ import javax.inject.Singleton;
|
||||
@Singleton
|
||||
public class Formatters {
|
||||
|
||||
private PlanConfig config;
|
||||
private final DateHolderFormatter yearFormatter;
|
||||
private final DateHolderFormatter dayFormatter;
|
||||
private final DateHolderFormatter secondFormatter;
|
||||
private final DateHolderFormatter clockFormatter;
|
||||
private final DateHolderFormatter iso8601NoClockFormatter;
|
||||
|
||||
private final YearFormatter yearLongFormatter;
|
||||
private final DayFormatter dayLongFormatter;
|
||||
private final SecondFormatter secondLongFormatter;
|
||||
private final ClockFormatter clockLongFormatter;
|
||||
private final ISO8601NoClockFormatter iso8601NoClockLongFormatter;
|
||||
|
||||
private final TimeAmountFormatter timeAmountFormatter;
|
||||
|
||||
private final DecimalFormatter decimalFormatter;
|
||||
private final PercentageFormatter percentageFormatter;
|
||||
|
||||
@Inject
|
||||
public Formatters(PlanConfig config) {
|
||||
this.config = config;
|
||||
yearLongFormatter = new YearFormatter(config);
|
||||
dayLongFormatter = new DayFormatter(config);
|
||||
clockLongFormatter = new ClockFormatter(config);
|
||||
secondLongFormatter = new SecondFormatter(config);
|
||||
iso8601NoClockLongFormatter = new ISO8601NoClockFormatter(config);
|
||||
|
||||
yearFormatter = new DateHolderFormatter(yearLongFormatter);
|
||||
dayFormatter = new DateHolderFormatter(dayLongFormatter);
|
||||
secondFormatter = new DateHolderFormatter(secondLongFormatter);
|
||||
clockFormatter = new DateHolderFormatter(clockLongFormatter);
|
||||
iso8601NoClockFormatter = new DateHolderFormatter(iso8601NoClockLongFormatter);
|
||||
|
||||
timeAmountFormatter = new TimeAmountFormatter(config);
|
||||
|
||||
decimalFormatter = new DecimalFormatter(config);
|
||||
percentageFormatter = new PercentageFormatter(decimalFormatter);
|
||||
|
||||
}
|
||||
|
||||
public Formatter<DateHolder> year() {
|
||||
return new DateHolderFormatter(yearLong());
|
||||
return this.yearFormatter;
|
||||
}
|
||||
|
||||
public Formatter<Long> yearLong() {
|
||||
return new YearFormatter(config);
|
||||
return yearLongFormatter;
|
||||
}
|
||||
|
||||
public Formatter<DateHolder> day() {
|
||||
return new DateHolderFormatter(dayLong());
|
||||
return dayFormatter;
|
||||
}
|
||||
|
||||
public Formatter<Long> dayLong() {
|
||||
return new DayFormatter(config);
|
||||
return dayLongFormatter;
|
||||
}
|
||||
|
||||
public Formatter<DateHolder> second() {
|
||||
return new DateHolderFormatter(secondLong());
|
||||
return secondFormatter;
|
||||
}
|
||||
|
||||
public Formatter<Long> secondLong() {
|
||||
return new SecondFormatter(config);
|
||||
return secondLongFormatter;
|
||||
}
|
||||
|
||||
public Formatter<DateHolder> clock() {
|
||||
return new DateHolderFormatter(clockLong());
|
||||
return clockFormatter;
|
||||
}
|
||||
|
||||
public Formatter<Long> clockLong() {
|
||||
return new ClockFormatter(config);
|
||||
return clockLongFormatter;
|
||||
}
|
||||
|
||||
public Formatter<DateHolder> iso8601NoClock() {
|
||||
return new DateHolderFormatter(iso8601NoClockLong());
|
||||
return iso8601NoClockFormatter;
|
||||
}
|
||||
|
||||
public Formatter<Long> iso8601NoClockLong() {
|
||||
return new ISO8601NoClockFormatter(config);
|
||||
return iso8601NoClockLongFormatter;
|
||||
}
|
||||
|
||||
public Formatter<Long> timeAmount() {
|
||||
return new TimeAmountFormatter(config);
|
||||
return timeAmountFormatter;
|
||||
}
|
||||
|
||||
public Formatter<Double> percentage() {
|
||||
return new PercentageFormatter(decimals());
|
||||
return percentageFormatter;
|
||||
}
|
||||
|
||||
public Formatter<Double> decimals() {
|
||||
return new DecimalFormatter(config);
|
||||
return decimalFormatter;
|
||||
}
|
||||
}
|
@ -9,7 +9,7 @@ import com.djrapitops.plan.system.database.databases.Database;
|
||||
import com.djrapitops.plan.system.info.server.Server;
|
||||
import com.djrapitops.plan.system.info.server.ServerInfo;
|
||||
import com.djrapitops.plan.system.info.server.properties.ServerProperties;
|
||||
import com.djrapitops.plan.utilities.FormatUtils;
|
||||
import com.djrapitops.plan.utilities.formatting.Formatter;
|
||||
import com.djrapitops.plan.utilities.html.graphs.Graphs;
|
||||
import com.djrapitops.plan.utilities.html.icon.Color;
|
||||
import com.djrapitops.plan.utilities.html.icon.Icon;
|
||||
@ -67,7 +67,8 @@ public class HtmlStructure {
|
||||
ServerProperties properties = ServerInfo.getServerProperties_Old();
|
||||
int maxPlayers = properties.getMaxPlayers();
|
||||
int online = properties.getOnlinePlayers();
|
||||
String refresh = FormatUtils.formatTimeStampClock(System.currentTimeMillis());
|
||||
Formatter<Long> clockLongFormatter = null; //TODO
|
||||
String refresh = clockLongFormatter.apply(System.currentTimeMillis());
|
||||
|
||||
Server server = ServerInfo.getServer_Old();
|
||||
|
||||
|
@ -6,10 +6,10 @@ package com.djrapitops.plan.utilities.html.pages;
|
||||
|
||||
import com.djrapitops.plan.api.exceptions.ParseException;
|
||||
import com.djrapitops.plan.data.store.containers.AnalysisContainer;
|
||||
import com.djrapitops.plan.utilities.formatting.PlaceholderReplacer;
|
||||
import com.djrapitops.plan.system.webserver.response.errors.ErrorResponse;
|
||||
import com.djrapitops.plan.utilities.FormatUtils;
|
||||
import com.djrapitops.plan.utilities.file.FileUtil;
|
||||
import com.djrapitops.plan.utilities.formatting.Formatter;
|
||||
import com.djrapitops.plan.utilities.formatting.PlaceholderReplacer;
|
||||
import com.djrapitops.plugin.api.Benchmark;
|
||||
import com.djrapitops.plugin.api.utility.log.Log;
|
||||
|
||||
@ -24,11 +24,15 @@ import static com.djrapitops.plan.data.store.keys.AnalysisKeys.*;
|
||||
*/
|
||||
public class AnalysisPage implements Page {
|
||||
|
||||
private final AnalysisContainer analysisContainer;
|
||||
private static final String DEBUG = "Analysis";
|
||||
|
||||
AnalysisPage(AnalysisContainer analysisContainer) {
|
||||
private final AnalysisContainer analysisContainer;
|
||||
|
||||
private final Formatter<Double> decimalFormatter;
|
||||
|
||||
AnalysisPage(AnalysisContainer analysisContainer, Formatter<Double> decimalFormatter) {
|
||||
this.analysisContainer = analysisContainer;
|
||||
this.decimalFormatter = decimalFormatter;
|
||||
}
|
||||
|
||||
public static String getRefreshingHtml() {
|
||||
@ -137,14 +141,14 @@ public class AnalysisPage implements Page {
|
||||
placeholderReplacer.addAllPlaceholdersFrom(analysisContainer,
|
||||
TPS_SPIKE_MONTH, TPS_SPIKE_WEEK, TPS_SPIKE_DAY
|
||||
);
|
||||
placeholderReplacer.addAllPlaceholdersFrom(analysisContainer, FormatUtils::cutDecimals,
|
||||
placeholderReplacer.addAllPlaceholdersFrom(analysisContainer, decimalFormatter,
|
||||
AVG_TPS_MONTH, AVG_TPS_WEEK, AVG_TPS_DAY,
|
||||
AVG_RAM_MONTH, AVG_RAM_WEEK, AVG_RAM_DAY,
|
||||
AVG_ENTITY_MONTH, AVG_ENTITY_WEEK, AVG_ENTITY_DAY,
|
||||
AVG_CHUNK_MONTH, AVG_CHUNK_WEEK, AVG_CHUNK_DAY
|
||||
);
|
||||
placeholderReplacer.addAllPlaceholdersFrom(analysisContainer,
|
||||
value -> value != -1 ? FormatUtils.cutDecimals(value) : "Unavailable",
|
||||
value -> value != -1 ? decimalFormatter.apply(value) : "Unavailable",
|
||||
AVG_CPU_MONTH, AVG_CPU_WEEK, AVG_CPU_DAY
|
||||
);
|
||||
Benchmark.stop(DEBUG, DEBUG + " Performance Numbers");
|
||||
|
@ -77,7 +77,7 @@ public class PageFactory {
|
||||
}
|
||||
|
||||
public AnalysisPage analysisPage(UUID serverUUID) {
|
||||
return new AnalysisPage(new AnalysisContainer(database.get().fetch().getServerContainer(serverUUID)));
|
||||
return new AnalysisPage(new AnalysisContainer(database.get().fetch().getServerContainer(serverUUID)), decimalFormatter);
|
||||
}
|
||||
|
||||
public InspectPage inspectPage(UUID uuid) {
|
||||
|
@ -3,13 +3,16 @@ package com.djrapitops.plan.utilities.html.tables;
|
||||
import com.djrapitops.plan.data.container.Ping;
|
||||
import com.djrapitops.plan.data.element.TableContainer;
|
||||
import com.djrapitops.plan.data.store.mutators.PingMutator;
|
||||
import com.djrapitops.plan.utilities.FormatUtils;
|
||||
import com.djrapitops.plan.utilities.formatting.Formatter;
|
||||
import com.djrapitops.plan.utilities.html.icon.Icon;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class PingTable extends TableContainer {
|
||||
|
||||
// TODO
|
||||
private Formatter<Double> decimalFormatter;
|
||||
|
||||
public PingTable(Map<String, List<Ping>> pingPerCountry) {
|
||||
super(
|
||||
Icon.called("globe") + " Country",
|
||||
@ -44,7 +47,7 @@ public class PingTable extends TableContainer {
|
||||
Integer minimum = min.get(country);
|
||||
addRow(
|
||||
country,
|
||||
average >= 0 ? FormatUtils.cutDecimals(average) + " ms" : "-",
|
||||
average >= 0 ? decimalFormatter.apply(average) + " ms" : "-",
|
||||
maximum >= 0 ? maximum + " ms" : "-",
|
||||
minimum >= 0 ? minimum + " ms" : "-"
|
||||
);
|
||||
|
@ -24,4 +24,24 @@ public class GeoInfoTest {
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormatIP() throws UnknownHostException {
|
||||
InetAddress ip = InetAddress.getByName("1.2.3.4");
|
||||
InetAddress ip2 = InetAddress.getByName("1.2.3.26");
|
||||
InetAddress ip3 = InetAddress.getByName("1.2.3.235");
|
||||
String expected = "1.2.xx.xx";
|
||||
|
||||
assertEquals(expected, GeoInfo.formatIP(ip));
|
||||
assertEquals(expected, GeoInfo.formatIP(ip2));
|
||||
assertEquals(expected, GeoInfo.formatIP(ip3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormatIPv6() throws UnknownHostException {
|
||||
InetAddress ip = InetAddress.getByName("1234:1234:1234:1234:1234:1234:1234:1234%0");
|
||||
String expected = "1234:1234:1234:xx..";
|
||||
|
||||
assertEquals(expected, GeoInfo.formatIP(ip));
|
||||
}
|
||||
|
||||
}
|
@ -1,89 +0,0 @@
|
||||
package com.djrapitops.plan.utilities;
|
||||
|
||||
import com.djrapitops.plugin.utilities.ArrayUtil;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import utilities.RandomData;
|
||||
import utilities.Teardown;
|
||||
import utilities.mocks.SystemMockUtil;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Rsl1122
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.Silent.class)
|
||||
public class FormatUtilsTest {
|
||||
|
||||
@ClassRule
|
||||
public static TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpClass() throws Exception {
|
||||
SystemMockUtil.setUp(temporaryFolder.getRoot())
|
||||
.enableConfigSystem();
|
||||
Teardown.resetSettingsTempValues();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMergeArrays() {
|
||||
String randomString1 = RandomData.randomString(10);
|
||||
String randomString2 = RandomData.randomString(10);
|
||||
String randomString3 = RandomData.randomString(10);
|
||||
String randomString4 = RandomData.randomString(10);
|
||||
|
||||
String[][] arrays = new String[][]{new String[]{randomString1, randomString2}, new String[]{randomString3, randomString4}};
|
||||
String[] expResult = new String[]{randomString1, randomString2, randomString3, randomString4};
|
||||
|
||||
String[] result = ArrayUtil.merge(arrays);
|
||||
|
||||
assertArrayEquals(expResult, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCutDecimalsWhichIsRoundedDown() {
|
||||
double d = 0.05234;
|
||||
|
||||
String result = FormatUtils.cutDecimals(d);
|
||||
|
||||
assertTrue("0.05".equals(result) || "0,05".equals(result));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCutDecimalsWhichIsRoundedUp() {
|
||||
double d = 0.05634;
|
||||
|
||||
String result = FormatUtils.cutDecimals(d);
|
||||
|
||||
assertTrue("0.06".equals(result) || "0,06".equals(result));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormatIP() throws UnknownHostException {
|
||||
InetAddress ip = InetAddress.getByName("1.2.3.4");
|
||||
InetAddress ip2 = InetAddress.getByName("1.2.3.26");
|
||||
InetAddress ip3 = InetAddress.getByName("1.2.3.235");
|
||||
String expected = "1.2.xx.xx";
|
||||
|
||||
assertEquals(expected, FormatUtils.formatIP(ip));
|
||||
assertEquals(expected, FormatUtils.formatIP(ip2));
|
||||
assertEquals(expected, FormatUtils.formatIP(ip3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormatIPv6() throws UnknownHostException {
|
||||
InetAddress ip = InetAddress.getByName("1234:1234:1234:1234:1234:1234:1234:1234%0");
|
||||
String expected = "1234:1234:1234:xx..";
|
||||
|
||||
assertEquals(expected, FormatUtils.formatIP(ip));
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package com.djrapitops.plan.utilities.formatting;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Test for {@link DecimalFormatter} class.
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
public class DecimalFormatterTest {
|
||||
|
||||
@Test
|
||||
@Ignore("Config missing")
|
||||
public void testCutDecimalsWhichIsRoundedDown() {
|
||||
double d = 0.05234;
|
||||
|
||||
String result = new DecimalFormatter(null).apply(d);
|
||||
|
||||
assertTrue("0.05".equals(result) || "0,05".equals(result));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore("Config missing")
|
||||
public void testCutDecimalsWhichIsRoundedUp() {
|
||||
double d = 0.05634;
|
||||
|
||||
String result = new DecimalFormatter(null).apply(d);
|
||||
|
||||
assertTrue("0.06".equals(result) || "0,06".equals(result));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user