Skip to content

Commit d07e128

Browse files
perf: 重构统计聚合器,提升大数据量查询效率
1 parent 61cbde6 commit d07e128

1 file changed

Lines changed: 17 additions & 59 deletions

File tree

networksecurity/stats/aggregator.py

Lines changed: 17 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -34,79 +34,37 @@ def get_overview(
3434
end_time: Optional[datetime] = None
3535
) -> TrafficStats:
3636
"""
37-
获取流量统计概览
38-
39-
Args:
40-
start_time: 开始时间
41-
end_time: 结束时间
42-
43-
Returns:
44-
TrafficStats对象
37+
获取流量统计概览 - 使用SQL聚合提高性能
4538
"""
4639
if end_time is None:
4740
end_time = datetime.now()
4841
if start_time is None:
4942
start_time = end_time - timedelta(hours=24)
5043

51-
logs = self.logger.query(
52-
start_time=start_time,
53-
end_time=end_time,
54-
limit=100000
55-
)
44+
# 使用高效的SQL聚合查询
45+
data = self.logger.get_aggregated_stats(start_time, end_time)
5646

5747
stats = TrafficStats(
5848
time_range_start=start_time,
59-
time_range_end=end_time
49+
time_range_end=end_time,
50+
total_requests=data['total_requests'],
51+
blocked_requests=data['blocked_requests'],
52+
allowed_requests=data['allowed_requests'],
53+
challenged_requests=data['challenged_requests'],
54+
threat_counts=data['threat_counts'],
55+
action_counts=data['action_counts'],
56+
risk_level_counts=data['risk_level_counts'],
57+
top_source_ips=data['top_source_ips'],
58+
avg_risk_score=data['avg_risk_score'],
59+
avg_processing_time_ms=data['avg_processing_time_ms']
6060
)
6161

62-
if not logs:
63-
return stats
64-
65-
# 基础统计
66-
stats.total_requests = len(logs)
67-
68-
# 按动作统计
69-
action_counts = defaultdict(int)
70-
threat_counts = defaultdict(int)
71-
risk_level_counts = defaultdict(int)
72-
ip_counts = defaultdict(int)
73-
74-
total_risk_score = 0.0
75-
total_processing_time = 0.0
76-
77-
for log in logs:
78-
action_counts[log.action.value] += 1
79-
threat_counts[log.threat_type.value] += 1
80-
risk_level_counts[log.risk_level.value] += 1
81-
ip_counts[log.source_ip] += 1
82-
total_risk_score += log.risk_score
83-
total_processing_time += log.processing_time_ms
84-
85-
stats.blocked_requests = action_counts.get('block', 0)
86-
stats.allowed_requests = action_counts.get('allow', 0)
87-
stats.challenged_requests = action_counts.get('challenge', 0)
88-
89-
stats.action_counts = dict(action_counts)
90-
stats.threat_counts = dict(threat_counts)
91-
stats.risk_level_counts = dict(risk_level_counts)
92-
93-
# TOP IP
94-
stats.top_source_ips = sorted(
95-
ip_counts.items(),
96-
key=lambda x: x[1],
97-
reverse=True
98-
)[:10]
99-
100-
# TOP 威胁类型
62+
# TOP威胁类型
10163
stats.top_threat_types = sorted(
102-
[(k, v) for k, v in threat_counts.items() if k != 'benign'],
64+
[(k, v) for k, v in data['threat_counts'].items() if k != 'benign'],
10365
key=lambda x: x[1],
10466
reverse=True
105-
)[:10]
106-
107-
# 平均值
108-
stats.avg_risk_score = total_risk_score / len(logs)
109-
stats.avg_processing_time_ms = total_processing_time / len(logs)
67+
)[:5]
11068

11169
return stats
11270

0 commit comments

Comments
 (0)