Browse Source

增加计算ip成功率代码

main
Mr.Wang 11 months ago
parent
commit
3e0739078a
  1. 45
      src/main/java/com/example/device/controller/DeviceStatus.java
  2. 20
      src/main/java/com/example/device/entity/Status.java
  3. 3
      src/main/java/com/example/device/mapper/StatusMapper.java
  4. 5
      src/main/java/com/example/device/service/StatusService.java
  5. 19
      src/main/resources/mapping/StatusMapper.xml

45
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<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;
}
}

20
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;
private LocalDateTime startTime;
private String deviceIp;
}

3
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);
List<Status> listStatus(@Param("status")Status status);
}

5
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<Status> list(Status status) {
List<Status> list = statusMapper.listStatus(status);
return list;
}
}

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

@ -7,6 +7,7 @@
<result column="device_no" jdbcType="VARCHAR" property="deviceNo"/>
<result column="device_name" jdbcType="VARCHAR" property="deviceName"/>
<result column="device_status" jdbcType="INTEGER" property="deviceStatus"/>
<result column="device_ip" jdbcType="VARCHAR" property="deviceIp"/>
</resultMap>
@ -43,4 +44,22 @@
</trim>
</insert>
<sql id="selectStatus">
select id, device_no, device_name, device_status, time from status
</sql>
<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
from status s
LEFT JOIN device d on s.device_no = d.device_no
<where>
<if test="status.time != null">
AND s.time BETWEEN #{status.startTime,jdbcType=DATE} AND #{status.time,jdbcType=DATE}
</if>
<if test="status.deviceNo != null">
AND s.device_no = #{status.deviceNo}
</if>
</where>
</select>
</mapper>
Loading…
Cancel
Save