Browse Source

交通指标 每小时的30分统计上一小时数据

develop
王兴琳 4 months ago
parent
commit
f182d22b17
  1. 2
      zc-business/src/main/java/com/zc/business/service/IDcGantryMetricsStatisticsDataService.java
  2. 28
      zc-business/src/main/java/com/zc/business/service/impl/IDcGantryMetricsStatisticsDataServiceImpl.java
  3. 57
      zc-business/src/main/java/com/zc/business/statistics/handler/TrafficGantryMetricsStatistics.java

2
zc-business/src/main/java/com/zc/business/service/IDcGantryMetricsStatisticsDataService.java

@ -49,4 +49,6 @@ public interface IDcGantryMetricsStatisticsDataService extends IService<DcGantry
Map<String, Object> radarMapOfTrafficIndicators(String startDate, String direction, String periodType);
List<ComprehensiveIndexQuery> comprehensiveIndexQuery(ComprehensiveIndexQuery comprehensiveIndexQuery);
List<DcGantryMetricsStatisticsData> threeDays();
}

28
zc-business/src/main/java/com/zc/business/service/impl/IDcGantryMetricsStatisticsDataServiceImpl.java

@ -102,6 +102,34 @@ public class IDcGantryMetricsStatisticsDataServiceImpl
}
}
/**
* 查询门架指标三天数据
* @return
*/
@Override
public List<DcGantryMetricsStatisticsData> threeDays() {
// 计算三天前前的时间点
DateTime threeDays = DateUtil.offsetDay(new Date(), -3);
String startTime = DateUtil.beginOfDay(threeDays).toString();
String endTime = DateUtil.endOfHour(new Date()).toString();
// 获取当月门架数指标据
JSONArray jsonArray = null;
try {
jsonArray = dcTrafficStatisticsService.gantryMetrics(startTime, endTime);
// 计算每个门架数指标据的统计信息,并添加到每日门架指标统计缓存中
return calculateGantryMetricsStatistics(jsonArray);
} catch (HttpException | IOException e) {
logger.error(UniversalEnum.FAILED_TO_OBTAIN_THE_SHELF_INDICATOR_DATA_OF_THE_PAST_HOUR.getValue(), e);
return new ArrayList<>();
}
}
/**
* 获取当月的门架指标数据列表
*

57
zc-business/src/main/java/com/zc/business/statistics/handler/TrafficGantryMetricsStatistics.java

@ -1,5 +1,11 @@
package com.zc.business.statistics.handler;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.druid.util.DaemonThreadFactory;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zc.business.domain.DcGantryMetricsStatisticsData;
import com.zc.business.enums.TrafficDataPeriodTypeEnum;
import com.zc.business.mapper.DcGantryMetricsStatisticsDataMapper;
@ -16,9 +22,8 @@ import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.time.LocalDateTime;
import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;
@ -42,7 +47,7 @@ public class TrafficGantryMetricsStatistics {
/**
* 定义每小时第20分钟执行的任务用于清除过期缓存数据并将缓存中的数据整合后保存至数据库
*/
@Scheduled(cron = "0 20 * * * ?") // 每小时的20分整点执行该任务
@Scheduled(cron = "0 30 * * * ?") // 每小时的30分整点执行该任务
public void performHourlyCleanupAndPersist() {
List<DcGantryMetricsStatisticsData> lastHourData = dcGantryMetricsStatisticsDataService.lastHourData();
@ -50,12 +55,48 @@ public class TrafficGantryMetricsStatistics {
if (lastHourData == null || lastHourData.isEmpty()) {
return;
}
//查询三天数据
/* List<DcGantryMetricsStatisticsData> threeDays = dcGantryMetricsStatisticsDataService.threeDays();
if (threeDays == null || threeDays.isEmpty()) {
return;
}*/
/* DateTime threeDay = DateUtil.offsetDay(new Date(), -3);
String startTime = DateUtil.beginOfDay(threeDay).toString();
String endTime = DateUtil.endOfHour(new Date()).toString();
List<DcGantryMetricsStatisticsData> threeData = dcGantryMetricsStatisticsDataMapper.selectList(
new QueryWrapper<DcGantryMetricsStatisticsData>()
.ge("statistical_date", startTime)
.le("statistical_date", endTime)
);
Map<String, DcGantryMetricsStatisticsData> existingRecords = new HashMap<>();
for (DcGantryMetricsStatisticsData data : threeData) {
String key = createKey(data.getGantryCode(), data.getStatisticalDate());
existingRecords.put(key, data);
}
for (DcGantryMetricsStatisticsData data : threeDays) {
String key = createKey(data.getGantryCode(), data.getStatisticalDate());
if (existingRecords.containsKey(key)) {
// 更新现有记录
dcGantryMetricsStatisticsDataMapper.update(null,
new UpdateWrapper<DcGantryMetricsStatisticsData>()
.eq("gantry_code", data.getGantryCode())
.eq("statistical_date", data.getStatisticalDate())
.setEntity(data));
} else {
// 插入新记录
dcGantryMetricsStatisticsDataMapper.insert(data);
}
}*/
// 数据库批量插入最近一小时的数据
dcGantryMetricsStatisticsDataMapper.insertOrUpdateBatch(lastHourData);
// 添加日门架指标数据到缓存中
lastHourData.forEach(DailyGantryMetricsStatisticsCache::addCacheData);
// 添加三日门架指标数据到缓存中
// threeDays.forEach(DailyGantryMetricsStatisticsCache::addCacheData);
// 清除已过期的缓存数据
DailyGantryMetricsStatisticsCache.clearExpiredData();
@ -75,8 +116,12 @@ public class TrafficGantryMetricsStatistics {
persistAggregatedData(QuarterlyGantryMetricsStatisticsCache.getCache(), TrafficDataPeriodTypeEnum.QUARTER, YearlyGantryMetricsStatisticsCache::addCacheData);
// 将缓存中的数据按年统计后保存至数据库
persistAggregatedData(YearlyGantryMetricsStatisticsCache.getCache(), TrafficDataPeriodTypeEnum.YEAR, (a) -> {});
}
}
//创建一个唯一的键(key)
private String createKey(String gantryCode, Date statisticalDate) {
return String.format("%s:%s", gantryCode, statisticalDate.toString());
}
/**
* 将缓存中的数据统计后保存至数据库
*/

Loading…
Cancel
Save