1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
| @Override public ResponseResult getVisitTrend(String startDate, String endDate) { LocalDate start = LocalDate.parse(startDate, DateTimeFormatter.ISO_LOCAL_DATE); LocalDate end = LocalDate.parse(endDate, DateTimeFormatter.ISO_LOCAL_DATE); if (start.isAfter(end)) { throw new SystemException(AppHttpCodeEnum.PARAM_INVALID); }
List<LocalDate> dates = new ArrayList<>(); for (LocalDate date = start; !date.isAfter(end); date = date.plusDays(1)) { dates.add(date); }
List<String> dateStrings = dates.stream() .map(d -> d.format(DateTimeFormatter.ISO_LOCAL_DATE)) .collect(Collectors.toList());
List<Date> startTimes = dates.stream() .map(d -> Date.from(d.atStartOfDay(ZoneId.systemDefault()).toInstant())) .toList(); List<Date> endTimes = dates.stream() .map(d -> Date.from(d.plusDays(1).atStartOfDay(ZoneId.systemDefault()).toInstant())) .toList();
List<Integer> pvList = new ArrayList<>(dates.size()); List<Integer> uvList = new ArrayList<>(dates.size()); List<Integer> ipList = new ArrayList<>(dates.size());
for (int i = 0; i < dates.size(); i++) { Date dayStart = startTimes.get(i); Date dayEnd = endTimes.get(i);
long pvCount = visitLogMapper.selectCount(new LambdaQueryWrapper<VisitLog>() .ge(VisitLog::getVisitTime, dayStart) .lt(VisitLog::getVisitTime, dayEnd)); pvList.add((int) pvCount);
List<VisitLog> logs = visitLogMapper.selectList(new LambdaQueryWrapper<VisitLog>() .ge(VisitLog::getVisitTime, dayStart) .lt(VisitLog::getVisitTime, dayEnd) .select(VisitLog::getIp));
Set<String> uniqueIpSet = logs.stream() .map(VisitLog::getIp) .filter(Objects::nonNull) .collect(Collectors.toSet()); uvList.add(uniqueIpSet.size()); ipList.add(uniqueIpSet.size()); }
VisitTrendVo visitTrendVo = new VisitTrendVo(dateStrings, pvList, uvList, ipList); return ResponseResult.okResult(visitTrendVo); }
@Override public ResponseResult getVisitOverview() { Long totalPvCount = getCachedLong(SystemConstants.SITE_TOTAL_VIEWS, SystemConstants.SITE_TOTAL_VIEWS_FIELD); Long totalUvCount = getCachedLong(SystemConstants.SITE_TOTAL_VISITORS, SystemConstants.SITE_TOTAL_VISITORS_FIELD);
LocalDate today = LocalDate.now(); LocalDate yesterday = today.minusDays(1); Date todayStart = Date.from(today.atStartOfDay(ZoneId.systemDefault()).toInstant()); Date tomorrowStart = Date.from(today.plusDays(1).atStartOfDay(ZoneId.systemDefault()).toInstant()); Date yesterdayStart = Date.from(yesterday.atStartOfDay(ZoneId.systemDefault()).toInstant());
long todayPvCount = visitLogMapper.selectCount(new LambdaQueryWrapper<VisitLog>() .ge(VisitLog::getVisitTime, todayStart) .lt(VisitLog::getVisitTime, tomorrowStart)); long yesterdayPvCount = visitLogMapper.selectCount(new LambdaQueryWrapper<VisitLog>() .ge(VisitLog::getVisitTime, yesterdayStart) .lt(VisitLog::getVisitTime, todayStart));
List<VisitLog> todayLogs = visitLogMapper.selectList(new LambdaQueryWrapper<VisitLog>() .ge(VisitLog::getVisitTime, todayStart) .lt(VisitLog::getVisitTime, tomorrowStart) .select(VisitLog::getIp)); List<VisitLog> yesterdayLogs = visitLogMapper.selectList(new LambdaQueryWrapper<VisitLog>() .ge(VisitLog::getVisitTime, yesterdayStart) .lt(VisitLog::getVisitTime, todayStart) .select(VisitLog::getIp));
long todayUvCount = todayLogs.stream() .map(VisitLog::getIp) .filter(Objects::nonNull) .distinct() .count(); long yesterdayUvCount = yesterdayLogs.stream() .map(VisitLog::getIp) .filter(Objects::nonNull) .distinct() .count();
double pvGrowthRate = calcGrowthRate(todayPvCount, yesterdayPvCount); double uvGrowthRate = calcGrowthRate(todayUvCount, yesterdayUvCount);
VisitOverviewVo visitOverviewVo = new VisitOverviewVo( Math.toIntExact(todayUvCount), Math.toIntExact(totalUvCount), uvGrowthRate, Math.toIntExact(todayPvCount), Math.toIntExact(totalPvCount), pvGrowthRate );
return ResponseResult.okResult(visitOverviewVo); }
private Long getCachedLong(String key, String field) { Integer value = redisCache.getCacheMapValue(key, field); return value == null ? 0L : value.longValue(); }
private double calcGrowthRate(long current, long previous) { if (previous == 0) { return current == 0 ? 0.0 : 100.0; } return Math.round(((current - previous) * 1000.0 / previous)) / 10.0; }
|