-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluation.py
89 lines (64 loc) · 2.52 KB
/
evaluation.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
from datetime import datetime
def get_booking_date(booking: dict[str, str]) -> datetime | None:
try:
unix_date = int(booking['repetition-start'])
except:
print(f'❌ Could not find key `repetition-start` here: {booking}')
return None
return datetime.utcfromtimestamp(unix_date)
def evaluate_bookings_per_week(bookings: list[dict[str, str]]) -> dict[int, list[int]]:
print('💠 Evaluate bookings per week')
# year -> [count_calender_week_1, count_calender_week_2, ..., count_calender_week_53]
results: dict[int, list[int]] = {}
count_confirmed = 0
for booking in bookings:
if booking['status'] != 'confirmed':
continue
count_confirmed += 1
date = get_booking_date(booking)
if not date:
continue
year = date.year
calender_week = date.isocalendar().week
if year not in results:
results[year] = [0 for _ in range(53)] # 53 calender weeks
results[year][calender_week-1] += 1
print(f'{count_confirmed}/{len(bookings)} confirmed bookings')
return results
def evaluate_bookings_per_weekday(bookings: list[dict[str, str]]) -> dict[int, list[int]]:
print('💠 Evaluate bookings per weekday')
# year -> [count_monday, count_tuesday ..., count_sunday]
results: dict[int, list[int]] = {}
for booking in bookings:
if booking['status'] != 'confirmed':
continue
date = get_booking_date(booking)
if not date:
continue
year = date.year
weekday = date.isocalendar().weekday # Monday is 1, Sunday is 7
if year not in results:
results[year] = [0 for _ in range(7)] # 7 weekdays
results[year][weekday-1] += 1
return results
def evaluate_bookings_per_item(bookings: list[dict],
items: dict[int, str]) -> dict[int, dict[str, int]]:
print('💠 Evaluate bookings per item')
# year -> { 'item1': count_item1, 'item2': count_item_2, ... }
results: dict[int, dict[str, int]] = {}
for booking in bookings:
if booking['status'] != 'confirmed':
continue
date = get_booking_date(booking)
if not date:
continue
year = date.year
item = int(booking['item-id'])
item_name = items[item]
if year not in results:
results[year] = {}
try:
results[year][item_name] += 1
except KeyError:
results[year][item_name] = 0
return results