-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
613 lines (496 loc) · 20.9 KB
/
main.py
File metadata and controls
613 lines (496 loc) · 20.9 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
"""
Qt Log Analyzer - Main Application
A modern desktop application for analyzing large log files.
"""
import sys
import csv
import json
from datetime import datetime
from typing import List, Optional
from PyQt5.QtWidgets import (
QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout,
QTableWidget, QTableWidgetItem, QPushButton, QFileDialog,
QLabel, QLineEdit, QComboBox, QCheckBox, QSplitter,
QGroupBox, QProgressBar, QDateTimeEdit, QTextEdit,
QTabWidget, QMessageBox, QHeaderView
)
from PyQt5.QtCore import Qt, QThread, pyqtSignal, QDateTime
from PyQt5.QtGui import QColor, QFont
from log_parser import LogParser, LogEntry, LogLevel
from log_filter import LogFilter
from log_statistics import LogStatistics
from timeline_widget import TimelineWidget
from statistics_widget import StatisticsWidget
class LogLoaderThread(QThread):
"""Background thread for loading log files"""
progress = pyqtSignal(int)
finished = pyqtSignal(list)
error = pyqtSignal(str)
def __init__(self, filepath: str, is_json: bool = False):
super().__init__()
self.filepath = filepath
self.is_json = is_json
def run(self):
"""Load and parse log file"""
try:
parser = LogParser()
entries = []
# Count total lines first
with open(self.filepath, 'r', encoding='utf-8', errors='replace') as f:
total_lines = sum(1 for _ in f)
# Parse lines with progress
with open(self.filepath, 'r', encoding='utf-8', errors='replace') as f:
for i, line in enumerate(f):
if self.is_json:
entry = parser.parse_json_line(line)
else:
entry = parser.parse_text_line(line)
entries.append(entry)
# Emit progress every 1000 lines
if i % 1000 == 0:
progress_pct = int((i / total_lines) * 100)
self.progress.emit(progress_pct)
# Detect anomalies
parser.detect_anomalies(entries)
self.progress.emit(100)
self.finished.emit(entries)
except Exception as e:
self.error.emit(str(e))
class MainWindow(QMainWindow):
"""Main application window"""
def __init__(self):
super().__init__()
self.log_entries: List[LogEntry] = []
self.filtered_entries: List[LogEntry] = []
self.log_filter = LogFilter()
self.current_file = ""
self.init_ui()
def init_ui(self):
"""Initialize the user interface"""
self.setWindowTitle("Qt Log Analyzer")
self.setGeometry(100, 100, 1400, 900)
# Create central widget
central_widget = QWidget()
self.setCentralWidget(central_widget)
# Main layout
main_layout = QVBoxLayout(central_widget)
# Add toolbar
toolbar = self.create_toolbar()
main_layout.addLayout(toolbar)
# Add filter panel
filter_panel = self.create_filter_panel()
main_layout.addWidget(filter_panel)
# Create splitter for main content
splitter = QSplitter(Qt.Vertical)
# Add tabs for different views
self.tab_widget = QTabWidget()
# Log table tab
self.log_table = self.create_log_table()
self.tab_widget.addTab(self.log_table, "Log Entries")
# Timeline tab
self.timeline_widget = TimelineWidget()
self.tab_widget.addTab(self.timeline_widget, "Timeline")
# Statistics tab
self.statistics_widget = StatisticsWidget()
self.tab_widget.addTab(self.statistics_widget, "Statistics")
splitter.addWidget(self.tab_widget)
# Add status panel
self.status_panel = self.create_status_panel()
splitter.addWidget(self.status_panel)
splitter.setSizes([700, 200])
main_layout.addWidget(splitter)
# Progress bar
self.progress_bar = QProgressBar()
self.progress_bar.setVisible(False)
main_layout.addWidget(self.progress_bar)
# Apply modern styling
self.apply_styles()
def create_toolbar(self) -> QHBoxLayout:
"""Create toolbar with action buttons"""
toolbar = QHBoxLayout()
# Open file button
self.btn_open = QPushButton("📁 Open Log File")
self.btn_open.clicked.connect(self.open_file)
toolbar.addWidget(self.btn_open)
# Export button
self.btn_export = QPushButton("💾 Export Filtered")
self.btn_export.clicked.connect(self.export_filtered)
self.btn_export.setEnabled(False)
toolbar.addWidget(self.btn_export)
# Clear filters button
self.btn_clear = QPushButton("🔄 Clear Filters")
self.btn_clear.clicked.connect(self.clear_filters)
toolbar.addWidget(self.btn_clear)
toolbar.addStretch()
# File info label
self.lbl_file_info = QLabel("No file loaded")
toolbar.addWidget(self.lbl_file_info)
return toolbar
def create_filter_panel(self) -> QGroupBox:
"""Create filter panel"""
group = QGroupBox("Filters")
layout = QHBoxLayout()
# Log level filter
layout.addWidget(QLabel("Level:"))
self.combo_level = QComboBox()
self.combo_level.addItem("All Levels", None)
for level in LogLevel:
self.combo_level.addItem(level.value, level)
self.combo_level.currentIndexChanged.connect(self.apply_filters)
layout.addWidget(self.combo_level)
# Source filter
layout.addWidget(QLabel("Source:"))
self.combo_source = QComboBox()
self.combo_source.addItem("All Sources", None)
self.combo_source.currentIndexChanged.connect(self.apply_filters)
layout.addWidget(self.combo_source)
# Time range
layout.addWidget(QLabel("From:"))
self.datetime_start = QDateTimeEdit()
self.datetime_start.setCalendarPopup(True)
self.datetime_start.setEnabled(False)
self.datetime_start.dateTimeChanged.connect(self.apply_filters)
layout.addWidget(self.datetime_start)
layout.addWidget(QLabel("To:"))
self.datetime_end = QDateTimeEdit()
self.datetime_end.setCalendarPopup(True)
self.datetime_end.setEnabled(False)
self.datetime_end.dateTimeChanged.connect(self.apply_filters)
layout.addWidget(self.datetime_end)
# Regex search
layout.addWidget(QLabel("Search:"))
self.txt_search = QLineEdit()
self.txt_search.setPlaceholderText("Regex pattern...")
self.txt_search.textChanged.connect(self.apply_filters)
layout.addWidget(self.txt_search)
# Anomalies only checkbox
self.chk_anomalies = QCheckBox("Anomalies Only")
self.chk_anomalies.stateChanged.connect(self.apply_filters)
layout.addWidget(self.chk_anomalies)
group.setLayout(layout)
return group
def create_log_table(self) -> QTableWidget:
"""Create log entries table"""
table = QTableWidget()
table.setColumnCount(5)
table.setHorizontalHeaderLabels(["Timestamp", "Level", "Source", "Message", "Anomaly"])
# Set column widths
header = table.horizontalHeader()
header.setSectionResizeMode(0, QHeaderView.ResizeToContents)
header.setSectionResizeMode(1, QHeaderView.ResizeToContents)
header.setSectionResizeMode(2, QHeaderView.ResizeToContents)
header.setSectionResizeMode(3, QHeaderView.Stretch)
header.setSectionResizeMode(4, QHeaderView.ResizeToContents)
table.setAlternatingRowColors(True)
table.setSelectionBehavior(QTableWidget.SelectRows)
return table
def create_status_panel(self) -> QGroupBox:
"""Create status panel for summary info"""
group = QGroupBox("Status")
layout = QVBoxLayout()
self.txt_status = QTextEdit()
self.txt_status.setReadOnly(True)
self.txt_status.setMaximumHeight(150)
layout.addWidget(self.txt_status)
group.setLayout(layout)
return group
def open_file(self):
"""Open and load a log file"""
filepath, _ = QFileDialog.getOpenFileName(
self,
"Open Log File",
"",
"Log Files (*.log *.txt *.json);;All Files (*.*)"
)
if filepath:
self.current_file = filepath
is_json = filepath.endswith('.json')
# Show progress bar
self.progress_bar.setVisible(True)
self.progress_bar.setValue(0)
self.btn_open.setEnabled(False)
# Start loading in background thread
self.loader_thread = LogLoaderThread(filepath, is_json)
self.loader_thread.progress.connect(self.on_load_progress)
self.loader_thread.finished.connect(self.on_load_finished)
self.loader_thread.error.connect(self.on_load_error)
self.loader_thread.start()
def on_load_progress(self, value: int):
"""Update progress bar"""
self.progress_bar.setValue(value)
def on_load_finished(self, entries: List[LogEntry]):
"""Handle loaded log entries"""
self.log_entries = entries
self.filtered_entries = entries
# Update UI
self.progress_bar.setVisible(False)
self.btn_open.setEnabled(True)
self.btn_export.setEnabled(True)
# Update file info
self.lbl_file_info.setText(f"Loaded: {len(entries)} entries from {self.current_file.split('/')[-1]}")
# Populate source filter
self.update_source_filter()
# Update time range filters
self.update_time_range()
# Display entries
self.display_entries(entries)
# Update statistics
self.update_statistics()
# Update timeline
self.update_timeline()
# Update status
self.update_status()
def on_load_error(self, error_msg: str):
"""Handle loading error"""
self.progress_bar.setVisible(False)
self.btn_open.setEnabled(True)
QMessageBox.critical(self, "Error", f"Failed to load file: {error_msg}")
def update_source_filter(self):
"""Update source filter combo box"""
self.combo_source.clear()
self.combo_source.addItem("All Sources", None)
sources = set()
for entry in self.log_entries:
if entry.source:
sources.add(entry.source)
for source in sorted(sources):
self.combo_source.addItem(source, source)
def update_time_range(self):
"""Update time range filters"""
timestamps = [entry.timestamp for entry in self.log_entries if entry.timestamp]
if timestamps:
min_time = min(timestamps)
max_time = max(timestamps)
self.datetime_start.setDateTime(QDateTime(min_time))
self.datetime_end.setDateTime(QDateTime(max_time))
self.datetime_start.setEnabled(True)
self.datetime_end.setEnabled(True)
def apply_filters(self):
"""Apply current filters to log entries"""
if not self.log_entries:
return
# Update filter settings
selected_level = self.combo_level.currentData()
if selected_level:
self.log_filter.set_levels([selected_level])
else:
self.log_filter.set_levels([])
selected_source = self.combo_source.currentData()
if selected_source:
self.log_filter.set_sources([selected_source])
else:
self.log_filter.set_sources([])
if self.datetime_start.isEnabled():
start_time = self.datetime_start.dateTime().toPyDateTime()
end_time = self.datetime_end.dateTime().toPyDateTime()
self.log_filter.set_time_range(start_time, end_time)
search_text = self.txt_search.text()
self.log_filter.set_regex(search_text)
self.log_filter.set_anomalies_only(self.chk_anomalies.isChecked())
# Apply filter
self.filtered_entries = self.log_filter.filter_entries(self.log_entries)
# Update display
self.display_entries(self.filtered_entries)
self.update_status()
def clear_filters(self):
"""Clear all filters"""
self.combo_level.setCurrentIndex(0)
self.combo_source.setCurrentIndex(0)
self.txt_search.clear()
self.chk_anomalies.setChecked(False)
if self.datetime_start.isEnabled():
timestamps = [entry.timestamp for entry in self.log_entries if entry.timestamp]
if timestamps:
self.datetime_start.setDateTime(QDateTime(min(timestamps)))
self.datetime_end.setDateTime(QDateTime(max(timestamps)))
self.log_filter.clear()
self.filtered_entries = self.log_entries
self.display_entries(self.filtered_entries)
self.update_status()
def display_entries(self, entries: List[LogEntry]):
"""Display log entries in table"""
# Limit display to prevent UI freeze (show first 10000)
display_limit = 10000
display_entries = entries[:display_limit]
self.log_table.setRowCount(len(display_entries))
for i, entry in enumerate(display_entries):
# Timestamp
timestamp_str = entry.timestamp.strftime('%Y-%m-%d %H:%M:%S') if entry.timestamp else ""
item_timestamp = QTableWidgetItem(timestamp_str)
self.log_table.setItem(i, 0, item_timestamp)
# Level
item_level = QTableWidgetItem(entry.level.value)
item_level.setForeground(self.get_level_color(entry.level))
if entry.level in [LogLevel.ERROR, LogLevel.CRITICAL]:
font = item_level.font()
font.setBold(True)
item_level.setFont(font)
self.log_table.setItem(i, 1, item_level)
# Source
item_source = QTableWidgetItem(entry.source)
self.log_table.setItem(i, 2, item_source)
# Message
item_message = QTableWidgetItem(entry.message)
if entry.is_anomaly:
item_message.setBackground(QColor(255, 255, 200))
self.log_table.setItem(i, 3, item_message)
# Anomaly indicator
item_anomaly = QTableWidgetItem("⚠️" if entry.is_anomaly else "")
item_anomaly.setTextAlignment(Qt.AlignCenter)
self.log_table.setItem(i, 4, item_anomaly)
if len(entries) > display_limit:
self.txt_status.append(f"\nNote: Displaying first {display_limit} of {len(entries)} entries")
def get_level_color(self, level: LogLevel) -> QColor:
"""Get color for log level"""
colors = {
LogLevel.DEBUG: QColor(128, 128, 128),
LogLevel.INFO: QColor(0, 128, 0),
LogLevel.WARNING: QColor(255, 165, 0),
LogLevel.ERROR: QColor(255, 0, 0),
LogLevel.CRITICAL: QColor(139, 0, 0),
LogLevel.UNKNOWN: QColor(0, 0, 0)
}
return colors.get(level, QColor(0, 0, 0))
def update_statistics(self):
"""Update statistics view"""
stats = LogStatistics(self.log_entries)
self.statistics_widget.update_statistics(stats)
def update_timeline(self):
"""Update timeline view"""
stats = LogStatistics(self.log_entries)
self.timeline_widget.update_timeline(stats)
def update_status(self):
"""Update status panel"""
total = len(self.log_entries)
filtered = len(self.filtered_entries)
stats = LogStatistics(self.filtered_entries)
summary = stats.get_summary()
status_text = f"""
<b>Total Entries:</b> {total}<br>
<b>Filtered Entries:</b> {filtered}<br>
<b>Anomalies:</b> {summary['anomaly_count']}<br>
<b>Error Rate:</b> {summary['error_rate']:.2f}%<br>
<b>Unique Sources:</b> {summary['unique_sources']}<br>
"""
self.txt_status.setHtml(status_text)
def export_filtered(self):
"""Export filtered entries to file"""
if not self.filtered_entries:
QMessageBox.warning(self, "Warning", "No entries to export")
return
filepath, file_type = QFileDialog.getSaveFileName(
self,
"Export Filtered Logs",
"",
"CSV Files (*.csv);;JSON Files (*.json);;Text Files (*.txt)"
)
if not filepath:
return
try:
if filepath.endswith('.json'):
self.export_json(filepath)
elif filepath.endswith('.csv'):
self.export_csv(filepath)
else:
self.export_text(filepath)
QMessageBox.information(self, "Success", f"Exported {len(self.filtered_entries)} entries")
except Exception as e:
QMessageBox.critical(self, "Error", f"Export failed: {str(e)}")
def export_json(self, filepath: str):
"""Export to JSON format"""
data = [entry.to_dict() for entry in self.filtered_entries]
with open(filepath, 'w', encoding='utf-8') as f:
json.dump(data, f, indent=2, default=str)
def export_csv(self, filepath: str):
"""Export to CSV format"""
with open(filepath, 'w', newline='', encoding='utf-8') as f:
writer = csv.writer(f)
writer.writerow(['Timestamp', 'Level', 'Source', 'Message', 'Anomaly'])
for entry in self.filtered_entries:
timestamp_str = entry.timestamp.isoformat() if entry.timestamp else ""
writer.writerow([
timestamp_str,
entry.level.value,
entry.source,
entry.message,
entry.is_anomaly
])
def export_text(self, filepath: str):
"""Export to text format"""
with open(filepath, 'w', encoding='utf-8') as f:
for entry in self.filtered_entries:
f.write(entry.raw_line + '\n')
def apply_styles(self):
"""Apply modern styling to the application"""
self.setStyleSheet("""
QMainWindow {
background-color: #f5f5f5;
}
QPushButton {
background-color: #4CAF50;
color: white;
border: none;
padding: 8px 16px;
font-size: 14px;
border-radius: 4px;
min-width: 100px;
}
QPushButton:hover {
background-color: #45a049;
}
QPushButton:disabled {
background-color: #cccccc;
}
QGroupBox {
font-weight: bold;
border: 2px solid #cccccc;
border-radius: 5px;
margin-top: 10px;
padding-top: 10px;
}
QGroupBox::title {
subcontrol-origin: margin;
left: 10px;
padding: 0 5px;
}
QTableWidget {
background-color: white;
border: 1px solid #ddd;
gridline-color: #e0e0e0;
}
QHeaderView::section {
background-color: #4CAF50;
color: white;
padding: 6px;
border: none;
font-weight: bold;
}
QLineEdit, QComboBox, QDateTimeEdit {
padding: 5px;
border: 1px solid #ccc;
border-radius: 3px;
background-color: white;
}
QTextEdit {
border: 1px solid #ccc;
border-radius: 3px;
background-color: white;
}
QProgressBar {
border: 1px solid #ccc;
border-radius: 3px;
text-align: center;
}
QProgressBar::chunk {
background-color: #4CAF50;
}
""")
def main():
"""Main application entry point"""
app = QApplication(sys.argv)
app.setStyle('Fusion')
window = MainWindow()
window.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()