-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_analyzer.py
405 lines (328 loc) · 11.3 KB
/
log_analyzer.py
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
import re
import statistics
from dataclasses import dataclass
import numpy as np
log_path = "/mnt/synology/logs/thnr2.home.arpa/"
start_date = (2023, 1) # year, day of year
end_date = (2023, 365) # year, day of year
year_day_pairs = []
cur_year = start_date[0]
cur_day = start_date[1]
while (cur_year < end_date[0]) or (cur_year == end_date[0] and cur_day <= end_date[1]):
year_day_pairs.append((cur_year, cur_day))
cur_day += 1
if cur_day > 366:
cur_day = 1
cur_year += 1
@dataclass
class StoryProcessingEvent:
id: int = -1
start_time: int = -1
end_time: int = -1
processing_duration: int = -1
uncached_story: bool = False
cached_story: bool = False
freshened_story: bool = False
reused_story: bool = False
has_thumbnail: bool = False
@dataclass
class StoryTypeSession:
id: str
start_time: int = -1
end_time: int = -1
duration: int = -1
num_stories: int = 0
stories_per_second: float = 0.0
story_type: str = None
events = {}
sessions = {}
id_to_uid = {}
def create_incrementer(starting_number):
def inner():
nonlocal starting_number
while True:
yield starting_number
starting_number += 1
return inner()
uid_generator = create_incrementer(1)
cur_session_id = None
for each_year_day_pair in year_day_pairs:
year, day = each_year_day_pair
log_file = f"thnr-thnr-{year}-{day:0>3}.log"
print(f"{log_file=}")
# first check if the file exists
try:
with open(log_path + log_file, mode="r", encoding="utf-8") as file:
pass
except FileNotFoundError:
continue
with open(log_path + log_file, mode="r", encoding="utf-8") as file:
lines = file.readlines()
for line in lines:
# print(line.strip())
# check for an id in line
pattern = r"id (\d{8})"
match = re.search(pattern, line)
if match:
id = match.group(1)
else:
continue
try:
event_time = (
int(line[11:13]) * 3600 + int(line[14:16]) * 60 + int(line[17:19])
)
except Exception as exc:
print(f"{exc}: {line}")
continue
# 2023, 2024
if "no cached story found" in line:
uid = next(uid_generator)
events[uid] = StoryProcessingEvent(
id=id, start_time=event_time, uncached_story=True
)
id_to_uid[id] = uid
if cur_session_id:
sessions[cur_session_id].num_stories += 1
# 2023, 2024
elif "cached story found" in line:
uid = next(uid_generator)
events[uid] = StoryProcessingEvent(
id=id, start_time=event_time, cached_story=True
)
id_to_uid[id] = uid
if cur_session_id:
sessions[cur_session_id].num_stories += 1
# 2023
elif "og:image file has magic type" in line:
uid = id_to_uid[id]
if uid in events:
events[uid].has_thumbnail = True
# 2024
elif "will have a thumbnail" in line:
uid = id_to_uid[id]
if uid in events:
events[uid].has_thumbnail = True
# 2023
elif "pickling item for the first time" in line:
uid = id_to_uid[id]
if uid in events:
events[uid].end_time = event_time
events[uid].processing_duration = (
events[uid].end_time - events[uid].start_time
)
id_to_uid[id] = None
# 2024
elif "saving item to disk for the first time" in line:
uid = id_to_uid[id]
if uid in events:
events[uid].end_time = event_time
events[uid].processing_duration = (
events[uid].end_time - events[uid].start_time
)
id_to_uid[id] = None
# 2023, early 2024
elif "re-pickling freshened story" in line:
uid = id_to_uid[id]
if uid in events:
events[uid].end_time = event_time
events[uid].processing_duration = (
events[uid].end_time - events[uid].start_time
)
events[uid].freshened_story = True
id_to_uid[id] = None
# 2023
elif "re-pickling re-used cached story" in line:
uid = id_to_uid[id]
if uid in events:
events[uid].end_time = event_time
events[uid].processing_duration = (
events[uid].end_time - events[uid].start_time
)
events[uid].reused_story = True
id_to_uid[id] = None
# 2024
elif "re-using cached story" in line:
uid = id_to_uid[id]
if uid in events:
events[uid].reused_story = True
events[uid].cached_story = True
# 2024
elif "successfully freshened story" in line:
uid = id_to_uid[id]
if uid in events:
events[uid].freshened_story = True
events[uid].cached_story = True
# 2024
elif "successfully created story_card_html" in line:
uid = id_to_uid[id]
if uid in events:
events[uid].end_time = event_time
events[uid].processing_duration = (
events[uid].end_time - events[uid].start_time
)
id_to_uid[id] = None
else:
# 2023-01-13 00:16:28 CST [top] INFO supervisor(top) with unique id eadb8101687e4588fae5bae270d09145433f4c2a started at 2023-01-13T06:16:28Z
match = re.search(
r"supervisor\(([^\)]+)\) with unique id ([a-f0-9]{40})", line
)
if match:
story_type = str(match.group(1))
session_id = str(match.group(2))
if "started" in line:
sessions[session_id] = StoryTypeSession(
id=session_id, start_time=event_time, story_type=story_type
)
cur_session_id = session_id
elif "completed" in line:
sessions[session_id].end_time = event_time
sessions[session_id].duration = (
sessions[session_id].end_time
- sessions[session_id].start_time
+ 86400
) % 86400
sessions[session_id].stories_per_second = (
sessions[session_id].num_stories / sessions[session_id].duration
)
cur_session_id = None
continue
# 2024-03-01T03:57:24Z [new] INFO supervisor(new) with id 5f351bbe0781: completed in 00:03:22.451 at 2024-03-01T03:57:24Z
match = re.search(r"supervisor\(([^\)]+)\) with id ([a-f0-9]{12})", line)
if match:
story_type = str(match.group(1))
session_id = str(match.group(2))
if "started" in line:
sessions[session_id] = StoryTypeSession(
id=session_id, start_time=event_time, story_type=story_type
)
cur_session_id = session_id
elif "completed" in line:
sessions[session_id].end_time = event_time
sessions[session_id].duration = (
sessions[session_id].end_time
- sessions[session_id].start_time
+ 86400
) % 86400
sessions[session_id].stories_per_second = (
sessions[session_id].num_stories / sessions[session_id].duration
)
cur_session_id = None
continue
for _ in range(10):
print()
story_types = [
"active",
"best",
"classic",
"new",
"top",
]
rates = {}
for each in story_types:
rates[each] = []
for each_session in sessions.values():
rates[each_session.story_type].append(each_session.stories_per_second)
for each in rates.keys():
rates[each] = np.array(rates[each])
try:
mean_duration = statistics.mean(rates[each])
except statistics.StatisticsError:
mean_duration = "No mean found"
try:
median_duration = statistics.median(rates[each])
except statistics.StatisticsError:
median_duration = "No median found"
try:
mode_duration = statistics.mode(rates[each])
except statistics.StatisticsError:
mode_duration = "No unique mode found"
try:
first_quartile = np.percentile(rates[each], 25)
except Exception as exc:
first_quartile = f"Error: {exc}"
try:
third_quartile = np.percentile(rates[each], 75)
except Exception as exc:
third_quartile = f"Error: {exc}"
try:
min_val = min(rates[each])
max_val = max(rates[each])
except Exception as exc:
min_val = f"Error: {exc}"
max_val = f"Error: {exc}"
print(
f"""
Story Type: {each}
Events: {len(rates[each])}
Min: {min_val}
Max: {max_val}
Mean: {mean_duration}
Median: {median_duration}
Mode: {mode_duration}
First Quartile: {first_quartile}
Third Quartile: {third_quartile}
"""
)
for _ in range(10):
print()
processing_types = [
"freshened",
"reused",
"uncached_with_thumb",
"uncached_without_thumb",
]
durations = {}
for each in processing_types:
durations[each] = []
for e in events.values():
if e.processing_duration >= 0:
if e.uncached_story:
if e.has_thumbnail:
durations["uncached_with_thumb"].append(e.processing_duration)
else:
durations["uncached_without_thumb"].append(e.processing_duration)
elif e.freshened_story:
durations["freshened"].append(e.processing_duration)
elif e.reused_story:
durations["reused"].append(e.processing_duration)
for each in durations.keys():
durations[each] = np.array(durations[each])
try:
mean_duration = statistics.mean(durations[each])
except statistics.StatisticsError:
mean_duration = "No mean found"
try:
median_duration = statistics.median(durations[each])
except statistics.StatisticsError:
median_duration = "No median found"
try:
mode_duration = statistics.mode(durations[each])
except statistics.StatisticsError:
mode_duration = "No unique mode found"
try:
first_quartile = np.percentile(durations[each], 25)
except Exception as exc:
first_quartile = f"Error: {exc}"
try:
third_quartile = np.percentile(durations[each], 75)
except Exception as exc:
third_quartile = f"Error: {exc}"
try:
min_val = min(durations[each])
max_val = max(durations[each])
except Exception as exc:
min_val = f"Error: {exc}"
max_val = f"Error: {exc}"
print(
f"""
Processing Category: {each}
Events: {len(durations[each])}
Min: {min_val}
Max: {max_val}
Mean: {mean_duration}
Median: {median_duration}
Mode: {mode_duration}
First Quartile: {first_quartile}
Third Quartile: {third_quartile}
"""
)