feat(report): 日期列补满区间、博主补上「只有资料没有作品」的号、加条件格式
三个问题一起修: 1. **日期列按完整区间生成**。原先只取「有作品的那几天」,某天断更那一列就消失了 —— 看图的人会以为漏采,而空白本身也是信息。现在固定输出最近 N 个自然日(含今天)。 2. **博主名单改成「账号快照 ∪ 作品」**,和界面一致。线上有 3 个号(昵称是纯数字 uid、作品数 0)只存在于快照表里,一条作品都没有;名单只从作品里取的话它们整行 消失。现在它们会占一行,格子里是空的。 3. **条件格式**:头部加「高亮 [指标] ≥ [数值]」。命中的格子换底色 + 粉色描边, 只在本地重画,不重新请求数据;条件写进地址栏(`cf=likes:1000`)可直接收藏。 细节: * 计数/指标的去重口径不变(同一作品挂两个任务只算一次); * 指标未知仍是 None,参与不了条件格式 —— 不会被当成 0 高亮。 测试 11 条(新增:只有资料没有作品的号仍占一行、日期列是完整区间)。
This commit is contained in:
+56
-24
@@ -20,7 +20,7 @@
|
||||
|
||||
import asyncio
|
||||
import re
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Any, Dict, List, Optional, Sequence, Tuple
|
||||
from urllib.parse import parse_qs, urlparse
|
||||
|
||||
@@ -1499,7 +1499,17 @@ async def daily_tag_matrix(
|
||||
if not task_ids:
|
||||
return empty
|
||||
|
||||
since = get_current_timestamp() - max(1, days) * 86_400_000
|
||||
# 日期列**按完整区间生成**,不是只取「有作品的那几天」。否则某天断更,那一列会
|
||||
# 悄无声息地消失,看图的人会以为是漏采 —— 空白本身也是信息。
|
||||
first_day = datetime.now().replace(
|
||||
hour=0, minute=0, second=0, microsecond=0
|
||||
) - timedelta(days=max(1, days) - 1)
|
||||
dates = [
|
||||
(first_day + timedelta(days=offset)).strftime("%Y-%m-%d")
|
||||
for offset in range(max(1, days))
|
||||
][::-1]
|
||||
since = int(first_day.timestamp() * 1000)
|
||||
|
||||
notes = (
|
||||
await session.execute(
|
||||
select(
|
||||
@@ -1517,26 +1527,25 @@ async def daily_tag_matrix(
|
||||
.order_by(MonitorNote.published_at.desc())
|
||||
)
|
||||
).all()
|
||||
if not notes:
|
||||
return empty
|
||||
|
||||
note_ids = {row.note_id for row in notes}
|
||||
|
||||
# 标签一次取完,按 note_id 归组;顺序跟词表里的 sort_order 一致。
|
||||
tag_rows = (
|
||||
await session.execute(
|
||||
select(MonitorNoteTag.note_id, MonitorTag.name)
|
||||
.join(MonitorTag, MonitorTag.id == MonitorNoteTag.tag_id)
|
||||
.where(
|
||||
MonitorNoteTag.platform == platform,
|
||||
MonitorNoteTag.note_id.in_(note_ids),
|
||||
)
|
||||
.order_by(MonitorTag.sort_order, MonitorTag.id)
|
||||
)
|
||||
).all()
|
||||
tags_by_note: Dict[str, List[str]] = {}
|
||||
for note_id, name in tag_rows:
|
||||
tags_by_note.setdefault(note_id, []).append(name)
|
||||
if note_ids:
|
||||
tag_rows = (
|
||||
await session.execute(
|
||||
select(MonitorNoteTag.note_id, MonitorTag.name)
|
||||
.join(MonitorTag, MonitorTag.id == MonitorNoteTag.tag_id)
|
||||
.where(
|
||||
MonitorNoteTag.platform == platform,
|
||||
MonitorNoteTag.note_id.in_(note_ids),
|
||||
)
|
||||
.order_by(MonitorTag.sort_order, MonitorTag.id)
|
||||
)
|
||||
).all()
|
||||
for note_id, name in tag_rows:
|
||||
tags_by_note.setdefault(note_id, []).append(name)
|
||||
|
||||
# 指标:每篇作品取**最近一轮**(run_id 最大)。没有指标行的作品不参与求和 ——
|
||||
# 未知和 0 是两回事,格子的提示里会显示成「—」而不是「0」。
|
||||
@@ -1563,8 +1572,35 @@ async def daily_tag_matrix(
|
||||
if current is None or metric.run_id > current.run_id:
|
||||
latest_metric[key] = metric
|
||||
|
||||
# 博主名单取**账号快照 ∪ 作品**,和界面一致。只在快照里出现的号也要占一行 ——
|
||||
# 典型是资料读成纯数字昵称、作品数为 0 的那几个;只从作品里取的话它们会整行消失。
|
||||
stat_rows = (
|
||||
await session.execute(
|
||||
select(
|
||||
MonitorCreatorStat.creator_hash,
|
||||
MonitorCreatorStat.nickname,
|
||||
MonitorCreatorStat.captured_at,
|
||||
).where(
|
||||
MonitorCreatorStat.task_id.in_(task_ids),
|
||||
MonitorCreatorStat.creator_hash != "",
|
||||
)
|
||||
)
|
||||
).all()
|
||||
latest_nickname: Dict[str, tuple] = {}
|
||||
for stat in stat_rows:
|
||||
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)
|
||||
|
||||
known: Dict[str, str] = {
|
||||
hash_: (name or f"未知({hash_[:6]})")
|
||||
for hash_, (name, _captured) in latest_nickname.items()
|
||||
}
|
||||
for row in notes:
|
||||
key = row.creator_hash or f"note:{row.note_id}"
|
||||
known.setdefault(key, row.creator_name or f"未知({key[:6]})")
|
||||
|
||||
cells: Dict[str, Dict[str, Dict[str, Any]]] = {}
|
||||
names: Dict[str, str] = {}
|
||||
# 同一个作品可能挂在多个任务下(monitor_note 的唯一键是 (task_id, note_id)),
|
||||
# 不按 note_id 去重会把计数和指标都翻倍。
|
||||
seen: set = set()
|
||||
@@ -1577,7 +1613,6 @@ async def daily_tag_matrix(
|
||||
|
||||
day = datetime.fromtimestamp((row.published_at or 0) / 1000).strftime("%Y-%m-%d")
|
||||
creator_key = row.creator_hash or f"note:{row.note_id}"
|
||||
names.setdefault(creator_key, row.creator_name or "")
|
||||
|
||||
bucket = cells.setdefault(day, {}).setdefault(
|
||||
creator_key,
|
||||
@@ -1607,17 +1642,14 @@ async def daily_tag_matrix(
|
||||
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()
|
||||
]
|
||||
creators = [{"hash": hash_, "name": name} for hash_, name in known.items()]
|
||||
creators.sort(key=lambda item: item["name"])
|
||||
|
||||
return {
|
||||
"platform": platform,
|
||||
"days": days,
|
||||
"task_id": task_id,
|
||||
"dates": sorted(cells, reverse=True),
|
||||
"dates": dates,
|
||||
"creators": creators,
|
||||
"cells": cells,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user