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:
1 parent
54a7b36aae
commit
4566066f2b
10 files changed
+171
-1
No files matched your search
@@ -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()
|
||||
|
||||
@@ -820,6 +820,55 @@ async function restoreBulk(items, clearHideUndo = false) {
|
||||
}
|
||||
}
|
||||
|
||||
function renderActivity(data) {
|
||||
const byDate = new Map(data.days.map((day) => [day.date, day]));
|
||||
const year = Number(data.year);
|
||||
const start = new Date(Date.UTC(year, 0, 1));
|
||||
const end = new Date(Date.UTC(year + 1, 0, 1));
|
||||
const step = Math.max(1, Math.ceil(data.max_games / 4));
|
||||
const cells = [];
|
||||
const offset = (start.getUTCDay() + 6) % 7;
|
||||
for (let i = 0; i < offset; i++) {
|
||||
cells.push('<i class="activity-cell blank"></i>');
|
||||
}
|
||||
for (const day = new Date(start); day < end; day.setUTCDate(day.getUTCDate() + 1)) {
|
||||
const date = day.toISOString().slice(0, 10);
|
||||
const item = byDate.get(date);
|
||||
const games = item ? item.games : 0;
|
||||
const level = games ? Math.min(4, 1 + Math.floor((games - 1) / step)) : 0;
|
||||
const title = games
|
||||
? `${date}: игр ${games}, ${item.hours} ч`
|
||||
: `${date}: запусков нет`;
|
||||
cells.push(`<i class="activity-cell l${level}" title="${title}" aria-label="${title}"></i>`);
|
||||
}
|
||||
$("#activityHeatmap").innerHTML = cells.join("");
|
||||
$("#activitySummary").textContent = `${data.total_games} игр · ${data.total_hours} ч`;
|
||||
}
|
||||
|
||||
async function loadActivity(year = "") {
|
||||
const params = new URLSearchParams();
|
||||
if (year) params.set("year", year);
|
||||
try {
|
||||
const data = await api(`/api/activity${params.toString() ? `?${params}` : ""}`);
|
||||
const select = $("#activityYear");
|
||||
select.innerHTML = data.years.map((value) => `<option value="${value}">${value}</option>`).join("");
|
||||
if (!data.years.includes(data.year)) {
|
||||
select.insertAdjacentHTML("afterbegin", `<option value="${data.year}">${data.year}</option>`);
|
||||
}
|
||||
select.value = data.year;
|
||||
renderActivity(data);
|
||||
} catch (err) {
|
||||
toast(err.message);
|
||||
}
|
||||
}
|
||||
|
||||
$("#activityToggle").addEventListener("click", () => {
|
||||
const body = $("#activityBody");
|
||||
body.hidden = !body.hidden;
|
||||
if (!body.hidden) loadActivity($("#activityYear").value || "");
|
||||
});
|
||||
$("#activityYear").addEventListener("change", () => loadActivity($("#activityYear").value));
|
||||
|
||||
/* ---------- library ---------- */
|
||||
|
||||
function filterParams() {
|
||||
|
||||
@@ -205,6 +205,20 @@ try {
|
||||
<button id="bulkTag" class="ghost" type="button" title="Скрыть все игры с тегом">скрыть тег</button>
|
||||
<button id="bulkClear" class="link" type="button">снять выбор</button>
|
||||
</div>
|
||||
<div class="activity-panel">
|
||||
<button id="activityToggle" class="link" type="button">активность по времени</button>
|
||||
<div id="activityBody" class="activity-body" hidden>
|
||||
<div class="activity-head">
|
||||
<select id="activityYear" aria-label="Год активности"></select>
|
||||
<span class="muted" id="activitySummary"></span>
|
||||
</div>
|
||||
<div id="activityHeatmap" class="activity-heatmap" role="img"
|
||||
aria-label="Тепловая карта последних запусков по дням"></div>
|
||||
<div class="activity-legend muted">
|
||||
<span>меньше</span><i class="l1"></i><i class="l2"></i><i class="l3"></i><i class="l4"></i><span>больше</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div id="grid" class="grid"></div>
|
||||
<button id="more" class="more hidden">Показать ещё</button>
|
||||
</div>
|
||||
|
||||
@@ -444,6 +444,36 @@ summary:focus-visible,
|
||||
.bulk-bar select { width: auto; min-width: 130px; padding: 7px 9px; }
|
||||
.bulk-bar .ghost { flex: 0 0 auto; padding: 7px 10px; }
|
||||
|
||||
.activity-panel { display: flex; flex-direction: column; gap: 8px; margin-bottom: 18px; }
|
||||
.activity-body {
|
||||
padding: 14px;
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 14px;
|
||||
background: var(--panel);
|
||||
}
|
||||
.activity-head { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; margin-bottom: 10px; }
|
||||
.activity-head select { width: auto; min-width: 110px; }
|
||||
.activity-heatmap {
|
||||
display: grid;
|
||||
grid-auto-flow: column;
|
||||
grid-template-rows: repeat(7, 12px);
|
||||
gap: 3px;
|
||||
overflow-x: auto;
|
||||
padding-bottom: 4px;
|
||||
}
|
||||
.activity-cell { width: 12px; height: 12px; border-radius: 3px; background: var(--bg-2); }
|
||||
.activity-cell.blank { background: transparent; }
|
||||
.activity-cell.l1 { background: rgba(94, 234, 212, .22); }
|
||||
.activity-cell.l2 { background: rgba(94, 234, 212, .45); }
|
||||
.activity-cell.l3 { background: rgba(94, 234, 212, .7); }
|
||||
.activity-cell.l4 { background: rgba(94, 234, 212, .95); }
|
||||
.activity-legend { display: flex; align-items: center; gap: 4px; margin-top: 8px; font-size: 12px; }
|
||||
.activity-legend i { display: inline-block; width: 12px; height: 12px; border-radius: 3px; }
|
||||
.activity-legend .l1 { background: rgba(94, 234, 212, .22); }
|
||||
.activity-legend .l2 { background: rgba(94, 234, 212, .45); }
|
||||
.activity-legend .l3 { background: rgba(94, 234, 212, .7); }
|
||||
.activity-legend .l4 { background: rgba(94, 234, 212, .95); }
|
||||
|
||||
.select-box {
|
||||
position: absolute;
|
||||
right: 8px;
|
||||
|
||||
Reference in new issue
Block a user