diff --git a/api/monitor/service.py b/api/monitor/service.py index 4552ed1..0098b40 100644 --- a/api/monitor/service.py +++ b/api/monitor/service.py @@ -1663,6 +1663,7 @@ async def daily_tag_matrix( MonitorCreatorStat.creator_hash, MonitorCreatorStat.nickname, MonitorCreatorStat.captured_at, + MonitorCreatorStat.fans, ).where( MonitorCreatorStat.task_id.in_(task_ids), MonitorCreatorStat.creator_hash != "", @@ -1670,7 +1671,14 @@ async def daily_tag_matrix( ) ).all() latest_nickname: Dict[str, tuple] = {} + # 粉丝数是给博主列显示用的「最近一次已知值」。某一轮没解析出来(None)不让它把 + # 之前的好数字盖掉 —— 一个号有几千粉丝,不会因为这一轮没读到就变成不知道。 + latest_fans: Dict[str, tuple] = {} for stat in stat_rows: + if stat.fans is not None: + current_fans = latest_fans.get(stat.creator_hash) + if current_fans is None or stat.captured_at > current_fans[1]: + latest_fans[stat.creator_hash] = (stat.fans, stat.captured_at) # **空昵称不算名字。** 资料接口偶尔会回一个空对象(昵称、sec_uid、粉丝、 # 作品数全空),如果把这一轮当最新快照,就会用一个空字符串把之前的好名字 # 盖掉,界面上变成「未知(hash)」。实测线上有两个号就是这么丢的名字。 @@ -1749,7 +1757,16 @@ async def daily_tag_matrix( # 作品是按发布时间倒序取回来的,明细按时间正序更好读(1、2、3…)。 bucket["details"].reverse() - creators = [{"hash": hash_, "name": name} for hash_, name in known.items()] + creators = [ + { + "hash": hash_, + "name": name, + # 没有账号快照的博主(作品模式刚导入、还没跑过一轮)就是 None, + # 前端据此不画那个数字,而不是显示 0。 + "fans": latest_fans.get(hash_, (None, 0))[0], + } + for hash_, name in known.items() + ] creators.sort(key=lambda item: item["name"]) # 账号池状态只有抖音那套池子有;取不到就是空串,前端不加括号。 diff --git a/api/reports/matrix.html b/api/reports/matrix.html index a61c0ee..cf3e2de 100644 --- a/api/reports/matrix.html +++ b/api/reports/matrix.html @@ -136,6 +136,20 @@ .untagged { color: var(--muted); font-size: 11px; } .empty { color: #2a3646; } + /* 博主名字左边的粉丝数。压暗一点:它是参照信息,不该和名字抢视线。 */ + .fans { + display: inline-block; + min-width: 36px; + margin-right: 6px; + padding: 0 4px; + border-radius: 4px; + background: rgba(34, 211, 238, 0.08); + color: var(--muted); + font-size: 11px; + font-variant-numeric: tabular-nums; + text-align: right; + } + /* 条件格式命中的格子。用 inset 描边而不是 box-shadow 扩散:不改变布局, 也不会触发整行重排。 */ tbody td.hit { @@ -245,6 +259,7 @@
+ 名字左边 = 粉丝数 N = 当天发出的作品条数 无标签 = 发了但没打标签 空白 = 当天没发作品 @@ -462,8 +477,20 @@ const label = creator.account_status ? creator.name + '(' + creator.account_status + ')' : creator.name; - th.textContent = label; - th.title = label; + // 粉丝数放在**名字左边**,一眼能看出这个号多大。没有快照的博主 + // (作品模式刚导入还没跑过一轮)就不画 —— 不写 0,那会被读成「没有粉丝」。 + const fansKnown = creator.fans !== null && creator.fans !== undefined; + if (fansKnown) { + const fans = document.createElement('span'); + fans.className = 'fans'; + fans.textContent = fmt(creator.fans); + fans.title = '粉丝数'; + th.appendChild(fans); + } + const nameSpan = document.createElement('span'); + nameSpan.textContent = label; + th.appendChild(nameSpan); + th.title = (fansKnown ? fmt(creator.fans) + ' 粉丝 · ' : '') + label; tr.appendChild(th); for (const day of data.dates) { const rowCells = data.cells[day] || {}; diff --git a/tests/test_monitor_matrix.py b/tests/test_monitor_matrix.py index c30ee69..af709dd 100644 --- a/tests/test_monitor_matrix.py +++ b/tests/test_monitor_matrix.py @@ -256,6 +256,7 @@ async def _stat( nickname: str, captured_at: int | None = None, run_id: int = 1, + fans: int | None = None, ) -> None: async with monitor_db.get_session() as session: session.add( @@ -266,6 +267,7 @@ async def _stat( nickname=nickname, creator_id="", works_count=0, + fans=fans, captured_at=captured_at or get_current_timestamp(), ) ) @@ -289,6 +291,40 @@ async def test_creators_without_notes_still_get_a_row(db): assert len(data["dates"]) == 7 +@pytest.mark.asyncio +async def test_the_creator_row_carries_the_latest_fan_count(db): + """博主列左边要显示的粉丝数 —— 取最近一次**读到了数字**的快照。 + + 最新一轮如果是 None(没解析出来),不能让几千粉丝变成一个空白 —— + 「这一轮没读到」不等于「这个号没有粉丝」。 + """ + task_id = await _task() + now = get_current_timestamp() + await _stat(task_id, "hash-a", "博主A", captured_at=now - 120_000, fans=1000, run_id=1) + await _stat(task_id, "hash-a", "博主A", captured_at=now - 60_000, fans=1200, run_id=2) + await _stat(task_id, "hash-a", "博主A", captured_at=now, fans=None, run_id=3) + + async with monitor_db.get_session() as session: + data = await service.daily_tag_matrix(session, "dy", days=7) + + assert len(data["creators"]) == 1 + assert data["creators"][0]["name"] == "博主A" + assert data["creators"][0]["fans"] == 1200 + + +@pytest.mark.asyncio +async def test_a_creator_without_a_snapshot_has_no_fan_count(db): + """作品模式刚导入、还没跑过一轮:粉丝数留 None,界面不画那个数字(不是 0)。""" + task_id = await _task() + now = get_current_timestamp() + await _note(task_id, "n1", "只有作品", now - 1000) + + async with monitor_db.get_session() as session: + data = await service.daily_tag_matrix(session, "dy", days=7) + + assert data["creators"][0]["fans"] is None + + @pytest.mark.asyncio async def test_degraded_stat_snapshot_does_not_hide_the_name(db): """资料接口偶尔回空对象(昵称/粉丝/作品数全空)。