feat(growth): same-day 1-day baselines for new videos
Let 1-day view growth use a zero baseline for videos published today, and the first successful sample for older videos ingested today, without mixing those into 7/30-day rankings.
This commit is contained in:
1 parent
8e7ead1669
commit
c5b7185675
21 files changed
+411
-59
No files matched your search
@@ -38,8 +38,8 @@ def test_fresh_and_idempotent():
|
||||
conn = sqlite3.connect(Path(tmp) / "app.db")
|
||||
try:
|
||||
applied = run_migrations(conn)
|
||||
assert applied == [1, 2, 3, 4, 5, 6, 7]
|
||||
assert current_version(conn) == 7
|
||||
assert applied == [1, 2, 3, 4, 5, 6, 7, 8]
|
||||
assert current_version(conn) == 8
|
||||
assert EXPECTED_TABLES <= table_names(conn)
|
||||
assert "schema_version" in table_names(conn)
|
||||
columns = {
|
||||
@@ -70,10 +70,14 @@ def test_fresh_and_idempotent():
|
||||
assert {
|
||||
"stat_date", "ip", "path", "hits", "first_at", "last_at",
|
||||
} <= visit_columns
|
||||
snapshot_columns = {
|
||||
row[1] for row in conn.execute("PRAGMA table_info(snapshot)")
|
||||
}
|
||||
assert {"view_open", "open_captured_at"} <= snapshot_columns
|
||||
# 再跑一次不应重复应用
|
||||
assert run_migrations(conn) == []
|
||||
row = conn.execute("SELECT COUNT(*) FROM schema_version").fetchone()
|
||||
assert row[0] == 7
|
||||
assert row[0] == 8
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
@@ -88,15 +92,15 @@ def test_backup_before_upgrade():
|
||||
conn.execute("INSERT INTO kv(k, v) VALUES ('hello', 'world')")
|
||||
conn.commit()
|
||||
|
||||
v8 = Migration(8, "add probe", ("CREATE TABLE probe (id INTEGER PRIMARY KEY)",))
|
||||
v9 = Migration(9, "add probe", ("CREATE TABLE probe (id INTEGER PRIMARY KEY)",))
|
||||
applied = run_migrations(
|
||||
conn,
|
||||
backup_dir=backup_dir,
|
||||
now=1_800_000_000,
|
||||
migrations=MIGRATIONS + (v8,),
|
||||
migrations=MIGRATIONS + (v9,),
|
||||
)
|
||||
assert applied == [8]
|
||||
backups = list(backup_dir.glob("app-pre-v7-*.db"))
|
||||
assert applied == [9]
|
||||
backups = list(backup_dir.glob("app-pre-v8-*.db"))
|
||||
assert len(backups) == 1
|
||||
check = sqlite3.connect(backups[0])
|
||||
try:
|
||||
@@ -181,13 +185,46 @@ def test_v5_normalizes_unfaved():
|
||||
)
|
||||
conn.commit()
|
||||
applied = run_migrations(conn)
|
||||
assert applied == [5, 6, 7]
|
||||
assert applied == [5, 6, 7, 8]
|
||||
rows = dict(conn.execute("SELECT aid, status FROM video").fetchall())
|
||||
assert rows == {1: "active", 2: "dropped"}
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
def test_v8_backfills_view_open():
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
conn = sqlite3.connect(Path(tmp) / "app.db")
|
||||
try:
|
||||
run_migrations(conn, migrations=MIGRATIONS[:7])
|
||||
conn.execute(
|
||||
"INSERT INTO video(aid, title, pubdate, status, first_seen) "
|
||||
"VALUES (1, 't', 1, 'active', 1)"
|
||||
)
|
||||
conn.execute(
|
||||
'INSERT INTO snapshot(stat_date, aid, "view", captured_at, ok) '
|
||||
"VALUES ('2026-10-31', 1, 500, 100, 1)"
|
||||
)
|
||||
conn.execute(
|
||||
'INSERT INTO snapshot(stat_date, aid, "view", captured_at, ok) '
|
||||
"VALUES ('2026-10-30', 1, NULL, 90, 0)"
|
||||
)
|
||||
conn.commit()
|
||||
assert run_migrations(conn) == [8]
|
||||
ok_row = conn.execute(
|
||||
"SELECT view_open, open_captured_at FROM snapshot "
|
||||
"WHERE stat_date='2026-10-31'"
|
||||
).fetchone()
|
||||
assert ok_row[0] == 500 and ok_row[1] == 100
|
||||
fail_row = conn.execute(
|
||||
"SELECT view_open, open_captured_at FROM snapshot "
|
||||
"WHERE stat_date='2026-10-30'"
|
||||
).fetchone()
|
||||
assert fail_row[0] is None and fail_row[1] is None
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
def main():
|
||||
test_fresh_and_idempotent()
|
||||
test_backup_before_upgrade()
|
||||
@@ -195,6 +232,7 @@ def main():
|
||||
test_prune_backups()
|
||||
test_validate_migration_order()
|
||||
test_v5_normalizes_unfaved()
|
||||
test_v8_backfills_view_open()
|
||||
print("OK: SQLite 迁移、备份与回滚测试全部通过")
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user