增加计算服务器平均流量方法、优化95值自动计算方法
This commit is contained in:
@@ -27,8 +27,12 @@ import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.util.*;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 设备消息处理器
|
||||
@@ -81,6 +85,8 @@ public class MessageHandler {
|
||||
private IRmMonitorPolicyService rmMonitorPolicyService;
|
||||
@Autowired
|
||||
private IRmDeploymentPolicyService rmDeploymentPolicyService;
|
||||
@Autowired
|
||||
private IInitialBandwidthTrafficTempService initialBandwidthTrafficTempService;
|
||||
|
||||
|
||||
/**
|
||||
@@ -631,22 +637,88 @@ public class MessageHandler {
|
||||
private void handleNetMessage(DeviceMessage message) {
|
||||
List<InitialBandwidthTraffic> interfaces = JsonDataParser.parseJsonData(message.getData(), InitialBandwidthTraffic.class);
|
||||
if(!interfaces.isEmpty()){
|
||||
String clientId = message.getClientId();
|
||||
// 时间戳转换
|
||||
long timestamp = interfaces.get(0).getTimestamp();
|
||||
long millis = timestamp * 1000;
|
||||
Date createTime = new Date(millis / 1000 * 1000); // 去除毫秒
|
||||
String timeStr = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",createTime);
|
||||
// 创建比timestamp少5分钟的时间
|
||||
long fiveMinutesEarlier = millis - (5 * 60 * 1000); // 减去5分钟的毫秒数
|
||||
Date fiveMinutesEarlierDate = new Date(fiveMinutesEarlier / 1000 * 1000); // 同样去除毫秒
|
||||
// 查询临时表信息,计算实际流量值
|
||||
InitialBandwidthTrafficTemp temp = new InitialBandwidthTrafficTemp();
|
||||
temp.setCreateTime(fiveMinutesEarlierDate);
|
||||
temp.setClientId(clientId);
|
||||
List<InitialBandwidthTrafficTemp> tempList = initialBandwidthTrafficTempService.selectInitialBandwidthTrafficTempList(temp);
|
||||
if(!tempList.isEmpty()){
|
||||
// 1. 构建快速查找的Map
|
||||
Map<String, InitialBandwidthTrafficTemp> tempMap = tempList.stream()
|
||||
.collect(Collectors.toMap(
|
||||
InitialBandwidthTrafficTemp::getMac,
|
||||
Function.identity(),
|
||||
(existing, replacement) -> existing
|
||||
));
|
||||
|
||||
// 2. 预计算除数(避免重复创建对象)
|
||||
BigDecimal divisor = new BigDecimal(300);
|
||||
|
||||
interfaces.forEach(iface -> {
|
||||
iface.setClientId(clientId);
|
||||
iface.setCreateTime(createTime);
|
||||
// 发送流量
|
||||
iface.setTotalOutSpeed(dataProcessUtil.bytesToBits(iface.getOutSpeed()));
|
||||
// 接收流量
|
||||
iface.setTotalInSpeed(dataProcessUtil.bytesToBits(iface.getInSpeed()));
|
||||
InitialBandwidthTrafficTemp tempInfo = tempMap.get(iface.getMac());
|
||||
if (tempInfo != null) {
|
||||
// 计算inSpeed
|
||||
if (iface.getTotalInSpeed() != null && tempInfo.getTotalInSpeed() != null) {
|
||||
BigDecimal nowInSpeed = new BigDecimal(iface.getTotalInSpeed());
|
||||
BigDecimal tempInSpeed = new BigDecimal(tempInfo.getTotalInSpeed());
|
||||
BigDecimal inDiff = nowInSpeed.subtract(tempInSpeed);
|
||||
|
||||
// 检查相减结果是否为非负数
|
||||
if (inDiff.compareTo(BigDecimal.ZERO) >= 0) {
|
||||
iface.setInSpeed(inDiff.divide(divisor, 2, RoundingMode.HALF_UP).toString());
|
||||
}else{
|
||||
iface.setInSpeed(null);
|
||||
}
|
||||
}
|
||||
// 计算outSpeed
|
||||
if (iface.getTotalOutSpeed() != null && tempInfo.getTotalOutSpeed() != null) {
|
||||
BigDecimal nowOutSpeed = new BigDecimal(iface.getTotalOutSpeed());
|
||||
BigDecimal tempOutSpeed = new BigDecimal(tempInfo.getTotalOutSpeed());
|
||||
BigDecimal outDiff = nowOutSpeed.subtract(tempOutSpeed);
|
||||
|
||||
// 检查相减结果是否为非负数
|
||||
if (outDiff.compareTo(BigDecimal.ZERO) >= 0) {
|
||||
iface.setOutSpeed(outDiff.divide(divisor, 2, RoundingMode.HALF_UP).toString());
|
||||
}else{
|
||||
iface.setOutSpeed(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
// 清空临时表对应server信息
|
||||
initialBandwidthTrafficTempService.deleteTempMsgByClientId(clientId);
|
||||
}else{
|
||||
interfaces.forEach(iface -> {
|
||||
iface.setClientId(clientId);
|
||||
iface.setCreateTime(createTime);
|
||||
iface.setInSpeed(null);
|
||||
iface.setOutSpeed(null);
|
||||
// 总发送流量
|
||||
iface.setTotalOutSpeed(dataProcessUtil.bytesToBits(iface.getOutSpeed()));
|
||||
// 总接收流量
|
||||
iface.setTotalInSpeed(dataProcessUtil.bytesToBits(iface.getInSpeed()));
|
||||
});
|
||||
}
|
||||
InitialBandwidthTraffic data = new InitialBandwidthTraffic();
|
||||
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);
|
||||
// 临时表 用来计算inSpeed outSeppd
|
||||
initialBandwidthTrafficTempService.batchInsertServerTemp(interfaces);
|
||||
// 初始流量数据入库
|
||||
initialBandwidthTrafficService.batchInsert(data);
|
||||
EpsInitialTrafficDataRemote epsInitialTrafficDataRemote = new EpsInitialTrafficDataRemote();
|
||||
|
||||
Reference in New Issue
Block a user