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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
@Service public class DataStatisticsService { @Autowired private RedisTemplate<String, Object> redisTemplate;
public Map<String, Object> getBusinessStatistics() { Map<String, Object> stats = new HashMap<>(); try { Set<String> keys = redisTemplate.keys("aggregation:business:*"); Map<String, Long> dataSourceStats = new HashMap<>(); if (keys != null) { for (String key : keys) { String dataSource = key.substring(key.lastIndexOf(":") + 1); Object count = redisTemplate.opsForHash().get(key, "count"); dataSourceStats.put(dataSource, count != null ? Long.valueOf(count.toString()) : 0L); } } stats.put("dataSourceStats", dataSourceStats); stats.put("totalCount", dataSourceStats.values().stream().mapToLong(Long::longValue).sum()); } catch (Exception e) { log.error("获取业务数据统计失败", e); } return stats; }
public Map<String, Object> getSystemMetrics() { Map<String, Object> metrics = new HashMap<>(); try { Set<String> keys = redisTemplate.keys("aggregation:metrics:*"); Map<String, Map<String, Object>> metricStats = new HashMap<>(); if (keys != null) { for (String key : keys) { String metricName = key.substring(key.lastIndexOf(":") + 1); Map<Object, Object> rawStats = redisTemplate.opsForHash().entries(key); Map<String, Object> stats = new HashMap<>(); stats.put("sum", rawStats.get("sum")); stats.put("count", rawStats.get("count")); stats.put("max", rawStats.get("max")); stats.put("min", rawStats.get("min")); Double sum = (Double) rawStats.get("sum"); Long count = (Long) rawStats.get("count"); if (sum != null && count != null && count > 0) { stats.put("avg", sum / count); } metricStats.put(metricName, stats); } } metrics.put("metricStats", metricStats); } catch (Exception e) { log.error("获取系统指标失败", e); } return metrics; }
public Map<String, Object> getUserBehaviorStatistics() { Map<String, Object> stats = new HashMap<>(); try { Set<String> pageKeys = redisTemplate.keys("aggregation:page:*"); Map<String, Map<String, Long>> pageStats = new HashMap<>(); if (pageKeys != null) { for (String key : pageKeys) { String page = key.substring(key.lastIndexOf(":") + 1); Map<Object, Object> rawStats = redisTemplate.opsForHash().entries(key); Map<String, Long> pageData = new HashMap<>(); rawStats.forEach((k, v) -> pageData.put(k.toString(), Long.valueOf(v.toString()))); pageStats.put(page, pageData); } } stats.put("pageStats", pageStats); Set<String> timeKeys = redisTemplate.keys("aggregation:time:*"); Map<String, Map<String, Long>> timeStats = new HashMap<>(); if (timeKeys != null) { for (String key : timeKeys) { String time = key.substring(key.lastIndexOf(":") + 1); Map<Object, Object> rawStats = redisTemplate.opsForHash().entries(key); Map<String, Long> timeData = new HashMap<>(); rawStats.forEach((k, v) -> timeData.put(k.toString(), Long.valueOf(v.toString()))); timeStats.put(time, timeData); } } stats.put("timeStats", timeStats); } catch (Exception e) { log.error("获取用户行为统计失败", e); } return stats; }
public List<Map<String, Object>> getHistoryData(String dataType, String timeRange) { List<Map<String, Object>> data = new ArrayList<>(); try { } catch (Exception e) { log.error("获取历史数据失败: dataType={}, timeRange={}", dataType, timeRange, e); } return data; }
public Map<String, Object> getChartData(String chartType, String timeRange) { Map<String, Object> data = new HashMap<>(); try { switch (chartType) { case "line": data = getLineChartData(timeRange); break; case "bar": data = getBarChartData(timeRange); break; case "pie": data = getPieChartData(timeRange); break; default: log.warn("未知图表类型: {}", chartType); } } catch (Exception e) { log.error("获取图表数据失败: chartType={}, timeRange={}", chartType, timeRange, e); } return data; }
private Map<String, Object> getLineChartData(String timeRange) { Map<String, Object> data = new HashMap<>(); return data; }
private Map<String, Object> getBarChartData(String timeRange) { Map<String, Object> data = new HashMap<>(); return data; }
private Map<String, Object> getPieChartData(String timeRange) { Map<String, Object> data = new HashMap<>(); return data; } }
|