-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimeline_widget.py
More file actions
91 lines (71 loc) · 2.93 KB
/
timeline_widget.py
File metadata and controls
91 lines (71 loc) · 2.93 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
"""
Timeline Widget
Displays a timeline visualization of log entries over time.
"""
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLabel
from PyQt5.QtCore import Qt
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter
from datetime import datetime
from log_statistics import LogStatistics, HOURLY_FORMAT
class TimelineWidget(QWidget):
"""Widget for displaying timeline visualization"""
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
"""Initialize the UI"""
layout = QVBoxLayout()
# Title
title = QLabel("Log Timeline - Events Over Time")
title.setAlignment(Qt.AlignCenter)
title.setStyleSheet("font-size: 16px; font-weight: bold; padding: 10px;")
layout.addWidget(title)
# Create matplotlib figure
self.figure = Figure(figsize=(12, 6))
self.canvas = FigureCanvas(self.figure)
layout.addWidget(self.canvas)
self.setLayout(layout)
def update_timeline(self, statistics: LogStatistics):
"""Update timeline with new data"""
self.figure.clear()
timeline_data = statistics.get_timeline_data()
if not timeline_data:
ax = self.figure.add_subplot(111)
ax.text(0.5, 0.5, 'No timeline data available',
ha='center', va='center', fontsize=14)
self.canvas.draw()
return
# Parse timestamps and counts
timestamps = []
counts = []
for time_str, count in timeline_data.items():
try:
dt = datetime.strptime(time_str, HOURLY_FORMAT)
timestamps.append(dt)
counts.append(count)
except ValueError:
continue
if not timestamps:
ax = self.figure.add_subplot(111)
ax.text(0.5, 0.5, 'No timeline data available',
ha='center', va='center', fontsize=14)
self.canvas.draw()
return
# Create timeline plot
ax = self.figure.add_subplot(111)
ax.plot(timestamps, counts, 'b-', linewidth=2, marker='o', markersize=4)
ax.fill_between(timestamps, counts, alpha=0.3)
ax.set_xlabel('Time', fontsize=12, fontweight='bold')
ax.set_ylabel('Log Entries Count', fontsize=12, fontweight='bold')
ax.set_title('Log Activity Timeline', fontsize=14, fontweight='bold')
ax.grid(True, alpha=0.3)
# Format x-axis dates
date_formatter = DateFormatter('%Y-%m-%d %H:%M')
ax.xaxis.set_major_formatter(date_formatter)
self.figure.autofmt_xdate()
# Tight layout
self.figure.tight_layout()
self.canvas.draw()