diff --git a/src/main/java/com/example/device/controller/DeviceStatus.java b/src/main/java/com/example/device/controller/DeviceStatus.java index 17569d1..cafab55 100644 --- a/src/main/java/com/example/device/controller/DeviceStatus.java +++ b/src/main/java/com/example/device/controller/DeviceStatus.java @@ -20,6 +20,8 @@ import java.util.stream.Collectors; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.FileOutputStream; +import java.io.BufferedReader; +import java.io.InputStreamReader; @Component @EnableScheduling @@ -34,28 +36,34 @@ public class DeviceStatus { private ExcelExportService excelExportService; - @Scheduled(cron = "0 51 8 * * ?") + @Scheduled(cron = "0 20 14 * * ?") // @Scheduled(cron = "0 0 1,5,7,8,11,14,17,19,21,23") public void generateDeviceStatus() { 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) { @@ -66,7 +74,44 @@ public class DeviceStatus { } /** - * 计算成功率 + * 计算丢包率 + */ + public String getPingPacketLossRate(String ipAddress) { + try { + // Execute the ping command + Process process = Runtime.getRuntime().exec("ping -c 4 " + ipAddress); // Sending 4 ICMP Echo Request packets + + // Read the output of the command + BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); + String line; + int packetsSent = 0; + int packetsLost = 0; + + while ((line = reader.readLine()) != null) { + // Check for lines containing packet loss information + if (line.contains("packets transmitted")) { + // Extract the number of packets sent and lost + String[] stats = line.split(", "); + packetsSent = Integer.parseInt(stats[0].split(" ")[0]); + packetsLost = Integer.parseInt(stats[0].split(" ")[0])-Integer.parseInt(stats[1].split(" ")[0]); + } + } + + // Calculate and return the packet loss rate + if (packetsSent > 0) { + return String.format("%.2f%%", (double) packetsLost / packetsSent * 100); + } else { + return "0.00%"; // No packets sent + } + } catch (IOException e) { + e.printStackTrace(); + return "-1.00%"; // Error occurred + } + } + + + /** + * 成功率和丢包率按小时分组 */ public void calculateSuccessRate() { @@ -79,19 +124,11 @@ public class DeviceStatus { status.setStartTime(todayStart); status.setTime(currentTime); List listStatus = statusService.list(status); - Map> map = listStatus.stream().collect(Collectors.groupingBy(Status::getDeviceName)); - for (String name : map.keySet()) { - // 成功的次数 - long successNumber = map.get(name).stream().filter(item -> item.getDeviceStatus() == 1).count(); - // 此ip对应的成功率 - String successRate = String.format("%.2f%%", (double) successNumber / map.get(name).size() * 100); - ipMap.put(name, successRate); - } + Map> map = listStatus.stream() + .collect(Collectors.groupingBy(Status -> Status.getTime().getHour())); String filePath=CreateNamedExcel(); - excelExportService.exportDataToExcel(ipMap,filePath); + 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 bc770e7..24a437d 100644 --- a/src/main/java/com/example/device/entity/Status.java +++ b/src/main/java/com/example/device/entity/Status.java @@ -87,8 +87,28 @@ public class Status { private LocalDateTime time; + public String getSuccessRate() { + return successRate; + } + + public void setSuccessRate(String successRate) { + this.successRate = successRate; + } + + public String getLostRate() { + return lostRate; + } + + public void setLostRate(String lostRate) { + this.lostRate = lostRate; + } + private LocalDateTime startTime; private String deviceIp; + private String successRate; + + private String lostRate; + } diff --git a/src/main/java/com/example/device/service/ExcelExportService.java b/src/main/java/com/example/device/service/ExcelExportService.java index c0a4bd7..b418d02 100644 --- a/src/main/java/com/example/device/service/ExcelExportService.java +++ b/src/main/java/com/example/device/service/ExcelExportService.java @@ -1,4 +1,5 @@ package com.example.device.service; +import com.example.device.entity.Status; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.stereotype.Service; @@ -8,36 +9,53 @@ import java.io.IOException; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; +import java.util.List; import java.util.Map; @Service public class ExcelExportService { - public void exportDataToExcel(Map ipMap, String filePath) { + public void exportDataToExcel(Map> ipMap, String filePath) { Workbook workbook = new XSSFWorkbook(); - Sheet sheet = workbook.createSheet("设备故障率"); + 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); int rowNum = 0; - for (String name : ipMap.keySet()) { - Row row = sheet.createRow(rowNum++); - row.createCell(0).setCellValue(name); - row.createCell(1).setCellValue(ipMap.get(name)); - row.createCell(2).setCellValue(formattedDateTime); - CellStyle style = workbook.createCellStyle(); - // 应用样式到单元格 - row.getCell(0).setCellStyle(style); - row.getCell(1).setCellStyle(style); - row.getCell(2).setCellStyle(style); + 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); + } } - try (FileOutputStream outputStream = new FileOutputStream(filePath)) { workbook.write(outputStream); } catch (IOException e) { e.printStackTrace(); } + System.out.println("excel写入成功!!!!!!"); } } diff --git a/src/main/resources/mapping/StatusMapper.xml b/src/main/resources/mapping/StatusMapper.xml index df0763f..4e33941 100644 --- a/src/main/resources/mapping/StatusMapper.xml +++ b/src/main/resources/mapping/StatusMapper.xml @@ -8,6 +8,8 @@ + + @@ -27,6 +29,13 @@ time, + + success_rate, + + + + lost_rate, + @@ -41,6 +50,12 @@ #{status.time,jdbcType=DATE}, + + #{status.successRate,jdbcType=VARCHAR}, + + + #{status.lostRate,jdbcType=VARCHAR}, + @@ -49,7 +64,7 @@