验收爬虫与截图按自然日归批

This commit is contained in:
2026-09-01 23:11:57 +08:00
parent fd083e0438
commit 55b04c1e36
4 changed files with 19 additions and 12 deletions

View File

@@ -1183,8 +1183,9 @@ def _copy_acceptance_task(public_id: str, *, trigger_source: str = "acceptance")
with connection() as conn: with connection() as conn:
conn.execute( conn.execute(
"""UPDATE acceptance_tasks SET scheduled_from_task_id=?,dashboard_batch_key=?, """UPDATE acceptance_tasks SET scheduled_from_task_id=?,dashboard_batch_key=?,
use_all_devices=? WHERE public_task_id=?""", use_all_devices=?,batch_date=? WHERE public_task_id=?""",
(task["id"], task["dashboard_batch_key"], task["use_all_devices"], child["task_id"]), (task["id"], task["dashboard_batch_key"], task["use_all_devices"],
datetime.now().astimezone().date().isoformat(), child["task_id"]),
) )
return get_acceptance_task(child["task_id"]) return get_acceptance_task(child["task_id"])
@@ -1195,7 +1196,7 @@ def collect_acceptance_now(public_id: str) -> dict[str, Any]:
def collect_acceptance_crawler_now(public_id: str) -> dict[str, Any]: def collect_acceptance_crawler_now(public_id: str) -> dict[str, Any]:
"""按项目搜索词启动独立的 Cookie 爬虫采集批次,不下发截图设备。""" """按项目搜索词同时启动 Cookie 爬虫和截图设备,按自然日归入批次。"""
with connection() as conn: with connection() as conn:
task = conn.execute("SELECT * FROM acceptance_tasks WHERE public_task_id=?", (public_id,)).fetchone() task = conn.execute("SELECT * FROM acceptance_tasks WHERE public_task_id=?", (public_id,)).fetchone()
if not task: if not task:
@@ -1205,8 +1206,8 @@ def collect_acceptance_crawler_now(public_id: str) -> dict[str, Any]:
keywords = [row["keyword"] for row in conn.execute("SELECT keyword FROM acceptance_keywords WHERE task_id=? ORDER BY keyword_order", (root_id,)).fetchall()] keywords = [row["keyword"] for row in conn.execute("SELECT keyword FROM acceptance_keywords WHERE task_id=? ORDER BY keyword_order", (root_id,)).fetchall()]
matches = [row["keyword"] for row in conn.execute("SELECT keyword FROM acceptance_match_keywords WHERE task_id=? ORDER BY keyword_order", (root_id,)).fetchall()] matches = [row["keyword"] for row in conn.execute("SELECT keyword FROM acceptance_match_keywords WHERE task_id=? ORDER BY keyword_order", (root_id,)).fetchall()]
child = create_acceptance_task({ child = create_acceptance_task({
"name": f"{root['name']}|爬虫数据采集", "name": f"{root['name']}|立即完整采集",
"acceptance_method": "data-analysis", "acceptance_method": "combined",
"feishu_links": [], "feishu_links": [],
"keywords": keywords, "keywords": keywords,
"match_keywords": matches, "match_keywords": matches,
@@ -1216,9 +1217,10 @@ def collect_acceptance_crawler_now(public_id: str) -> dict[str, Any]:
}) })
with connection() as conn: with connection() as conn:
conn.execute( conn.execute(
"""UPDATE acceptance_tasks SET scheduled_from_task_id=?,dashboard_batch_key=? """UPDATE acceptance_tasks SET scheduled_from_task_id=?,dashboard_batch_key=?,
use_all_devices=1,batch_date=?
WHERE public_task_id=?""", WHERE public_task_id=?""",
(root_id, root["dashboard_batch_key"], child["task_id"]), (root_id, root["dashboard_batch_key"], datetime.now().astimezone().date().isoformat(), child["task_id"]),
) )
return get_acceptance_task(child["task_id"]) return get_acceptance_task(child["task_id"])
@@ -1559,7 +1561,7 @@ def _task_dict(conn, task) -> dict[str, Any]:
).fetchall()] ).fetchall()]
root_id = task["scheduled_from_task_id"] or task["id"] root_id = task["scheduled_from_task_id"] or task["id"]
batches = [dict(row) for row in conn.execute( batches = [dict(row) for row in conn.execute(
"""SELECT public_task_id,status,progress_percent,created_at,updated_at """SELECT public_task_id,status,progress_percent,batch_date,created_at,updated_at
FROM acceptance_tasks WHERE id=? OR scheduled_from_task_id=? FROM acceptance_tasks WHERE id=? OR scheduled_from_task_id=?
ORDER BY created_at DESC""", ORDER BY created_at DESC""",
(root_id, root_id), (root_id, root_id),
@@ -1701,6 +1703,7 @@ def _task_dict(conn, task) -> dict[str, Any]:
"cookie_count": task["cookie_count"], "cookie_count": task["cookie_count"],
"dashboard_batch_key": task["dashboard_batch_key"], "dashboard_batch_key": task["dashboard_batch_key"],
"use_all_devices": bool(task["use_all_devices"]), "use_all_devices": bool(task["use_all_devices"]),
"batch_date": task["batch_date"],
"schedule_enabled": bool(task["schedule_enabled"]), "schedule_enabled": bool(task["schedule_enabled"]),
"schedule_weekday": task["schedule_weekday"], "schedule_weekday": task["schedule_weekday"],
"schedule_weekdays": _decode_json(task["schedule_weekdays"], None) or ( "schedule_weekdays": _decode_json(task["schedule_weekdays"], None) or (
@@ -1733,7 +1736,9 @@ def get_acceptance_task(public_id: str) -> dict[str, Any]:
def list_acceptance_tasks() -> dict[str, Any]: def list_acceptance_tasks() -> dict[str, Any]:
with connection() as conn: with connection() as conn:
rows = conn.execute("SELECT * FROM acceptance_tasks ORDER BY created_at DESC").fetchall() rows = conn.execute(
"SELECT * FROM acceptance_tasks WHERE scheduled_from_task_id IS NULL ORDER BY created_at DESC"
).fetchall()
items = [_task_dict(conn, row) for row in rows] items = [_task_dict(conn, row) for row in rows]
return {"items": items, "total": len(items)} return {"items": items, "total": len(items)}

View File

@@ -57,8 +57,8 @@
}, },
"/api/v1/acceptance-tasks/{task_id}/collect-crawler-now": { "/api/v1/acceptance-tasks/{task_id}/collect-crawler-now": {
"post": { "post": {
"summary": "立即启动验收爬虫数据采集", "summary": "立即启动完整验收采集",
"description": "使用项目的搜索词和当前全部可用 Cookie 创建独立数据采集批次,不下发截图设备。", "description": "使用项目搜索词、当前全部可用 Cookie 和全部启用截图设备创建完整采集;批次按 Asia/Shanghai 自然日归属,同日重采仍属于同一日期批次。",
"tags": ["项目验收"], "tags": ["项目验收"],
"parameters": [{"$ref": "#/components/parameters/TaskId"}], "parameters": [{"$ref": "#/components/parameters/TaskId"}],
"responses": {"201": {"description": "爬虫采集批次已启动"}} "responses": {"201": {"description": "爬虫采集批次已启动"}}

View File

@@ -388,6 +388,7 @@ def init_database() -> None:
"cookie_count": "INTEGER NOT NULL DEFAULT 0", "cookie_count": "INTEGER NOT NULL DEFAULT 0",
"dashboard_batch_key": "TEXT", "dashboard_batch_key": "TEXT",
"use_all_devices": "INTEGER NOT NULL DEFAULT 0", "use_all_devices": "INTEGER NOT NULL DEFAULT 0",
"batch_date": "TEXT",
} }
for column, definition in acceptance_migrations.items(): for column, definition in acceptance_migrations.items():
if column not in acceptance_columns: if column not in acceptance_columns:

View File

@@ -142,10 +142,11 @@ class AcceptanceCreationTest(unittest.TestCase):
child = collect_acceptance_crawler_now(source["task_id"]) child = collect_acceptance_crawler_now(source["task_id"])
self.assertEqual(child["acceptance_method"], "data-analysis") self.assertEqual(child["acceptance_method"], "combined")
self.assertEqual(child["keywords"], ["山姆会员卡"]) self.assertEqual(child["keywords"], ["山姆会员卡"])
self.assertEqual(child["match_keywords"], ["民生"]) self.assertEqual(child["match_keywords"], ["民生"])
self.assertEqual(child["cookie_count"], 2) self.assertEqual(child["cookie_count"], 2)
self.assertIsNotNone(child["batch_date"])
@patch("acceptance_service.threading.Thread", _ThreadWithoutStart) @patch("acceptance_service.threading.Thread", _ThreadWithoutStart)
@patch("acceptance_service._copy_acceptance_task") @patch("acceptance_service._copy_acceptance_task")