优化图形分析单位、服务器流量业务表逻辑

This commit is contained in:
gaoyutao
2025-11-05 13:02:51 +08:00
parent 1dc32ec24e
commit e9970b94a9
9 changed files with 140 additions and 29 deletions
@@ -49,7 +49,7 @@ public class EchartsDataUtils {
* 微秒转秒
*/
public static double convertMicrosecondsToSeconds(long microseconds) {
return microseconds / 1_000_000.0;
return Math.round(microseconds / 1_000_000.0 * 100.0) / 100.0;
}
/**
* 构建ECharts图表数据(带时间补全和0值填充)
@@ -2,17 +2,44 @@ package com.ruoyi.common.core.utils;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.concurrent.TimeUnit;
public class UnitChangeUtil {
private static final double GB = 1024.0 * 1024.0 * 1024.0;
/**
* long类型字节转化为Gb
* @param bytes
* @return
*/
public static double bytesToGb(long bytes) {
double gb = bytes / GB;
BigDecimal bd = new BigDecimal(gb);
bd = bd.setScale(2, RoundingMode.HALF_UP);
return bd.doubleValue();
}
/**
* String类型字节转化为Gb
* @param bytes
* @return
*/
public static BigDecimal bytesToGb(String bytes) {
// 参数校验
if (bytes == null || bytes.trim().isEmpty()) {
return BigDecimal.ZERO;
}
BigDecimal bytesValue = new BigDecimal(bytes);
BigDecimal bd = bytesValue.divide(BigDecimal.valueOf(GB), 2, RoundingMode.HALF_UP);
return bd;
}
/**
* Kb转化为Gb
* @param kbValue
* @return
*/
public static double convertKbToGb(String kbValue) {
if (kbValue == null || kbValue.trim().isEmpty()) {
throw new IllegalArgumentException("KB值不能为空");
@@ -26,4 +53,64 @@ public class UnitChangeUtil {
return bd.doubleValue();
}
/**
* 工具方法:将毫秒值转换为运行时长
* @param uptimeMillis
* @return
*/
public static String formatUptime(long uptimeMillis) {
if (uptimeMillis <= 0) {
return "0秒";
}
long days = TimeUnit.MILLISECONDS.toDays(uptimeMillis);
long hours = TimeUnit.MILLISECONDS.toHours(uptimeMillis) % 24;
long minutes = TimeUnit.MILLISECONDS.toMinutes(uptimeMillis) % 60;
long seconds = TimeUnit.MILLISECONDS.toSeconds(uptimeMillis) % 60;
long months = days / 30;
days = days % 30;
return String.format("%d个月%d天%d小时%d分%d秒", months, days, hours, minutes, seconds);
}
/**
* 工具方法- 将秒值转换为运行时长
* @param uptimeSeconds
* @return
*/
public static String formatUptimeSeconds(long uptimeSeconds) {
if (uptimeSeconds <= 0) {
return "0秒";
}
long days = TimeUnit.SECONDS.toDays(uptimeSeconds);
long hours = TimeUnit.SECONDS.toHours(uptimeSeconds) % 24;
long minutes = TimeUnit.SECONDS.toMinutes(uptimeSeconds) % 60;
long seconds = uptimeSeconds % 60;
long months = days / 30;
days = days % 30;
return String.format("%d个月%d天%d小时%d分%d秒", months, days, hours, minutes, seconds);
}
/**
* 将字符串小数值格式化为保留两位小数(高精度)
* @param numberStr 数字字符串
* @return 保留两位小数的字符串,如果转换失败返回原字符串
*/
public static String formatDecimal(String numberStr) {
if (numberStr == null || numberStr.trim().isEmpty()) {
return numberStr;
}
try {
BigDecimal number = new BigDecimal(numberStr);
return number.setScale(1, RoundingMode.HALF_UP).toString();
} catch (NumberFormatException e) {
return numberStr; // 转换失败返回原字符串
}
}
}