|
|
@ -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<Device> 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<String, String> calculateSuccessRate() { |
|
|
|
|
|
|
|
LocalDateTime todayStart = LocalDateTime.now().truncatedTo(ChronoUnit.DAYS); |
|
|
|
LocalDateTime currentTime = LocalDateTime.now(); |
|
|
|
Map<String, String> ipMap = new HashMap<>(); |
|
|
|
|
|
|
|
|
|
|
|
Status status = new Status(); |
|
|
|
status.setStartTime(todayStart); |
|
|
|
status.setTime(currentTime); |
|
|
|
List<Status> listStatus = statusService.list(status); |
|
|
|
Map<String, List<Status>> 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; |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|