Browse Source

----修改多线程

main
mengff 11 months ago
parent
commit
0cdf892505
  1. 101
      src/main/java/com/example/device/controller/DeviceStatus.java
  2. 50
      src/main/java/com/example/device/entity/Status.java
  3. 73
      src/main/java/com/example/device/service/ExcelExportService.java
  4. 17
      src/main/resources/mapping/StatusMapper.xml

101
src/main/java/com/example/device/controller/DeviceStatus.java

@ -13,6 +13,7 @@ import java.net.InetAddress;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit; import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -22,6 +23,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.concurrent.*;
@Component @Component
@EnableScheduling @EnableScheduling
@ -36,7 +38,7 @@ public class DeviceStatus {
private ExcelExportService excelExportService; private ExcelExportService excelExportService;
@Scheduled(cron = "0 20 14 * * ?") @Scheduled(cron = "* * 0/1 * * ?")
// @Scheduled(cron = "0 0 1,5,7,8,11,14,17,19,21,23") // @Scheduled(cron = "0 0 1,5,7,8,11,14,17,19,21,23")
public void generateDeviceStatus() { public void generateDeviceStatus() {
deviceStatus(); deviceStatus();
@ -44,35 +46,90 @@ public class DeviceStatus {
// public void deviceStatus() {
// Status status = new Status();
// List<Device> deviceList = deviceService.SelectList();
// for (Device device : deviceList) {
// try {
// InetAddress address = InetAddress.getByName(device.getDeviceIp());
// String lostReat=getPingPacketLossRate(device.getDeviceIp());
// boolean reachable = address.isReachable(5000); // Timeout: 5 seconds
// status.setDeviceNo(device.getDeviceNo());
// status.setDeviceName(device.getDeviceName());
// LocalDateTime localDateTime = LocalDateTime.now();
// status.setTime(localDateTime);
// status.setLostRate(lostReat);
// //1-在线 0-离线
// if (reachable) {
// status.setDeviceStatus(1);
// status.setSuccessRate("100.00%");
// } else {
// status.setDeviceStatus(0);
// status.setSuccessRate("0.00%");
// }
// statusService.Add(status);
// } catch (IOException e) {
// System.out.println("Error pinging " + device.getDeviceIp() + ": " + e.getMessage());
// }
// }
// calculateSuccessRate();
// }
public void deviceStatus() { public void deviceStatus() {
Status status = new Status(); ExecutorService executor = Executors.newFixedThreadPool(100);
List<Device> deviceList = deviceService.SelectList(); List<Device> deviceList = deviceService.SelectList();
List<Future<Void>> futures = new ArrayList<>();
for (Device device : deviceList) { for (Device device : deviceList) {
try { Callable<Void> task = () -> {
InetAddress address = InetAddress.getByName(device.getDeviceIp()); try {
String lostReat=getPingPacketLossRate(device.getDeviceIp()); Status status = new Status();
boolean reachable = address.isReachable(5000); // Timeout: 5 seconds InetAddress address = InetAddress.getByName(device.getDeviceIp());
status.setDeviceNo(device.getDeviceNo()); String lostRate = getPingPacketLossRate(device.getDeviceIp());
status.setDeviceName(device.getDeviceName()); boolean reachable = address.isReachable(5000); // Timeout: 5 seconds
LocalDateTime localDateTime = LocalDateTime.now();
status.setTime(localDateTime); status.setDeviceNo(device.getDeviceNo());
status.setLostRate(lostReat); status.setDeviceName(device.getDeviceName());
//1-在线 0-离线 status.setDeviceIp(device.getDeviceIp());
if (reachable) { LocalDateTime localDateTime = LocalDateTime.now();
status.setDeviceStatus(1); status.setTime(localDateTime);
status.setSuccessRate("100.00%"); status.setLostRate(lostRate);
} else {
status.setDeviceStatus(0); if (reachable) {
status.setSuccessRate("0.00%"); status.setDeviceStatus(1);
status.setSuccessRate("100.00%");
} else {
status.setDeviceStatus(0);
status.setSuccessRate("0.00%");
}
statusService.Add(status);
} catch (IOException e) {
System.out.println("Error pinging " + device.getDeviceIp() + ": " + e.getMessage());
} }
statusService.Add(status);
} catch (IOException e) { return null;
System.out.println("Error pinging " + device.getDeviceIp() + ": " + e.getMessage()); };
futures.add(executor.submit(task));
}
for (Future<Void> future : futures) {
try {
future.get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
} }
} }
executor.shutdown();
calculateSuccessRate(); calculateSuccessRate();
} }
/** /**
* 计算丢包率 * 计算丢包率
*/ */
@ -129,6 +186,8 @@ public class DeviceStatus {
String filePath=CreateNamedExcel(); String filePath=CreateNamedExcel();
excelExportService.exportDataToExcel(map,filePath); excelExportService.exportDataToExcel(map,filePath);
} }
public String CreateNamedExcel() { public String CreateNamedExcel() {
LocalDateTime dateTime = LocalDateTime.now(); LocalDateTime dateTime = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

50
src/main/java/com/example/device/entity/Status.java

@ -111,4 +111,54 @@ public class Status {
private String lostRate; private String lostRate;
private String direction;
private String production;
public String getDirection() {
return direction;
}
public void setDirection(String direction) {
this.direction = direction;
}
public String getProduction() {
return production;
}
public void setProduction(String production) {
this.production = production;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getNetwork() {
return network;
}
public void setNetwork(String network) {
this.network = network;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
private String model;
private String network;
private String content;
} }

73
src/main/java/com/example/device/service/ExcelExportService.java

@ -1,6 +1,7 @@
package com.example.device.service; package com.example.device.service;
import com.example.device.entity.Status; import com.example.device.entity.Status;
import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -9,6 +10,7 @@ import java.io.IOException;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit; import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -17,38 +19,49 @@ public class ExcelExportService {
public void exportDataToExcel(Map<Integer, List<Status>> ipMap, String filePath) { public void exportDataToExcel(Map<Integer, List<Status>> ipMap, String filePath) {
Workbook workbook = new XSSFWorkbook(); Workbook workbook = new XSSFWorkbook();
for (Map.Entry<Integer, List<Status>> entry : ipMap.entrySet()) { Sheet sheet = workbook.createSheet("设备故障率统计页");
Sheet sheet = workbook.createSheet(entry.getKey()+"小时设备故障率");
LocalDateTime dateTime = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDateTime = dateTime.format(formatter);
int rowNum = 0; int rowNum = 0;
Row row = sheet.createRow(rowNum); Row row0 = sheet.createRow(0);
row.createCell(0).setCellValue("设备名称"); row0.createCell(0).setCellValue("设备名称");
row.createCell(1).setCellValue("设备IP"); row0.createCell(1).setCellValue("设备IP");
row.createCell(2).setCellValue("在线率"); row0.createCell(2).setCellValue("设备桩号");
row.createCell(3).setCellValue("丢包率"); row0.createCell(3).setCellValue("设备方向");
CellStyle style = workbook.createCellStyle(); row0.createCell(4).setCellValue("设备厂家");
// 应用样式到单元格 row0.createCell(5).setCellValue("设备型号");
row.getCell(0).setCellStyle(style); row0.createCell(6).setCellValue("备注");
row.getCell(1).setCellStyle(style); sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 0));
row.getCell(2).setCellStyle(style); sheet.addMergedRegion(new CellRangeAddress(0, 1, 1, 1));
row.getCell(3).setCellStyle(style); sheet.addMergedRegion(new CellRangeAddress(0, 1, 2, 2));
rowNum = 1; sheet.addMergedRegion(new CellRangeAddress(0, 1, 3, 3));
sheet.addMergedRegion(new CellRangeAddress(0, 1, 4, 4));
List<Status> groupItems = entry.getValue(); sheet.addMergedRegion(new CellRangeAddress(0, 1, 5, 5));
for (Status ignored : groupItems) { sheet.addMergedRegion(new CellRangeAddress(0, 1, 6, 6));
row= sheet.createRow(rowNum++); int i=0;
row.createCell(0).setCellValue(ignored.getDeviceName()); boolean flag = true;
row.createCell(1).setCellValue(ignored.getDeviceIp()); Row row1= sheet.createRow(1);
row.createCell(2).setCellValue(ignored.getSuccessRate()); for (Map.Entry<Integer, List<Status>> entry : ipMap.entrySet()) {
row.createCell(3).setCellValue(ignored.getLostRate()); row0.createCell(2*i+7).setCellValue(entry.getKey()+"时");
// 应用样式到单元格 sheet.addMergedRegion(new CellRangeAddress(0, 0, 2*i+7, 2*i+8));
row.getCell(0).setCellStyle(style); row1.createCell(2*i+7).setCellValue("在线率");
row.getCell(1).setCellStyle(style); row1.createCell(2*i+8).setCellValue("丢包率");
row.getCell(2).setCellStyle(style); rowNum = 2;
row.getCell(3).setCellStyle(style); List<Status> groupItems = entry.getValue();
for (Status ignored : groupItems) {
Row row = sheet.createRow(rowNum++);
if(flag) {
row.createCell(0).setCellValue(ignored.getDeviceName());
row.createCell(1).setCellValue(ignored.getDeviceIp());
row.createCell(2).setCellValue(ignored.getDeviceNo());
row.createCell(3).setCellValue(ignored.getDirection());
row.createCell(4).setCellValue(ignored.getProduction());
row.createCell(5).setCellValue(ignored.getModel());
row.createCell(6).setCellValue(ignored.getContent());
}
row.createCell(2*i+7).setCellValue(ignored.getSuccessRate());
row.createCell(2*i+8).setCellValue(ignored.getLostRate());
} }
i++;
flag = false;
} }
try (FileOutputStream outputStream = new FileOutputStream(filePath)) { try (FileOutputStream outputStream = new FileOutputStream(filePath)) {
workbook.write(outputStream); workbook.write(outputStream);

17
src/main/resources/mapping/StatusMapper.xml

@ -10,6 +10,12 @@
<result column="device_ip" jdbcType="VARCHAR" property="deviceIp"/> <result column="device_ip" jdbcType="VARCHAR" property="deviceIp"/>
<result column="success_rate" jdbcType="VARCHAR" property="successRate"/> <result column="success_rate" jdbcType="VARCHAR" property="successRate"/>
<result column="lost_rate" jdbcType="VARCHAR" property="lostRate"/> <result column="lost_rate" jdbcType="VARCHAR" property="lostRate"/>
<result column="direction" jdbcType="VARCHAR" property="direction"/>
<result column="production" jdbcType="VARCHAR" property="production"/>
<result column="model" jdbcType="VARCHAR" property="model"/>
<result column="network" jdbcType="VARCHAR" property="network"/>
<result column="content" jdbcType="VARCHAR" property="content"/>
</resultMap> </resultMap>
@ -36,6 +42,9 @@
<if test="status.lostRate != null"> <if test="status.lostRate != null">
lost_rate, lost_rate,
</if> </if>
<if test="status.deviceIp != null">
device_ip,
</if>
</trim> </trim>
<trim prefix="VALUES (" suffix=")" suffixOverrides=","> <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
<if test="status.deviceNo != null"> <if test="status.deviceNo != null">
@ -56,6 +65,10 @@
<if test="status.lostRate != null"> <if test="status.lostRate != null">
#{status.lostRate,jdbcType=VARCHAR}, #{status.lostRate,jdbcType=VARCHAR},
</if> </if>
<if test="status.deviceIp != null">
#{status.deviceIp,jdbcType=VARCHAR},
</if>
</trim> </trim>
</insert> </insert>
@ -64,9 +77,9 @@
</sql> </sql>
<select id="listStatus" parameterType="com.example.device.entity.Status" resultMap="BaseResultMap"> <select id="listStatus" parameterType="com.example.device.entity.Status" resultMap="BaseResultMap">
select s.id, s.device_no, s.device_name, s.device_status, s.time, d.device_ip,s.success_rate,s.lost_rate select s.id, s.device_no, s.device_name, s.device_status, s.time, d.device_ip,s.success_rate,s.lost_rate,d.direction,d.production,d.model,d.network,d.content
from status s from status s
LEFT JOIN device d on s.device_no = d.device_no LEFT JOIN device d on s.device_ip = d.device_ip
<where> <where>
<if test="status.time != null"> <if test="status.time != null">
AND s.time BETWEEN #{status.startTime,jdbcType=DATE} AND #{status.time,jdbcType=DATE} AND s.time BETWEEN #{status.startTime,jdbcType=DATE} AND #{status.time,jdbcType=DATE}

Loading…
Cancel
Save