底层流量数据改为bit,测试金山云服务器API,准备1.1分支
This commit is contained in:
@@ -10,6 +10,7 @@ import com.ruoyi.rocketmq.domain.*;
|
||||
import com.ruoyi.rocketmq.domain.vo.CollectDataVo;
|
||||
import com.ruoyi.rocketmq.domain.vo.RspVo;
|
||||
import com.ruoyi.rocketmq.service.*;
|
||||
import com.ruoyi.rocketmq.utils.DataProcessUtil;
|
||||
import com.ruoyi.rocketmq.utils.JsonDataParser;
|
||||
import com.ruoyi.rocketmq.utils.SwitchJsonDataParser;
|
||||
import com.ruoyi.system.api.RemoteRevenueConfigService;
|
||||
@@ -93,6 +94,8 @@ public class DeviceMessageHandler {
|
||||
private IRmResourceRemoteService rmResourceRemoteService;
|
||||
@Autowired
|
||||
private IRmAgentManagementService rmAgentManagementService;
|
||||
@Autowired
|
||||
private DataProcessUtil dataProcessUtil;
|
||||
// 在类中添加
|
||||
private static final ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
@@ -186,6 +189,10 @@ public class DeviceMessageHandler {
|
||||
interfaces.forEach(iface -> {
|
||||
iface.setClientId(message.getClientId());
|
||||
iface.setCreateTime(createTime);
|
||||
// 发送流量
|
||||
iface.setOutSpeed(dataProcessUtil.bytesToBits(iface.getOutSpeed()));
|
||||
// 接收流量
|
||||
iface.setInSpeed(dataProcessUtil.bytesToBits(iface.getInSpeed()));
|
||||
});
|
||||
// 批量入库集合
|
||||
data.setList(interfaces);
|
||||
@@ -498,13 +505,17 @@ public class DeviceMessageHandler {
|
||||
// 计算inSpeed
|
||||
if (switchInfo.getInBytes() != null && tempInfo.getInBytes() != null) {
|
||||
BigDecimal inDiff = switchInfo.getInBytes().subtract(tempInfo.getInBytes());
|
||||
switchInfo.setInSpeed(inDiff.divide(divisor, 2, RoundingMode.HALF_UP));
|
||||
// 字节转为bit
|
||||
BigDecimal inDiffBit = inDiff.multiply(new BigDecimal(8)).setScale(0, RoundingMode.HALF_UP);
|
||||
switchInfo.setInSpeed(inDiffBit.divide(divisor, 2, RoundingMode.HALF_UP));
|
||||
}
|
||||
|
||||
// 计算outSpeed
|
||||
if (switchInfo.getOutBytes() != null && tempInfo.getOutBytes() != null) {
|
||||
BigDecimal outDiff = switchInfo.getOutBytes().subtract(tempInfo.getOutBytes());
|
||||
switchInfo.setOutSpeed(outDiff.divide(divisor, 2, RoundingMode.HALF_UP));
|
||||
// 字节转为bit
|
||||
BigDecimal outDiffBit = outDiff.multiply(new BigDecimal(8)).setScale(0, RoundingMode.HALF_UP);
|
||||
switchInfo.setOutSpeed(outDiffBit.divide(divisor, 2, RoundingMode.HALF_UP));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -140,4 +140,40 @@ public class DataProcessUtil {
|
||||
|
||||
return devicesResponse != null ? devicesResponse.getData() : Collections.emptyList();
|
||||
}
|
||||
|
||||
/**
|
||||
* 将采集到的字节流量转化为比特流量
|
||||
* 只是简单地将字节值乘以8得到比特值
|
||||
* @param speed 字节数值字符串
|
||||
* @return 比特数值字符串
|
||||
*/
|
||||
public String bytesToBits(String speed) {
|
||||
if (speed == null || speed.trim().isEmpty()) {
|
||||
return "0";
|
||||
}
|
||||
|
||||
String cleanedSpeed = speed.trim();
|
||||
|
||||
try {
|
||||
// 解析字节值并乘以8转换为比特值
|
||||
long bytesValue = Long.parseLong(cleanedSpeed);
|
||||
long bitsValue = bytesValue * 8;
|
||||
return String.valueOf(bitsValue);
|
||||
|
||||
} catch (NumberFormatException e) {
|
||||
// 如果解析失败,尝试使用double类型
|
||||
try {
|
||||
double bytesValue = Double.parseDouble(cleanedSpeed);
|
||||
double bitsValue = bytesValue * 8;
|
||||
// 如果是整数则返回整数形式,否则返回小数形式
|
||||
if (bitsValue == (long) bitsValue) {
|
||||
return String.valueOf((long) bitsValue);
|
||||
} else {
|
||||
return String.valueOf(bitsValue);
|
||||
}
|
||||
} catch (NumberFormatException ex) {
|
||||
throw new IllegalArgumentException("Invalid speed format: " + speed);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user