From 4a15c1485c817dcb8f3fba912f3abeb4181c6912 Mon Sep 17 00:00:00 2001 From: butubb Date: Thu, 22 Oct 2026 14:28:33 +0800 Subject: [PATCH] =?UTF-8?q?fix(report):=20=E7=A9=BA=E6=98=B5=E7=A7=B0?= =?UTF-8?q?=E7=9A=84=E5=BF=AB=E7=85=A7=E4=B8=8D=E5=86=8D=E6=8A=8A=E5=A5=BD?= =?UTF-8?q?=E5=90=8D=E5=AD=97=E7=9B=96=E6=88=90=E3=80=8C=E6=9C=AA=E7=9F=A5?= =?UTF-8?q?=EF=BC=88hash=EF=BC=89=E3=80=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 抖音资料接口偶尔会回一个空对象:nickname、sec_uid、fans、aweme_count 全空 (实测 2026-10-22 12:49 那一轮有两个号中招)。报表原先取「最新一条快照的昵称」, 空字符串把之前的好名字盖掉了,界面上就变成「未知(c873cb)」。 改成:**空昵称不算名字**,取「最近一条有名字的快照」;没有再退回作品里的 creator_name;都没有才显示「未知(hash 前 6 位)」。 注意这只修了展示。根因是采集端的资料读取会写进这种残缺快照 —— 它同时会让 粉丝数/作品数的曲线出现假的 0。那条要单独改 douyin_api.author_profile 的判空。 测试 14 条(新增:坏快照不遮盖旧名字、只有坏快照时退回作品里的名字)。 --- api/monitor/service.py | 20 ++++++++++++----- tests/test_monitor_matrix.py | 43 +++++++++++++++++++++++++++++++++--- 2 files changed, 54 insertions(+), 9 deletions(-) diff --git a/api/monitor/service.py b/api/monitor/service.py index 6ca911e..4a5c6e7 100644 --- a/api/monitor/service.py +++ b/api/monitor/service.py @@ -1593,17 +1593,25 @@ async def daily_tag_matrix( ).all() latest_nickname: Dict[str, tuple] = {} for stat in stat_rows: + # **空昵称不算名字。** 资料接口偶尔会回一个空对象(昵称、sec_uid、粉丝、 + # 作品数全空),如果把这一轮当最新快照,就会用一个空字符串把之前的好名字 + # 盖掉,界面上变成「未知(hash)」。实测线上有两个号就是这么丢的名字。 + if not stat.nickname: + continue current = latest_nickname.get(stat.creator_hash) if current is None or stat.captured_at > current[1]: - latest_nickname[stat.creator_hash] = (stat.nickname or "", stat.captured_at) + latest_nickname[stat.creator_hash] = (stat.nickname, stat.captured_at) - known: Dict[str, str] = { - hash_: (name or f"未知({hash_[:6]})") - for hash_, (name, _captured) in latest_nickname.items() - } + known: Dict[str, str] = {} for row in notes: key = row.creator_hash or f"note:{row.note_id}" - known.setdefault(key, row.creator_name or f"未知({key[:6]})") + if row.creator_name: + known[key] = row.creator_name + else: + known.setdefault(key, f"未知({key[:6]})") + # 快照里的名字优先于作品里的 —— 它来自资料接口,是最新的。 + for hash_, (name, _captured) in latest_nickname.items(): + known[hash_] = name cells: Dict[str, Dict[str, Dict[str, Any]]] = {} # 同一个作品可能挂在多个任务下(monitor_note 的唯一键是 (task_id, note_id)), diff --git a/tests/test_monitor_matrix.py b/tests/test_monitor_matrix.py index 6e7d3c3..c68fa18 100644 --- a/tests/test_monitor_matrix.py +++ b/tests/test_monitor_matrix.py @@ -250,17 +250,23 @@ async def test_no_tasks_returns_an_empty_shape(db): } -async def _stat(task_id: int, creator_hash: str, nickname: str) -> None: +async def _stat( + task_id: int, + creator_hash: str, + nickname: str, + captured_at: int | None = None, + run_id: int = 1, +) -> None: async with monitor_db.get_session() as session: session.add( MonitorCreatorStat( task_id=task_id, - run_id=1, + run_id=run_id, creator_hash=creator_hash, nickname=nickname, creator_id="", works_count=0, - captured_at=get_current_timestamp(), + captured_at=captured_at or get_current_timestamp(), ) ) @@ -283,6 +289,37 @@ async def test_creators_without_notes_still_get_a_row(db): assert len(data["dates"]) == 7 +@pytest.mark.asyncio +async def test_degraded_stat_snapshot_does_not_hide_the_name(db): + """资料接口偶尔回空对象(昵称/粉丝/作品数全空)。 + + 那一轮不能算「最新的名字」—— 否则一个空字符串会把之前的好名字盖掉,界面上 + 变成「未知(hash)」。线上真有两个号这么丢过名字。 + """ + task_id = await _task() + now = get_current_timestamp() + await _stat(task_id, "hash-a", "好名字", captured_at=now - 60_000, run_id=1) + await _stat(task_id, "hash-a", "", captured_at=now, run_id=2) # 更新,但是坏的 + + async with monitor_db.get_session() as session: + data = await service.daily_tag_matrix(session, "dy", days=7) + + assert [c["name"] for c in data["creators"]] == ["好名字"] + + +@pytest.mark.asyncio +async def test_empty_stat_falls_back_to_the_note_name(db): + task_id = await _task() + now = get_current_timestamp() + await _note(task_id, "n1", "作品里的名字", now - 1000) + await _stat(task_id, "hash-a", "") + + async with monitor_db.get_session() as session: + data = await service.daily_tag_matrix(session, "dy", days=7) + + assert [c["name"] for c in data["creators"]] == ["作品里的名字"] + + @pytest.mark.asyncio async def test_cell_sums_the_latest_metric_of_each_note(db): task_id = await _task()