Skip to content

Commit

Permalink
#1596 rounding for >100 mbps
Browse files Browse the repository at this point in the history
  • Loading branch information
cproof committed Dec 6, 2023
1 parent b3fe8d5 commit d7b5523
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
13 changes: 9 additions & 4 deletions src/main/java/at/rtr/rmbt/service/impl/TestServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -701,11 +701,9 @@ private void addTestFields(List<TestResultDetailContainerResponse> propertiesLis
.ifPresentOrElse(mobileProviderShortName -> addNetworkOperator(propertiesList, locale, test, mobileProviderShortName), () -> addString(propertiesList, locale, "network_operator_name", test.getNetworkOperatorName()));
}
Optional.ofNullable(test.getDownloadSpeed())
.map(x -> x / Constants.BYTES_UNIT_CONVERSION_MULTIPLICATOR)
.ifPresent(downloadSpeed -> addDoubleAndUnitString(propertiesList, locale, "speed_download", downloadSpeed, "RESULT_DOWNLOAD_UNIT"));
.ifPresent(downloadSpeed -> addSpeedAndUnitString(propertiesList, locale, "speed_download", downloadSpeed, "RESULT_DOWNLOAD_UNIT"));
Optional.ofNullable(test.getUploadSpeed())
.map(x -> x / Constants.BYTES_UNIT_CONVERSION_MULTIPLICATOR)
.ifPresent(uploadSpeed -> addDoubleAndUnitString(propertiesList, locale, "speed_upload", uploadSpeed, "RESULT_UPLOAD_UNIT"));
.ifPresent(uploadSpeed -> addSpeedAndUnitString(propertiesList, locale, "speed_upload", uploadSpeed, "RESULT_UPLOAD_UNIT"));
Optional.ofNullable(test.getPingMedian())
.map(this::getPingMsFromPingMedian)
.ifPresent(pingMedian -> addDoubleAndUnitString(propertiesList, locale, "ping_median", pingMedian, "RESULT_PING_UNIT"));
Expand Down Expand Up @@ -933,6 +931,13 @@ private void addDoubleAndUnitString(List<TestResultDetailContainerResponse> prop
}
}

private void addSpeedAndUnitString(List<TestResultDetailContainerResponse> propertiesList, Locale locale, String title, Integer value, String unitKey) {
String unit = getStringFromBundle(unitKey, locale);
if (Objects.nonNull(value)) {
addString(propertiesList, locale, title, FormatUtils.formatSpeedValueAndUnit(value, unit, locale));
}
}

private void addIntegerAndUnitString(List<TestResultDetailContainerResponse> propertiesList, Locale locale, String title, Integer value, String unitKey) {
String unit = getStringFromBundle(unitKey, locale);
if (Objects.nonNull(value)) {
Expand Down
18 changes: 15 additions & 3 deletions src/main/java/at/rtr/rmbt/utils/FormatUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public static String formatValueAndUnit(Double value, String unitValue, Locale l
return String.format(Constants.VALUE_AND_UNIT_TEMPLATE, format.format(new BigDecimal(value, mathContext)), unitValue);
}

public static String formatSpeedValueAndUnit(Integer speedMbps, String unitValue, Locale locale) {
Format format = NumberFormat.getNumberInstance(locale);
return String.format(Constants.VALUE_AND_UNIT_TEMPLATE, formatSpeed(speedMbps), unitValue);
}

public static String formatValueAndUnit(Long value, String unitValue) {
return String.format(Constants.VALUE_AND_UNIT_TEMPLATE, value, unitValue);
}
Expand All @@ -48,11 +53,18 @@ public static String formatValueAndUnit(Integer value, String unitValue) {
return String.format(Constants.VALUE_AND_UNIT_TEMPLATE, value, unitValue);
}

public static String formatSpeed(Integer speed) {
return Optional.ofNullable(speed)
public static String formatSpeed(Integer speedKbps) {
return Optional.ofNullable(speedKbps)
.map(s -> ObjectUtils.defaultIfNull(s, NumberUtils.INTEGER_ZERO))
.map(x -> x / Constants.BYTES_UNIT_CONVERSION_MULTIPLICATOR)
.map(x -> new BigDecimal(x, mathContext))
.map(x -> {
if (x < 100) { //don't round over 100 mbps
return new BigDecimal(x, mathContext);
} else {
return Math.round(x);

}
})
.map(numberFormat::format)
.orElse(StringUtils.EMPTY);
}
Expand Down
31 changes: 31 additions & 0 deletions src/test/java/at/rtr/rmbt/util/FormatUtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package at.rtr.rmbt.util;

import at.rtr.rmbt.utils.FormatUtils;
import org.junit.Test;

import static org.junit.jupiter.api.Assertions.*;

public class FormatUtilsTest {

@Test
public void testSpeedFormatting() {
String s = FormatUtils.formatSpeed(111);
assertEquals("0.11",s);

s = FormatUtils.formatSpeed(1111);
assertEquals("1.1",s);

s = FormatUtils.formatSpeed(11111);
assertEquals("11",s);

s = FormatUtils.formatSpeed(111111);
assertEquals("111",s);

s = FormatUtils.formatSpeed(1111111);
assertEquals("1,111",s);

s = FormatUtils.formatSpeed(11111111);
assertEquals("11,111",s);
}

}

0 comments on commit d7b5523

Please sign in to comment.