Browse Source

增加计算ip成功率代码

main
Mr.Wang 11 months ago
parent
commit
3e0739078a
  1. 39
      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

39
src/main/java/com/example/device/controller/DeviceStatus.java

@ -10,7 +10,11 @@ import org.springframework.stereotype.Component;
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress; import java.net.InetAddress;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Component @Component
@EnableScheduling @EnableScheduling
@ -23,12 +27,14 @@ public class DeviceStatus {
//每天凌晨开始执行,每4小时执行一次 //每天凌晨开始执行,每4小时执行一次
@Scheduled(cron = "0 0 4/0 * * ?") @Scheduled(cron = "0 33 * * * ?")
public void generateDeviceStatus(){ // @Scheduled(cron = "0 0 1,5,7,8,11,14,17,19,21,23")
public void generateDeviceStatus() {
deviceStatus(); deviceStatus();
} }
public void deviceStatus() { public void deviceStatus() {
Status status=new Status(); Status status = new Status();
List<Device> deviceList = deviceService.SelectList(); List<Device> deviceList = deviceService.SelectList();
for (Device device : deviceList) { for (Device device : deviceList) {
try { try {
@ -51,5 +57,32 @@ public class DeviceStatus {
} }
} }
/**
* 计算成功率
*/
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 * @Date 2024/01/04
*/ */
public class Status { 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() { public LocalDateTime getTime() {
return time; return time;
} }
@ -61,6 +75,8 @@ public class Status {
", deviceName='" + deviceName + '\'' + ", deviceName='" + deviceName + '\'' +
", deviceStatus=" + deviceStatus + ", deviceStatus=" + deviceStatus +
", time=" + time + ", time=" + time +
", startTime=" + startTime +
", deviceIp=" + deviceIp +
'}'; '}';
} }
@ -71,4 +87,8 @@ public class Status {
private LocalDateTime time; 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 com.example.device.entity.Status;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.List;
/** /**
* @author mengff * @author mengff
@ -15,4 +16,6 @@ public interface StatusMapper {
int Add(@Param("status")Status status); 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 com.example.device.mapper.StatusMapper;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List;
/** /**
* @author mengff * @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_no" jdbcType="VARCHAR" property="deviceNo"/>
<result column="device_name" jdbcType="VARCHAR" property="deviceName"/> <result column="device_name" jdbcType="VARCHAR" property="deviceName"/>
<result column="device_status" jdbcType="INTEGER" property="deviceStatus"/> <result column="device_status" jdbcType="INTEGER" property="deviceStatus"/>
<result column="device_ip" jdbcType="VARCHAR" property="deviceIp"/>
</resultMap> </resultMap>
@ -43,4 +44,22 @@
</trim> </trim>
</insert> </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> </mapper>
Loading…
Cancel
Save