feat(report): 格子悬停显示当天点赞/评论/收藏/分享;报表可按任务过滤
Deploy VitePress site to Pages / build (push) Canceled after 0s
Deploy VitePress site to Pages / Deploy (push) Canceled after 0s

两件事:

1. **格子悬停提示**(matrix.html)
   每个有数据的格子挂一个自定义浮层,显示「日期 · 博主 / 条数 / 点赞 / 评论 /
   收藏 / 分享 / 标签」。浮层 `pointer-events: none`,否则它会盖住格子触发
   mouseleave,自己闪个不停;表格滚动时主动收起。内容用 DOM 拼而不是
   innerHTML —— 昵称来自平台,不能当 HTML 解释。

2. **任务过滤**(/api/monitor/matrix 增加 task_id)
   平台下拉旁边加「任务」下拉,数据来自现有的 /api/monitor/tasks。
   `task_id` 会先校验平台归属,传错平台宁可返回空,也不给出别的平台的数。
   选择会写进地址栏,可以直接收藏某一组条件。

指标口径:每篇作品取**最近一轮**快照(run_id 最大)再按格子求和。未知是 None
不是 0 —— 提示里显示成「—」,避免把「没读到」说成「没有互动」。

测试:10 条(新增指标求和取最新一轮、未知为 None、任务过滤、跨平台任务被拒)。
This commit is contained in:
butubb
2026-10-22 14:11:38 +08:00
parent c095530879
commit ffafd90073
4 changed files with 340 additions and 35 deletions
+75 -6
View File
@@ -1452,7 +1452,10 @@ async def mark_events_read(session: AsyncSession, task_id: Optional[int] = None)
async def daily_tag_matrix(
session: AsyncSession, platform: str, days: int
session: AsyncSession,
platform: str,
days: int,
task_id: Optional[int] = None,
) -> Dict[str, Any]:
"""日期 × 博主 → 当天发出去的作品标签。给 ``/report/matrix`` 用。
@@ -1465,12 +1468,30 @@ async def daily_tag_matrix(
{"dates": ["2026-10-21", ...], # 新到旧
"creators": [{"hash": ..., "name": ...}], # 按昵称排序
"cells": {"2026-10-21": {"<hash>": {"count": 2, "tags": ["大字报", "养号"]}}}}
"cells": {"2026-10-21": {"<hash>": {
"count": 2, "tags": ["大字报", "养号"],
"likes": 120, "comments": 8, "collected": 30, "shares": 4,
}}}}
``likes/comments/collected/shares`` 是当天这些作品**最近一轮**指标的和;
指标未知时是 ``None``(不是 0),前端显示成「—」。``task_id`` 非空时只统计那一个任务。
"""
task_ids = await platform_task_ids(session, platform)
if task_id is not None:
# 只看一个任务。顺带把平台也校验一遍 —— 传错平台时宁可空,也别给出别的平台的数。
task_ids = list(
await session.scalars(
select(MonitorTask.id).where(
MonitorTask.id == task_id, MonitorTask.platform == platform
)
)
)
else:
task_ids = await platform_task_ids(session, platform)
empty = {
"platform": platform,
"days": days,
"task_id": task_id,
"dates": [],
"creators": [],
"cells": {},
@@ -1482,6 +1503,7 @@ async def daily_tag_matrix(
notes = (
await session.execute(
select(
MonitorNote.task_id,
MonitorNote.note_id,
MonitorNote.creator_hash,
MonitorNote.creator_name,
@@ -1498,8 +1520,9 @@ async def daily_tag_matrix(
if not notes:
return empty
# 标签一次取完,按 note_id 归组;顺序跟词表里的 sort_order 一致。
note_ids = {row.note_id for row in notes}
# 标签一次取完,按 note_id 归组;顺序跟词表里的 sort_order 一致。
tag_rows = (
await session.execute(
select(MonitorNoteTag.note_id, MonitorTag.name)
@@ -1515,10 +1538,35 @@ async def daily_tag_matrix(
for note_id, name in tag_rows:
tags_by_note.setdefault(note_id, []).append(name)
# 指标:每篇作品取**最近一轮**(run_id 最大)。没有指标行的作品不参与求和 ——
# 未知和 0 是两回事,格子的提示里会显示成「—」而不是「0」。
metric_rows = (
await session.execute(
select(
MonitorNoteMetric.task_id,
MonitorNoteMetric.note_id,
MonitorNoteMetric.run_id,
MonitorNoteMetric.liked_count,
MonitorNoteMetric.comment_count,
MonitorNoteMetric.collected_count,
MonitorNoteMetric.share_count,
).where(
MonitorNoteMetric.task_id.in_(task_ids),
MonitorNoteMetric.note_id.in_(note_ids),
)
)
).all()
latest_metric: Dict[tuple, Any] = {}
for metric in metric_rows:
key = (metric.task_id, metric.note_id)
current = latest_metric.get(key)
if current is None or metric.run_id > current.run_id:
latest_metric[key] = metric
cells: Dict[str, Dict[str, Dict[str, Any]]] = {}
names: Dict[str, str] = {}
# 同一个作品可能挂在多个任务下(monitor_note 的唯一键是 (task_id, note_id)),
# 不按 note_id 去重会把计数翻倍。
# 不按 note_id 去重会把计数和指标都翻倍。
seen: set = set()
for row in notes:
@@ -1532,13 +1580,33 @@ async def daily_tag_matrix(
names.setdefault(creator_key, row.creator_name or "")
bucket = cells.setdefault(day, {}).setdefault(
creator_key, {"count": 0, "tags": []}
creator_key,
{
"count": 0,
"tags": [],
"likes": None,
"comments": None,
"collected": None,
"shares": None,
},
)
bucket["count"] += 1
for tag in tags_by_note.get(row.note_id, []):
if tag not in bucket["tags"]:
bucket["tags"].append(tag)
metric = latest_metric.get((row.task_id, row.note_id))
if metric is not None:
for column, key in (
("liked_count", "likes"),
("comment_count", "comments"),
("collected_count", "collected"),
("share_count", "shares"),
):
value = getattr(metric, column)
if value is not None:
bucket[key] = (bucket[key] or 0) + value
creators = [
{"hash": key, "name": name or f"未知({key[:6]})"}
for key, name in names.items()
@@ -1548,6 +1616,7 @@ async def daily_tag_matrix(
return {
"platform": platform,
"days": days,
"task_id": task_id,
"dates": sorted(cells, reverse=True),
"creators": creators,
"cells": cells,