-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathgui_optimized.py
More file actions
1066 lines (866 loc) · 38.1 KB
/
gui_optimized.py
File metadata and controls
1066 lines (866 loc) · 38.1 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
SSlogs v3.1 优化版GUI界面
集成异步AI分析、内存优化、安全验证等所有优化功能
"""
import sys
import os
import asyncio
import time
import threading
from pathlib import Path
# 添加项目根目录到Python路径
sys.path.insert(0, str(Path(__file__).parent))
from PyQt6.QtWidgets import (
QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout,
QLabel, QLineEdit, QPushButton, QCheckBox, QComboBox,
QProgressBar, QTextEdit, QFileDialog, QMessageBox, QGroupBox,
QTabWidget, QSpinBox, QDoubleSpinBox, QSlider, QTableWidget,
QTableWidgetItem, QHeaderView, QSplitter, QFrame, QScrollArea,
QDialog, QDialogButtonBox, QFormLayout
)
from PyQt6.QtCore import Qt, QThread, pyqtSignal, QTimer, QPropertyAnimation, QEasingCurve
from PyQt6.QtGui import QFont, QPixmap, QIcon, QColor, QPalette
# 导入优化后的核心模块
from core import (
AsyncAIAnalyzer, analyze_logs_concurrently,
MemoryOptimizedProcessor, process_large_logs_async,
get_event_bus, publish_event, event_handler,
get_config_manager, get_cache_manager, get_security_manager,
validate_input, sanitize_input, handle_exceptions,
create_error_context, cache_result, CachePolicy
)
class AnalysisWorker(QThread):
"""增强的分析工作线程 - 集成所有优化功能"""
progress_updated = pyqtSignal(int, str, dict) # 进度、消息、性能数据
log_output = pyqtSignal(str)
analysis_finished = pyqtSignal(bool, str, dict)
threat_detected = pyqtSignal(str, dict)
def __init__(self, config):
super().__init__()
self.config = config
self.is_interrupted = False
self.start_time = 0
def run(self):
"""执行优化的分析流程"""
try:
self.start_time = time.time()
self.log_output.emit("🚀 启动SSlogs v3.1优化分析引擎...")
# 初始化各个优化模块
self._initialize_optimized_modules()
# 执行分析
results = self._run_optimized_analysis()
# 完成分析
end_time = time.time()
performance_data = {
'total_time': end_time - self.start_time,
'processed_count': len(results.get('processed_logs', [])),
'threats_found': len(results.get('threats', [])),
'memory_peak': self._get_memory_usage(),
'cache_hits': self._get_cache_stats()
}
self.progress_updated.emit(100, "✅ 分析完成!", performance_data)
self.analysis_finished.emit(True, "分析成功完成", results)
except Exception as e:
error_context = create_error_context("analysis_worker", "run_analysis")
error_msg = f"分析过程发生错误: {str(e)}"
self.log_output.emit(f"❌ {error_msg}")
self.analysis_finished.emit(False, error_msg, {'error': str(e)})
def _initialize_optimized_modules(self):
"""初始化优化模块"""
self.log_output.emit("🔧 初始化优化模块...")
# 初始化事件总线
self.event_bus = get_event_bus()
# 初始化缓存管理器
self.cache_manager = get_cache_manager()
if self.config.get('enable_cache', True):
self.cache_manager.create_memory_cache(
"analysis_cache",
max_size=self.config.get('cache_size', 1000),
policy=CachePolicy.LRU
)
# 初始化安全管理器
self.security_manager = get_security_manager()
# 注册事件处理器
self._register_event_handlers()
self.log_output.emit("✅ 优化模块初始化完成")
def _register_event_handlers(self):
"""注册事件处理器"""
@event_handler("performance_update", priority="high")
def handle_performance(event):
performance_data = event.data
self.progress_updated.emit(
performance_data.get('progress', 0),
performance_data.get('message', ''),
performance_data
)
def _run_optimized_analysis(self):
"""执行优化分析"""
log_path = self.config.get('log_path', 'logs/*.log')
ai_enabled = self.config.get('ai_enabled', False)
memory_optimized = self.config.get('memory_optimized', True)
security_validation = self.config.get('security_validation', True)
results = {'processed_logs': [], 'threats': [], 'performance': {}}
# 使用内存优化处理器
if memory_optimized:
self.progress_updated.emit(10, "📊 启动内存优化处理...", {})
processor = MemoryOptimizedProcessor(
chunk_size=self.config.get('chunk_size', 8192),
max_memory_usage=self.config.get('max_memory_usage', 0.8)
)
def process_log_entry(log_line):
if self.is_interrupted:
return None
try:
# 安全验证
if security_validation:
validation = validate_input(log_line)
if not validation.is_valid:
threat_data = {
'line': log_line[:100] + "...",
'threats': validation.threats,
'risk_score': validation.risk_score
}
results['threats'].append(threat_data)
self.threat_detected.emit("安全威胁检测", threat_data)
return None
# 处理日志条目
processed_log = {'raw': log_line, 'timestamp': time.time()}
results['processed_logs'].append(processed_log)
return processed_log
except Exception as e:
self.log_output.emit(f"⚠️ 处理日志行失败: {e}")
return None
# 处理大文件
processed_count = 0
for result in processor.process_large_file_streaming(log_path, process_log_entry):
if self.is_interrupted:
break
processed_count += 1
# 更新进度
if processed_count % 1000 == 0:
progress = min(10 + (processed_count // 1000) * 5, 80)
memory_usage = processor.get_memory_usage()
self.progress_updated.emit(progress,
f"📝 已处理 {processed_count} 行日志,内存使用: {memory_usage:.1%}",
{'memory_usage': memory_usage, 'processed_count': processed_count})
# 简化的AI分析(模拟)
if ai_enabled and results['processed_logs']:
self.progress_updated.emit(85, "🤖 启动AI分析...", {})
self._simulate_ai_analysis(results)
return results
def _simulate_ai_analysis(self, results):
"""模拟AI分析(简化版)"""
try:
# 准备AI分析数据
log_count = min(len(results['processed_logs']), 10) # 限制数量
for i in range(log_count):
if self.is_interrupted:
break
# 模拟AI分析结果
log_entry = results['processed_logs'][i]
log_entry['ai_analysis'] = f"AI分析结果 #{i+1}: 威胁等级低,建议持续监控"
# 模拟处理延迟
time.sleep(0.1) # 短暂延迟模拟
if i % 3 == 0: # 每3条更新一次进度
progress = 85 + (i * 15 // log_count)
self.progress_updated.emit(progress, f"🧠 AI分析进度: {i+1}/{log_count}", {})
self.log_output.emit(f"🧠 AI分析完成,处理了 {log_count} 条日志")
except Exception as e:
self.log_output.emit(f"⚠️ AI分析失败: {e}")
def _get_memory_usage(self):
"""获取当前内存使用率"""
try:
import psutil
return psutil.virtual_memory().percent / 100.0
except:
return 0.0
def _get_cache_stats(self):
"""获取缓存统计"""
try:
cache = self.cache_manager.get_cache("analysis_cache")
if hasattr(cache, 'get_stats'):
stats = cache.get_stats()
return {
'hit_rate': stats.get('hit_rate', 0),
'size': stats.get('size', 0),
'max_size': stats.get('max_size', 0)
}
except:
pass
return {'hit_rate': 0, 'size': 0, 'max_size': 0}
def interrupt(self):
"""中断分析"""
self.is_interrupted = True
self.log_output.emit("⏹️ 用户中断分析...")
class OptimizedLogAnalyzerGUI(QMainWindow):
"""SSlogs v3.1 优化版GUI界面"""
def __init__(self):
super().__init__()
self.setWindowTitle("🚀 SSlogs v3.1 - 企业级智能安全日志分析平台")
self.setGeometry(100, 100, 1200, 900)
# 设置应用图标和样式
self._setup_appearance()
# 创建中央部件
central_widget = QWidget()
self.setCentralWidget(central_widget)
# 创建主布局
main_layout = QVBoxLayout(central_widget)
main_layout.setSpacing(10)
main_layout.setContentsMargins(10, 10, 10, 10)
# 创建各个功能区域
self._create_header(main_layout)
self._create_main_content(main_layout)
self._create_status_bar(main_layout)
# 初始化变量
self.worker = None
self.start_time = 0
# 初始化定时器
self._setup_timers()
def _setup_appearance(self):
"""设置界面外观"""
# 设置应用样式
self.setStyleSheet("""
QMainWindow {
background-color: #f8fafc;
}
QGroupBox {
font-weight: bold;
border: 2px solid #e2e8f0;
border-radius: 8px;
margin-top: 1ex;
padding-top: 10px;
}
QGroupBox::title {
subcontrol-origin: margin;
left: 10px;
padding: 0 5px 0 5px;
color: #1e293b;
}
QPushButton {
background-color: #3b82f6;
color: white;
border: none;
padding: 8px 16px;
border-radius: 6px;
font-weight: bold;
}
QPushButton:hover {
background-color: #2563eb;
}
QPushButton:pressed {
background-color: #1d4ed8;
}
QPushButton:disabled {
background-color: #94a3b8;
color: #cbd5e1;
}
QProgressBar {
border: 1px solid #e2e8f0;
border-radius: 6px;
text-align: center;
font-weight: bold;
color: #1e293b;
}
QProgressBar::chunk {
background: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0,
stop: 0 #3b82f6, stop: 1 #1d4ed8);
border-radius: 5px;
}
QTextEdit {
border: 1px solid #e2e8f0;
border-radius: 6px;
background-color: white;
font-family: 'Monaco', 'Menlo', 'Courier New', monospace;
font-size: 12px;
}
QTabWidget::pane {
border: 1px solid #e2e8f0;
border-radius: 6px;
}
QTabBar::tab {
background-color: #e2e8f0;
padding: 8px 16px;
margin-right: 2px;
border-top-left-radius: 6px;
border-top-right-radius: 6px;
}
QTabBar::tab:selected {
background-color: #3b82f6;
color: white;
}
""")
def _create_header(self, parent_layout):
"""创建头部区域"""
header = QGroupBox("🛡️ SSlogs v3.1 - 智能安全日志分析平台")
header_layout = QHBoxLayout()
# 标题和版本信息
title_layout = QVBoxLayout()
title_label = QLabel("企业级智能安全日志分析平台")
title_label.setStyleSheet("font-size: 18px; font-weight: bold; color: #1e293b;")
subtitle_label = QLabel("集成异步AI分析 • 内存优化 • 安全验证 • 高性能缓存")
subtitle_label.setStyleSheet("font-size: 12px; color: #64748b;")
title_layout.addWidget(title_label)
title_layout.addWidget(subtitle_label)
header_layout.addLayout(title_layout)
header_layout.addStretch()
parent_layout.addWidget(header)
def _create_main_content(self, parent_layout):
"""创建主内容区域"""
# 创建分割器
splitter = QSplitter(Qt.Orientation.Horizontal)
# 左侧配置区域
left_widget = QWidget()
left_layout = QVBoxLayout(left_widget)
self._create_configuration_tabs(left_layout)
# 右侧结果区域
right_widget = QWidget()
right_layout = QVBoxLayout(right_widget)
self._create_results_area(right_layout)
# 添加到分割器
splitter.addWidget(left_widget)
splitter.addWidget(right_widget)
splitter.setSizes([400, 800])
parent_layout.addWidget(splitter)
def _create_configuration_tabs(self, parent_layout):
"""创建配置选项卡"""
config_tabs = QTabWidget()
# 基本配置选项卡
basic_tab = QWidget()
basic_layout = QVBoxLayout(basic_tab)
self._create_basic_config(basic_layout)
# AI分析配置选项卡
ai_tab = QWidget()
ai_layout = QVBoxLayout(ai_tab)
self._create_ai_config(ai_layout)
# 性能优化配置选项卡
perf_tab = QWidget()
perf_layout = QVBoxLayout(perf_tab)
self._create_performance_config(perf_layout)
# 安全配置选项卡
security_tab = QWidget()
security_layout = QVBoxLayout(security_tab)
self._create_security_config(security_layout)
# 缓存配置选项卡
cache_tab = QWidget()
cache_layout = QVBoxLayout(cache_tab)
self._create_cache_config(cache_layout)
config_tabs.addTab(basic_tab, "📋 基本配置")
config_tabs.addTab(ai_tab, "🤖 AI分析")
config_tabs.addTab(perf_tab, "⚡ 性能优化")
config_tabs.addTab(security_tab, "🔒 安全配置")
config_tabs.addTab(cache_tab, "💾 缓存配置")
parent_layout.addWidget(config_tabs)
def _create_basic_config(self, parent_layout):
"""创建基本配置"""
# 日志路径配置
log_group = QGroupBox("📁 日志文件配置")
log_layout = QVBoxLayout()
path_layout = QHBoxLayout()
path_layout.addWidget(QLabel("日志路径:"))
self.log_path_input = QLineEdit("logs/*.log")
browse_btn = QPushButton("浏览...")
browse_btn.clicked.connect(self.browse_log_path)
path_layout.addWidget(self.log_path_input)
path_layout.addWidget(browse_btn)
log_layout.addLayout(path_layout)
log_group.setLayout(log_layout)
parent_layout.addWidget(log_group)
# 输出配置
output_group = QGroupBox("📄 输出配置")
output_layout = QFormLayout()
self.output_dir_input = QLineEdit("output")
output_browse_btn = QPushButton("浏览...")
output_browse_btn.clicked.connect(self.browse_output_dir)
output_layout.addRow("输出目录:", self.output_dir_input)
output_layout.addRow("", output_browse_btn)
self.format_combo = QComboBox()
self.format_combo.addItems(["html", "json", "csv", "txt"])
output_layout.addRow("报告格式:", self.format_combo)
output_group.setLayout(output_layout)
parent_layout.addWidget(output_group)
# 分析选项
analysis_group = QGroupBox("🔍 分析选项")
analysis_layout = QVBoxLayout()
self.ai_enabled_cb = QCheckBox("启用AI智能分析")
self.ai_enabled_cb.setChecked(True)
analysis_layout.addWidget(self.ai_enabled_cb)
self.memory_optimized_cb = QCheckBox("启用内存优化处理")
self.memory_optimized_cb.setChecked(True)
analysis_layout.addWidget(self.memory_optimized_cb)
self.security_validation_cb = QCheckBox("启用安全输入验证")
self.security_validation_cb.setChecked(True)
analysis_layout.addWidget(self.security_validation_cb)
self.enable_cache_cb = QCheckBox("启用分析缓存")
self.enable_cache_cb.setChecked(True)
analysis_layout.addWidget(self.enable_cache_cb)
analysis_group.setLayout(analysis_layout)
parent_layout.addWidget(analysis_group)
parent_layout.addStretch()
def _create_ai_config(self, parent_layout):
"""创建AI配置"""
# AI服务配置
service_group = QGroupBox("🤖 AI服务配置")
service_layout = QVBoxLayout()
# AI模型类型
model_layout = QHBoxLayout()
model_layout.addWidget(QLabel("AI模型类型:"))
self.ai_model_type = QComboBox()
self.ai_model_type.addItems(["云端 DeepSeek", "本地 LM Studio", "本地 Ollama"])
model_layout.addWidget(self.ai_model_type)
service_layout.addLayout(model_layout)
# 模型选择
model_select_layout = QHBoxLayout()
model_select_layout.addWidget(QLabel("模型名称:"))
self.model_name_combo = QComboBox()
self.model_name_combo.setEditable(True)
self.model_name_combo.setMinimumWidth(200)
refresh_btn = QPushButton("刷新")
refresh_btn.clicked.connect(self.refresh_models)
model_select_layout.addWidget(self.model_name_combo)
model_select_layout.addWidget(refresh_btn)
service_layout.addLayout(model_select_layout)
# API密钥(云端)
api_layout = QHBoxLayout()
api_layout.addWidget(QLabel("API密钥:"))
self.api_key_input = QLineEdit()
self.api_key_input.setEchoMode(QLineEdit.EchoMode.Password)
self.api_key_input.setPlaceholderText("云端服务API密钥")
api_layout.addWidget(self.api_key_input)
service_layout.addLayout(api_layout)
# AI并发配置
concurrent_layout = QHBoxLayout()
concurrent_layout.addWidget(QLabel("并发处理数:"))
self.ai_concurrent_spin = QSpinBox()
self.ai_concurrent_spin.setRange(1, 20)
self.ai_concurrent_spin.setValue(5)
concurrent_layout.addWidget(self.ai_concurrent_spin)
concurrent_layout.addStretch()
service_layout.addLayout(concurrent_layout)
service_group.setLayout(service_layout)
parent_layout.addWidget(service_group)
# 测试区域
test_group = QGroupBox("🧪 连接测试")
test_layout = QVBoxLayout()
test_btn = QPushButton("测试AI连接")
test_btn.clicked.connect(self.test_ai_connection)
test_layout.addWidget(test_btn)
self.ai_status_label = QLabel("状态: 未测试")
test_layout.addWidget(self.ai_status_label)
test_group.setLayout(test_layout)
parent_layout.addWidget(test_group)
parent_layout.addStretch()
def _create_performance_config(self, parent_layout):
"""创建性能配置"""
# 内存配置
memory_group = QGroupBox("💾 内存优化配置")
memory_layout = QFormLayout()
self.chunk_size_spin = QSpinBox()
self.chunk_size_spin.setRange(1024, 65536)
self.chunk_size_spin.setValue(8192)
memory_layout.addRow("处理块大小:", self.chunk_size_spin)
self.max_memory_usage_slider = QSlider(Qt.Orientation.Horizontal)
self.max_memory_usage_slider.setRange(50, 90)
self.max_memory_usage_slider.setValue(80)
self.memory_usage_label = QLabel("80%")
memory_layout.addRow("最大内存使用:", self.max_memory_usage_slider)
memory_layout.addRow("", self.memory_usage_label)
# 连接滑块信号
self.max_memory_usage_slider.valueChanged.connect(
lambda v: self.memory_usage_label.setText(f"{v}%")
)
memory_group.setLayout(memory_layout)
parent_layout.addWidget(memory_group)
# 处理配置
processing_group = QGroupBox("⚙️ 处理配置")
processing_layout = QFormLayout()
self.batch_size_spin = QSpinBox()
self.batch_size_spin.setRange(10, 1000)
self.batch_size_spin.setValue(100)
processing_layout.addRow("批处理大小:", self.batch_size_spin)
self.processing_timeout_spin = QSpinBox()
self.processing_timeout_spin.setRange(10, 300)
self.processing_timeout_spin.setValue(60)
processing_layout.addRow("处理超时(秒):", self.processing_timeout_spin)
processing_group.setLayout(processing_layout)
parent_layout.addWidget(processing_group)
parent_layout.addStretch()
def _create_security_config(self, parent_layout):
"""创建安全配置"""
# 验证级别
validation_group = QGroupBox("🔍 输入验证")
validation_layout = QFormLayout()
self.validation_level = QComboBox()
self.validation_level.addItems(["宽松", "适中", "严格", "偏执"])
self.validation_level.setCurrentText("适中")
validation_layout.addRow("验证级别:", self.validation_level)
validation_group.setLayout(validation_layout)
parent_layout.addWidget(validation_group)
# 威胁检测
threat_group = QGroupBox("⚠️ 威胁检测")
threat_layout = QVBoxLayout()
self.enable_sql_injection_cb = QCheckBox("SQL注入检测")
self.enable_sql_injection_cb.setChecked(True)
threat_layout.addWidget(self.enable_sql_injection_cb)
self.enable_xss_cb = QCheckBox("XSS攻击检测")
self.enable_xss_cb.setChecked(True)
threat_layout.addWidget(self.enable_xss_cb)
self.enable_path_traversal_cb = QCheckBox("路径遍历检测")
self.enable_path_traversal_cb.setChecked(True)
threat_layout.addWidget(self.enable_path_traversal_cb)
self.enable_command_injection_cb = QCheckBox("命令注入检测")
self.enable_command_injection_cb.setChecked(True)
threat_layout.addWidget(self.enable_command_injection_cb)
threat_group.setLayout(threat_layout)
parent_layout.addWidget(threat_group)
parent_layout.addStretch()
def _create_cache_config(self, parent_layout):
"""创建缓存配置"""
# 内存缓存
memory_cache_group = QGroupBox("🧠 内存缓存")
memory_cache_layout = QFormLayout()
self.cache_size_spin = QSpinBox()
self.cache_size_spin.setRange(100, 10000)
self.cache_size_spin.setValue(1000)
memory_cache_layout.addRow("缓存大小:", self.cache_size_spin)
self.cache_policy_combo = QComboBox()
self.cache_policy_combo.addItems(["LRU", "LFU", "FIFO"])
self.cache_policy_combo.setCurrentText("LRU")
memory_cache_layout.addRow("缓存策略:", self.cache_policy_combo)
self.cache_ttl_spin = QSpinBox()
self.cache_ttl_spin.setRange(60, 3600)
self.cache_ttl_spin.setValue(300)
memory_cache_layout.addRow("缓存TTL(秒):", self.cache_ttl_spin)
memory_cache_group.setLayout(memory_cache_layout)
parent_layout.addWidget(memory_cache_group)
# 缓存操作
cache_ops_group = QGroupBox("🔧 缓存操作")
cache_ops_layout = QVBoxLayout()
clear_cache_btn = QPushButton("清空缓存")
clear_cache_btn.clicked.connect(self.clear_cache)
cache_ops_layout.addWidget(clear_cache_btn)
self.cache_stats_label = QLabel("缓存统计: 未加载")
cache_ops_layout.addWidget(self.cache_stats_label)
cache_ops_group.setLayout(cache_ops_layout)
parent_layout.addWidget(cache_ops_group)
parent_layout.addStretch()
def _create_results_area(self, parent_layout):
"""创建结果显示区域"""
# 进度区域
progress_group = QGroupBox("📊 分析进度")
progress_layout = QVBoxLayout()
self.progress_bar = QProgressBar()
self.progress_bar.setRange(0, 100)
self.progress_bar.setValue(0)
self.progress_label = QLabel("准备就绪")
self.progress_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
progress_layout.addWidget(self.progress_bar)
progress_layout.addWidget(self.progress_label)
# 性能指标
perf_metrics_layout = QHBoxLayout()
self.memory_label = QLabel("💾 内存: --")
self.speed_label = QLabel("⚡ 速度: --")
self.cache_hit_label = QLabel("🎯 缓存: --")
perf_metrics_layout.addWidget(self.memory_label)
perf_metrics_layout.addWidget(self.speed_label)
perf_metrics_layout.addWidget(self.cache_hit_label)
perf_metrics_layout.addStretch()
progress_layout.addLayout(perf_metrics_layout)
progress_group.setLayout(progress_layout)
parent_layout.addWidget(progress_group)
# 威胁检测结果表格
threat_group = QGroupBox("🚨 威胁检测结果")
threat_layout = QVBoxLayout()
self.threat_table = QTableWidget()
self.threat_table.setColumnCount(4)
self.threat_table.setHorizontalHeaderLabels(["时间", "威胁类型", "风险评分", "预览"])
# 设置表格属性
header = self.threat_table.horizontalHeader()
header.setSectionResizeMode(0, QHeaderView.ResizeMode.ResizeToContents)
header.setSectionResizeMode(1, QHeaderView.ResizeMode.Stretch)
header.setSectionResizeMode(2, QHeaderView.ResizeMode.ResizeToContents)
header.setSectionResizeMode(3, QHeaderView.ResizeMode.Stretch)
threat_layout.addWidget(self.threat_table)
threat_group.setLayout(threat_layout)
parent_layout.addWidget(threat_group)
# 日志输出
log_group = QGroupBox("📝 分析日志")
log_layout = QVBoxLayout()
self.log_output = QTextEdit()
self.log_output.setMaximumHeight(200)
self.log_output.setReadOnly(True)
log_layout.addWidget(self.log_output)
log_group.setLayout(log_layout)
parent_layout.addWidget(log_group)
def _create_status_bar(self, parent_layout):
"""创建状态栏"""
# 控制按钮
button_layout = QHBoxLayout()
self.start_button = QPushButton("🚀 开始分析")
self.start_button.clicked.connect(self.start_analysis)
self.start_button.setStyleSheet("""
QPushButton {
background-color: #10b981;
font-size: 14px;
padding: 12px 24px;
}
QPushButton:hover {
background-color: #059669;
}
""")
self.stop_button = QPushButton("⏹️ 停止分析")
self.stop_button.clicked.connect(self.stop_analysis)
self.stop_button.setEnabled(False)
self.stop_button.setStyleSheet("""
QPushButton {
background-color: #ef4444;
font-size: 14px;
padding: 12px 24px;
}
QPushButton:hover {
background-color: #dc2626;
}
""")
self.export_button = QPushButton("📊 导出报告")
self.export_button.clicked.connect(self.export_report)
button_layout.addStretch()
button_layout.addWidget(self.start_button)
button_layout.addWidget(self.stop_button)
button_layout.addWidget(self.export_button)
parent_layout.addLayout(button_layout)
def _setup_timers(self):
"""设置定时器"""
# 性能监控定时器
self.performance_timer = QTimer()
self.performance_timer.timeout.connect(self.update_performance_stats)
# 缓存统计定时器
self.cache_timer = QTimer()
self.cache_timer.timeout.connect(self.update_cache_stats)
def browse_log_path(self):
"""浏览日志路径"""
file_path, _ = QFileDialog.getOpenFileName(
self, "选择日志文件", "", "日志文件 (*.log *.txt);;所有文件 (*)")
if file_path:
self.log_path_input.setText(file_path)
def browse_output_dir(self):
"""浏览输出目录"""
directory = QFileDialog.getExistingDirectory(self, "选择输出目录")
if directory:
self.output_dir_input.setText(directory)
def refresh_models(self):
"""刷新AI模型列表"""
self.model_name_combo.clear()
self.model_name_combo.addItem("正在加载模型...")
# 模拟模型加载
QTimer.singleShot(1000, self._load_models)
def _load_models(self):
"""加载模型列表"""
try:
# 这里应该调用实际的模型加载逻辑
models = [
"deepseek-r1:1.5b",
"deepseek-r1:7b",
"deepseek-r1:14b",
"qwen2.5-coder:7b",
"llama3.1:8b",
"mistral:7b"
]
self.model_name_combo.clear()
self.model_name_combo.addItems(models)
self.model_name_combo.setCurrentIndex(0)
except Exception as e:
self.model_name_combo.clear()
self.model_name_combo.addItem(f"加载失败: {str(e)[:30]}...")
def test_ai_connection(self):
"""测试AI连接"""
try:
self.ai_status_label.setText("🔄 正在测试...")
# 模拟连接测试
QTimer.singleShot(2000, self._ai_test_result)
except Exception as e:
self.ai_status_label.setText(f"❌ 测试失败: {str(e)}")
def _ai_test_result(self):
"""AI测试结果"""
# 模拟测试成功
self.ai_status_label.setText("✅ 连接测试成功")
self.ai_status_label.setStyleSheet("color: #10b981;")
def clear_cache(self):
"""清空缓存"""
try:
cache_manager = get_cache_manager()
cache_manager.clear_all_caches()
self.cache_stats_label.setText("✅ 缓存已清空")
except Exception as e:
self.cache_stats_label.setText(f"❌ 清空失败: {str(e)}")
def update_performance_stats(self):
"""更新性能统计"""
try:
import psutil
memory_percent = psutil.virtual_memory().percent
self.memory_label.setText(f"💾 内存: {memory_percent:.1f}%")
if hasattr(self, 'start_time') and self.start_time > 0:
elapsed = time.time() - self.start_time
self.speed_label.setText(f"⚡ 用时: {elapsed:.1f}秒")
except:
pass
def update_cache_stats(self):
"""更新缓存统计"""
try:
cache_manager = get_cache_manager()
cache = cache_manager.get_cache("analysis_cache")
if hasattr(cache, 'get_stats'):
stats = cache.get_stats()
hit_rate = stats.get('hit_rate', 0) * 100
size = stats.get('size', 0)
self.cache_hit_label.setText(f"🎯 缓存: {hit_rate:.1f}% ({size}项)")
except:
pass
def start_analysis(self):
"""开始分析"""
# 收集配置
config = {
'log_path': self.log_path_input.text(),
'output_dir': self.output_dir_input.text(),
'report_format': self.format_combo.currentText(),
'ai_enabled': self.ai_enabled_cb.isChecked(),
'memory_optimized': self.memory_optimized_cb.isChecked(),
'security_validation': self.security_validation_cb.isChecked(),
'enable_cache': self.enable_cache_cb.isChecked(),
'ai_model_type': self.ai_model_type.currentText(),
'model_name': self.model_name_combo.currentText(),
'api_key': self.api_key_input.text(),
'ai_concurrent_limit': self.ai_concurrent_spin.value(),
'chunk_size': self.chunk_size_spin.value(),
'max_memory_usage': self.max_memory_usage_slider.value() / 100,
'batch_size': self.batch_size_spin.value(),
'validation_level': self.validation_level.currentText(),
'cache_size': self.cache_size_spin.value(),
'cache_policy': self.cache_policy_combo.currentText(),
'cache_ttl': self.cache_ttl_spin.value()
}
# 验证配置
if not config['log_path']:
QMessageBox.warning(self, "配置错误", "请指定日志文件路径")
return
if config['ai_enabled'] and not config['model_name']:
QMessageBox.warning(self, "配置错误", "启用AI分析时需要指定模型名称")
return
# 禁用按钮
self.start_button.setEnabled(False)
self.stop_button.setEnabled(True)
# 清空日志和威胁表格
self.log_output.clear()
self.threat_table.setRowCount(0)
# 启动定时器
self.start_time = time.time()
self.performance_timer.start(1000)
self.cache_timer.start(2000)
# 创建并启动工作线程
self.worker = AnalysisWorker(config)
self.worker.progress_updated.connect(self.update_progress)
self.worker.log_output.connect(self.append_log)
self.worker.analysis_finished.connect(self.analysis_completed)
self.worker.threat_detected.connect(self.threat_detected)
self.worker.start()
def stop_analysis(self):
"""停止分析"""
if self.worker:
self.worker.interrupt()
# 停止定时器
self.performance_timer.stop()
self.cache_timer.stop()
# 恢复按钮状态
self.start_button.setEnabled(True)
self.stop_button.setEnabled(False)
self.append_log("⏹️ 分析已停止")
def update_progress(self, value, message, performance_data):
"""更新进度"""
self.progress_bar.setValue(value)
self.progress_label.setText(message)
# 更新性能数据
if 'memory_usage' in performance_data:
memory = performance_data['memory_usage'] * 100
self.memory_label.setText(f"💾 内存: {memory:.1f}%")
if 'processed_count' in performance_data:
count = performance_data['processed_count']
elapsed = time.time() - self.start_time
speed = count / max(elapsed, 1)
self.speed_label.setText(f"⚡ 速度: {speed:.1f}条/秒")
def append_log(self, message):
"""添加日志"""
timestamp = time.strftime("%H:%M:%S")
self.log_output.append(f"[{timestamp}] {message}")
# 自动滚动到底部
scrollbar = self.log_output.verticalScrollBar()
scrollbar.setValue(scrollbar.maximum())
def threat_detected(self, threat_type, threat_data):
"""处理威胁检测"""
row = self.threat_table.rowCount()
self.threat_table.insertRow(row)
# 添加威胁数据
timestamp = time.strftime("%H:%M:%S")
threats = ', '.join([t.value for t in threat_data.get('threats', [])])
risk_score = threat_data.get('risk_score', 0)
preview = threat_data.get('line', '')[:50] + "..."
self.threat_table.setItem(row, 0, QTableWidgetItem(timestamp))
self.threat_table.setItem(row, 1, QTableWidgetItem(threats))
self.threat_table.setItem(row, 2, QTableWidgetItem(f"{risk_score:.1f}"))
self.threat_table.setItem(row, 3, QTableWidgetItem(preview))
# 设置行颜色
if risk_score >= 8.0:
color = QColor(239, 68, 68) # 红色
elif risk_score >= 5.0:
color = QColor(245, 158, 11) # 橙色
else:
color = QColor(59, 130, 246) # 蓝色