From 2e3b2af397bb38b0e1a728b477869dcc2fe9319f Mon Sep 17 00:00:00 2001 From: gaoyutao Date: Wed, 18 Mar 2026 18:06:11 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E6=96=B0=E5=A2=9E=E4=B8=9A=E5=8A=A1?= =?UTF-8?q?=E8=BF=9B=E7=A8=8B=E4=B8=8E=E5=90=8D=E7=A7=B0=E5=AF=B9=E5=BA=94?= =?UTF-8?q?=E8=A1=A8=E3=80=82=202=E3=80=81=E6=96=B0=E5=A2=9E=E4=B8=9A?= =?UTF-8?q?=E5=8A=A1=E6=B5=81=E9=87=8F=E6=9F=A5=E8=AF=A2=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tongran/common/core/utils/SpeedUtils.java | 50 +++-- .../system/EpsInitialTrafficDataMapper.xml | 2 +- .../AllBusinessNetNameController.java | 91 ++++++++ .../InitialNetBusinessTrafficController.java | 7 + .../rocketmq/domain/AllBusinessNetName.java | 29 +++ .../domain/InitialBandwidthTraffic.java | 2 + .../mapper/AllBusinessNetNameMapper.java | 61 ++++++ .../service/IAllBusinessNetNameService.java | 61 ++++++ .../IInitialNetBusinessTrafficService.java | 3 + .../impl/AllBusinessNetNameServiceImpl.java | 96 +++++++++ .../InitialBandwidthTrafficServiceImpl.java | 200 +++++++++++++++++- .../InitialNetBusinessTrafficServiceImpl.java | 57 ++++- .../rocketmq/AllBusinessNetNameMapper.xml | 82 +++++++ 13 files changed, 721 insertions(+), 20 deletions(-) create mode 100644 tongran-rocketmq/src/main/java/com/tongran/rocketmq/controller/AllBusinessNetNameController.java create mode 100644 tongran-rocketmq/src/main/java/com/tongran/rocketmq/domain/AllBusinessNetName.java create mode 100644 tongran-rocketmq/src/main/java/com/tongran/rocketmq/mapper/AllBusinessNetNameMapper.java create mode 100644 tongran-rocketmq/src/main/java/com/tongran/rocketmq/service/IAllBusinessNetNameService.java create mode 100644 tongran-rocketmq/src/main/java/com/tongran/rocketmq/service/impl/AllBusinessNetNameServiceImpl.java create mode 100644 tongran-rocketmq/src/main/resources/mapper/rocketmq/AllBusinessNetNameMapper.xml diff --git a/tongran-common/tongran-common-core/src/main/java/com/tongran/common/core/utils/SpeedUtils.java b/tongran-common/tongran-common-core/src/main/java/com/tongran/common/core/utils/SpeedUtils.java index 31858c9..bb312e3 100644 --- a/tongran-common/tongran-common-core/src/main/java/com/tongran/common/core/utils/SpeedUtils.java +++ b/tongran-common/tongran-common-core/src/main/java/com/tongran/common/core/utils/SpeedUtils.java @@ -150,7 +150,7 @@ public class SpeedUtils { } } public static String calculateUnit(List list, - String inSpeedField, String outSpeedField) + String inSpeedField, String outSpeedField) throws NoSuchFieldException, IllegalAccessException { if (list == null || list.isEmpty()) { @@ -159,10 +159,6 @@ public class SpeedUtils { 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"); @@ -175,8 +171,9 @@ public class SpeedUtils { inField.setAccessible(true); outField.setAccessible(true); - BigDecimal inSpeedBit = (BigDecimal) inField.get(obj) == null ? BigDecimal.ZERO : (BigDecimal) inField.get(obj); - BigDecimal outSpeedBit = (BigDecimal) outField.get(obj) == null ? BigDecimal.ZERO : (BigDecimal) outField.get(obj); + // 安全地获取字段值并转换为BigDecimal + BigDecimal inSpeedBit = convertToBigDecimal(inField.get(obj)); + BigDecimal outSpeedBit = convertToBigDecimal(outField.get(obj)); // 累加bit值(用于计算平均值) totalInSpeedBit = totalInSpeedBit.add(inSpeedBit); @@ -188,24 +185,53 @@ public class SpeedUtils { // 计算Gb平均值 BigDecimal size = new BigDecimal(list.size()); - BigDecimal avgInSpeedGb = totalInSpeedBit.divide(size, 2, RoundingMode.HALF_UP); - BigDecimal avgOutSpeedGb = totalOutSpeedBit.divide(size, 2, RoundingMode.HALF_UP); + BigDecimal avgInSpeedBit = totalInSpeedBit.divide(size, 2, RoundingMode.HALF_UP); + BigDecimal avgOutSpeedBit = totalOutSpeedBit.divide(size, 2, RoundingMode.HALF_UP); // 基于平均值的较大值确定推荐单位 - BigDecimal maxAvgBit = avgInSpeedGb.compareTo(avgOutSpeedGb) > 0 ? - avgInSpeedGb : avgOutSpeedGb; + 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{ + } else { recommendedUnit = "Kb"; } return recommendedUnit; } + + /** + * 安全地将不同类型转换为BigDecimal + */ + private static BigDecimal convertToBigDecimal(Object value) { + if (value == null) { + return BigDecimal.ZERO; + } + + try { + if (value instanceof BigDecimal) { + return (BigDecimal) value; + } else if (value instanceof String) { + String strValue = ((String) value).trim(); + if (strValue.isEmpty()) { + return BigDecimal.ZERO; + } + return new BigDecimal(strValue); + } else if (value instanceof Number) { + return new BigDecimal(value.toString()); + } else { + // 尝试转换为字符串再解析 + return new BigDecimal(value.toString()); + } + } catch (Exception e) { + // 转换失败时返回0 + return BigDecimal.ZERO; + } + } /** * 计算String类型(存储数字)的速度统计结果 * @param list 数据列表 diff --git a/tongran-modules/tongran-system/src/main/resources/mapper/system/EpsInitialTrafficDataMapper.xml b/tongran-modules/tongran-system/src/main/resources/mapper/system/EpsInitialTrafficDataMapper.xml index 482a228..9a2545a 100644 --- a/tongran-modules/tongran-system/src/main/resources/mapper/system/EpsInitialTrafficDataMapper.xml +++ b/tongran-modules/tongran-system/src/main/resources/mapper/system/EpsInitialTrafficDataMapper.xml @@ -318,7 +318,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" `total_in_speed` varchar(50) DEFAULT NULL COMMENT '累计接收字总节数', `total_out_speed` varchar(50) DEFAULT NULL COMMENT '累计发送总字节数', PRIMARY KEY (`id`), - UNIQUE INDEX `uk_client_mac_name_time`(`client_id`, `mac`, `name`, `create_time`) + UNIQUE INDEX `uk_client_mac_name_time`(`client_id`,`process_name`, `create_time`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='网络业务流量监控表'; diff --git a/tongran-rocketmq/src/main/java/com/tongran/rocketmq/controller/AllBusinessNetNameController.java b/tongran-rocketmq/src/main/java/com/tongran/rocketmq/controller/AllBusinessNetNameController.java new file mode 100644 index 0000000..b7e8276 --- /dev/null +++ b/tongran-rocketmq/src/main/java/com/tongran/rocketmq/controller/AllBusinessNetNameController.java @@ -0,0 +1,91 @@ +package com.tongran.rocketmq.controller; + +import com.tongran.common.core.utils.poi.ExcelUtil; +import com.tongran.common.core.web.controller.BaseController; +import com.tongran.common.core.web.domain.AjaxResult; +import com.tongran.common.log.annotation.Log; +import com.tongran.common.log.enums.BusinessType; +import com.tongran.common.security.annotation.RequiresPermissions; +import com.tongran.rocketmq.domain.AllBusinessNetName; +import com.tongran.rocketmq.service.IAllBusinessNetNameService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import javax.servlet.http.HttpServletResponse; +import java.util.List; + +/** + * 业务进程对照Controller + * + * @author gyt + * @date 2026-03-18 + */ +@RestController +@RequestMapping("/allBusinessNetName") +@RequiresPermissions("rocketmq:traffic") +public class AllBusinessNetNameController extends BaseController +{ + @Autowired + private IAllBusinessNetNameService allBusinessNetNameService; + + /** + * 查询业务进程对照列表 + */ + @GetMapping("/list") + public AjaxResult list(AllBusinessNetName allBusinessNetName) + { + List list = allBusinessNetNameService.selectAllBusinessNetNameList(allBusinessNetName); + return success(list); + } + + /** + * 导出业务进程对照列表 + */ + @Log(title = "业务进程对照", businessType = BusinessType.EXPORT) + @PostMapping("/export") + public void export(HttpServletResponse response, AllBusinessNetName allBusinessNetName) + { + List list = allBusinessNetNameService.selectAllBusinessNetNameList(allBusinessNetName); + ExcelUtil util = new ExcelUtil(AllBusinessNetName.class); + util.exportExcel(response, list, "业务进程对照数据"); + } + + /** + * 获取业务进程对照详细信息 + */ + @GetMapping(value = "/{id}") + public AjaxResult getInfo(@PathVariable("id") Long id) + { + return success(allBusinessNetNameService.selectAllBusinessNetNameById(id)); + } + + /** + * 新增业务进程对照 + */ + @Log(title = "业务进程对照", businessType = BusinessType.INSERT) + @PostMapping + public AjaxResult add(@RequestBody AllBusinessNetName allBusinessNetName) + { + return toAjax(allBusinessNetNameService.insertAllBusinessNetName(allBusinessNetName)); + } + + /** + * 修改业务进程对照 + */ + @Log(title = "业务进程对照", businessType = BusinessType.UPDATE) + @PutMapping + public AjaxResult edit(@RequestBody AllBusinessNetName allBusinessNetName) + { + return toAjax(allBusinessNetNameService.updateAllBusinessNetName(allBusinessNetName)); + } + + /** + * 删除业务进程对照 + */ + @Log(title = "业务进程对照", businessType = BusinessType.DELETE) + @DeleteMapping("/{ids}") + public AjaxResult remove(@PathVariable Long[] ids) + { + return toAjax(allBusinessNetNameService.deleteAllBusinessNetNameByIds(ids)); + } +} diff --git a/tongran-rocketmq/src/main/java/com/tongran/rocketmq/controller/InitialNetBusinessTrafficController.java b/tongran-rocketmq/src/main/java/com/tongran/rocketmq/controller/InitialNetBusinessTrafficController.java index b4679c3..44092fb 100644 --- a/tongran-rocketmq/src/main/java/com/tongran/rocketmq/controller/InitialNetBusinessTrafficController.java +++ b/tongran-rocketmq/src/main/java/com/tongran/rocketmq/controller/InitialNetBusinessTrafficController.java @@ -14,6 +14,7 @@ import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletResponse; import java.util.List; +import java.util.Map; /** * 网络业务流量监控Controller @@ -90,4 +91,10 @@ public class InitialNetBusinessTrafficController extends BaseController { return toAjax(initialNetBusinessTrafficService.deleteInitialNetBusinessTrafficByIds(ids)); } + + @PostMapping("/businessTrafficEcharts") + public AjaxResult businessTrafficEcharts(@RequestBody InitialNetBusinessTraffic initialNetBusinessTraffic) { + Map echartsData = initialNetBusinessTrafficService.businessTrafficEcharts(initialNetBusinessTraffic); + return success(echartsData); + } } diff --git a/tongran-rocketmq/src/main/java/com/tongran/rocketmq/domain/AllBusinessNetName.java b/tongran-rocketmq/src/main/java/com/tongran/rocketmq/domain/AllBusinessNetName.java new file mode 100644 index 0000000..95447fb --- /dev/null +++ b/tongran-rocketmq/src/main/java/com/tongran/rocketmq/domain/AllBusinessNetName.java @@ -0,0 +1,29 @@ +package com.tongran.rocketmq.domain; + +import com.tongran.common.core.annotation.Excel; +import com.tongran.common.core.web.domain.BaseEntity; +import lombok.Data; + +/** + * 业务进程对照对象 all_business_net_name + * + * @author gyt + * @date 2026-03-18 + */ +@Data +public class AllBusinessNetName extends BaseEntity +{ + private static final long serialVersionUID = 1L; + + /** 主键ID */ + private Long id; + + /** 客户端标识 */ + @Excel(name = "客户端标识") + private String clientId; + + /** 进程名称 */ + @Excel(name = "进程名称") + private String processName; + +} diff --git a/tongran-rocketmq/src/main/java/com/tongran/rocketmq/domain/InitialBandwidthTraffic.java b/tongran-rocketmq/src/main/java/com/tongran/rocketmq/domain/InitialBandwidthTraffic.java index 6d9903f..36493d3 100644 --- a/tongran-rocketmq/src/main/java/com/tongran/rocketmq/domain/InitialBandwidthTraffic.java +++ b/tongran-rocketmq/src/main/java/com/tongran/rocketmq/domain/InitialBandwidthTraffic.java @@ -111,4 +111,6 @@ public class InitialBandwidthTraffic extends BaseEntity private Map> clientInterfaces; /** 是否最后一次 */ private boolean lastTrafficFlag; + /** 业务名 */ + private String businessName; } diff --git a/tongran-rocketmq/src/main/java/com/tongran/rocketmq/mapper/AllBusinessNetNameMapper.java b/tongran-rocketmq/src/main/java/com/tongran/rocketmq/mapper/AllBusinessNetNameMapper.java new file mode 100644 index 0000000..9315bf7 --- /dev/null +++ b/tongran-rocketmq/src/main/java/com/tongran/rocketmq/mapper/AllBusinessNetNameMapper.java @@ -0,0 +1,61 @@ +package com.tongran.rocketmq.mapper; + +import java.util.List; +import com.tongran.rocketmq.domain.AllBusinessNetName; + +/** + * 业务进程对照Mapper接口 + * + * @author gyt + * @date 2026-03-18 + */ +public interface AllBusinessNetNameMapper +{ + /** + * 查询业务进程对照 + * + * @param id 业务进程对照主键 + * @return 业务进程对照 + */ + public AllBusinessNetName selectAllBusinessNetNameById(Long id); + + /** + * 查询业务进程对照列表 + * + * @param allBusinessNetName 业务进程对照 + * @return 业务进程对照集合 + */ + public List selectAllBusinessNetNameList(AllBusinessNetName allBusinessNetName); + + /** + * 新增业务进程对照 + * + * @param allBusinessNetName 业务进程对照 + * @return 结果 + */ + public int insertAllBusinessNetName(AllBusinessNetName allBusinessNetName); + + /** + * 修改业务进程对照 + * + * @param allBusinessNetName 业务进程对照 + * @return 结果 + */ + public int updateAllBusinessNetName(AllBusinessNetName allBusinessNetName); + + /** + * 删除业务进程对照 + * + * @param id 业务进程对照主键 + * @return 结果 + */ + public int deleteAllBusinessNetNameById(Long id); + + /** + * 批量删除业务进程对照 + * + * @param ids 需要删除的数据主键集合 + * @return 结果 + */ + public int deleteAllBusinessNetNameByIds(Long[] ids); +} diff --git a/tongran-rocketmq/src/main/java/com/tongran/rocketmq/service/IAllBusinessNetNameService.java b/tongran-rocketmq/src/main/java/com/tongran/rocketmq/service/IAllBusinessNetNameService.java new file mode 100644 index 0000000..2131b87 --- /dev/null +++ b/tongran-rocketmq/src/main/java/com/tongran/rocketmq/service/IAllBusinessNetNameService.java @@ -0,0 +1,61 @@ +package com.tongran.rocketmq.service; + +import java.util.List; +import com.tongran.rocketmq.domain.AllBusinessNetName; + +/** + * 业务进程对照Service接口 + * + * @author gyt + * @date 2026-03-18 + */ +public interface IAllBusinessNetNameService +{ + /** + * 查询业务进程对照 + * + * @param id 业务进程对照主键 + * @return 业务进程对照 + */ + public AllBusinessNetName selectAllBusinessNetNameById(Long id); + + /** + * 查询业务进程对照列表 + * + * @param allBusinessNetName 业务进程对照 + * @return 业务进程对照集合 + */ + public List selectAllBusinessNetNameList(AllBusinessNetName allBusinessNetName); + + /** + * 新增业务进程对照 + * + * @param allBusinessNetName 业务进程对照 + * @return 结果 + */ + public int insertAllBusinessNetName(AllBusinessNetName allBusinessNetName); + + /** + * 修改业务进程对照 + * + * @param allBusinessNetName 业务进程对照 + * @return 结果 + */ + public int updateAllBusinessNetName(AllBusinessNetName allBusinessNetName); + + /** + * 批量删除业务进程对照 + * + * @param ids 需要删除的业务进程对照主键集合 + * @return 结果 + */ + public int deleteAllBusinessNetNameByIds(Long[] ids); + + /** + * 删除业务进程对照信息 + * + * @param id 业务进程对照主键 + * @return 结果 + */ + public int deleteAllBusinessNetNameById(Long id); +} diff --git a/tongran-rocketmq/src/main/java/com/tongran/rocketmq/service/IInitialNetBusinessTrafficService.java b/tongran-rocketmq/src/main/java/com/tongran/rocketmq/service/IInitialNetBusinessTrafficService.java index fa5dc17..6857409 100644 --- a/tongran-rocketmq/src/main/java/com/tongran/rocketmq/service/IInitialNetBusinessTrafficService.java +++ b/tongran-rocketmq/src/main/java/com/tongran/rocketmq/service/IInitialNetBusinessTrafficService.java @@ -3,6 +3,7 @@ package com.tongran.rocketmq.service; import com.tongran.rocketmq.domain.InitialNetBusinessTraffic; import java.util.List; +import java.util.Map; /** * 网络业务流量监控Service接口 @@ -61,4 +62,6 @@ public interface IInitialNetBusinessTrafficService public int deleteInitialNetBusinessTrafficById(Long id); void batchInsertBusinessTraffic(InitialNetBusinessTraffic data); void batchInsertBusinessRecoverTraffic(InitialNetBusinessTraffic data); + + Map businessTrafficEcharts(InitialNetBusinessTraffic initialNetBusinessTraffic); } diff --git a/tongran-rocketmq/src/main/java/com/tongran/rocketmq/service/impl/AllBusinessNetNameServiceImpl.java b/tongran-rocketmq/src/main/java/com/tongran/rocketmq/service/impl/AllBusinessNetNameServiceImpl.java new file mode 100644 index 0000000..6edf7ef --- /dev/null +++ b/tongran-rocketmq/src/main/java/com/tongran/rocketmq/service/impl/AllBusinessNetNameServiceImpl.java @@ -0,0 +1,96 @@ +package com.tongran.rocketmq.service.impl; + +import java.util.List; +import com.tongran.common.core.utils.DateUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import com.tongran.rocketmq.mapper.AllBusinessNetNameMapper; +import com.tongran.rocketmq.domain.AllBusinessNetName; +import com.tongran.rocketmq.service.IAllBusinessNetNameService; + +/** + * 业务进程对照Service业务层处理 + * + * @author gyt + * @date 2026-03-18 + */ +@Service +public class AllBusinessNetNameServiceImpl implements IAllBusinessNetNameService +{ + @Autowired + private AllBusinessNetNameMapper allBusinessNetNameMapper; + + /** + * 查询业务进程对照 + * + * @param id 业务进程对照主键 + * @return 业务进程对照 + */ + @Override + public AllBusinessNetName selectAllBusinessNetNameById(Long id) + { + return allBusinessNetNameMapper.selectAllBusinessNetNameById(id); + } + + /** + * 查询业务进程对照列表 + * + * @param allBusinessNetName 业务进程对照 + * @return 业务进程对照 + */ + @Override + public List selectAllBusinessNetNameList(AllBusinessNetName allBusinessNetName) + { + return allBusinessNetNameMapper.selectAllBusinessNetNameList(allBusinessNetName); + } + + /** + * 新增业务进程对照 + * + * @param allBusinessNetName 业务进程对照 + * @return 结果 + */ + @Override + public int insertAllBusinessNetName(AllBusinessNetName allBusinessNetName) + { + allBusinessNetName.setCreateTime(DateUtils.getNowDate()); + return allBusinessNetNameMapper.insertAllBusinessNetName(allBusinessNetName); + } + + /** + * 修改业务进程对照 + * + * @param allBusinessNetName 业务进程对照 + * @return 结果 + */ + @Override + public int updateAllBusinessNetName(AllBusinessNetName allBusinessNetName) + { + allBusinessNetName.setUpdateTime(DateUtils.getNowDate()); + return allBusinessNetNameMapper.updateAllBusinessNetName(allBusinessNetName); + } + + /** + * 批量删除业务进程对照 + * + * @param ids 需要删除的业务进程对照主键 + * @return 结果 + */ + @Override + public int deleteAllBusinessNetNameByIds(Long[] ids) + { + return allBusinessNetNameMapper.deleteAllBusinessNetNameByIds(ids); + } + + /** + * 删除业务进程对照信息 + * + * @param id 业务进程对照主键 + * @return 结果 + */ + @Override + public int deleteAllBusinessNetNameById(Long id) + { + return allBusinessNetNameMapper.deleteAllBusinessNetNameById(id); + } +} diff --git a/tongran-rocketmq/src/main/java/com/tongran/rocketmq/service/impl/InitialBandwidthTrafficServiceImpl.java b/tongran-rocketmq/src/main/java/com/tongran/rocketmq/service/impl/InitialBandwidthTrafficServiceImpl.java index 08ca3f4..aa9187a 100644 --- a/tongran-rocketmq/src/main/java/com/tongran/rocketmq/service/impl/InitialBandwidthTrafficServiceImpl.java +++ b/tongran-rocketmq/src/main/java/com/tongran/rocketmq/service/impl/InitialBandwidthTrafficServiceImpl.java @@ -7,10 +7,7 @@ import com.tongran.rocketmq.domain.*; import com.tongran.rocketmq.enums.AlarmTypeEnum; import com.tongran.rocketmq.enums.ConditionItemEnum; import com.tongran.rocketmq.mapper.*; -import com.tongran.rocketmq.service.IInitialBandwidthTrafficService; -import com.tongran.rocketmq.service.IInitialDiskInfoService; -import com.tongran.rocketmq.service.IRmAlarmLogService; -import com.tongran.rocketmq.service.IRmNetworkInterfaceService; +import com.tongran.rocketmq.service.*; import com.tongran.rocketmq.utils.SendAlarmPushUtil; import com.tongran.rocketmq.utils.TableRouterUtil; import com.tongran.system.api.RemoteRevenueConfigService; @@ -23,6 +20,7 @@ import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; +import java.math.RoundingMode; import java.time.LocalDateTime; import java.time.ZoneId; import java.util.*; @@ -59,6 +57,10 @@ public class InitialBandwidthTrafficServiceImpl implements IInitialBandwidthTraf private IRmNetworkInterfaceService rmNetworkInterfaceService; @Autowired private RemoteRevenueConfigService remoteRevenueConfigService; + @Autowired + private IInitialNetBusinessTrafficService initialNetBusinessTrafficService; + @Autowired + private IAllBusinessNetNameService allBusinessNetNameService; /** * 查询初始带宽流量 @@ -583,6 +585,120 @@ public class InitialBandwidthTrafficServiceImpl implements IInitialBandwidthTraf resultMap.put("yData", yData); } + try { + // 根据业务名称查询业务进程名 + String businessName = initialBandwidthTraffic.getBusinessName(); + if(businessName != null){ + List allBusinessNetNameList = allBusinessNetNameService.selectAllBusinessNetNameList(new AllBusinessNetName()); + + // 第一步:过滤 + List filteredList = allBusinessNetNameList.stream() + .filter(item -> businessName.contains(item.getRemark())) + .collect(Collectors.toList()); + + // 第二步:根据processName去重 + Map uniqueMap = new LinkedHashMap<>(); + for (AllBusinessNetName item : filteredList) { + uniqueMap.putIfAbsent(item.getProcessName(), item); + } + + List result = new ArrayList<>(uniqueMap.values()); + + // 用于存储所有业务的单位,用于后续统一单位处理 + Map businessUnits = new HashMap<>(); + // 用于存储业务的显示名称 + Map businessDisplayNames = new HashMap<>(); + + for (AllBusinessNetName allBusinessNetName : result) { + // 构建业务流量查询对象 + InitialNetBusinessTraffic businessTrafficQuery = new InitialNetBusinessTraffic(); + businessTrafficQuery.setClientId(initialBandwidthTraffic.getClientId()); + businessTrafficQuery.setProcessName(allBusinessNetName.getProcessName()); + businessTrafficQuery.setStartTime(initialBandwidthTraffic.getStartTime()); + businessTrafficQuery.setEndTime(initialBandwidthTraffic.getEndTime()); + // 获取业务流量图表数据 + Map businessResult = initialNetBusinessTrafficService.businessTrafficEcharts(businessTrafficQuery); + + if (businessResult != null && !businessResult.isEmpty()) { + // 获取业务名称作为前缀标识 + String businessPrefix = allBusinessNetName.getProcessName(); + // 如果进程名包含特殊字符,可以进一步处理为合法的key + businessPrefix = businessPrefix.replaceAll("[^a-zA-Z0-9]", "_"); + + // 获取业务流量的y轴数据 + Map businessYData = (Map) businessResult.get("yData"); + if (businessYData != null && !businessYData.isEmpty()) { + // 获取主Map的yData + Map mainYData = (Map) resultMap.get("yData"); + + // 将业务流量数据合并到主yData中 + for (Map.Entry entry : businessYData.entrySet()) { + String businessKey = entry.getKey(); + Object businessValue = entry.getValue(); + + // 添加业务前缀避免key冲突,格式:business_{进程名}_{数据类型} + String newKey = "business_" + businessPrefix + "_" + businessKey; + mainYData.put(newKey, businessValue); + } + + resultMap.put("yData", mainYData); + } + + // 获取业务流量的单位 + String businessUnit = (String) businessResult.get("unit"); + businessUnits.put(businessPrefix, businessUnit); + + // 构建业务的显示名称 + String displayName = allBusinessNetName.getProcessName(); + if (allBusinessNetName.getRemark() != null && !allBusinessNetName.getRemark().isEmpty()) { + displayName = allBusinessNetName.getRemark() + "(" + allBusinessNetName.getProcessName() + ")"; + } + businessDisplayNames.put(businessPrefix, displayName); + } + } + + // 在所有业务数据处理完成后,统一进行单位转换 + if (!businessUnits.isEmpty()) { + // 检查是否有业务的单位与主单位不一致 + boolean needUnitConversion = false; + for (String businessUnit : businessUnits.values()) { + if (!unit.equals(businessUnit)) { + needUnitConversion = true; + break; + } + } + + // 如果需要单位转换,统一处理 + if (needUnitConversion) { + convertAllBusinessDataUnit(resultMap, businessUnits, unit); + } + + // 单位转换完成后,添加业务流量的展示关系(不再包含单位信息) + for (Map.Entry entry : businessDisplayNames.entrySet()) { + String businessPrefix = entry.getKey(); + String displayName = entry.getValue(); + + showRealation.put("business_" + businessPrefix + "_netInSpeedData", + displayName + " 入站流量"); + showRealation.put("business_" + businessPrefix + "_netOutSpeedData", + displayName + " 出站流量"); + showRealation.put("business_" + businessPrefix + "_netIpv4InSpeedData", + displayName + " IPv4入站流量"); + showRealation.put("business_" + businessPrefix + "_netIpv4OutSpeedData", + displayName + " IPv4出站流量"); + showRealation.put("business_" + businessPrefix + "_netIpv6InSpeedData", + displayName + " IPv6入站流量"); + showRealation.put("business_" + businessPrefix + "_netIpv6OutSpeedData", + displayName + " IPv6出站流量"); + } + } + } + } catch (Exception e) { + // 业务流量数据获取失败,记录日志但不影响主流程 + System.err.println("获取业务流量数据失败: " + e.getMessage()); + e.printStackTrace(); + } + if(!showRealation.isEmpty() && hasSubInterface){ showRealation.put("totalNetInTraffic", name+"总入站流量" + " IPv4: "+ipv4); showRealation.put("totalNetOutTraffic", name+"总出站流量" + " IPv4: "+ipv4); @@ -597,6 +713,82 @@ public class InitialBandwidthTrafficServiceImpl implements IInitialBandwidthTraf return new HashMap<>(); } } + + /** + * 统一转换所有业务数据的单位 + */ + private void convertAllBusinessDataUnit(Map resultMap, + Map businessUnits, + String targetUnit) { + try { + // 获取主Map的yData + Map mainYData = (Map) resultMap.get("yData"); + if (mainYData == null || mainYData.isEmpty()) { + return; + } + + // 为每个业务计算转换因子 + Map conversionFactors = new HashMap<>(); + for (Map.Entry entry : businessUnits.entrySet()) { + String businessPrefix = entry.getKey(); + String fromUnit = entry.getValue(); + + if (!targetUnit.equals(fromUnit)) { + BigDecimal fromDivisor = SpeedUtils.getDivisor(fromUnit); + BigDecimal toDivisor = SpeedUtils.getDivisor(targetUnit); + BigDecimal conversionFactor = fromDivisor.divide(toDivisor, 10, RoundingMode.HALF_UP); + conversionFactors.put(businessPrefix, conversionFactor); + } + } + + // 遍历所有业务流量相关的数据项进行转换 + for (Map.Entry entry : mainYData.entrySet()) { + String key = entry.getKey(); + + // 检查是否是业务数据 + if (key.startsWith("business_")) { + // 提取业务前缀 + String[] keyParts = key.split("_"); + if (keyParts.length >= 3) { + String businessPrefix = keyParts[1]; + + // 如果该业务需要转换 + if (conversionFactors.containsKey(businessPrefix)) { + BigDecimal conversionFactor = conversionFactors.get(businessPrefix); + Object value = entry.getValue(); + + if (value instanceof List) { + List dataList = (List) value; + List convertedList = new ArrayList<>(); + + for (BigDecimal dataPoint : dataList) { + if (dataPoint != null) { + // 应用转换因子 + BigDecimal converted = dataPoint.multiply(conversionFactor) + .setScale(2, RoundingMode.HALF_UP); + convertedList.add(converted); + } else { + convertedList.add(null); + } + } + + // 更新转换后的数据 + entry.setValue(convertedList); + } + } + } + } + } + + // 更新单位为统一单位 + resultMap.put("unit", targetUnit); + + System.out.println("所有业务数据单位已统一转换为: " + targetUnit); + + } catch (Exception e) { + System.err.println("业务流量单位统一转换失败: " + e.getMessage()); + } + } // 安全转换方法(处理可能的NumberFormatException) private Double safeConvertToKB(String byteValue) { try { diff --git a/tongran-rocketmq/src/main/java/com/tongran/rocketmq/service/impl/InitialNetBusinessTrafficServiceImpl.java b/tongran-rocketmq/src/main/java/com/tongran/rocketmq/service/impl/InitialNetBusinessTrafficServiceImpl.java index f968aa9..a996edd 100644 --- a/tongran-rocketmq/src/main/java/com/tongran/rocketmq/service/impl/InitialNetBusinessTrafficServiceImpl.java +++ b/tongran-rocketmq/src/main/java/com/tongran/rocketmq/service/impl/InitialNetBusinessTrafficServiceImpl.java @@ -1,11 +1,12 @@ package com.tongran.rocketmq.service.impl; import com.tongran.common.core.utils.DateUtils; +import com.tongran.common.core.utils.EchartsDataUtils; +import com.tongran.common.core.utils.SpeedUtils; import com.tongran.common.core.utils.TableSubUtil; import com.tongran.rocketmq.domain.InitialNetBusinessTraffic; import com.tongran.rocketmq.mapper.InitialNetBusinessTrafficMapper; import com.tongran.rocketmq.service.IInitialNetBusinessTrafficService; -import com.tongran.rocketmq.utils.TableRouterUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -13,7 +14,10 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Transactional; +import java.math.BigDecimal; +import java.math.RoundingMode; import java.util.*; +import java.util.function.Function; import java.util.stream.Collectors; /** @@ -211,9 +215,56 @@ public class InitialNetBusinessTrafficServiceImpl implements IInitialNetBusiness }); } + @Override + public Map businessTrafficEcharts(InitialNetBusinessTraffic initialNetBusinessTraffic) { + List list = getListByTableName(initialNetBusinessTraffic); + try { + String unit = SpeedUtils.calculateUnit(list, "inSpeed", "outSpeed"); + if(initialNetBusinessTraffic.getUnit() != null){ + unit = initialNetBusinessTraffic.getUnit(); + } + BigDecimal divisor = SpeedUtils.getDivisor(unit); + Map> extractors = new LinkedHashMap<>(); + extractors.put("netInSpeedData", info -> + info != null && info.getInSpeed() != null ? + new BigDecimal(info.getInSpeed()).divide(divisor, 2, RoundingMode.HALF_UP) : + null); + + extractors.put("netOutSpeedData", info -> + info != null && info.getOutSpeed() != null ? + new BigDecimal(info.getOutSpeed()).divide(divisor, 2, RoundingMode.HALF_UP) : + null); + extractors.put("netIpv4InSpeedData", info -> + info != null && info.getIpv4InSpeed() != null ? + new BigDecimal(info.getIpv4InSpeed()).divide(divisor, 2, RoundingMode.HALF_UP) : + null); + extractors.put("netIpv4OutSpeedData", info -> + info != null && info.getIpv4OutSpeed() != null ? + new BigDecimal(info.getIpv4OutSpeed()).divide(divisor, 2, RoundingMode.HALF_UP) : + null); + extractors.put("netIpv6InSpeedData", info -> + info != null && info.getIpv6InSpeed() != null ? + new BigDecimal(info.getIpv6InSpeed()).divide(divisor, 2, RoundingMode.HALF_UP) : + null); + extractors.put("netIpv6OutSpeedData", info -> + info != null && info.getIpv6OutSpeed() != null ? + new BigDecimal(info.getIpv6OutSpeed()).divide(divisor, 2, RoundingMode.HALF_UP) : + null); + Map resultMap = EchartsDataUtils.buildEchartsDataAutoPadding( + list, InitialNetBusinessTraffic::getCreateTime, extractors, + initialNetBusinessTraffic.getStartTime(), initialNetBusinessTraffic.getEndTime() + ); + resultMap.put("unit", unit); + return resultMap; + } catch (Exception e){ + e.printStackTrace(); + } + return new HashMap<>(); + } + public List getListByTableName(InitialNetBusinessTraffic initialNetBusinessTraffic){ // 获取涉及的表名 - Set tableNames = TableRouterUtil.getTableNamesBetween(initialNetBusinessTraffic.getStartTime(), initialNetBusinessTraffic.getEndTime()); + Set tableNames = TableSubUtil.getTableNamesBetween(initialNetBusinessTraffic.getStartTime(), initialNetBusinessTraffic.getEndTime(),"initial_net_business_traffic"); // 并行查询各表 List list = tableNames.parallelStream() .flatMap(tableName -> { @@ -222,7 +273,7 @@ public class InitialNetBusinessTrafficServiceImpl implements IInitialNetBusiness condition.setStartTime(initialNetBusinessTraffic.getStartTime()); condition.setEndTime(initialNetBusinessTraffic.getEndTime()); condition.setClientId(initialNetBusinessTraffic.getClientId()); - condition.setName(initialNetBusinessTraffic.getName()); + condition.setProcessName(initialNetBusinessTraffic.getProcessName()); return initialNetBusinessTrafficMapper.getNetBusinessTrafficList(condition).stream(); }) .collect(Collectors.toList()); diff --git a/tongran-rocketmq/src/main/resources/mapper/rocketmq/AllBusinessNetNameMapper.xml b/tongran-rocketmq/src/main/resources/mapper/rocketmq/AllBusinessNetNameMapper.xml new file mode 100644 index 0000000..e00da82 --- /dev/null +++ b/tongran-rocketmq/src/main/resources/mapper/rocketmq/AllBusinessNetNameMapper.xml @@ -0,0 +1,82 @@ + + + + + + + + + + + + + + + + + select id, client_id, process_name, remark, create_by, create_time, update_by, update_time from all_business_net_name + + + + + + + + insert into all_business_net_name + + client_id, + process_name, + remark, + create_by, + create_time, + update_by, + update_time, + + + #{clientId}, + #{processName}, + #{remark}, + #{createBy}, + #{createTime}, + #{updateBy}, + #{updateTime}, + + + + + update all_business_net_name + + client_id = #{clientId}, + process_name = #{processName}, + remark = #{remark}, + create_by = #{createBy}, + create_time = #{createTime}, + update_by = #{updateBy}, + update_time = #{updateTime}, + + where id = #{id} + + + + delete from all_business_net_name where id = #{id} + + + + delete from all_business_net_name where id in + + #{id} + + + \ No newline at end of file