Browse Source

优化查询单个设备在线率请求

develop
zhaoxianglong 11 months ago
parent
commit
cc1968351b
  1. 28
      zc-business/src/main/java/com/zc/business/controller/StatusController.java
  2. 1
      zc-business/src/main/java/com/zc/business/mapper/StatusMapper.java
  3. 3
      zc-business/src/main/java/com/zc/business/service/impl/StatusService.java
  4. 13
      zc-business/src/main/resources/mapper/business/StatusMapper.xml

28
zc-business/src/main/java/com/zc/business/controller/StatusController.java

@ -13,10 +13,7 @@ import com.zc.business.service.impl.StatusService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
@ -99,9 +96,8 @@ public class StatusController extends BaseController {
@ApiOperation("根据设备Id查询折线图数据")
@GetMapping("/deviceStatusList")
public AjaxResult getDeviceStatusList(Long deviceId) {
@GetMapping("/deviceStatusList/{deviceId}")
public AjaxResult getDeviceStatusList(@PathVariable Long deviceId) {
LocalDateTime thirtyDaysAgo = LocalDateTime.now().minusDays(30);
LocalDateTime currentTime = LocalDateTime.now();
@ -110,20 +106,22 @@ public class StatusController extends BaseController {
status.setTime(currentTime);
status.setDeviceId(deviceId);
List<Status> listStatus = statusService.list(status);
List<Status> listStatus = statusService.deviceStatusListById(status);
// Group by day and calculate average successRate with two decimal places
Map<Integer, Integer> averageSuccessRateByDay = listStatus.stream()
// Calculate average successRate by day
Map<Integer, Double> averageSuccessRateByDay = listStatus.stream()
.collect(Collectors.groupingBy(s -> s.getTime().getDayOfMonth(),
Collectors.collectingAndThen(
Collectors.averagingDouble(s -> Double.parseDouble(s.getSuccessRate().replace("%", ""))),
avg -> Math.round(Float.parseFloat(String.format("%.2f", avg)))
)));
Collectors.averagingDouble(s -> Double.parseDouble(s.getSuccessRate().replace("%", "")))));
if (averageSuccessRateByDay.isEmpty()) {
return AjaxResult.success("暂无数据");
}
return AjaxResult.success(averageSuccessRateByDay);
// Round average successRate to two decimal places
Map<Integer, Double> roundedAverageSuccessRateByDay = averageSuccessRateByDay.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, entry -> Math.round(entry.getValue() * 100.0) / 100.0));
return AjaxResult.success(roundedAverageSuccessRateByDay);
}
//按类型划分设备

1
zc-business/src/main/java/com/zc/business/mapper/StatusMapper.java

@ -17,5 +17,6 @@ public interface StatusMapper {
int Add(@Param("status")Status status);
List<Status> listStatus(@Param("status")Status status);
List<Status> deviceStatusListById(@Param("status")Status status);
}

3
zc-business/src/main/java/com/zc/business/service/impl/StatusService.java

@ -30,4 +30,7 @@ public class StatusService {
return list;
}
public List<Status> deviceStatusListById(Status status) {
return statusMapper.deviceStatusListById(status);
}
}

13
zc-business/src/main/resources/mapper/business/StatusMapper.xml

@ -109,4 +109,17 @@
</where>
</select>
<select id="deviceStatusListById" parameterType="com.zc.business.domain.Status" resultMap="BaseResultMap">
select s.id,s.time,s.success_rate,s.lost_rate
from status s
<where>
<if test="status.time != null">
AND s.time BETWEEN #{status.startTime,jdbcType=DATE} AND #{status.time,jdbcType=DATE}
</if>
<if test="status.deviceId != null">
AND s.device_id = #{status.deviceId}
</if>
</where>
</select>
</mapper>

Loading…
Cancel
Save