From 809692a957988a831beeb51192a58ecdae90f582 Mon Sep 17 00:00:00 2001 From: butubb <1422726308@qq.com> Date: Sun, 25 Oct 2026 07:49:09 +0800 Subject: [PATCH] =?UTF-8?q?feat(matrix):=20=E6=82=AC=E5=81=9C=E9=87=8C?= =?UTF-8?q?=E7=9A=84=E6=AF=8F=E6=9D=A1=E8=A7=86=E9=A2=91=E6=98=BE=E7=A4=BA?= =?UTF-8?q?=E8=87=AA=E5=B7=B1=E7=9A=84=E8=B5=9E/=E8=AF=84/=E8=97=8F/?= =?UTF-8?q?=E8=BD=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 一格合并了多条时,并集只能说明「这天整体怎么样」,哪条是爆款要看每条自己的 数字。后端在 details 里带上各自的最近一轮指标,提示里缩进显示在该条标题下面。 --- api/monitor/service.py | 15 ++++++++++++--- api/reports/matrix.html | 30 ++++++++++++++++++++++++++---- tests/test_monitor_matrix.py | 23 +++++++++++++++++++---- 3 files changed, 57 insertions(+), 11 deletions(-) diff --git a/api/monitor/service.py b/api/monitor/service.py index 0098b40..92f1c7e 100644 --- a/api/monitor/service.py +++ b/api/monitor/service.py @@ -1549,8 +1549,11 @@ async def daily_tag_matrix( "cells": {"2026-10-21": {"": { "count": 2, "tags": ["大字报", "养号"], "likes": 120, "comments": 8, "collected": 30, "shares": 4, - "details": [{"title": "…", "tags": ["大字报"]}, - {"title": "…", "tags": ["养号"]}], # 按发布时间正序 + "details": [{"title": "…", "tags": ["大字报"], + "likes": 80, "comments": 5, "collected": 20, "shares": 4}, + {"title": "…", "tags": ["养号"], + "likes": 40, "comments": 3, "collected": 10, "shares": 0}], + # 按发布时间正序 }}}} ``likes/comments/collected/shares`` 是当天这些作品**最近一轮**指标的和; @@ -1727,17 +1730,23 @@ async def daily_tag_matrix( }, ) bucket["count"] += 1 + metric = latest_metric.get((row.task_id, row.note_id)) + # 逐条明细带**各自的**指标。一格合并了多条时,并集只说明「这天整体怎么样」, + # 哪条是爆款要看每条自己的数字 —— 悬停提示里直接看得到。 bucket["details"].append( { "title": row.title or row.note_id, "tags": list(tags_by_note.get(row.note_id, [])), + "likes": metric.liked_count if metric is not None else None, + "comments": metric.comment_count if metric is not None else None, + "collected": metric.collected_count if metric is not None else None, + "shares": metric.share_count if metric is not None else None, } ) 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"), diff --git a/api/reports/matrix.html b/api/reports/matrix.html index 4a9ed4b..48248f5 100644 --- a/api/reports/matrix.html +++ b/api/reports/matrix.html @@ -236,9 +236,16 @@ border-top: 1px solid var(--line); white-space: normal; } - #tip .tip-notes div { line-height: 1.55; } + #tip .tip-note { margin-bottom: 4px; line-height: 1.55; } #tip .tip-note-title { color: var(--text); } - #tip .tip-note-tags { color: var(--pink); margin-left: 6px; } + /* 每条作品自己的指标。缩进到标题文字下面,一眼对得上是哪条。 */ + #tip .tip-note-meta { + padding-left: 14px; + color: var(--muted); + font-variant-numeric: tabular-nums; + } + #tip .tip-note-metrics { margin-right: 8px; } + #tip .tip-note-tags { color: var(--pink); } #tip .tip-note-more { color: var(--muted); } @@ -415,15 +422,30 @@ for (let index = 0; index < Math.min(details.length, limit); index += 1) { const item = details[index]; const row = document.createElement('div'); - const title = document.createElement('span'); + row.className = 'tip-note'; + + const title = document.createElement('div'); title.className = 'tip-note-title'; title.textContent = index + 1 + '. ' + item.title; row.appendChild(title); + + const meta = document.createElement('div'); + meta.className = 'tip-note-meta'; + const metrics = document.createElement('span'); + metrics.className = 'tip-note-metrics'; + metrics.textContent = + '赞 ' + compact(item.likes) + + ' · 评 ' + compact(item.comments) + + ' · 藏 ' + compact(item.collected) + + ' · 转 ' + compact(item.shares); + meta.appendChild(metrics); const noteTags = document.createElement('span'); noteTags.className = 'tip-note-tags'; noteTags.textContent = item.tags && item.tags.length ? item.tags.join(' / ') : '无标签'; - row.appendChild(noteTags); + meta.appendChild(noteTags); + row.appendChild(meta); + list.appendChild(row); } if (details.length > limit) { diff --git a/tests/test_monitor_matrix.py b/tests/test_monitor_matrix.py index af709dd..40c0ee2 100644 --- a/tests/test_monitor_matrix.py +++ b/tests/test_monitor_matrix.py @@ -76,14 +76,19 @@ async def _task(platform: str = "dy", name: str = "矩阵测试") -> int: async def _note( - task_id: int, note_id: str, name: str, published_at: int, creator_hash: str = "hash-a" + task_id: int, + note_id: str, + name: str, + published_at: int, + creator_hash: str = "hash-a", + title: str = "t", ) -> None: async with monitor_db.get_session() as session: session.add( MonitorNote( task_id=task_id, note_id=note_id, - title="t", + title=title, note_url="", cover="", creator_hash=creator_hash, @@ -360,8 +365,8 @@ async def test_empty_stat_falls_back_to_the_note_name(db): 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) + await _note(task_id, "n1", "博主A", now, title="作品一") + await _note(task_id, "n2", "博主A", now, title="作品二") # 同一篇有两轮快照,只取 run_id 最大的那一轮 await _metric(task_id, "n1", run_id=1, liked=10, comment=1, collected=2, share=3) @@ -378,6 +383,16 @@ async def test_cell_sums_the_latest_metric_of_each_note(db): assert cell["collected"] == 5 assert cell["shares"] == 5 + # 逐条明细带**各自的**指标,不是格子的合计 —— 悬停时看的就是这个。 + by_title = {item["title"]: item for item in cell["details"]} + assert by_title["作品一"]["likes"] == 20 # 取最近一轮 + assert by_title["作品一"]["comments"] == 2 + assert by_title["作品一"]["collected"] == 4 + assert by_title["作品一"]["shares"] == 5 + assert by_title["作品二"]["likes"] == 5 + assert by_title["作品二"]["comments"] is None # 未知不是 0 + assert by_title["作品二"]["shares"] == 0 + @pytest.mark.asyncio async def test_missing_metrics_are_none_not_zero(db):