From f182d22b17e75febf9ab3477b1fc2b62682e0512 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=85=B4=E7=90=B3?= <1911390090@qq.com> Date: Sat, 27 Jul 2024 16:41:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BA=A4=E9=80=9A=E6=8C=87=E6=A0=87=20?= =?UTF-8?q?=E6=AF=8F=E5=B0=8F=E6=97=B6=E7=9A=8430=E5=88=86=E7=BB=9F?= =?UTF-8?q?=E8=AE=A1=E4=B8=8A=E4=B8=80=E5=B0=8F=E6=97=B6=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...IDcGantryMetricsStatisticsDataService.java | 2 + ...antryMetricsStatisticsDataServiceImpl.java | 28 +++++++++ .../TrafficGantryMetricsStatistics.java | 57 +++++++++++++++++-- 3 files changed, 81 insertions(+), 6 deletions(-) diff --git a/zc-business/src/main/java/com/zc/business/service/IDcGantryMetricsStatisticsDataService.java b/zc-business/src/main/java/com/zc/business/service/IDcGantryMetricsStatisticsDataService.java index fb94a4c9..f3598651 100644 --- a/zc-business/src/main/java/com/zc/business/service/IDcGantryMetricsStatisticsDataService.java +++ b/zc-business/src/main/java/com/zc/business/service/IDcGantryMetricsStatisticsDataService.java @@ -49,4 +49,6 @@ public interface IDcGantryMetricsStatisticsDataService extends IService radarMapOfTrafficIndicators(String startDate, String direction, String periodType); List comprehensiveIndexQuery(ComprehensiveIndexQuery comprehensiveIndexQuery); + + List threeDays(); } diff --git a/zc-business/src/main/java/com/zc/business/service/impl/IDcGantryMetricsStatisticsDataServiceImpl.java b/zc-business/src/main/java/com/zc/business/service/impl/IDcGantryMetricsStatisticsDataServiceImpl.java index 14688a76..887adbde 100644 --- a/zc-business/src/main/java/com/zc/business/service/impl/IDcGantryMetricsStatisticsDataServiceImpl.java +++ b/zc-business/src/main/java/com/zc/business/service/impl/IDcGantryMetricsStatisticsDataServiceImpl.java @@ -102,6 +102,34 @@ public class IDcGantryMetricsStatisticsDataServiceImpl } } + /** + * 查询门架指标三天数据 + * @return + */ + @Override + public List 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<>(); + } + } + /** * 获取当月的门架指标数据列表。 * diff --git a/zc-business/src/main/java/com/zc/business/statistics/handler/TrafficGantryMetricsStatistics.java b/zc-business/src/main/java/com/zc/business/statistics/handler/TrafficGantryMetricsStatistics.java index b66ace17..35a6a2ab 100644 --- a/zc-business/src/main/java/com/zc/business/statistics/handler/TrafficGantryMetricsStatistics.java +++ b/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 lastHourData = dcGantryMetricsStatisticsDataService.lastHourData(); @@ -50,12 +55,48 @@ public class TrafficGantryMetricsStatistics { if (lastHourData == null || lastHourData.isEmpty()) { return; } + //查询三天数据 +/* List 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 threeData = dcGantryMetricsStatisticsDataMapper.selectList( + new QueryWrapper() + .ge("statistical_date", startTime) + .le("statistical_date", endTime) + ); + + Map 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() + .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()); + } /** * 将缓存中的数据统计后保存至数据库。 */