feat: add activity heatmap by day

Aggregate last-played dates via GET /api/activity and render a calendar heatmap with a year selector in the library view.
This commit is contained in:
strsv committed 2026-11-07 00:55:11 +03:00
1 parent 54a7b36aae
commit 4566066f2b
10 files changed
+171 -1

No files matched your search

+38
View File
@@ -1005,6 +1005,8 @@ class Handler(BaseHTTPRequestHandler):
self._moods()
elif path == "/api/compare":
self._compare(qs)
elif path == "/api/activity":
self._activity(qs)
elif path == "/api/moods/export":
self._export_moods(qs)
elif path == "/api/descriptions":
@@ -1328,6 +1330,42 @@ class Handler(BaseHTTPRequestHandler):
finally:
con.close()
def _activity(self, qs):
year = (qs.get("year", [""])[0] or "").strip()
if year and (len(year) != 4 or not year.isdigit()):
self._error(400, "invalid year")
return
con = connect()
try:
years = [r["year"] for r in con.execute(
"""SELECT DISTINCT substr(last_played, 1, 4) AS year
FROM game_playtime
WHERE last_played IS NOT NULL AND last_played <> ''
AND length(last_played) >= 4
ORDER BY year DESC""")]
if not year:
year = years[0] if years else str(datetime.now(timezone.utc).year)
rows = con.execute(
"""SELECT substr(p.last_played, 1, 10) AS day,
COUNT(*) AS games,
COALESCE(SUM(p.minutes), 0) / 60.0 AS hours
FROM game_playtime p JOIN games g ON g.appid = p.appid
WHERE LOWER(g.type) = 'game'
AND substr(p.last_played, 1, 4) = ?
GROUP BY day ORDER BY day""", (year,)).fetchall()
days = [{"date": r["day"], "games": r["games"], "hours": round(r["hours"], 1)}
for r in rows]
self._json({
"year": year,
"years": years,
"days": days,
"total_games": sum(day["games"] for day in days),
"total_hours": round(sum(day["hours"] for day in days), 1),
"max_games": max((day["games"] for day in days), default=0),
})
finally:
con.close()
def _compare(self, qs):
a = (qs.get("a", [""])[0] or "").strip()
b = (qs.get("b", [""])[0] or "").strip()