feat(digest): 改成每天 9 点推「昨天」的视频情况
当天还没过完,上午发「今天的」只会是半张表,晚上数据又翻了几倍;改报前一天, 和运营早上看的复盘口径一致。 - default_day() = 昨天,定时 / 手动发送 / 预览三处共用,口径不会再分叉 - 去重标记记的是**被报的那一天**,不是发送时刻 —— 9 点失败、10 点补发成功后 不会再发一遍 - 推送时间默认 09:00;配置写坏时也按 09:00 兜底,不再是 22:00
This commit is contained in:
@@ -14,9 +14,13 @@ from api.monitor.models import (
|
||||
MonitorNoteMetric,
|
||||
MonitorTask,
|
||||
)
|
||||
from tools.time_util import get_current_timestamp
|
||||
def _at(day_offset: int, hour: int = 12) -> int:
|
||||
"""某天某个整点的毫秒时间戳(``day_offset`` 相对今天,-1 就是昨天)。
|
||||
|
||||
MS_PER_DAY = 86_400_000
|
||||
不用 ``now - 24h``:凌晨跑测试时它会掉到前天,让本来正确的断言莫名其妙地挂。
|
||||
"""
|
||||
day = date.today() + timedelta(days=day_offset)
|
||||
return int(datetime.combine(day, datetime.min.time()).timestamp() * 1000) + hour * 3_600_000
|
||||
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
@@ -83,25 +87,40 @@ async def _metric(task_id: int, note_id: str, run_id: int, liked: int | None) ->
|
||||
)
|
||||
|
||||
|
||||
def test_the_digest_reports_yesterday():
|
||||
"""早上九点推的是前一天的账 —— 补发、预览、定时三处都从这里取日子。"""
|
||||
assert digest.default_day() == date.today() - timedelta(days=1)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_only_todays_notes_are_listed(db):
|
||||
async def test_by_default_only_yesterdays_notes_are_listed(db):
|
||||
task_id = await _task()
|
||||
now = get_current_timestamp()
|
||||
await _note(task_id, "today", "今天", now - 1000)
|
||||
await _note(task_id, "yesterday", "昨天", now - MS_PER_DAY)
|
||||
await _note(task_id, "today", "今天", _at(0))
|
||||
await _note(task_id, "yesterday", "昨天", _at(-1))
|
||||
|
||||
async with monitor_db.get_session() as session:
|
||||
rows = await digest.daily_rows(session, "dy")
|
||||
|
||||
assert [row["note_id"] for row in rows] == ["yesterday"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_an_explicit_day_wins_over_the_default(db):
|
||||
task_id = await _task()
|
||||
await _note(task_id, "today", "今天", _at(0))
|
||||
await _note(task_id, "yesterday", "昨天", _at(-1))
|
||||
|
||||
async with monitor_db.get_session() as session:
|
||||
rows = await digest.daily_rows(session, "dy", date.today())
|
||||
|
||||
assert [row["note_id"] for row in rows] == ["today"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_latest_metric_wins_and_rows_sort_by_likes(db):
|
||||
task_id = await _task()
|
||||
now = get_current_timestamp()
|
||||
await _note(task_id, "a", "博主A", now - 1000)
|
||||
await _note(task_id, "b", "博主B", now - 2000)
|
||||
await _note(task_id, "a", "博主A", _at(-1, 11))
|
||||
await _note(task_id, "b", "博主B", _at(-1, 10))
|
||||
await _metric(task_id, "a", run_id=1, liked=5)
|
||||
await _metric(task_id, "a", run_id=2, liked=99)
|
||||
await _metric(task_id, "b", run_id=1, liked=500)
|
||||
@@ -117,9 +136,8 @@ async def test_latest_metric_wins_and_rows_sort_by_likes(db):
|
||||
async def test_the_same_note_under_two_tasks_is_listed_once(db):
|
||||
first = await _task(name="一")
|
||||
second = await _task(name="二")
|
||||
now = get_current_timestamp()
|
||||
await _note(first, "same", "博主A", now - 1000)
|
||||
await _note(second, "same", "博主A", now - 1000)
|
||||
await _note(first, "same", "博主A", _at(-1))
|
||||
await _note(second, "same", "博主A", _at(-1))
|
||||
|
||||
async with monitor_db.get_session() as session:
|
||||
rows = await digest.daily_rows(session, "dy")
|
||||
@@ -134,12 +152,13 @@ def _time_reached(send_at: str, now: datetime) -> bool:
|
||||
|
||||
|
||||
def test_time_reached_handles_bad_config():
|
||||
now = datetime(2026, 10, 23, 21, 30)
|
||||
assert _time_reached("21:00", now) is True
|
||||
assert _time_reached("22:00", now) is False
|
||||
# 写错就按 22:00 算,而不是永远不发
|
||||
assert _time_reached("十点半", datetime(2026, 10, 23, 22, 1)) is True
|
||||
assert _time_reached("", datetime(2026, 10, 23, 21, 0)) is False
|
||||
now = datetime(2026, 10, 23, 8, 30)
|
||||
assert _time_reached("08:00", now) is True
|
||||
assert _time_reached("09:00", now) is False
|
||||
assert _time_reached("09:00", datetime(2026, 10, 23, 9, 0)) is True
|
||||
# 写错就按 09:00 算,而不是永远不发
|
||||
assert _time_reached("九点半", datetime(2026, 10, 23, 9, 1)) is True
|
||||
assert _time_reached("", datetime(2026, 10, 23, 8, 59)) is False
|
||||
|
||||
|
||||
@pytest.mark.skipif(digest._font_path() is None, reason="本机没有中文字体")
|
||||
|
||||
Reference in New Issue
Block a user