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
+93 -1
View File
@@ -22,6 +22,7 @@ from api.monitor import service
from api.monitor.models import (
MODE_CREATOR,
MonitorNote,
MonitorNoteMetric,
MonitorNoteTag,
MonitorTag,
MonitorTask,
@@ -109,6 +110,30 @@ async def _attach(platform: str, note_id: str, tag_id: int) -> None:
)
async def _metric(
task_id: int,
note_id: str,
run_id: int,
liked: int | None = None,
comment: int | None = None,
collected: int | None = None,
share: int | None = None,
) -> None:
async with monitor_db.get_session() as session:
session.add(
MonitorNoteMetric(
task_id=task_id,
note_id=note_id,
run_id=run_id,
captured_at=run_id,
liked_count=liked,
comment_count=comment,
collected_count=collected,
share_count=share,
)
)
@pytest.mark.asyncio
async def test_same_day_notes_collapse_with_tags_union(db):
task_id = await _task()
@@ -185,12 +210,76 @@ async def test_no_tasks_returns_an_empty_shape(db):
assert data == {
"platform": "dy",
"days": 7,
"task_id": None,
"dates": [],
"creators": [],
"cells": {},
}
@pytest.mark.asyncio
async def test_cell_sums_the_latest_metric_of_each_note(db):
task_id = await _task()
now = get_current_timestamp() - 1000
await _note(task_id, "n1", "博主A", now)
await _note(task_id, "n2", "博主A", now)
# 同一篇有两轮快照,只取 run_id 最大的那一轮
await _metric(task_id, "n1", run_id=1, liked=10, comment=1, collected=2, share=3)
await _metric(task_id, "n1", run_id=2, liked=20, comment=2, collected=4, share=5)
await _metric(task_id, "n2", run_id=1, liked=5, comment=None, collected=1, share=0)
async with monitor_db.get_session() as session:
data = await service.daily_tag_matrix(session, "dy", days=7)
cell = data["cells"][_day(now)]["hash-a"]
assert cell["count"] == 2
assert cell["likes"] == 25
assert cell["comments"] == 2 # None 不参与求和
assert cell["collected"] == 5
assert cell["shares"] == 5
@pytest.mark.asyncio
async def test_missing_metrics_are_none_not_zero(db):
task_id = await _task()
now = get_current_timestamp() - 1000
await _note(task_id, "n1", "博主A", now)
async with monitor_db.get_session() as session:
data = await service.daily_tag_matrix(session, "dy", days=7)
cell = data["cells"][_day(now)]["hash-a"]
# 「不知道」和「是 0」要分开 —— 前者在提示里显示成「—」
assert cell["likes"] is None
assert cell["comments"] is None
@pytest.mark.asyncio
async def test_task_filter_scopes_the_matrix(db):
first = await _task(name="任务一")
second = await _task(name="任务二")
now = get_current_timestamp() - 1000
await _note(first, "a", "博主A", now, creator_hash="hash-a")
await _note(second, "b", "博主B", now, creator_hash="hash-b")
async with monitor_db.get_session() as session:
data = await service.daily_tag_matrix(session, "dy", days=7, task_id=second)
assert data["task_id"] == second
assert [c["name"] for c in data["creators"]] == ["博主B"]
@pytest.mark.asyncio
async def test_task_from_another_platform_is_rejected(db):
xhs_task = await _task(platform="xhs")
async with monitor_db.get_session() as session:
data = await service.daily_tag_matrix(session, "dy", days=7, task_id=xhs_task)
assert data["dates"] == []
@pytest.mark.asyncio
async def test_endpoint_returns_the_long_shape(client):
task_id = await _task()
@@ -205,4 +294,7 @@ async def test_endpoint_returns_the_long_shape(client):
body = response.json()
assert body["dates"] == [_day(now)]
assert body["creators"][0]["name"] == "博主A"
assert body["cells"][_day(now)]["hash-a"] == {"count": 1, "tags": ["大字报"]}
cell = body["cells"][_day(now)]["hash-a"]
assert cell["count"] == 1
assert cell["tags"] == ["大字报"]
assert "likes" in cell