-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperformance_view.py
More file actions
311 lines (252 loc) · 13.4 KB
/
performance_view.py
File metadata and controls
311 lines (252 loc) · 13.4 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
import gi
import data_engine as db
from datetime import datetime, timedelta
gi.require_version('Gtk', '4.0')
gi.require_version('Adw', '1')
from gi.repository import Gtk, Adw, Gdk
class PerformanceView(Gtk.Box):
def __init__(self, filename, session_stats=None, back_callback=None):
super().__init__(orientation=Gtk.Orientation.VERTICAL, spacing=0) # Remove spacing on root to flush header
self.filename = filename
self.back_callback = back_callback
deck_name = filename.replace(".json", "").replace("_", " ").title()
css_provider = Gtk.CssProvider()
css_provider.load_from_string("""
.bar-green block.filled { background-color: #2ec27e; }
.bar-yellow block.filled { background-color: #f5c211; }
.bar-red block.filled { background-color: #ed333b; }
""")
Gtk.StyleContext.add_provider_for_display(Gdk.Display.get_default(), css_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
# Load Data
raw_history = db.get_deck_history(filename)
sessions = self.group_into_sessions(raw_history)
if session_stats and session_stats.get('total', 0) > 0:
is_dup = False
target_id = session_stats.get('session_id')
# 1. Primary Check: Match by Session ID
if target_id:
for s in sessions:
if s.get('session_id') == target_id:
is_dup = True
break
# 2. Fallback Check: Timestamp heuristic
elif sessions:
last = sessions[-1]
try:
dt_last = datetime.fromisoformat(last['start_time'])
if (datetime.now() - dt_last) < timedelta(minutes=10) and last['count'] == session_stats['total']:
is_dup = True
except: pass
if not is_dup:
sessions.append({
'start_time': datetime.now().isoformat(),
'count': session_stats['total'],
'good': session_stats['good'],
'hard': session_stats['hard'],
'miss': session_stats['miss'],
'session_id': target_id
})
# --- 1. FIXED HEADER (Sticky) ---
# We keep this outside the scroller so it never scrolls away
header = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=10)
header.set_margin_top(15); header.set_margin_bottom(15)
header.set_margin_start(15); header.set_margin_end(15)
if self.back_callback:
btn_back = Gtk.Button(icon_name="go-previous-symbolic")
btn_back.add_css_class("flat")
btn_back.set_tooltip_text("Return")
btn_back.connect("clicked", lambda x: self.back_callback())
header.append(btn_back)
lbl_title = Gtk.Label(label=f"Stats: {deck_name}") # Shortened "Performance" to "Stats" for mobile title safety
lbl_title.add_css_class("title-2")
lbl_title.set_ellipsize(3) # Ellipsize END if title is too long
header.append(lbl_title)
self.append(header)
self.append(Gtk.Separator(orientation=Gtk.Orientation.HORIZONTAL))
# --- 2. SCROLLABLE CONTENT AREA ---
scrolled = Gtk.ScrolledWindow()
scrolled.set_hexpand(True); scrolled.set_vexpand(True)
self.append(scrolled)
# --- 3. ADAPTIVE CLAMP (Mobile Magic) ---
# Wraps content to max 800px width, but shrinks to 12px margins on mobile
clamp = Adw.Clamp(maximum_size=800)
clamp.set_margin_top(20)
clamp.set_margin_bottom(40)
clamp.set_margin_start(12)
clamp.set_margin_end(12)
scrolled.set_child(clamp)
content_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=30)
clamp.set_child(content_box)
# --- Overall Accuracy Section ---
if raw_history:
overall_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=15)
head_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=10)
head_box.append(Gtk.Label(label="Overall Accuracy", xalign=0, css_classes=["title-2"]))
icon_info = Gtk.Image.new_from_icon_name("dialog-information-symbolic")
icon_info.set_tooltip_text("Lifetime average.\n• Good/Hard: 100%\n• Hard+Hint: 50%\n• Miss: 0%")
head_box.append(icon_info)
overall_box.append(head_box)
total_score = 0.0
total_reviews = 0
for e in raw_history:
r = e['rating']
hint = e.get('hint_used', False)
if r == 3: total_score += 1.0
elif r == 2: total_score += 0.5 if hint else 1.0
total_reviews += 1
final_acc = total_score / total_reviews if total_reviews > 0 else 0.0
lbl_acc = Gtk.Label(label=f"{int(final_acc*100)}%", css_classes=["display-1"])
lbl_acc.set_halign(Gtk.Align.CENTER)
overall_box.append(lbl_acc)
bar = Gtk.LevelBar(min_value=0, max_value=1.0)
bar.set_value(final_acc)
bar.set_hexpand(True)
bar.set_size_request(-1, 10)
if final_acc > 0.75: bar.add_css_class("bar-green")
elif final_acc >= 0.50: bar.add_css_class("bar-yellow")
else: bar.add_css_class("bar-red")
overall_box.append(bar)
content_box.append(overall_box)
content_box.append(Gtk.Separator())
# --- Daily Accuracy Section ---
if raw_history:
daily_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=15)
head_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=10)
head_box.append(Gtk.Label(label="Daily Accuracy", xalign=0, css_classes=["title-2"]))
icon_info = Gtk.Image.new_from_icon_name("dialog-information-symbolic")
icon_info.set_tooltip_text("Accuracy Algorithm:\n• Good: 100%\n• Hard (No Hint): 100%\n• Hard (With Hint): 50%\n• Miss: 0%")
head_box.append(icon_info)
daily_box.append(head_box)
daily_stats = {}
for entry in raw_history:
ts = entry.get('timestamp', entry.get('date', ''))
date_str = ts.split("T")[0]
if date_str not in daily_stats: daily_stats[date_str] = []
daily_stats[date_str].append(entry)
sorted_dates = sorted(daily_stats.keys())[-7:]
grid = Gtk.Grid(column_spacing=15, row_spacing=10) # Reduced column spacing for mobile
for i, d in enumerate(sorted_dates):
entries = daily_stats[d]
total_score = 0.0
for e in entries:
r = e['rating']
hint = e.get('hint_used', False)
if r == 3: total_score += 1.0
elif r == 2: total_score += 0.5 if hint else 1.0
acc = total_score / len(entries) if entries else 0.0
# Date Label (Truncated year if needed to save space)
lbl_date = Gtk.Label(label=d[5:], xalign=0) # Show "MM-DD" instead of "YYYY-MM-DD" for space
grid.attach(lbl_date, 0, i, 1, 1)
bar = Gtk.LevelBar(min_value=0, max_value=1.0)
bar.set_value(acc)
bar.set_hexpand(True) # Expand bar to fill space
if acc > 0.75: bar.add_css_class("bar-green")
elif acc >= 0.50: bar.add_css_class("bar-yellow")
else: bar.add_css_class("bar-red")
grid.attach(bar, 1, i, 1, 1)
grid.attach(Gtk.Label(label=f"{int(acc*100)}%"), 2, i, 1, 1)
daily_box.append(grid)
content_box.append(daily_box)
content_box.append(Gtk.Separator())
# --- Session History ---
sess_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=20)
sess_box.append(Gtk.Label(label="Session Log", xalign=0, css_classes=["title-2"]))
if not sessions:
sess_box.append(Gtk.Label(label="No sessions recorded yet.", css_classes=["dim-label"]))
else:
for sess in reversed(sessions):
sess_frame = Gtk.Frame()
sess_inner = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=10)
sess_inner.set_margin_top(15); sess_inner.set_margin_bottom(15)
sess_inner.set_margin_start(15); sess_inner.set_margin_end(15)
sess_frame.set_child(sess_inner)
try:
dt_obj = datetime.fromisoformat(sess['start_time'])
pretty_date = dt_obj.strftime("%a, %b %d • %H:%M") # Shorter date format
except:
pretty_date = "Unknown Date"
info_row = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
info_row.append(Gtk.Label(label=pretty_date, css_classes=["heading"]))
info_row.append(Gtk.Label(hexpand=True))
info_row.append(Gtk.Label(label=f"{sess['count']} cards", css_classes=["dim-label"]))
sess_inner.append(info_row)
# Stats Grid (Compact)
stats_grid = Gtk.Grid(column_spacing=10, row_spacing=5)
stats_grid.attach(Gtk.Label(label="Good", xalign=0), 0, 0, 1, 1)
bar_g = Gtk.LevelBar(min_value=0, max_value=sess['count'])
bar_g.set_value(sess['good']); bar_g.add_css_class("bar-green"); bar_g.set_hexpand(True)
stats_grid.attach(bar_g, 1, 0, 1, 1)
stats_grid.attach(Gtk.Label(label=str(sess['good'])), 2, 0, 1, 1)
stats_grid.attach(Gtk.Label(label="Hard", xalign=0), 0, 1, 1, 1)
bar_h = Gtk.LevelBar(min_value=0, max_value=sess['count'])
bar_h.set_value(sess['hard']); bar_h.add_css_class("bar-yellow"); bar_h.set_hexpand(True)
stats_grid.attach(bar_h, 1, 1, 1, 1)
stats_grid.attach(Gtk.Label(label=str(sess['hard'])), 2, 1, 1, 1)
stats_grid.attach(Gtk.Label(label="Miss", xalign=0), 0, 2, 1, 1)
bar_m = Gtk.LevelBar(min_value=0, max_value=sess['count'])
bar_m.set_value(sess['miss']); bar_m.add_css_class("bar-red"); bar_m.set_hexpand(True)
stats_grid.attach(bar_m, 1, 2, 1, 1)
stats_grid.attach(Gtk.Label(label=str(sess['miss'])), 2, 2, 1, 1)
sess_inner.append(stats_grid)
sess_box.append(sess_frame)
content_box.append(sess_box)
def group_into_sessions(self, raw_history):
if not raw_history: return []
legacy = []
modern = []
for r in raw_history:
if 'session_id' in r and r['session_id']:
modern.append(r)
else:
if 'timestamp' not in r and 'date' in r:
r['timestamp'] = r['date'] + "T12:00:00"
if 'timestamp' in r:
legacy.append(r)
sessions = []
modern_groups = {}
modern_order = []
for entry in modern:
sid = entry['session_id']
if sid not in modern_groups:
modern_groups[sid] = self.new_session_dict(datetime.fromisoformat(entry['timestamp']))
modern_groups[sid]['session_id'] = sid
modern_order.append(sid)
s = modern_groups[sid]
s['count'] += 1
ts = datetime.fromisoformat(entry['timestamp'])
st = datetime.fromisoformat(s['start_time'])
if ts < st: s['start_time'] = entry['timestamp']
r = entry['rating']
if r == 3: s['good'] += 1
elif r == 2: s['hard'] += 1
elif r == 1: s['miss'] += 1
for sid in modern_order:
sessions.append(modern_groups[sid])
if legacy:
legacy.sort(key=lambda x: x['timestamp'])
current_session = None
for entry in legacy:
ts = datetime.fromisoformat(entry['timestamp'])
if not current_session:
current_session = self.new_session_dict(ts)
else:
last_ts = current_session['last_ts_obj']
if (ts - last_ts) > timedelta(minutes=2):
sessions.append(current_session)
current_session = self.new_session_dict(ts)
current_session['count'] += 1
current_session['last_ts_obj'] = ts
r = entry['rating']
if r == 3: current_session['good'] += 1
elif r == 2: current_session['hard'] += 1
elif r == 1: current_session['miss'] += 1
if current_session: sessions.append(current_session)
sessions.sort(key=lambda s: s['start_time'])
return sessions
def new_session_dict(self, ts_obj):
return {
'start_time': ts_obj.isoformat(),
'last_ts_obj': ts_obj,
'count': 0, 'good': 0, 'hard': 0, 'miss': 0,
'session_id': None
}