|
|
@ -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()); |
|
|
|
} |
|
|
|
/** |
|
|
|
* 将缓存中的数据统计后保存至数据库。 |
|
|
|
*/ |
|
|
|