|
|
@@ -83,6 +83,15 @@ public class StatsController {
|
|
|
@Resource private ProductViewRecordMapper productViewRecordMapper;
|
|
|
@Resource private ShareReadMapper shareReadMapper;
|
|
|
|
|
|
+ /** 单条停留时长上限(秒):超过 30 分钟按 30 分钟计,避免后台挂起/埋点异常把时长放大成天文数字 */
|
|
|
+ private static final int MAX_DWELL_SECONDS = 1800;
|
|
|
+
|
|
|
+ /** 停留时长归一:null/负数归零,超上限截断(聚合与入库共用同一口径) */
|
|
|
+ private static long saneDwell(Integer seconds) {
|
|
|
+ if (seconds == null || seconds <= 0) return 0L;
|
|
|
+ return Math.min(seconds, MAX_DWELL_SECONDS);
|
|
|
+ }
|
|
|
+
|
|
|
// ── 管理后台仪表盘 ──
|
|
|
|
|
|
@Operation(summary = "管理后台仪表盘汇总")
|
|
|
@@ -426,60 +435,51 @@ public class StatsController {
|
|
|
Date thirtyDaysAgo = cal.getTime();
|
|
|
SimpleDateFormat dayFmt = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
|
|
|
- // 1. 用户访问情况(近30天按日汇总 UV/PV)
|
|
|
- // PV:article_reading_records 中的阅读记录数;UV:distinct userId
|
|
|
+ // 1. 用户访问情况(近30天按日汇总 PV/UV)
|
|
|
+ // PV = 文章阅读次数(每条阅读记录计 1 次);UV = 当日去重读者数(distinct userId)。
|
|
|
+ // 修复:此前 UV 直接复用 PV 计数(counts[1]++),导致 PV/UV 曲线与 KPI 恒等。
|
|
|
List<ArticleReadingRecord> readingRecords = articleReadingRecordMapper.selectList(
|
|
|
new LambdaQueryWrapper<ArticleReadingRecord>().ge(ArticleReadingRecord::getReadAt, thirtyDaysAgo));
|
|
|
- Map<String, int[]> pvUvMap = new LinkedHashMap<>();
|
|
|
+ Map<String, Integer> pvMap = new LinkedHashMap<>();
|
|
|
+ Map<String, Set<Long>> uvMap = new LinkedHashMap<>();
|
|
|
for (int i = 29; i >= 0; i--) {
|
|
|
Calendar dayCal = Calendar.getInstance();
|
|
|
dayCal.add(Calendar.DAY_OF_MONTH, -i);
|
|
|
- dayCal.set(Calendar.HOUR_OF_DAY, 0); dayCal.set(Calendar.MINUTE, 0);
|
|
|
- dayCal.set(Calendar.SECOND, 0); dayCal.set(Calendar.MILLISECOND, 0);
|
|
|
- Date dayStart = dayCal.getTime();
|
|
|
- dayCal.set(Calendar.HOUR_OF_DAY, 23); dayCal.set(Calendar.MINUTE, 59); dayCal.set(Calendar.SECOND, 59);
|
|
|
- Date dayEnd = dayCal.getTime();
|
|
|
- String dayKey = dayFmt.format(dayStart);
|
|
|
- int[] counts = pvUvMap.computeIfAbsent(dayKey, k -> new int[]{0, 0});
|
|
|
- for (ArticleReadingRecord r : readingRecords) {
|
|
|
- if (r.getReadAt() != null && r.getReadAt().after(dayStart) && r.getReadAt().before(dayEnd)) {
|
|
|
- counts[0]++;
|
|
|
- counts[1]++; // 简化:按记录数计UV,精确UV需distinct
|
|
|
- }
|
|
|
- }
|
|
|
+ String dayKey = dayFmt.format(dayCal.getTime());
|
|
|
+ pvMap.put(dayKey, 0);
|
|
|
+ uvMap.put(dayKey, new java.util.HashSet<>());
|
|
|
}
|
|
|
- // 用 distinct userId 修正 UV
|
|
|
- Set<Long> dailyUv = new java.util.HashSet<>();
|
|
|
for (ArticleReadingRecord r : readingRecords) {
|
|
|
- if (r.getReadAt() != null && r.getReadAt().after(thirtyDaysAgo)) {
|
|
|
- dailyUv.add(r.getUserId());
|
|
|
+ if (r.getReadAt() == null) continue;
|
|
|
+ String dayKey = dayFmt.format(r.getReadAt());
|
|
|
+ if (!pvMap.containsKey(dayKey)) continue;
|
|
|
+ pvMap.merge(dayKey, 1, Integer::sum);
|
|
|
+ if (r.getUserId() != null) {
|
|
|
+ uvMap.get(dayKey).add(r.getUserId());
|
|
|
}
|
|
|
}
|
|
|
- List<String> trafficDates = new ArrayList<>(pvUvMap.keySet());
|
|
|
+ List<String> trafficDates = new ArrayList<>(pvMap.keySet());
|
|
|
List<Integer> pvData = new ArrayList<>();
|
|
|
List<Integer> uvData = new ArrayList<>();
|
|
|
+ int monthPvTotal = 0;
|
|
|
for (String d : trafficDates) {
|
|
|
- pvData.add(pvUvMap.get(d)[0]);
|
|
|
- uvData.add(pvUvMap.get(d)[1]);
|
|
|
+ pvData.add(pvMap.get(d));
|
|
|
+ uvData.add(uvMap.get(d).size());
|
|
|
+ monthPvTotal += pvMap.get(d);
|
|
|
}
|
|
|
+ Set<Long> dailyUv = new java.util.HashSet<>();
|
|
|
+ for (Set<Long> s : uvMap.values()) dailyUv.addAll(s);
|
|
|
data.put("trafficDates", trafficDates);
|
|
|
data.put("pvData", pvData);
|
|
|
data.put("uvData", uvData);
|
|
|
- // 今日实时数据
|
|
|
- Calendar todayCal = Calendar.getInstance();
|
|
|
- todayCal.set(Calendar.HOUR_OF_DAY, 0); todayCal.set(Calendar.MINUTE, 0);
|
|
|
- todayCal.set(Calendar.SECOND, 0); todayCal.set(Calendar.MILLISECOND, 0);
|
|
|
- long todayPv = readingRecords.stream()
|
|
|
- .filter(r -> r.getReadAt() != null && r.getReadAt().after(todayCal.getTime()))
|
|
|
- .count();
|
|
|
- long todayUv = readingRecords.stream()
|
|
|
- .filter(r -> r.getReadAt() != null && r.getReadAt().after(todayCal.getTime()))
|
|
|
- .map(ArticleReadingRecord::getUserId)
|
|
|
- .distinct().count();
|
|
|
+ // 今日实时数据(与趋势图同一口径,避免 KPI 与曲线对不上)
|
|
|
+ String todayKey = dayFmt.format(new Date());
|
|
|
+ long todayPv = pvMap.getOrDefault(todayKey, 0);
|
|
|
+ long todayUv = uvMap.getOrDefault(todayKey, java.util.Collections.emptySet()).size();
|
|
|
Map<String, Object> visitSummary = new HashMap<>();
|
|
|
visitSummary.put("todayPv", todayPv);
|
|
|
visitSummary.put("todayUv", todayUv);
|
|
|
- visitSummary.put("monthPv", readingRecords.size());
|
|
|
+ visitSummary.put("monthPv", monthPvTotal);
|
|
|
visitSummary.put("monthUv", dailyUv.size());
|
|
|
data.put("visitSummary", visitSummary);
|
|
|
|
|
|
@@ -624,7 +624,7 @@ public class StatsController {
|
|
|
Map<Long, Set<Long>> artReaders = new HashMap<>();
|
|
|
for (ArticleReadingRecord r : articleReads) {
|
|
|
long[] a = artAgg.computeIfAbsent(r.getArticleId(), k -> new long[]{0, 0});
|
|
|
- a[0] += (r.getDurationSeconds() != null ? r.getDurationSeconds() : 0);
|
|
|
+ a[0] += saneDwell(r.getDurationSeconds());
|
|
|
a[1] += 1;
|
|
|
if (r.getUserId() != null) {
|
|
|
artReaders.computeIfAbsent(r.getArticleId(), k -> new HashSet<>()).add(r.getUserId());
|
|
|
@@ -653,7 +653,7 @@ public class StatsController {
|
|
|
Map<Long, Long> userDwellMap = new HashMap<>();
|
|
|
for (ArticleReadingRecord r : articleReads) {
|
|
|
if (r.getUserId() != null && r.getDurationSeconds() != null) {
|
|
|
- userDwellMap.merge(r.getUserId(), (long) r.getDurationSeconds(), Long::sum);
|
|
|
+ userDwellMap.merge(r.getUserId(), saneDwell(r.getDurationSeconds()), Long::sum);
|
|
|
}
|
|
|
}
|
|
|
List<ShareRead> shareReads = shareReadMapper.selectList(
|
|
|
@@ -662,7 +662,7 @@ public class StatsController {
|
|
|
.isNotNull(ShareRead::getReadDuration));
|
|
|
for (ShareRead s : shareReads) {
|
|
|
if (s.getReaderId() != null && s.getReadDuration() != null) {
|
|
|
- userDwellMap.merge(s.getReaderId(), (long) s.getReadDuration(), Long::sum);
|
|
|
+ userDwellMap.merge(s.getReaderId(), saneDwell(s.getReadDuration()), Long::sum);
|
|
|
}
|
|
|
}
|
|
|
Map<Long, String> userNameMap = new HashMap<>();
|
|
|
@@ -695,7 +695,7 @@ public class StatsController {
|
|
|
Map<Long, long[]> prodAgg = new LinkedHashMap<>();
|
|
|
for (ProductViewRecord r : productViews) {
|
|
|
long[] a = prodAgg.computeIfAbsent(r.getProductId(), k -> new long[]{0, 0});
|
|
|
- a[0] += (r.getDurationSeconds() != null ? r.getDurationSeconds() : 0);
|
|
|
+ a[0] += saneDwell(r.getDurationSeconds());
|
|
|
a[1] += 1;
|
|
|
}
|
|
|
List<Map<String, Object>> productDwellList = new ArrayList<>();
|
|
|
@@ -726,14 +726,14 @@ public class StatsController {
|
|
|
if (r.getReadAt() != null && r.getDurationSeconds() != null) {
|
|
|
hourCal.setTime(r.getReadAt());
|
|
|
int h = hourCal.get(Calendar.HOUR_OF_DAY);
|
|
|
- if (h >= 0 && h < 24) dwellByHour[h] += r.getDurationSeconds();
|
|
|
+ if (h >= 0 && h < 24) dwellByHour[h] += saneDwell(r.getDurationSeconds());
|
|
|
}
|
|
|
}
|
|
|
for (ProductViewRecord r : productViews) {
|
|
|
if (r.getViewAt() != null && r.getDurationSeconds() != null) {
|
|
|
hourCal.setTime(r.getViewAt());
|
|
|
int h = hourCal.get(Calendar.HOUR_OF_DAY);
|
|
|
- if (h >= 0 && h < 24) dwellByHour[h] += r.getDurationSeconds();
|
|
|
+ if (h >= 0 && h < 24) dwellByHour[h] += saneDwell(r.getDurationSeconds());
|
|
|
}
|
|
|
}
|
|
|
List<Long> dwellByHourList = new ArrayList<>();
|
|
|
@@ -761,7 +761,11 @@ public class StatsController {
|
|
|
if (record == null || record.getProductId() == null) {
|
|
|
return Result.error("参数缺失");
|
|
|
}
|
|
|
- if (record.getDurationSeconds() == null) record.setDurationSeconds(0);
|
|
|
+ // 防御:埋点异常(如未 start 就 flush)会传出 epoch 量级的天文数字,入库前收敛到合理区间
|
|
|
+ int dwellSeconds = record.getDurationSeconds() == null ? 0 : record.getDurationSeconds();
|
|
|
+ if (dwellSeconds < 0) dwellSeconds = 0;
|
|
|
+ if (dwellSeconds > MAX_DWELL_SECONDS) dwellSeconds = MAX_DWELL_SECONDS;
|
|
|
+ record.setDurationSeconds(dwellSeconds);
|
|
|
if (record.getUserId() == null) record.setUserId(0L);
|
|
|
if (record.getSource() == null) record.setSource("miniprogram");
|
|
|
record.setViewAt(new Date());
|