实时95值计算功能、优化服务器收益功能
This commit is contained in:
+175
-4
@@ -95,8 +95,8 @@ public class SpeedUtils {
|
||||
totalOutSpeedBit = totalOutSpeedBit.add(outSpeedBit);
|
||||
|
||||
// 转换为Gb并更新最大值
|
||||
BigDecimal inSpeedGb = inSpeedBit.divide(GB_DIVISOR, 10, RoundingMode.HALF_UP);
|
||||
BigDecimal outSpeedGb = outSpeedBit.divide(GB_DIVISOR, 10, RoundingMode.HALF_UP);
|
||||
BigDecimal inSpeedGb = inSpeedBit.divide(GB_DIVISOR, 0, RoundingMode.HALF_UP);
|
||||
BigDecimal outSpeedGb = outSpeedBit.divide(GB_DIVISOR, 0, RoundingMode.HALF_UP);
|
||||
|
||||
maxInSpeedGb = maxInSpeedGb.max(inSpeedGb);
|
||||
maxOutSpeedGb = maxOutSpeedGb.max(outSpeedGb);
|
||||
@@ -111,9 +111,9 @@ public class SpeedUtils {
|
||||
|
||||
// 计算Gb平均值
|
||||
BigDecimal size = new BigDecimal(list.size());
|
||||
BigDecimal avgInSpeedGb = totalInSpeedBit.divide(GB_DIVISOR, 10, RoundingMode.HALF_UP)
|
||||
BigDecimal avgInSpeedGb = totalInSpeedBit.divide(GB_DIVISOR, 0, RoundingMode.HALF_UP)
|
||||
.divide(size, 2, RoundingMode.HALF_UP);
|
||||
BigDecimal avgOutSpeedGb = totalOutSpeedBit.divide(GB_DIVISOR, 10, RoundingMode.HALF_UP)
|
||||
BigDecimal avgOutSpeedGb = totalOutSpeedBit.divide(GB_DIVISOR, 0, RoundingMode.HALF_UP)
|
||||
.divide(size, 2, RoundingMode.HALF_UP);
|
||||
|
||||
// 基于平均值的较大值确定推荐单位
|
||||
@@ -201,6 +201,177 @@ public class SpeedUtils {
|
||||
recommendedUnit = "Kb";
|
||||
}
|
||||
|
||||
return recommendedUnit;
|
||||
}
|
||||
/**
|
||||
* 计算String类型(存储数字)的速度统计结果
|
||||
* @param list 数据列表
|
||||
* @param inSpeedField 输入速度字段名
|
||||
* @param outSpeedField 输出速度字段名
|
||||
* @return SpeedResult 速度统计结果
|
||||
*/
|
||||
public static SpeedResult calculateWithStringTraffic(List<?> list,
|
||||
String inSpeedField, String outSpeedField)
|
||||
throws NoSuchFieldException, IllegalAccessException {
|
||||
|
||||
if (list == null || list.isEmpty()) {
|
||||
return new SpeedResult(
|
||||
BigDecimal.ZERO, BigDecimal.ZERO,
|
||||
BigDecimal.ZERO, BigDecimal.ZERO,
|
||||
BigDecimal.ZERO, BigDecimal.ZERO,
|
||||
"bit"
|
||||
);
|
||||
}
|
||||
|
||||
BigDecimal totalInSpeedBit = BigDecimal.ZERO;
|
||||
BigDecimal totalOutSpeedBit = BigDecimal.ZERO;
|
||||
BigDecimal maxInSpeedGb = BigDecimal.ZERO;
|
||||
BigDecimal maxOutSpeedGb = BigDecimal.ZERO;
|
||||
BigDecimal lastInSpeedGb = BigDecimal.ZERO;
|
||||
BigDecimal lastOutSpeedGb = BigDecimal.ZERO;
|
||||
|
||||
final BigDecimal GB_DIVISOR = new BigDecimal("1000000000");
|
||||
|
||||
// 计算总和(bit)和Gb值
|
||||
for (Object obj : list) {
|
||||
try {
|
||||
java.lang.reflect.Field inField = obj.getClass().getDeclaredField(inSpeedField);
|
||||
java.lang.reflect.Field outField = obj.getClass().getDeclaredField(outSpeedField);
|
||||
|
||||
inField.setAccessible(true);
|
||||
outField.setAccessible(true);
|
||||
|
||||
// 处理String类型的数字值
|
||||
String inSpeedStr = (String) inField.get(obj);
|
||||
String outSpeedStr = (String) outField.get(obj);
|
||||
|
||||
BigDecimal inSpeedBit = convertStringToBigDecimal(inSpeedStr);
|
||||
BigDecimal outSpeedBit = convertStringToBigDecimal(outSpeedStr);
|
||||
|
||||
// 累加bit值(用于计算平均值)
|
||||
totalInSpeedBit = totalInSpeedBit.add(inSpeedBit);
|
||||
totalOutSpeedBit = totalOutSpeedBit.add(outSpeedBit);
|
||||
|
||||
// 转换为Gb并更新最大值
|
||||
BigDecimal inSpeedGb = inSpeedBit.divide(GB_DIVISOR, 2, RoundingMode.HALF_UP);
|
||||
BigDecimal outSpeedGb = outSpeedBit.divide(GB_DIVISOR, 2, RoundingMode.HALF_UP);
|
||||
|
||||
maxInSpeedGb = maxInSpeedGb.max(inSpeedGb);
|
||||
maxOutSpeedGb = maxOutSpeedGb.max(outSpeedGb);
|
||||
|
||||
// 记录最后一个值
|
||||
lastInSpeedGb = inSpeedGb;
|
||||
lastOutSpeedGb = outSpeedGb;
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("无法获取或转换速度字段值", e);
|
||||
}
|
||||
}
|
||||
|
||||
// 计算Gb平均值
|
||||
BigDecimal size = new BigDecimal(list.size());
|
||||
BigDecimal avgInSpeedGb = totalInSpeedBit.divide(GB_DIVISOR, 2, RoundingMode.HALF_UP)
|
||||
.divide(size, 2, RoundingMode.HALF_UP);
|
||||
BigDecimal avgOutSpeedGb = totalOutSpeedBit.divide(GB_DIVISOR, 2, RoundingMode.HALF_UP)
|
||||
.divide(size, 2, RoundingMode.HALF_UP);
|
||||
|
||||
// 基于平均值的较大值确定推荐单位
|
||||
BigDecimal maxAvgBit = avgInSpeedGb.compareTo(avgOutSpeedGb) > 0 ?
|
||||
avgInSpeedGb.multiply(GB_DIVISOR) :
|
||||
avgOutSpeedGb.multiply(GB_DIVISOR);
|
||||
|
||||
String recommendedUnit;
|
||||
if (maxAvgBit.compareTo(new BigDecimal("1000000000")) >= 0) {
|
||||
recommendedUnit = "Gb";
|
||||
} else if (maxAvgBit.compareTo(new BigDecimal("1000000")) >= 0) {
|
||||
recommendedUnit = "Mb";
|
||||
} else {
|
||||
recommendedUnit = "Kb";
|
||||
}
|
||||
|
||||
return new SpeedResult(
|
||||
avgInSpeedGb, avgOutSpeedGb,
|
||||
maxInSpeedGb, maxOutSpeedGb,
|
||||
lastInSpeedGb, lastOutSpeedGb,
|
||||
recommendedUnit
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将String类型的数字转换为BigDecimal
|
||||
* @param numberStr 数字字符串
|
||||
* @return BigDecimal值,转换失败返回0
|
||||
*/
|
||||
private static BigDecimal convertStringToBigDecimal(String numberStr) {
|
||||
if (numberStr == null || numberStr.trim().isEmpty()) {
|
||||
return BigDecimal.ZERO;
|
||||
}
|
||||
|
||||
try {
|
||||
return new BigDecimal(numberStr.trim());
|
||||
} catch (Exception e) {
|
||||
return BigDecimal.ZERO;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 仅计算String类型(存储数字)的推荐单位
|
||||
* @param list 数据列表
|
||||
* @param inSpeedField 输入速度字段名
|
||||
* @param outSpeedField 输出速度字段名
|
||||
* @return 推荐单位
|
||||
*/
|
||||
public static String calculateUnitWithStringTraffic(List<?> list,
|
||||
String inSpeedField, String outSpeedField)
|
||||
throws NoSuchFieldException, IllegalAccessException {
|
||||
|
||||
if (list == null || list.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
BigDecimal totalInSpeedBit = BigDecimal.ZERO;
|
||||
BigDecimal totalOutSpeedBit = BigDecimal.ZERO;
|
||||
|
||||
// 计算总和(bit)
|
||||
for (Object obj : list) {
|
||||
try {
|
||||
java.lang.reflect.Field inField = obj.getClass().getDeclaredField(inSpeedField);
|
||||
java.lang.reflect.Field outField = obj.getClass().getDeclaredField(outSpeedField);
|
||||
|
||||
inField.setAccessible(true);
|
||||
outField.setAccessible(true);
|
||||
|
||||
// 处理String类型的数字值
|
||||
String inSpeedStr = (String) inField.get(obj);
|
||||
String outSpeedStr = (String) outField.get(obj);
|
||||
|
||||
BigDecimal inSpeedBit = convertStringToBigDecimal(inSpeedStr);
|
||||
BigDecimal outSpeedBit = convertStringToBigDecimal(outSpeedStr);
|
||||
|
||||
totalInSpeedBit = totalInSpeedBit.add(inSpeedBit);
|
||||
totalOutSpeedBit = totalOutSpeedBit.add(outSpeedBit);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("无法获取或转换速度字段值", e);
|
||||
}
|
||||
}
|
||||
|
||||
// 计算平均值
|
||||
BigDecimal size = new BigDecimal(list.size());
|
||||
BigDecimal avgInSpeedBit = totalInSpeedBit.divide(size, 2, RoundingMode.HALF_UP);
|
||||
BigDecimal avgOutSpeedBit = totalOutSpeedBit.divide(size, 2, RoundingMode.HALF_UP);
|
||||
|
||||
// 基于平均值的较大值确定推荐单位
|
||||
BigDecimal maxAvgBit = avgInSpeedBit.compareTo(avgOutSpeedBit) > 0 ?
|
||||
avgInSpeedBit : avgOutSpeedBit;
|
||||
|
||||
String recommendedUnit;
|
||||
if (maxAvgBit.compareTo(new BigDecimal("1000000000")) >= 0) {
|
||||
recommendedUnit = "Gb";
|
||||
} else if (maxAvgBit.compareTo(new BigDecimal("1000000")) >= 0) {
|
||||
recommendedUnit = "Mb";
|
||||
} else {
|
||||
recommendedUnit = "Kb";
|
||||
}
|
||||
|
||||
return recommendedUnit;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user