Implemented a byte size formatter

Formats MB, GB and TB bytesizes appropriately

Affects issues:
- Close #1511
This commit is contained in:
Risto Lahtela 2020-10-06 10:45:52 +03:00
parent deb31cad58
commit af92a32ba1
4 changed files with 76 additions and 23 deletions

View File

@ -0,0 +1,42 @@
/*
* 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.delivery.formatting;
/**
* Formatter for amount of Megabytes (MB)
*
* @author Rsl1122
*/
public class ByteSizeFormatter implements Formatter<Double> {
private final DecimalFormatter decimals;
public ByteSizeFormatter(DecimalFormatter decimals) {
this.decimals = decimals;
}
@Override
public String apply(Double value) {
if (value > 1000000) {
return decimals.apply(value / 1000000) + " TB";
}
if (value > 1000) {
return decimals.apply(value / 1000) + " GB";
}
return decimals.apply(value) + " MB";
}
}

View File

@ -49,6 +49,7 @@ public class Formatters {
private final DecimalFormatter decimalFormatter;
private final PercentageFormatter percentageFormatter;
private final ByteSizeFormatter byteSizeFormatter;
@Inject
public Formatters(PlanConfig config, Locale locale) {
@ -69,7 +70,7 @@ public class Formatters {
decimalFormatter = new DecimalFormatter(config);
percentageFormatter = new PercentageFormatter(decimalFormatter);
byteSizeFormatter = new ByteSizeFormatter(decimalFormatter);
}
public Formatter<DateHolder> year() {
@ -127,4 +128,12 @@ public class Formatters {
public Formatter<Double> decimals() {
return decimalFormatter;
}
public Formatter<Double> byteSize() {
return byteSizeFormatter;
}
public Formatter<Long> byteSizeLong() {
return value -> byteSizeFormatter.apply((double) value);
}
}

View File

@ -50,8 +50,9 @@ public class PerformanceJSONCreator implements ServerTabJSONCreator<Map<String,
private final DBSystem dbSystem;
private final Formatter<Double> decimals;
private final Formatter<Long> timeAmountFormatter;
private final Formatter<Double> percentageFormatter;
private final Formatter<Long> timeAmount;
private final Formatter<Double> percentage;
private final Formatter<Double> byteSize;
@Inject
public PerformanceJSONCreator(
@ -65,8 +66,9 @@ public class PerformanceJSONCreator implements ServerTabJSONCreator<Map<String,
this.dbSystem = dbSystem;
decimals = formatters.decimals();
percentageFormatter = formatters.percentage();
timeAmountFormatter = formatters.timeAmount();
percentage = formatters.percentage();
timeAmount = formatters.timeAmount();
byteSize = formatters.byteSize();
}
public Map<String, Object> createJSONAsMap(UUID serverUUID) {
@ -97,9 +99,9 @@ public class PerformanceJSONCreator implements ServerTabJSONCreator<Map<String,
numbers.put("low_tps_spikes_7d", tpsDataWeek.lowTpsSpikeCount(tpsThreshold));
numbers.put("low_tps_spikes_24h", tpsDataDay.lowTpsSpikeCount(tpsThreshold));
numbers.put("server_downtime_30d", timeAmountFormatter.apply(tpsDataMonth.serverDownTime()));
numbers.put("server_downtime_7d", timeAmountFormatter.apply(tpsDataWeek.serverDownTime()));
numbers.put("server_downtime_24h", timeAmountFormatter.apply(tpsDataDay.serverDownTime()));
numbers.put("server_downtime_30d", timeAmount.apply(tpsDataMonth.serverDownTime()));
numbers.put("server_downtime_7d", timeAmount.apply(tpsDataWeek.serverDownTime()));
numbers.put("server_downtime_24h", timeAmount.apply(tpsDataDay.serverDownTime()));
numbers.put("tps_30d", format(tpsDataMonth.averageTPS()));
numbers.put("tps_7d", format(tpsDataWeek.averageTPS()));
@ -107,9 +109,9 @@ public class PerformanceJSONCreator implements ServerTabJSONCreator<Map<String,
numbers.put("cpu_30d", formatPerc(tpsDataMonth.averageCPU()));
numbers.put("cpu_7d", formatPerc(tpsDataWeek.averageCPU()));
numbers.put("cpu_24h", formatPerc(tpsDataDay.averageCPU()));
numbers.put("ram_30d", format(tpsDataMonth.averageRAM(), " MB"));
numbers.put("ram_7d", format(tpsDataWeek.averageRAM(), " MB"));
numbers.put("ram_24h", format(tpsDataDay.averageRAM(), " MB"));
numbers.put("ram_30d", formatBytes(tpsDataMonth.averageRAM()));
numbers.put("ram_7d", formatBytes(tpsDataWeek.averageRAM()));
numbers.put("ram_24h", formatBytes(tpsDataDay.averageRAM()));
numbers.put("entities_30d", format((int) tpsDataMonth.averageEntities()));
numbers.put("entities_7d", format((int) tpsDataWeek.averageEntities()));
numbers.put("entities_24h", format((int) tpsDataDay.averageEntities()));
@ -117,12 +119,12 @@ public class PerformanceJSONCreator implements ServerTabJSONCreator<Map<String,
numbers.put("chunks_7d", format((int) tpsDataWeek.averageChunks()));
numbers.put("chunks_24h", format((int) tpsDataDay.averageChunks()));
numbers.put("max_disk_30d", format(tpsDataMonth.maxFreeDisk(), " MB"));
numbers.put("max_disk_7d", format(tpsDataWeek.maxFreeDisk(), " MB"));
numbers.put("max_disk_24h", format(tpsDataDay.maxFreeDisk(), " MB"));
numbers.put("min_disk_30d", format(tpsDataMonth.minFreeDisk(), " MB"));
numbers.put("min_disk_7d", format(tpsDataWeek.minFreeDisk(), " MB"));
numbers.put("min_disk_24h", format(tpsDataDay.minFreeDisk(), " MB"));
numbers.put("max_disk_30d", formatBytes(tpsDataMonth.maxFreeDisk()));
numbers.put("max_disk_7d", formatBytes(tpsDataWeek.maxFreeDisk()));
numbers.put("max_disk_24h", formatBytes(tpsDataDay.maxFreeDisk()));
numbers.put("min_disk_30d", formatBytes(tpsDataMonth.minFreeDisk()));
numbers.put("min_disk_7d", formatBytes(tpsDataWeek.minFreeDisk()));
numbers.put("min_disk_24h", formatBytes(tpsDataDay.minFreeDisk()));
return numbers;
}
@ -131,12 +133,12 @@ public class PerformanceJSONCreator implements ServerTabJSONCreator<Map<String,
return value != -1 ? decimals.apply(value) : locale.get(GenericLang.UNAVAILABLE).toString();
}
private String format(double value, String suffix) {
return value != -1 ? decimals.apply(value) + suffix : locale.get(GenericLang.UNAVAILABLE).toString();
private String formatBytes(double value) {
return value != -1 ? byteSize.apply(value) : locale.get(GenericLang.UNAVAILABLE).toString();
}
private String formatPerc(double value) {
return value != -1 ? percentageFormatter.apply(value / 100.0) : locale.get(GenericLang.UNAVAILABLE).toString();
return value != -1 ? percentage.apply(value / 100.0) : locale.get(GenericLang.UNAVAILABLE).toString();
}
private Map<String, Object> createInsightsMap(List<TPS> tpsData) {

View File

@ -81,13 +81,13 @@ public class ServerPlaceHolders implements Placeholders {
() -> percentage.apply(database.query(TPSQueries.averageCPU(monthAgo(), now(), serverUUID))));
placeholders.registerStatic("server_ram_day",
() -> database.query(TPSQueries.averageRAM(dayAgo(), now(), serverUUID)) + " MB");
() -> formatters.byteSizeLong().apply(database.query(TPSQueries.averageRAM(dayAgo(), now(), serverUUID))));
placeholders.registerStatic("server_ram_week",
() -> database.query(TPSQueries.averageRAM(weekAgo(), now(), serverUUID)) + " MB");
() -> formatters.byteSizeLong().apply(database.query(TPSQueries.averageRAM(weekAgo(), now(), serverUUID))));
placeholders.registerStatic("server_ram_month",
() -> database.query(TPSQueries.averageRAM(monthAgo(), now(), serverUUID)) + " MB");
() -> formatters.byteSizeLong().apply(database.query(TPSQueries.averageRAM(monthAgo(), now(), serverUUID))));
placeholders.registerStatic("server_chunks_day",
() -> database.query(TPSQueries.averageChunks(dayAgo(), now(), serverUUID)));