diff --git a/api/monitor/service.py b/api/monitor/service.py index d818e9a..5362dd2 100644 --- a/api/monitor/service.py +++ b/api/monitor/service.py @@ -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": {"": {"count": 2, "tags": ["大字报", "养号"]}}}} + "cells": {"2026-10-21": {"": { + "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, diff --git a/api/reports/matrix.html b/api/reports/matrix.html index 8357590..c86ff02 100644 --- a/api/reports/matrix.html +++ b/api/reports/matrix.html @@ -18,9 +18,7 @@ --text: #e6f1ff; --muted: #7b8ba3; --cyan: #22d3ee; - --green: #34d399; --pink: #f472b6; - --orange: #fb923c; } * { box-sizing: border-box; } body { @@ -59,6 +57,7 @@ cursor: pointer; text-decoration: none; } + select { max-width: 200px; } select:hover, button:hover, a.btn:hover { border-color: var(--cyan); } #status { color: var(--muted); font-size: 12px; } #status.error { color: var(--pink); } @@ -99,7 +98,6 @@ text-align: left; white-space: nowrap; font-weight: 500; - font-variant-numeric: tabular-nums; } tbody td { border-right: 1px solid var(--line); @@ -107,7 +105,7 @@ padding: 5px 8px; white-space: nowrap; vertical-align: top; - min-width: 88px; + min-width: 92px; } tbody tr:hover th, tbody tr:hover td { background: #131c28; } @@ -157,6 +155,29 @@ color: var(--muted); } #login a { color: var(--cyan); } + + /* 悬停提示。跟着格子走,但不吃鼠标事件 —— 否则它一出现就会盖住格子、 + 触发 mouseleave,然后自己消失,来回闪。 */ + #tip { + display: none; + position: fixed; + z-index: 100; + pointer-events: none; + max-width: 320px; + padding: 8px 10px; + border: 1px solid var(--line); + border-radius: 8px; + background: #0f1620; + box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5); + font-size: 11px; + line-height: 1.7; + color: var(--text); + } + #tip .tip-head { color: var(--cyan); font-weight: 600; margin-bottom: 2px; } + #tip .tip-metrics { font-variant-numeric: tabular-nums; } + #tip .tip-metrics span { margin-right: 10px; white-space: nowrap; } + #tip .tip-metrics b { color: var(--muted); font-weight: 400; } + #tip .tip-tags { color: var(--muted); } @@ -168,6 +189,11 @@ +