-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_analyzer.py
More file actions
288 lines (235 loc) · 8.57 KB
/
Copy pathdata_analyzer.py
File metadata and controls
288 lines (235 loc) · 8.57 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import re
import os
from typing import List, Dict, Tuple, Optional, Any
from collections import Counter
class DataAnalyzer:
"""
数据分析器,负责关键词统计、异常日志筛选、数据计算。
"""
def __init__(
self,
keywords: Optional[List[str]] = None,
anomaly_markers: Optional[Dict[str, List[str]]] = None,
keywords_file: Optional[str] = None
):
"""
初始化数据分析器。
Args:
keywords: 关键词列表
anomaly_markers: 异常标识配置
keywords_file: 关键词基准文件路径
"""
self.keywords = set()
if keywords:
self.keywords.update(k.lower() for k in keywords)
if keywords_file and os.path.exists(keywords_file):
self._load_keywords_from_file(keywords_file)
self.anomaly_markers = anomaly_markers or {
"error_levels": ["ERROR", "FATAL", "CRITICAL"],
"warning_levels": ["WARN", "WARNING"],
"anomaly_keywords": [
"exception", "failed", "failure", "error",
"timeout", "refused", "denied", "unavailable"
]
}
self._keyword_stats: Dict[str, int] = {}
self._anomaly_logs: List[Dict[str, Any]] = []
self._error_logs: List[Dict[str, Any]] = []
self._warning_logs: List[Dict[str, Any]] = []
def _load_keywords_from_file(self, file_path: str):
"""
从文件加载关键词(只读)。
Args:
file_path: 关键词文件路径
"""
try:
with open(file_path, "r", encoding="utf-8") as file:
for line in file:
line = line.strip()
if line and not line.startswith("#"):
self.keywords.add(line.lower())
except (IOError, OSError):
pass
def analyze(self, lines: List[str], source_file: str = "") -> Dict[str, Any]:
"""
分析日志数据。
Args:
lines: 清洗后的日志行列表
source_file: 来源文件路径
Returns:
分析结果字典
"""
self._reset_stats()
self._analyze_keywords(lines)
self._identify_anomalies(lines, source_file)
return {
"keyword_stats": self._keyword_stats.copy(),
"anomaly_count": len(self._anomaly_logs),
"error_count": len(self._error_logs),
"warning_count": len(self._warning_logs),
"total_lines": len(lines)
}
def _analyze_keywords(self, lines: List[str]):
"""
统计关键词出现频次。
Args:
lines: 日志行列表
"""
keyword_counts = Counter()
for line in lines:
line_lower = line.lower()
for keyword in self.keywords:
count = len(re.findall(r"\b" + re.escape(keyword) + r"\b", line_lower))
if count > 0:
keyword_counts[keyword] += count
self._keyword_stats = dict(keyword_counts)
def _identify_anomalies(self, lines: List[str], source_file: str = ""):
"""
识别异常日志。
Args:
lines: 日志行列表
source_file: 来源文件路径
"""
error_levels = set(lvl.upper() for lvl in self.anomaly_markers.get("error_levels", []))
warning_levels = set(lvl.upper() for lvl in self.anomaly_markers.get("warning_levels", []))
anomaly_keywords = set(kw.lower() for kw in self.anomaly_markers.get("anomaly_keywords", []))
for line_num, line in enumerate(lines, 1):
is_anomaly = False
anomaly_type = None
log_level = self._extract_log_level(line)
if log_level in error_levels:
is_anomaly = True
anomaly_type = "ERROR"
elif log_level in warning_levels:
is_anomaly = True
anomaly_type = "WARNING"
else:
line_lower = line.lower()
for keyword in anomaly_keywords:
if keyword in line_lower:
is_anomaly = True
anomaly_type = "ANOMALY"
break
if is_anomaly:
anomaly_entry = {
"line_number": line_num,
"content": line,
"source_file": source_file,
"anomaly_type": anomaly_type,
"log_level": log_level
}
self._anomaly_logs.append(anomaly_entry)
if anomaly_type == "ERROR":
self._error_logs.append(anomaly_entry)
elif anomaly_type == "WARNING":
self._warning_logs.append(anomaly_entry)
def _extract_log_level(self, line: str) -> Optional[str]:
"""
从日志行中提取日志级别。
Args:
line: 日志行
Returns:
日志级别字符串或None
"""
level_pattern = r"\b(ERROR|WARN|WARNING|INFO|DEBUG|TRACE|FATAL|CRITICAL)\b"
match = re.search(level_pattern, line, re.IGNORECASE)
if match:
return match.group(1).upper()
return None
def _reset_stats(self):
"""
重置统计信息。
"""
self._keyword_stats = {}
self._anomaly_logs = []
self._error_logs = []
self._warning_logs = []
def get_keyword_stats(self) -> Dict[str, int]:
"""
获取关键词统计结果。
Returns:
关键词统计字典
"""
return self._keyword_stats.copy()
def get_sorted_keywords(self, reverse: bool = True) -> List[Tuple[str, int]]:
"""
获取按频次排序的关键词列表。
Args:
reverse: 是否降序排列
Returns:
(关键词, 频次) 元组列表
"""
return sorted(self._keyword_stats.items(), key=lambda x: x[1], reverse=reverse)
def get_anomaly_logs(self) -> List[Dict[str, Any]]:
"""
获取所有异常日志。
Returns:
异常日志列表
"""
return self._anomaly_logs.copy()
def get_error_logs(self) -> List[Dict[str, Any]]:
"""
获取错误级别日志。
Returns:
错误日志列表
"""
return self._error_logs.copy()
def get_warning_logs(self) -> List[Dict[str, Any]]:
"""
获取警告级别日志。
Returns:
警告日志列表
"""
return self._warning_logs.copy()
def get_top_keywords(self, n: int = 10) -> List[Tuple[str, int]]:
"""
获取出现频次最高的N个关键词。
Args:
n: 返回数量
Returns:
(关键词, 频次) 元组列表
"""
sorted_keywords = self.get_sorted_keywords(reverse=True)
return sorted_keywords[:n]
def analyze_multiple_files(self, file_contents: Dict[str, List[str]]) -> Dict[str, Any]:
"""
分析多个文件的内容。
Args:
file_contents: 文件内容字典,键为文件路径,值为行列表
Returns:
汇总分析结果
"""
all_lines = []
file_stats = {}
for file_path, lines in file_contents.items():
file_result = self.analyze(lines, file_path)
file_stats[file_path] = file_result
all_lines.extend(lines)
overall_result = self.analyze(all_lines, "ALL_FILES")
return {
"overall": overall_result,
"by_file": file_stats,
"total_files": len(file_contents),
"total_lines": len(all_lines)
}
def get_anomaly_summary(self) -> Dict[str, Any]:
"""
获取异常日志汇总信息。
Returns:
异常汇总字典
"""
error_types = Counter()
for log in self._anomaly_logs:
error_types[log["anomaly_type"]] += 1
files_with_anomalies = set()
for log in self._anomaly_logs:
if log["source_file"]:
files_with_anomalies.add(log["source_file"])
return {
"total_anomalies": len(self._anomaly_logs),
"error_count": len(self._error_logs),
"warning_count": len(self._warning_logs),
"anomaly_type_distribution": dict(error_types),
"files_affected": len(files_with_anomalies),
"affected_file_list": list(files_with_anomalies)
}