From 3e0739078a11f0016802d9d92af653c4e9a514bf Mon Sep 17 00:00:00 2001 From: "Mr.Wang" Date: Fri, 5 Jan 2024 16:36:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=AE=A1=E7=AE=97ip=E6=88=90?= =?UTF-8?q?=E5=8A=9F=E7=8E=87=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../device/controller/DeviceStatus.java | 45 ++++++++++++++++--- .../com/example/device/entity/Status.java | 22 ++++++++- .../example/device/mapper/StatusMapper.java | 5 ++- .../example/device/service/StatusService.java | 7 ++- src/main/resources/mapping/StatusMapper.xml | 21 ++++++++- 5 files changed, 90 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/example/device/controller/DeviceStatus.java b/src/main/java/com/example/device/controller/DeviceStatus.java index 855f3df..ceac7d4 100644 --- a/src/main/java/com/example/device/controller/DeviceStatus.java +++ b/src/main/java/com/example/device/controller/DeviceStatus.java @@ -10,7 +10,11 @@ import org.springframework.stereotype.Component; import java.io.IOException; import java.net.InetAddress; import java.time.LocalDateTime; +import java.time.temporal.ChronoUnit; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; @Component @EnableScheduling @@ -19,16 +23,18 @@ public class DeviceStatus { private DeviceService deviceService; @Autowired - private StatusService statusService; + private StatusService statusService; - //每天凌晨开始执行,每4小时执行一次 - @Scheduled(cron = "0 0 4/0 * * ?") - public void generateDeviceStatus(){ + //每天凌晨开始执行,每4小时执行一次 + @Scheduled(cron = "0 33 * * * ?") + // @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(); + + public void deviceStatus() { + Status status = new Status(); List deviceList = deviceService.SelectList(); for (Device device : deviceList) { try { @@ -49,7 +55,34 @@ public class DeviceStatus { System.out.println("Error pinging " + device.getDeviceIp() + ": " + e.getMessage()); } } + } + + /** + * 计算成功率 + */ + public Map calculateSuccessRate() { + + LocalDateTime todayStart = LocalDateTime.now().truncatedTo(ChronoUnit.DAYS); + LocalDateTime currentTime = LocalDateTime.now(); + Map ipMap = new HashMap<>(); + + + Status status = new Status(); + status.setStartTime(todayStart); + status.setTime(currentTime); + List listStatus = statusService.list(status); + Map> map = listStatus.stream().collect(Collectors.groupingBy(Status::getDeviceIp)); + + for (String ip : map.keySet()) { + // 成功的次数 + long successNumber = map.get(ip).stream().filter(item -> item.getDeviceStatus() == 1).count(); + // 此ip对应的成功率 + String successRate = String.format("%.2f%%", (double) successNumber / map.get(ip).size() * 100); + ipMap.put(ip, successRate); } + return ipMap; + } + } diff --git a/src/main/java/com/example/device/entity/Status.java b/src/main/java/com/example/device/entity/Status.java index 93e275d..bc770e7 100644 --- a/src/main/java/com/example/device/entity/Status.java +++ b/src/main/java/com/example/device/entity/Status.java @@ -7,6 +7,20 @@ import java.time.LocalDateTime; * @Date 2024/01/04 */ public class Status { + public String getDeviceIp() { + return deviceIp; + } + + public void setDeviceIp(String deviceIp) { + this.deviceIp = deviceIp; + } + public LocalDateTime getStartTime() { + return startTime; + } + + public void setStartTime(LocalDateTime startTime) { + this.startTime = startTime; + } public LocalDateTime getTime() { return time; } @@ -61,6 +75,8 @@ public class Status { ", deviceName='" + deviceName + '\'' + ", deviceStatus=" + deviceStatus + ", time=" + time + + ", startTime=" + startTime + + ", deviceIp=" + deviceIp + '}'; } @@ -71,4 +87,8 @@ public class Status { private LocalDateTime time; -} \ No newline at end of file + private LocalDateTime startTime; + + private String deviceIp; + +} diff --git a/src/main/java/com/example/device/mapper/StatusMapper.java b/src/main/java/com/example/device/mapper/StatusMapper.java index c81c662..312de56 100644 --- a/src/main/java/com/example/device/mapper/StatusMapper.java +++ b/src/main/java/com/example/device/mapper/StatusMapper.java @@ -3,6 +3,7 @@ package com.example.device.mapper; import com.example.device.entity.Status; import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Repository; +import java.util.List; /** * @author mengff @@ -15,4 +16,6 @@ public interface StatusMapper { int Add(@Param("status")Status status); -} \ No newline at end of file + List listStatus(@Param("status")Status status); + +} diff --git a/src/main/java/com/example/device/service/StatusService.java b/src/main/java/com/example/device/service/StatusService.java index daae780..1e642de 100644 --- a/src/main/java/com/example/device/service/StatusService.java +++ b/src/main/java/com/example/device/service/StatusService.java @@ -4,6 +4,7 @@ import com.example.device.entity.Status; import com.example.device.mapper.StatusMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.List; /** * @author mengff @@ -24,5 +25,9 @@ public class StatusService { } } + public List list(Status status) { + List list = statusMapper.listStatus(status); + return list; + } -} \ No newline at end of file +} diff --git a/src/main/resources/mapping/StatusMapper.xml b/src/main/resources/mapping/StatusMapper.xml index eee43fc..df0763f 100644 --- a/src/main/resources/mapping/StatusMapper.xml +++ b/src/main/resources/mapping/StatusMapper.xml @@ -7,6 +7,7 @@ + @@ -43,4 +44,22 @@ - \ No newline at end of file + + select id, device_no, device_name, device_status, time from status + + + + +