优化代码

main
liutao 1 week ago
parent 719cfb797e
commit 9585c9eb96

@ -159,46 +159,7 @@ public class DeviceOperationRecordServiceImpl implements DeviceOperationRecordSe
@Override @Override
public List<DeviceTotalTimeRecordRespVO> deviceOperationPageList(DeviceTotalTimeRecordReqVO pageReqVO) { public List<DeviceTotalTimeRecordRespVO> deviceOperationPageList(DeviceTotalTimeRecordReqVO pageReqVO) {
List<DeviceTotalTimeRecordRespVO> result = queryDeviceStatsRaw(pageReqVO);
applyPeriodIfNecessary(pageReqVO);
List<DeviceTotalTimeRecordRespVO> deviceList =
deviceOperationRecordMapper.selectDeviceListFromMySQL(pageReqVO);
if (CollectionUtils.isEmpty(deviceList)) {
return Collections.emptyList();
}
List<Long> deviceIds = deviceList.stream()
.map(DeviceTotalTimeRecordRespVO::getId)
.collect(Collectors.toList());
List<Map<String, Object>> statsList =
deviceOperationRecordMapper.selectDeviceStatsFromTD(
deviceIds,
pageReqVO.getStartTime(),
pageReqVO.getEndTime()
);
Map<Long, DeviceTotalTimeRecordRespVO> tdStatsMap = convertStatsListToMap(statsList);
List<DeviceTotalTimeRecordRespVO> result = new ArrayList<>();
for (DeviceTotalTimeRecordRespVO device : deviceList) {
DeviceTotalTimeRecordRespVO stats = tdStatsMap.get(device.getId());
if (stats != null) {
device.setTotalOfflineTime(stats.getTotalOfflineTime());
device.setTotalRunningTime(stats.getTotalRunningTime());
device.setTotalStandbyTime(stats.getTotalStandbyTime());
device.setTotalFaultTime(stats.getTotalFaultTime());
} else {
device.setTotalOfflineTime(0);
device.setTotalRunningTime(0);
device.setTotalStandbyTime(0);
device.setTotalFaultTime(0);
}
result.add(device);
}
calculateAndSetConvertedValues(result, pageReqVO); calculateAndSetConvertedValues(result, pageReqVO);
result.sort(Comparator.comparingDouble(this::parsePercentValue).reversed()); result.sort(Comparator.comparingDouble(this::parsePercentValue).reversed());
return result; return result;
@ -455,20 +416,26 @@ public class DeviceOperationRecordServiceImpl implements DeviceOperationRecordSe
//参数构造 //参数构造
applyPeriodIfNecessary(pageReqVO); applyPeriodIfNecessary(pageReqVO);
List<DeviceTotalTimeRecordRespVO> records = deviceOperationPageList(pageReqVO); List<DeviceTotalTimeRecordRespVO> records = queryDeviceStatsRaw(pageReqVO);
DeviceOperationOverviewRespVO respVO = new DeviceOperationOverviewRespVO(); DeviceOperationOverviewRespVO respVO = new DeviceOperationOverviewRespVO();
double totalRunningHours = records.stream().mapToDouble(DeviceTotalTimeRecordRespVO::getTotalRunningTime).sum(); double totalRunningSeconds = records.stream().mapToDouble(DeviceTotalTimeRecordRespVO::getTotalRunningTime).sum();
double totalStandbyHours = records.stream().mapToDouble(DeviceTotalTimeRecordRespVO::getTotalStandbyTime).sum(); double totalStandbySeconds = records.stream().mapToDouble(DeviceTotalTimeRecordRespVO::getTotalStandbyTime).sum();
double totalFaultHours = records.stream().mapToDouble(DeviceTotalTimeRecordRespVO::getTotalFaultTime).sum(); double totalFaultSeconds = records.stream().mapToDouble(DeviceTotalTimeRecordRespVO::getTotalFaultTime).sum();
double totalOfflineHours = records.stream().mapToDouble(DeviceTotalTimeRecordRespVO::getTotalOfflineTime).sum(); double totalOfflineSeconds = records.stream().mapToDouble(DeviceTotalTimeRecordRespVO::getTotalOfflineTime).sum();
double onlineHours = totalRunningHours + totalStandbyHours + totalFaultHours; double onlineSeconds = totalRunningSeconds + totalStandbySeconds + totalFaultSeconds;
double totalHours = onlineHours + totalOfflineHours; double totalSeconds = onlineSeconds + totalOfflineSeconds;
double utilizationRate = onlineHours > 0 ? totalRunningHours / onlineHours * 100 : 0D; double utilizationRate = calculatePercent(totalRunningSeconds, onlineSeconds);
double powerOnRate = totalHours > 0 ? onlineHours / totalHours * 100 : 0D; double powerOnRate = calculatePercent(onlineSeconds, totalSeconds);
double faultRate = onlineHours > 0 ? totalFaultHours / onlineHours * 100 : 0D; double faultRate = calculatePercent(totalFaultSeconds, onlineSeconds);
double standbyRate = onlineHours > 0 ? totalStandbyHours / onlineHours * 100 : 0D; double standbyRate = calculatePercent(totalStandbySeconds, onlineSeconds);
double totalRunningHours = secondsToHours(totalRunningSeconds);
double totalStandbyHours = secondsToHours(totalStandbySeconds);
double totalFaultHours = secondsToHours(totalFaultSeconds);
double totalOfflineHours = secondsToHours(totalOfflineSeconds);
double totalHours = secondsToHours(totalSeconds);
respVO.setMetrics(Arrays.asList( respVO.setMetrics(Arrays.asList(
DeviceOperationOverviewRespVO.MetricItem.builder().key("utilizationRate").icon("ep:pie-chart").value(round2(utilizationRate)).unit("%").change(0D).build(), DeviceOperationOverviewRespVO.MetricItem.builder().key("utilizationRate").icon("ep:pie-chart").value(round2(utilizationRate)).unit("%").change(0D).build(),
@ -554,6 +521,23 @@ public class DeviceOperationRecordServiceImpl implements DeviceOperationRecordSe
return denominator > 0 ? round2(numerator / denominator * 100) : 0D; return denominator > 0 ? round2(numerator / denominator * 100) : 0D;
} }
private double calculatePercent(double numerator, double denominator) {
return denominator > 0 ? numerator / denominator * 100D : 0D;
}
private double calculateUtilizationRate(DeviceTotalTimeRecordRespVO record) {
if (record == null) {
return 0D;
}
double runningSeconds = record.getTotalRunningTime();
double onlineSeconds = runningSeconds + record.getTotalStandbyTime() + record.getTotalFaultTime();
return round2(calculatePercent(runningSeconds, onlineSeconds));
}
private double secondsToHours(double seconds) {
return round2(seconds / 3600D);
}
private double round2(double value) { private double round2(double value) {
return Math.round(value * 100D) / 100D; return Math.round(value * 100D) / 100D;
} }
@ -585,7 +569,7 @@ public class DeviceOperationRecordServiceImpl implements DeviceOperationRecordSe
return DeviceOperationOverviewRespVO.TimelineRowItem.builder() return DeviceOperationOverviewRespVO.TimelineRowItem.builder()
.id(String.valueOf(record.getId())) .id(String.valueOf(record.getId()))
.name(record.getDeviceName()) .name(record.getDeviceName())
.utilizationRate(parsePercentValue(record)) .utilizationRate(calculateUtilizationRate(record))
.segments(segments) .segments(segments)
.build(); .build();
}).collect(Collectors.toList()); }).collect(Collectors.toList());
@ -604,6 +588,47 @@ public class DeviceOperationRecordServiceImpl implements DeviceOperationRecordSe
return rows.subList(fromIndex, toIndex); return rows.subList(fromIndex, toIndex);
} }
private List<DeviceTotalTimeRecordRespVO> queryDeviceStatsRaw(DeviceTotalTimeRecordReqVO pageReqVO) {
applyPeriodIfNecessary(pageReqVO);
List<DeviceTotalTimeRecordRespVO> deviceList =
deviceOperationRecordMapper.selectDeviceListFromMySQL(pageReqVO);
if (CollectionUtils.isEmpty(deviceList)) {
return Collections.emptyList();
}
List<Long> deviceIds = deviceList.stream()
.map(DeviceTotalTimeRecordRespVO::getId)
.collect(Collectors.toList());
List<Map<String, Object>> statsList =
deviceOperationRecordMapper.selectDeviceStatsFromTD(
deviceIds,
pageReqVO.getStartTime(),
pageReqVO.getEndTime()
);
Map<Long, DeviceTotalTimeRecordRespVO> tdStatsMap = convertStatsListToMap(statsList);
List<DeviceTotalTimeRecordRespVO> result = new ArrayList<>();
for (DeviceTotalTimeRecordRespVO device : deviceList) {
DeviceTotalTimeRecordRespVO stats = tdStatsMap.get(device.getId());
if (stats != null) {
device.setTotalOfflineTime(stats.getTotalOfflineTime());
device.setTotalRunningTime(stats.getTotalRunningTime());
device.setTotalStandbyTime(stats.getTotalStandbyTime());
device.setTotalFaultTime(stats.getTotalFaultTime());
} else {
device.setTotalOfflineTime(0);
device.setTotalRunningTime(0);
device.setTotalStandbyTime(0);
device.setTotalFaultTime(0);
}
result.add(device);
}
return result;
}
private List<DeviceOperationOverviewRespVO.TimelineSegmentItem> buildTimelineSegments(List<DeviceOperationRecordDO> timelineRecords, private List<DeviceOperationOverviewRespVO.TimelineSegmentItem> buildTimelineSegments(List<DeviceOperationRecordDO> timelineRecords,
LocalDateTime start, LocalDateTime start,
LocalDateTime end, LocalDateTime end,

Loading…
Cancel
Save