diff --git a/src/main/java/com/example/device/controller/DeviceStatus.java b/src/main/java/com/example/device/controller/DeviceStatus.java index cafab55..fc6ec3a 100644 --- a/src/main/java/com/example/device/controller/DeviceStatus.java +++ b/src/main/java/com/example/device/controller/DeviceStatus.java @@ -13,6 +13,7 @@ import java.net.InetAddress; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -22,6 +23,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.FileOutputStream; import java.io.BufferedReader; import java.io.InputStreamReader; +import java.util.concurrent.*; @Component @EnableScheduling @@ -36,7 +38,7 @@ public class DeviceStatus { 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") public void generateDeviceStatus() { deviceStatus(); @@ -44,35 +46,90 @@ public class DeviceStatus { +// public void deviceStatus() { +// Status status = new Status(); +// List 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() { - Status status = new Status(); + ExecutorService executor = Executors.newFixedThreadPool(100); List deviceList = deviceService.SelectList(); + + List> futures = new ArrayList<>(); + 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%"); + Callable task = () -> { + try { + Status status = new Status(); + InetAddress address = InetAddress.getByName(device.getDeviceIp()); + String lostRate = getPingPacketLossRate(device.getDeviceIp()); + boolean reachable = address.isReachable(5000); // Timeout: 5 seconds + + status.setDeviceNo(device.getDeviceNo()); + status.setDeviceName(device.getDeviceName()); + status.setDeviceIp(device.getDeviceIp()); + LocalDateTime localDateTime = LocalDateTime.now(); + status.setTime(localDateTime); + status.setLostRate(lostRate); + + 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()); } - statusService.Add(status); - } catch (IOException e) { - System.out.println("Error pinging " + device.getDeviceIp() + ": " + e.getMessage()); + + return null; + }; + + futures.add(executor.submit(task)); + } + + for (Future future : futures) { + try { + future.get(); + } catch (InterruptedException | ExecutionException e) { + e.printStackTrace(); } } + + executor.shutdown(); calculateSuccessRate(); } + /** * 计算丢包率 */ @@ -129,6 +186,8 @@ public class DeviceStatus { String filePath=CreateNamedExcel(); excelExportService.exportDataToExcel(map,filePath); } + + public String CreateNamedExcel() { LocalDateTime dateTime = LocalDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); diff --git a/src/main/java/com/example/device/entity/Status.java b/src/main/java/com/example/device/entity/Status.java index 24a437d..5c60048 100644 --- a/src/main/java/com/example/device/entity/Status.java +++ b/src/main/java/com/example/device/entity/Status.java @@ -111,4 +111,54 @@ public class Status { 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; + } diff --git a/src/main/java/com/example/device/service/ExcelExportService.java b/src/main/java/com/example/device/service/ExcelExportService.java index b418d02..e457104 100644 --- a/src/main/java/com/example/device/service/ExcelExportService.java +++ b/src/main/java/com/example/device/service/ExcelExportService.java @@ -1,6 +1,7 @@ package com.example.device.service; import com.example.device.entity.Status; import org.apache.poi.ss.usermodel.*; +import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.stereotype.Service; @@ -9,6 +10,7 @@ import java.io.IOException; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; +import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -17,38 +19,49 @@ public class ExcelExportService { public void exportDataToExcel(Map> ipMap, String filePath) { Workbook workbook = new XSSFWorkbook(); - for (Map.Entry> entry : ipMap.entrySet()) { - 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); + Sheet sheet = workbook.createSheet("设备故障率统计页"); int rowNum = 0; - Row row = sheet.createRow(rowNum); - row.createCell(0).setCellValue("设备名称"); - row.createCell(1).setCellValue("设备IP"); - row.createCell(2).setCellValue("在线率"); - row.createCell(3).setCellValue("丢包率"); - CellStyle style = workbook.createCellStyle(); - // 应用样式到单元格 - row.getCell(0).setCellStyle(style); - row.getCell(1).setCellStyle(style); - row.getCell(2).setCellStyle(style); - row.getCell(3).setCellStyle(style); - rowNum = 1; - - List groupItems = entry.getValue(); - for (Status ignored : groupItems) { - row= sheet.createRow(rowNum++); - row.createCell(0).setCellValue(ignored.getDeviceName()); - row.createCell(1).setCellValue(ignored.getDeviceIp()); - row.createCell(2).setCellValue(ignored.getSuccessRate()); - row.createCell(3).setCellValue(ignored.getLostRate()); - // 应用样式到单元格 - row.getCell(0).setCellStyle(style); - row.getCell(1).setCellStyle(style); - row.getCell(2).setCellStyle(style); - row.getCell(3).setCellStyle(style); + Row row0 = sheet.createRow(0); + row0.createCell(0).setCellValue("设备名称"); + row0.createCell(1).setCellValue("设备IP"); + row0.createCell(2).setCellValue("设备桩号"); + row0.createCell(3).setCellValue("设备方向"); + row0.createCell(4).setCellValue("设备厂家"); + row0.createCell(5).setCellValue("设备型号"); + row0.createCell(6).setCellValue("备注"); + sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 0)); + sheet.addMergedRegion(new CellRangeAddress(0, 1, 1, 1)); + sheet.addMergedRegion(new CellRangeAddress(0, 1, 2, 2)); + sheet.addMergedRegion(new CellRangeAddress(0, 1, 3, 3)); + sheet.addMergedRegion(new CellRangeAddress(0, 1, 4, 4)); + sheet.addMergedRegion(new CellRangeAddress(0, 1, 5, 5)); + sheet.addMergedRegion(new CellRangeAddress(0, 1, 6, 6)); + int i=0; + boolean flag = true; + Row row1= sheet.createRow(1); + for (Map.Entry> entry : ipMap.entrySet()) { + row0.createCell(2*i+7).setCellValue(entry.getKey()+"时"); + sheet.addMergedRegion(new CellRangeAddress(0, 0, 2*i+7, 2*i+8)); + row1.createCell(2*i+7).setCellValue("在线率"); + row1.createCell(2*i+8).setCellValue("丢包率"); + rowNum = 2; + List 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)) { workbook.write(outputStream); diff --git a/src/main/resources/mapping/StatusMapper.xml b/src/main/resources/mapping/StatusMapper.xml index 4e33941..055cef6 100644 --- a/src/main/resources/mapping/StatusMapper.xml +++ b/src/main/resources/mapping/StatusMapper.xml @@ -10,6 +10,12 @@ + + + + + + @@ -36,6 +42,9 @@ lost_rate, + + device_ip, + @@ -56,6 +65,10 @@ #{status.lostRate,jdbcType=VARCHAR}, + + + #{status.deviceIp,jdbcType=VARCHAR}, + @@ -64,9 +77,9 @@