设置定时任务,每月1号0点执行 计算 月均日95值,95带宽值/月
This commit is contained in:
+366
-8
@@ -1,25 +1,47 @@
|
||||
package com.ruoyi.system.config;
|
||||
|
||||
import com.ruoyi.system.domain.EpsInitialTrafficData;
|
||||
import com.ruoyi.system.domain.InitialSwitchInfoDetails;
|
||||
import com.ruoyi.system.service.EpsInitialTrafficDataService;
|
||||
import com.ruoyi.common.core.utils.DateUtils;
|
||||
import com.ruoyi.system.domain.*;
|
||||
import com.ruoyi.system.service.*;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 自动生成数据表
|
||||
*/
|
||||
@Configuration
|
||||
@EnableScheduling
|
||||
@Slf4j
|
||||
public class TableScheduleConfig {
|
||||
|
||||
private static final DateTimeFormatter TIME_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||
@Autowired
|
||||
private EpsInitialTrafficDataService epsInitialTrafficDataService;
|
||||
@Autowired
|
||||
private IInitialSwitchInfoDetailsService initialSwitchInfoDetailsService;
|
||||
@Autowired
|
||||
private IEpsNodeBandwidthService epsNodeBandwidthService;
|
||||
@Autowired
|
||||
private IAllInterfaceNameService allInterfaceNameService;
|
||||
@Autowired
|
||||
private IEpsServerRevenueConfigService epsServerRevenueConfigService;
|
||||
@Autowired
|
||||
private IRmEpsTopologyManagementService rmEpsTopologyManagementService;
|
||||
@Autowired
|
||||
private IEpsMethodChangeRecordService epsMethodChangeRecordService;
|
||||
|
||||
// 每月25号创建下月表
|
||||
@Scheduled(cron = "0 0 0 26 * ?")
|
||||
@Scheduled(cron = "0 0 0 25 * ?")
|
||||
// @Scheduled(initialDelay = 5000, fixedDelay = Long.MAX_VALUE)
|
||||
public void createNextMonthTables() {
|
||||
epsInitialTrafficDataService.createNextMonthTables();
|
||||
@@ -28,11 +50,347 @@ public class TableScheduleConfig {
|
||||
// 每天12点执行 计算95带宽值/日
|
||||
@Scheduled(cron = "0 0 0 * * ?", zone = "Asia/Shanghai")
|
||||
public void calculate95BandwidthDaily() {
|
||||
EpsInitialTrafficData queryParam = new EpsInitialTrafficData();
|
||||
epsInitialTrafficDataService.calculate95BandwidthDaily(queryParam);
|
||||
// 获取昨天的日期范围(北京时间)
|
||||
LocalDate yesterday = LocalDate.now(ZoneId.of("Asia/Shanghai")).minusDays(1);
|
||||
String dailyStartTime = yesterday.atStartOfDay().format(TIME_FORMAT); // 00:00:00
|
||||
String dailyEndTime = yesterday.atTime(23, 59, 59).format(TIME_FORMAT); // 23:59:59
|
||||
// 日
|
||||
String dayOrMonth = "1";
|
||||
// 有效-95带宽值/日
|
||||
epsInitialTrafficDataService.calculateBusiness95BandwidthDaily(queryParam);
|
||||
EpsInitialTrafficData queryParam = new EpsInitialTrafficData();
|
||||
queryParam.setDayOrMonth(dayOrMonth);
|
||||
epsInitialTrafficDataService.calculateBusiness95BandwidthDaily(queryParam, dailyStartTime, dailyEndTime);
|
||||
InitialSwitchInfoDetails initialSwitchInfoDetails = new InitialSwitchInfoDetails();
|
||||
epsInitialTrafficDataService.calculateSwitch95BandwidthDaily(initialSwitchInfoDetails);
|
||||
initialSwitchInfoDetails.setDayOrMonth(dayOrMonth);
|
||||
initialSwitchInfoDetailsService.calculateSwitch95BandwidthDaily(initialSwitchInfoDetails, dailyStartTime, dailyEndTime);
|
||||
}
|
||||
|
||||
// 每月1号0点执行 计算95带宽值/月
|
||||
@Scheduled(cron = "0 0 0 1 * ?", zone = "Asia/Shanghai")
|
||||
public void calculateMonthlyBandwidthTasks() {
|
||||
// 获取上个月的日期范围
|
||||
LocalDate lastMonth = LocalDate.now(ZoneId.of("Asia/Shanghai")).minusMonths(0);
|
||||
LocalDate firstDayOfMonth = lastMonth.withDayOfMonth(1);
|
||||
LocalDate lastDayOfMonth = lastMonth.withDayOfMonth(lastMonth.lengthOfMonth());
|
||||
|
||||
String monthlyStartTime = firstDayOfMonth.atStartOfDay().format(TIME_FORMAT);
|
||||
String monthlyEndTime = lastDayOfMonth.atTime(23, 59, 59).format(TIME_FORMAT);
|
||||
|
||||
// 顺序执行各任务,间隔10分钟
|
||||
executeWithDelay(() -> calculateServerMonthlyBandwidth(monthlyStartTime, monthlyEndTime), 0);
|
||||
executeWithDelay(() -> calculateSwitchMonthlyBandwidth(monthlyStartTime, monthlyEndTime), 10);
|
||||
executeWithDelay(() -> calculateServerAvgMonthlyBandwidth(monthlyStartTime, monthlyEndTime), 10);
|
||||
executeWithDelay(() -> calculateSwitchAvgMonthlyBandwidth(monthlyStartTime, monthlyEndTime), 10);
|
||||
}
|
||||
|
||||
/**
|
||||
* 延迟执行任务
|
||||
*/
|
||||
private void executeWithDelay(Runnable task, int delayMinutes) {
|
||||
try {
|
||||
if (delayMinutes > 0) {
|
||||
Thread.sleep(delayMinutes * 60 * 1000);
|
||||
}
|
||||
task.run();
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
log.error("任务执行被中断", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算服务器月95带宽值
|
||||
*/
|
||||
private void calculateServerMonthlyBandwidth(String monthlyStartTime, String monthlyEndTime) {
|
||||
log.info("开始计算服务器月95带宽值...");
|
||||
try {
|
||||
String dayOrMonth = "2";
|
||||
EpsInitialTrafficData queryParam = new EpsInitialTrafficData();
|
||||
queryParam.setDayOrMonth(dayOrMonth);
|
||||
epsInitialTrafficDataService.calculateBusiness95BandwidthDaily(queryParam, monthlyStartTime, monthlyEndTime);
|
||||
log.info("服务器月95带宽值计算完成");
|
||||
} catch (Exception e) {
|
||||
log.error("计算服务器月95带宽值失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算交换机月95带宽值
|
||||
*/
|
||||
private void calculateSwitchMonthlyBandwidth(String monthlyStartTime, String monthlyEndTime) {
|
||||
log.info("开始计算交换机月95带宽值...");
|
||||
try {
|
||||
String dayOrMonth = "2";
|
||||
InitialSwitchInfoDetails queryParam = new InitialSwitchInfoDetails();
|
||||
queryParam.setDayOrMonth(dayOrMonth);
|
||||
initialSwitchInfoDetailsService.calculateSwitch95BandwidthDaily(queryParam, monthlyStartTime, monthlyEndTime);
|
||||
log.info("交换机月95带宽值计算完成");
|
||||
} catch (Exception e) {
|
||||
log.error("计算交换机月95带宽值失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算服务器月平均95带宽值
|
||||
*/
|
||||
private void calculateServerAvgMonthlyBandwidth(String monthlyStartTime, String monthlyEndTime) {
|
||||
log.info("开始计算服务器月平均95带宽值...");
|
||||
try {
|
||||
EpsNodeBandwidth epsNodeBandwidth = new EpsNodeBandwidth();
|
||||
epsNodeBandwidth.setStartTime(monthlyStartTime);
|
||||
epsNodeBandwidth.setEndTime(monthlyEndTime);
|
||||
|
||||
List<AllInterfaceName> snList = allInterfaceNameService.getAllDeviceSn(new AllInterfaceName());
|
||||
for (AllInterfaceName allInterfaceName : snList) {
|
||||
processServerAvgBandwidth(epsNodeBandwidth, allInterfaceName);
|
||||
}
|
||||
log.info("服务器月平均95带宽值计算完成");
|
||||
} catch (Exception e) {
|
||||
log.error("计算服务器月平均95带宽值失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理单个服务器的平均带宽计算
|
||||
*/
|
||||
private void processServerAvgBandwidth(EpsNodeBandwidth epsNodeBandwidth, AllInterfaceName allInterfaceName) {
|
||||
final String deviceSn = allInterfaceName.getDeviceSn();
|
||||
|
||||
try {
|
||||
// 1. 设置基础设备信息
|
||||
epsNodeBandwidth.setHardwareSn(deviceSn);
|
||||
|
||||
// 2. 查询服务器配置
|
||||
EpsServerRevenueConfig queryConfig = new EpsServerRevenueConfig();
|
||||
queryConfig.setHardwareSn(deviceSn);
|
||||
List<EpsServerRevenueConfig> configList = epsServerRevenueConfigService.selectEpsServerRevenueConfigList(queryConfig);
|
||||
EpsServerRevenueConfig serverConfig = configList.isEmpty() ? new EpsServerRevenueConfig() : configList.get(0);
|
||||
epsNodeBandwidth.setNodeName(serverConfig.getNodeName());
|
||||
|
||||
// 3. 检查是否需要处理业务变更
|
||||
boolean shouldProcessChange = serverConfig.getUpdateTime() != null
|
||||
&& epsNodeBandwidth.getStartTime() != null
|
||||
&& epsNodeBandwidth.getStartTime().length() >= 10
|
||||
&& "1".equals(serverConfig.getChanged())
|
||||
&& DateUtils.parseDateToStr("yyyy-MM", serverConfig.getUpdateTime())
|
||||
.equals(epsNodeBandwidth.getStartTime().substring(0, 7));
|
||||
|
||||
// 4. 处理业务变更情况
|
||||
if (shouldProcessChange) {
|
||||
// 查询业务变更记录
|
||||
EpsMethodChangeRecord changeQuery = new EpsMethodChangeRecord();
|
||||
changeQuery.setHardwareSn(deviceSn);
|
||||
changeQuery.setStartTime(epsNodeBandwidth.getStartTime());
|
||||
changeQuery.setEndTime(epsNodeBandwidth.getEndTime());
|
||||
|
||||
List<EpsMethodChangeRecord> records = epsMethodChangeRecordService.selectEpsMethodChangeRecordList(changeQuery);
|
||||
|
||||
// 按businessCode分组处理(修复null key问题)
|
||||
if (!records.isEmpty()) {
|
||||
Map<String, List<EpsMethodChangeRecord>> groupedRecords = records.stream()
|
||||
.filter(record -> record.getBusinessCode() != null) // 过滤掉businessCode为null的记录
|
||||
.collect(Collectors.groupingBy(EpsMethodChangeRecord::getBusinessCode));
|
||||
|
||||
// 处理每个业务分组
|
||||
for (Map.Entry<String, List<EpsMethodChangeRecord>> entry : groupedRecords.entrySet()) {
|
||||
String businessCode = entry.getKey();
|
||||
// 取第一个有效业务名称(同businessCode的业务名称应该相同)
|
||||
String businessName = entry.getValue().stream()
|
||||
.filter(r -> r.getBusinessName() != null)
|
||||
.findFirst()
|
||||
.map(EpsMethodChangeRecord::getBusinessName)
|
||||
.orElse("未知业务");
|
||||
|
||||
// 创建业务带宽对象
|
||||
EpsNodeBandwidth bandwidthWithBiz = new EpsNodeBandwidth();
|
||||
bandwidthWithBiz.setHardwareSn(deviceSn);
|
||||
bandwidthWithBiz.setNodeName(serverConfig.getNodeName());
|
||||
bandwidthWithBiz.setStartTime(epsNodeBandwidth.getStartTime());
|
||||
bandwidthWithBiz.setEndTime(epsNodeBandwidth.getEndTime());
|
||||
bandwidthWithBiz.setBusinessId(businessCode);
|
||||
bandwidthWithBiz.setBusinessName(businessName);
|
||||
bandwidthWithBiz.setResourceType("1");
|
||||
|
||||
// 执行计算
|
||||
epsNodeBandwidthService.calculateAvg(bandwidthWithBiz);
|
||||
}
|
||||
|
||||
// 处理businessCode为null的记录(如果有)
|
||||
List<EpsMethodChangeRecord> nullBusinessRecords = records.stream()
|
||||
.filter(record -> record.getBusinessCode() == null)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
if (!nullBusinessRecords.isEmpty()) {
|
||||
log.warn("服务器 {} 有 {} 条记录的businessCode为null", deviceSn, nullBusinessRecords.size());
|
||||
// 可以选择记录日志或进行其他处理
|
||||
}
|
||||
|
||||
return; // 业务变更处理完成后直接返回
|
||||
}
|
||||
}
|
||||
|
||||
// 5. 处理正常情况(无业务变更或无需处理变更)
|
||||
EpsNodeBandwidth normalBandwidth = new EpsNodeBandwidth();
|
||||
normalBandwidth.setHardwareSn(deviceSn);
|
||||
normalBandwidth.setNodeName(serverConfig.getNodeName());
|
||||
normalBandwidth.setStartTime(epsNodeBandwidth.getStartTime());
|
||||
normalBandwidth.setEndTime(epsNodeBandwidth.getEndTime());
|
||||
normalBandwidth.setBusinessId(serverConfig.getBusinessCode());
|
||||
normalBandwidth.setBusinessName(serverConfig.getBusinessName());
|
||||
normalBandwidth.setResourceType("1");
|
||||
epsNodeBandwidthService.calculateAvg(normalBandwidth);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("处理服务器 {} 平均带宽失败", deviceSn, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算交换机月平均95带宽值
|
||||
*/
|
||||
private void calculateSwitchAvgMonthlyBandwidth(String monthlyStartTime, String monthlyEndTime) {
|
||||
log.info("开始计算交换机月平均95带宽值...");
|
||||
try {
|
||||
EpsNodeBandwidth epsNodeBandwidth = new EpsNodeBandwidth();
|
||||
epsNodeBandwidth.setStartTime(monthlyStartTime);
|
||||
epsNodeBandwidth.setEndTime(monthlyEndTime);
|
||||
|
||||
List<AllInterfaceName> switchSnList = allInterfaceNameService.getAllSwitchSn(new AllInterfaceName());
|
||||
for (AllInterfaceName switchSnMsg : switchSnList) {
|
||||
processSwitchAvgBandwidth(epsNodeBandwidth, switchSnMsg);
|
||||
}
|
||||
log.info("交换机月平均95带宽值计算完成");
|
||||
} catch (Exception e) {
|
||||
log.error("计算交换机月平均95带宽值失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理单个交换机的平均带宽计算
|
||||
*/
|
||||
private void processSwitchAvgBandwidth(EpsNodeBandwidth epsNodeBandwidth, AllInterfaceName switchSnMsg) {
|
||||
final String switchSn = switchSnMsg.getSwitchSn();
|
||||
|
||||
try {
|
||||
// 1. 查询交换机拓扑信息
|
||||
RmEpsTopologyManagement managementQuery = new RmEpsTopologyManagement();
|
||||
managementQuery.setSwitchSn(switchSn);
|
||||
List<RmEpsTopologyManagement> topologyList = rmEpsTopologyManagementService.selectRmEpsTopologyManagementList(managementQuery);
|
||||
|
||||
// 2. 设置基础信息
|
||||
RmEpsTopologyManagement topology = topologyList.isEmpty() ? new RmEpsTopologyManagement() : topologyList.get(0);
|
||||
epsNodeBandwidth.setSwitchSn(switchSn);
|
||||
epsNodeBandwidth.setInterfaceName(topology.getInterfaceName());
|
||||
|
||||
// 3. 判断连接设备类型
|
||||
boolean isServerConnected = "1".equals(topology.getConnectedDeviceType());
|
||||
|
||||
// 4. 处理服务器连接的情况
|
||||
if (isServerConnected && topology.getServerSn() != null) {
|
||||
String serverSn = topology.getServerSn();
|
||||
epsNodeBandwidth.setHardwareSn(serverSn);
|
||||
// 查询服务器配置
|
||||
EpsServerRevenueConfig queryConfig = new EpsServerRevenueConfig();
|
||||
queryConfig.setHardwareSn(serverSn);
|
||||
List<EpsServerRevenueConfig> configList = epsServerRevenueConfigService.selectEpsServerRevenueConfigList(queryConfig);
|
||||
EpsServerRevenueConfig serverConfig = configList.isEmpty() ? new EpsServerRevenueConfig() : configList.get(0);
|
||||
epsNodeBandwidth.setNodeName(serverConfig.getNodeName());
|
||||
|
||||
// 检查是否需要处理业务变更
|
||||
boolean shouldProcessChange = serverConfig.getUpdateTime() != null
|
||||
&& epsNodeBandwidth.getStartTime() != null
|
||||
&& epsNodeBandwidth.getStartTime().length() >= 10
|
||||
&& "1".equals(serverConfig.getChanged())
|
||||
&& DateUtils.parseDateToStr("yyyy-MM", serverConfig.getUpdateTime())
|
||||
.equals(epsNodeBandwidth.getStartTime().substring(0, 7));
|
||||
|
||||
if(shouldProcessChange){
|
||||
// 查询业务变更记录
|
||||
EpsMethodChangeRecord changeQuery = new EpsMethodChangeRecord();
|
||||
changeQuery.setHardwareSn(serverSn);
|
||||
changeQuery.setStartTime(epsNodeBandwidth.getStartTime());
|
||||
changeQuery.setEndTime(epsNodeBandwidth.getEndTime());
|
||||
|
||||
List<EpsMethodChangeRecord> records = epsMethodChangeRecordService.selectEpsMethodChangeRecordList(changeQuery);
|
||||
|
||||
// 按businessCode分组处理
|
||||
if (!records.isEmpty()) {
|
||||
Map<String, List<EpsMethodChangeRecord>> groupedRecords = records.stream()
|
||||
.filter(record -> record.getBusinessCode() != null) // 过滤掉businessCode为null的记录
|
||||
.collect(Collectors.groupingBy(EpsMethodChangeRecord::getBusinessCode));
|
||||
|
||||
// 处理每个业务分组
|
||||
for (Map.Entry<String, List<EpsMethodChangeRecord>> entry : groupedRecords.entrySet()) {
|
||||
String businessCode = entry.getKey();
|
||||
List<EpsMethodChangeRecord> businessRecords = entry.getValue();
|
||||
|
||||
// 取第一个有效业务名称
|
||||
String businessName = businessRecords.stream()
|
||||
.filter(r -> r.getBusinessName() != null)
|
||||
.findFirst()
|
||||
.map(EpsMethodChangeRecord::getBusinessName)
|
||||
.orElse("未知业务");
|
||||
|
||||
// 创建业务带宽对象
|
||||
EpsNodeBandwidth bandwidthWithBiz = new EpsNodeBandwidth();
|
||||
bandwidthWithBiz.setSwitchSn(switchSn);
|
||||
bandwidthWithBiz.setInterfaceName(topology.getInterfaceName());
|
||||
bandwidthWithBiz.setHardwareSn(serverSn);
|
||||
bandwidthWithBiz.setNodeName(topology.getServerName());
|
||||
bandwidthWithBiz.setUplinkSwitch(topology.getSwitchName());
|
||||
bandwidthWithBiz.setInterfaceLinkDeviceType(topology.getConnectedDeviceType());
|
||||
bandwidthWithBiz.setStartTime(epsNodeBandwidth.getStartTime());
|
||||
bandwidthWithBiz.setEndTime(epsNodeBandwidth.getEndTime());
|
||||
bandwidthWithBiz.setBusinessId(businessCode);
|
||||
bandwidthWithBiz.setBusinessName(businessName);
|
||||
bandwidthWithBiz.setResourceType("2");
|
||||
|
||||
// 执行计算
|
||||
epsNodeBandwidthService.calculateAvg(bandwidthWithBiz);
|
||||
}
|
||||
|
||||
// 处理businessCode为null的记录(如果有)
|
||||
List<EpsMethodChangeRecord> nullBusinessRecords = records.stream()
|
||||
.filter(record -> record.getBusinessCode() == null)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
if (!nullBusinessRecords.isEmpty()) {
|
||||
log.warn("交换机 {} 有 {} 条记录的businessCode为null", switchSn, nullBusinessRecords.size());
|
||||
// 可以选择记录日志或进行其他处理
|
||||
}
|
||||
|
||||
return; // 业务变更处理完成后直接返回
|
||||
}
|
||||
}else{
|
||||
// 5. 处理普通情况(无变更记录)
|
||||
EpsNodeBandwidth normalBandwidth = new EpsNodeBandwidth();
|
||||
normalBandwidth.setSwitchSn(switchSn);
|
||||
normalBandwidth.setInterfaceName(topology.getInterfaceName());
|
||||
normalBandwidth.setHardwareSn(topology.getServerSn());
|
||||
normalBandwidth.setNodeName(topology.getServerName());
|
||||
normalBandwidth.setUplinkSwitch(topology.getSwitchName());
|
||||
normalBandwidth.setInterfaceLinkDeviceType(topology.getConnectedDeviceType());
|
||||
normalBandwidth.setStartTime(epsNodeBandwidth.getStartTime());
|
||||
normalBandwidth.setEndTime(epsNodeBandwidth.getEndTime());
|
||||
normalBandwidth.setBusinessId(serverConfig.getBusinessCode());
|
||||
normalBandwidth.setBusinessName(serverConfig.getBusinessName());
|
||||
normalBandwidth.setResourceType("2");
|
||||
epsNodeBandwidthService.calculateAvg(normalBandwidth);
|
||||
}
|
||||
}
|
||||
|
||||
// 5. 处理机房出口情况
|
||||
EpsNodeBandwidth normalBandwidth = new EpsNodeBandwidth();
|
||||
normalBandwidth.setSwitchSn(switchSn);
|
||||
normalBandwidth.setInterfaceName(topology.getInterfaceName());
|
||||
normalBandwidth.setInterfaceLinkDeviceType(topology.getConnectedDeviceType());
|
||||
normalBandwidth.setStartTime(epsNodeBandwidth.getStartTime());
|
||||
normalBandwidth.setEndTime(epsNodeBandwidth.getEndTime());
|
||||
normalBandwidth.setResourceType("2");
|
||||
epsNodeBandwidthService.calculateAvg(normalBandwidth);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("处理交换机 {} 平均带宽失败", switchSn, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user