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
|
from collections import Counter
from calendar import month_name
from datetime import date, timedelta
from fastapi import APIRouter, Depends, Request
from fastapi.templating import Jinja2Templates
from sqlalchemy.orm import Session
from database import get_db
from models import Film
from services.countries import (
ISO_NUMERIC_TO_COUNTRY_NAME,
country_name_to_iso_numeric,
split_country_names,
)
from services.film_people import split_credit_names
router = APIRouter(tags=["stats"])
templates = Jinja2Templates(directory="templates")
def _build_stats_payload(films: list[Film]) -> dict:
countries = Counter()
country_codes = Counter()
directors = Counter()
star_counts = Counter({0: 0, 1: 0, 2: 0, 3: 0})
months = Counter()
days = Counter()
watched_with = Counter()
for film in films:
country_names = split_country_names(film.country)
countries.update(country_names)
for country in country_names:
iso_numeric = country_name_to_iso_numeric(country)
if iso_numeric is not None:
country_codes[iso_numeric] += 1
directors.update(split_credit_names(film.director))
stars = film.stars if film.stars in {0, 1, 2, 3} else 0
star_counts[stars] += 1
if film.date_watched:
months[film.date_watched.strftime("%Y-%m")] += 1
days[film.date_watched.isoformat()] += 1
companions = split_credit_names(film.watched_with)
if companions:
watched_with.update(companions)
else:
watched_with["solo"] += 1
total_watched = len(films)
rewatched = sum(1 for film in films if film.rewatch or film.rewatch_count > 0)
today = date.today()
start_day = today - timedelta(days=364)
trailing_days = []
cursor = start_day
while cursor <= today:
trailing_days.append({"date": cursor.isoformat(), "count": days[cursor.isoformat()]})
cursor += timedelta(days=1)
return {
"scope": {
"shelf": "diary",
"requires_date_watched": True,
},
"total_watched": total_watched,
"films_per_country": [
{"country": country, "count": count}
for country, count in sorted(countries.items(), key=lambda item: (-item[1], item[0]))
],
"films_per_country_codes": [
{"code": code, "count": count}
for code, count in sorted(country_codes.items(), key=lambda item: (-item[1], item[0]))
],
"country_labels_by_code": {
str(code): ISO_NUMERIC_TO_COUNTRY_NAME.get(code, str(code)) for code in country_codes
},
"most_watched_directors": [
{"director": director, "count": count}
for director, count in sorted(directors.items(), key=lambda item: (-item[1], item[0]))
],
"star_distribution": [{"stars": stars, "count": star_counts[stars]} for stars in (0, 1, 2, 3)],
"films_per_month": [
{"month": month, "count": count}
for month, count in sorted(months.items())
],
"films_per_day": [
{"date": watched_date, "count": count}
for watched_date, count in sorted(days.items())
],
"films_per_day_365": trailing_days,
"rewatch_rate": {
"rewatched": rewatched,
"total_watched": total_watched,
"rate": round(rewatched / total_watched, 4) if total_watched else 0,
},
"watched_with_breakdown": [
{"watched_with": watched_with_value, "count": count}
for watched_with_value, count in sorted(watched_with.items(), key=lambda item: (-item[1], item[0]))
],
}
def _diary_films(db: Session) -> list[Film]:
return (
db.query(Film)
.filter(Film.shelf == "diary", Film.date_watched.is_not(None))
.all()
)
def _available_years(films: list[Film]) -> list[int]:
years = {film.date_watched.year for film in films if film.date_watched}
return sorted(years, reverse=True)
def _format_film_excerpt(notes: str | None, limit: int = 140) -> str | None:
if not notes:
return None
excerpt = " ".join(notes.split())
if len(excerpt) <= limit:
return excerpt
return excerpt[: limit - 1].rstrip() + "..."
def _film_highlight_payload(film: Film) -> dict:
return {
"id": film.id,
"title": film.title,
"poster_url": film.poster_url,
"director": film.director,
"year": film.year,
"date_watched": film.date_watched.isoformat() if film.date_watched else None,
"stars": film.stars,
"notes_excerpt": _format_film_excerpt(film.notes),
"runtime": film.runtime,
"country": film.country,
"language": film.language,
}
def _select_year(selected_year: int | None, available_years: list[int]) -> int | None:
if selected_year is not None:
return selected_year
if available_years:
return available_years[0]
return None
def _films_for_year(films: list[Film], year: int) -> list[Film]:
return [
film
for film in films
if film.date_watched and film.date_watched.year == year
]
def _year_review_payload(db: Session, year: int | None) -> dict:
diary_films = _diary_films(db)
available_years = _available_years(diary_films)
selected_year = _select_year(year, available_years)
if selected_year is None:
return {
"selected_year": date.today().year if year is None else year,
"available_years": [],
"total_watched": 0,
"average_stars": 0,
"most_watched_directors": [],
"star_distribution": [{"stars": stars, "count": 0} for stars in (0, 1, 2, 3)],
"films_per_month": [{"month": month_name[index], "count": 0} for index in range(1, 13)],
"rewatch_rate": {"rewatched": 0, "total_watched": 0, "rate": 0},
"watched_with_breakdown": [],
"top_director": None,
"top_month": None,
"highlight_films": {
"highest_rated": [],
"first_watch": None,
"last_watch": None,
"most_rewatched": None,
},
}
year_films = _films_for_year(diary_films, selected_year)
countries = Counter()
directors = Counter()
star_counts = Counter({0: 0, 1: 0, 2: 0, 3: 0})
months = Counter({month_index: 0 for month_index in range(1, 13)})
watched_with = Counter()
for film in year_films:
countries.update(split_country_names(film.country))
directors.update(split_credit_names(film.director))
stars = film.stars if film.stars in {0, 1, 2, 3} else 0
star_counts[stars] += 1
if film.date_watched:
months[film.date_watched.month] += 1
companions = split_credit_names(film.watched_with)
if companions:
watched_with.update(companions)
else:
watched_with["solo"] += 1
total_watched = len(year_films)
rewatched = sum(1 for film in year_films if film.rewatch or film.rewatch_count > 0)
average_stars = round(sum(film.stars for film in year_films) / total_watched, 1) if total_watched else 0
top_director = None
if directors:
top_director_name, top_director_count = sorted(directors.items(), key=lambda item: (-item[1], item[0]))[0]
top_director = {"director": top_director_name, "count": top_director_count}
top_month = None
if total_watched:
top_month_index, top_month_count = sorted(months.items(), key=lambda item: (-item[1], item[0]))[0]
top_month = {
"month": month_name[top_month_index],
"count": top_month_count,
}
year_films_sorted = sorted(
year_films,
key=lambda film: (
-(film.stars or 0),
-(film.date_watched.toordinal()) if film.date_watched else 0,
film.title.casefold(),
film.id,
),
)
highest_rated = [_film_highlight_payload(film) for film in year_films_sorted[:4]]
first_watch = None
last_watch = None
if year_films:
first_watch_film = min(
year_films,
key=lambda film: (film.date_watched or date.max, film.id),
)
last_watch_film = max(
year_films,
key=lambda film: (film.date_watched or date.min, film.id),
)
first_watch = _film_highlight_payload(first_watch_film)
last_watch = _film_highlight_payload(last_watch_film)
most_rewatched = None
rewatched_candidates = [
film
for film in year_films
if film.rewatch_count > 0 or film.rewatch
]
if rewatched_candidates:
rewatched_candidates.sort(
key=lambda film: (
-film.rewatch_count,
-(film.date_watched.toordinal()) if film.date_watched else 0,
film.title.casefold(),
film.id,
)
)
most_rewatched = _film_highlight_payload(rewatched_candidates[0])
return {
"selected_year": selected_year,
"available_years": available_years,
"total_watched": total_watched,
"average_stars": average_stars,
"films_per_month": [
{"month": month_name[index], "count": months[index]}
for index in range(1, 13)
],
"star_distribution": [{"stars": stars, "count": star_counts[stars]} for stars in (0, 1, 2, 3)],
"most_watched_directors": [
{"director": director, "count": count}
for director, count in sorted(directors.items(), key=lambda item: (-item[1], item[0]))
],
"watched_with_breakdown": [
{"watched_with": watched_with_value, "count": count}
for watched_with_value, count in sorted(watched_with.items(), key=lambda item: (-item[1], item[0]))
],
"rewatch_rate": {
"rewatched": rewatched,
"total_watched": total_watched,
"rate": round(rewatched / total_watched, 4) if total_watched else 0,
},
"top_director": top_director,
"top_month": top_month,
"highlight_films": {
"highest_rated": highest_rated,
"first_watch": first_watch,
"last_watch": last_watch,
"most_rewatched": most_rewatched,
},
}
@router.get("/stats")
def stats_page(request: Request):
return templates.TemplateResponse(
request=request,
name="stats.html",
context={"request": request, "active_page": "stats"},
)
@router.get("/stats/data")
def stats_data(db: Session = Depends(get_db)):
return _build_stats_payload(_diary_films(db))
@router.get("/stats/year-in-review")
def year_in_review_page(
request: Request,
year: int | None = None,
db: Session = Depends(get_db),
):
review = _year_review_payload(db, year)
return templates.TemplateResponse(
request=request,
name="year_review.html",
context={"request": request, "active_page": "stats", **review},
)
@router.get("/stats/year-in-review/data")
def year_in_review_data(
year: int | None = None,
db: Session = Depends(get_db),
):
return _year_review_payload(db, year)
|