增加计算服务器平均流量方法、优化95值自动计算方法

This commit is contained in:
gaoyutao
2025-11-06 13:47:29 +08:00
parent b0868dfe2d
commit 5e5330a924
13 changed files with 942 additions and 48 deletions
@@ -0,0 +1,105 @@
package com.ruoyi.rocketmq.controller;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.rocketmq.domain.InitialBandwidthTrafficTemp;
import com.ruoyi.rocketmq.service.IInitialBandwidthTrafficTempService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
/**
* 初始带宽流量临时表Controller
*
* @author gyt
* @date 2025-11-05
*/
@RestController
@RequestMapping("/trafficTemp")
public class InitialBandwidthTrafficTempController extends BaseController
{
@Autowired
private IInitialBandwidthTrafficTempService initialBandwidthTrafficTempService;
/**
* 查询初始带宽流量临时表列表
*/
@RequiresPermissions("rocketmq:trafficTemp:list")
@GetMapping("/list")
public TableDataInfo list(InitialBandwidthTrafficTemp initialBandwidthTrafficTemp)
{
startPage();
List<InitialBandwidthTrafficTemp> list = initialBandwidthTrafficTempService.selectInitialBandwidthTrafficTempList(initialBandwidthTrafficTemp);
return getDataTable(list);
}
/**
* 导出初始带宽流量临时表列表
*/
@RequiresPermissions("rocketmq:trafficTemp:export")
@Log(title = "初始带宽流量临时表", businessType = BusinessType.EXPORT)
@PostMapping("/export")
public void export(HttpServletResponse response, InitialBandwidthTrafficTemp initialBandwidthTrafficTemp)
{
List<InitialBandwidthTrafficTemp> list = initialBandwidthTrafficTempService.selectInitialBandwidthTrafficTempList(initialBandwidthTrafficTemp);
ExcelUtil<InitialBandwidthTrafficTemp> util = new ExcelUtil<InitialBandwidthTrafficTemp>(InitialBandwidthTrafficTemp.class);
util.exportExcel(response, list, "初始带宽流量临时表数据");
}
/**
* 获取初始带宽流量临时表详细信息
*/
@RequiresPermissions("rocketmq:trafficTemp:query")
@GetMapping(value = "/{id}")
public AjaxResult getInfo(@PathVariable("id") Long id)
{
return success(initialBandwidthTrafficTempService.selectInitialBandwidthTrafficTempById(id));
}
/**
* 新增初始带宽流量临时表
*/
@RequiresPermissions("rocketmq:trafficTemp:add")
@Log(title = "初始带宽流量临时表", businessType = BusinessType.INSERT)
@PostMapping
public AjaxResult add(@RequestBody InitialBandwidthTrafficTemp initialBandwidthTrafficTemp)
{
return toAjax(initialBandwidthTrafficTempService.insertInitialBandwidthTrafficTemp(initialBandwidthTrafficTemp));
}
/**
* 修改初始带宽流量临时表
*/
@RequiresPermissions("rocketmq:trafficTemp:edit")
@Log(title = "初始带宽流量临时表", businessType = BusinessType.UPDATE)
@PutMapping
public AjaxResult edit(@RequestBody InitialBandwidthTrafficTemp initialBandwidthTrafficTemp)
{
return toAjax(initialBandwidthTrafficTempService.updateInitialBandwidthTrafficTemp(initialBandwidthTrafficTemp));
}
/**
* 删除初始带宽流量临时表
*/
@RequiresPermissions("rocketmq:trafficTemp:remove")
@Log(title = "初始带宽流量临时表", businessType = BusinessType.DELETE)
@DeleteMapping("/{ids}")
public AjaxResult remove(@PathVariable Long[] ids)
{
return toAjax(initialBandwidthTrafficTempService.deleteInitialBandwidthTrafficTempByIds(ids));
}
}
@@ -51,12 +51,12 @@ public class InitialBandwidthTraffic extends BaseEntity
@Excel(name = "出站丢包率(%)")
private BigDecimal outDropped;
/** 接收带宽(Mbps) */
@Excel(name = "接收带宽(Mbps)")
/** 接收带宽(bit) */
@Excel(name = "接收带宽(bit)")
private String inSpeed;
/** 发送带宽(Mbps) */
@Excel(name = "发送带宽(Mbps)")
/** 发送带宽(bit) */
@Excel(name = "发送带宽(bit)")
private String outSpeed;
/** 设备唯一标识 */
@@ -74,5 +74,12 @@ public class InitialBandwidthTraffic extends BaseEntity
private String endTime;
/** 单位 */
private String unit;
/** 总接收带宽 */
@Excel(name = "总接收带宽")
private String totalInSpeed;
/** 总发送带宽 */
@Excel(name = "总发送带宽")
private String totalOutSpeed;
}
@@ -0,0 +1,252 @@
package com.ruoyi.rocketmq.domain;
import java.math.BigDecimal;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.ruoyi.common.core.annotation.Excel;
import com.ruoyi.common.core.web.domain.BaseEntity;
/**
* 初始带宽流量临时表对象 initial_bandwidth_traffic_temp
*
* @author gyt
* @date 2025-11-05
*/
public class InitialBandwidthTrafficTemp extends BaseEntity
{
private static final long serialVersionUID = 1L;
/** 唯一标识ID */
private Long id;
/** 接口名称 */
@Excel(name = "接口名称")
private String name;
/** MAC地址 */
@Excel(name = "MAC地址")
private String mac;
/** 运行状态 */
@Excel(name = "运行状态")
private String status;
/** 接口类型 */
@Excel(name = "接口类型")
private String type;
/** IPv4地址 */
@Excel(name = "IPv4地址")
private String ipV4;
/** 入站丢包率(%) */
@Excel(name = "入站丢包率(%)")
private BigDecimal inDropped;
/** 出站丢包率(%) */
@Excel(name = "出站丢包率(%)")
private BigDecimal outDropped;
/** 接收带宽(big) */
@Excel(name = "接收带宽(big)")
private String inSpeed;
/** 发送带宽(bit) */
@Excel(name = "发送带宽(bit)")
private String outSpeed;
/** 协商速度 */
@Excel(name = "协商速度")
private String speed;
/** 工作模式 */
@Excel(name = "工作模式")
private String duplex;
/** 设备唯一标识 */
@Excel(name = "设备唯一标识")
private String clientId;
/** 总接收带宽(bit) */
@Excel(name = "总接收带宽(bit)")
private String totalInSpeed;
/** 总发送带宽(bit) */
@Excel(name = "总发送带宽(bit)")
private String totalOutSpeed;
public void setId(Long id)
{
this.id = id;
}
public Long getId()
{
return id;
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void setMac(String mac)
{
this.mac = mac;
}
public String getMac()
{
return mac;
}
public void setStatus(String status)
{
this.status = status;
}
public String getStatus()
{
return status;
}
public void setType(String type)
{
this.type = type;
}
public String getType()
{
return type;
}
public void setIpV4(String ipV4)
{
this.ipV4 = ipV4;
}
public String getIpV4()
{
return ipV4;
}
public void setInDropped(BigDecimal inDropped)
{
this.inDropped = inDropped;
}
public BigDecimal getInDropped()
{
return inDropped;
}
public void setOutDropped(BigDecimal outDropped)
{
this.outDropped = outDropped;
}
public BigDecimal getOutDropped()
{
return outDropped;
}
public void setInSpeed(String inSpeed)
{
this.inSpeed = inSpeed;
}
public String getInSpeed()
{
return inSpeed;
}
public void setOutSpeed(String outSpeed)
{
this.outSpeed = outSpeed;
}
public String getOutSpeed()
{
return outSpeed;
}
public void setSpeed(String speed)
{
this.speed = speed;
}
public String getSpeed()
{
return speed;
}
public void setDuplex(String duplex)
{
this.duplex = duplex;
}
public String getDuplex()
{
return duplex;
}
public void setClientId(String clientId)
{
this.clientId = clientId;
}
public String getClientId()
{
return clientId;
}
public void setTotalInSpeed(String totalInSpeed)
{
this.totalInSpeed = totalInSpeed;
}
public String getTotalInSpeed()
{
return totalInSpeed;
}
public void setTotalOutSpeed(String totalOutSpeed)
{
this.totalOutSpeed = totalOutSpeed;
}
public String getTotalOutSpeed()
{
return totalOutSpeed;
}
@Override
public String toString() {
return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
.append("id", getId())
.append("name", getName())
.append("mac", getMac())
.append("status", getStatus())
.append("type", getType())
.append("ipV4", getIpV4())
.append("inDropped", getInDropped())
.append("outDropped", getOutDropped())
.append("inSpeed", getInSpeed())
.append("outSpeed", getOutSpeed())
.append("speed", getSpeed())
.append("duplex", getDuplex())
.append("createTime", getCreateTime())
.append("updateTime", getUpdateTime())
.append("createBy", getCreateBy())
.append("updateBy", getUpdateBy())
.append("clientId", getClientId())
.append("totalInSpeed", getTotalInSpeed())
.append("totalOutSpeed", getTotalOutSpeed())
.toString();
}
}
@@ -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();
@@ -0,0 +1,73 @@
package com.ruoyi.rocketmq.mapper;
import com.ruoyi.rocketmq.domain.InitialBandwidthTraffic;
import com.ruoyi.rocketmq.domain.InitialBandwidthTrafficTemp;
import org.springframework.data.repository.query.Param;
import java.util.List;
/**
* 初始带宽流量临时表Mapper接口
*
* @author gyt
* @date 2025-11-05
*/
public interface InitialBandwidthTrafficTempMapper
{
/**
* 查询初始带宽流量临时表
*
* @param id 初始带宽流量临时表主键
* @return 初始带宽流量临时表
*/
public InitialBandwidthTrafficTemp selectInitialBandwidthTrafficTempById(Long id);
/**
* 查询初始带宽流量临时表列表
*
* @param initialBandwidthTrafficTemp 初始带宽流量临时表
* @return 初始带宽流量临时表集合
*/
public List<InitialBandwidthTrafficTemp> selectInitialBandwidthTrafficTempList(InitialBandwidthTrafficTemp initialBandwidthTrafficTemp);
/**
* 新增初始带宽流量临时表
*
* @param initialBandwidthTrafficTemp 初始带宽流量临时表
* @return 结果
*/
public int insertInitialBandwidthTrafficTemp(InitialBandwidthTrafficTemp initialBandwidthTrafficTemp);
/**
* 修改初始带宽流量临时表
*
* @param initialBandwidthTrafficTemp 初始带宽流量临时表
* @return 结果
*/
public int updateInitialBandwidthTrafficTemp(InitialBandwidthTrafficTemp initialBandwidthTrafficTemp);
/**
* 删除初始带宽流量临时表
*
* @param id 初始带宽流量临时表主键
* @return 结果
*/
public int deleteInitialBandwidthTrafficTempById(Long id);
/**
* 批量删除初始带宽流量临时表
*
* @param ids 需要删除的数据主键集合
* @return 结果
*/
public int deleteInitialBandwidthTrafficTempByIds(Long[] ids);
int deleteTempMsgByClientId(String clientId);
/**
* 批量新增
* @param interfaces
* @return
*/
int batchInsertServerTemp(@Param("list") List<InitialBandwidthTraffic> interfaces);
}
@@ -0,0 +1,77 @@
package com.ruoyi.rocketmq.service;
import com.ruoyi.rocketmq.domain.InitialBandwidthTraffic;
import com.ruoyi.rocketmq.domain.InitialBandwidthTrafficTemp;
import java.util.List;
/**
* 初始带宽流量临时表Service接口
*
* @author gyt
* @date 2025-11-05
*/
public interface IInitialBandwidthTrafficTempService
{
/**
* 查询初始带宽流量临时表
*
* @param id 初始带宽流量临时表主键
* @return 初始带宽流量临时表
*/
public InitialBandwidthTrafficTemp selectInitialBandwidthTrafficTempById(Long id);
/**
* 查询初始带宽流量临时表列表
*
* @param initialBandwidthTrafficTemp 初始带宽流量临时表
* @return 初始带宽流量临时表集合
*/
public List<InitialBandwidthTrafficTemp> selectInitialBandwidthTrafficTempList(InitialBandwidthTrafficTemp initialBandwidthTrafficTemp);
/**
* 新增初始带宽流量临时表
*
* @param initialBandwidthTrafficTemp 初始带宽流量临时表
* @return 结果
*/
public int insertInitialBandwidthTrafficTemp(InitialBandwidthTrafficTemp initialBandwidthTrafficTemp);
/**
* 修改初始带宽流量临时表
*
* @param initialBandwidthTrafficTemp 初始带宽流量临时表
* @return 结果
*/
public int updateInitialBandwidthTrafficTemp(InitialBandwidthTrafficTemp initialBandwidthTrafficTemp);
/**
* 批量删除初始带宽流量临时表
*
* @param ids 需要删除的初始带宽流量临时表主键集合
* @return 结果
*/
public int deleteInitialBandwidthTrafficTempByIds(Long[] ids);
/**
* 删除初始带宽流量临时表信息
*
* @param id 初始带宽流量临时表主键
* @return 结果
*/
public int deleteInitialBandwidthTrafficTempById(Long id);
/**
* 根据clientId删除信息
* @param clientId
* @return
*/
int deleteTempMsgByClientId(String clientId);
/**
* 批量新增临时表信息
* @param interfaces
* @return
*/
int batchInsertServerTemp(List<InitialBandwidthTraffic> interfaces);
}
@@ -0,0 +1,108 @@
package com.ruoyi.rocketmq.service.impl;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.rocketmq.domain.InitialBandwidthTraffic;
import com.ruoyi.rocketmq.domain.InitialBandwidthTrafficTemp;
import com.ruoyi.rocketmq.mapper.InitialBandwidthTrafficTempMapper;
import com.ruoyi.rocketmq.service.IInitialBandwidthTrafficTempService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* 初始带宽流量临时表Service业务层处理
*
* @author gyt
* @date 2025-11-05
*/
@Service
public class InitialBandwidthTrafficTempServiceImpl implements IInitialBandwidthTrafficTempService
{
@Autowired
private InitialBandwidthTrafficTempMapper initialBandwidthTrafficTempMapper;
/**
* 查询初始带宽流量临时表
*
* @param id 初始带宽流量临时表主键
* @return 初始带宽流量临时表
*/
@Override
public InitialBandwidthTrafficTemp selectInitialBandwidthTrafficTempById(Long id)
{
return initialBandwidthTrafficTempMapper.selectInitialBandwidthTrafficTempById(id);
}
/**
* 查询初始带宽流量临时表列表
*
* @param initialBandwidthTrafficTemp 初始带宽流量临时表
* @return 初始带宽流量临时表
*/
@Override
public List<InitialBandwidthTrafficTemp> selectInitialBandwidthTrafficTempList(InitialBandwidthTrafficTemp initialBandwidthTrafficTemp)
{
return initialBandwidthTrafficTempMapper.selectInitialBandwidthTrafficTempList(initialBandwidthTrafficTemp);
}
/**
* 新增初始带宽流量临时表
*
* @param initialBandwidthTrafficTemp 初始带宽流量临时表
* @return 结果
*/
@Override
public int insertInitialBandwidthTrafficTemp(InitialBandwidthTrafficTemp initialBandwidthTrafficTemp)
{
initialBandwidthTrafficTemp.setCreateTime(DateUtils.getNowDate());
return initialBandwidthTrafficTempMapper.insertInitialBandwidthTrafficTemp(initialBandwidthTrafficTemp);
}
/**
* 修改初始带宽流量临时表
*
* @param initialBandwidthTrafficTemp 初始带宽流量临时表
* @return 结果
*/
@Override
public int updateInitialBandwidthTrafficTemp(InitialBandwidthTrafficTemp initialBandwidthTrafficTemp)
{
initialBandwidthTrafficTemp.setUpdateTime(DateUtils.getNowDate());
return initialBandwidthTrafficTempMapper.updateInitialBandwidthTrafficTemp(initialBandwidthTrafficTemp);
}
/**
* 批量删除初始带宽流量临时表
*
* @param ids 需要删除的初始带宽流量临时表主键
* @return 结果
*/
@Override
public int deleteInitialBandwidthTrafficTempByIds(Long[] ids)
{
return initialBandwidthTrafficTempMapper.deleteInitialBandwidthTrafficTempByIds(ids);
}
/**
* 删除初始带宽流量临时表信息
*
* @param id 初始带宽流量临时表主键
* @return 结果
*/
@Override
public int deleteInitialBandwidthTrafficTempById(Long id)
{
return initialBandwidthTrafficTempMapper.deleteInitialBandwidthTrafficTempById(id);
}
@Override
public int deleteTempMsgByClientId(String clientId) {
return initialBandwidthTrafficTempMapper.deleteTempMsgByClientId(clientId);
}
@Override
public int batchInsertServerTemp(List<InitialBandwidthTraffic> interfaces) {
return initialBandwidthTrafficTempMapper.batchInsertServerTemp(interfaces);
}
}