增加验收设备管理与动态资源调度
This commit is contained in:
@@ -5,6 +5,7 @@ from typing import Any
|
||||
|
||||
import requests
|
||||
import json
|
||||
import hashlib
|
||||
|
||||
from config import Config
|
||||
|
||||
@@ -352,6 +353,66 @@ def schedule_xapi_screenshots(
|
||||
)
|
||||
|
||||
|
||||
def create_xapi_device(data: dict[str, Any]) -> dict[str, Any]:
|
||||
"""在 framework-db 中新增或重新启用人工维护的 XAPI 设备。"""
|
||||
import pymysql
|
||||
account_id = str(data.get("xhs_account_id") or "").strip()
|
||||
if not account_id:
|
||||
raise CrawlerError("xhs_account_id 不能为空")
|
||||
now = time.strftime("%Y-%m-%d %H:%M:%S")
|
||||
record_id = "manual-" + hashlib.sha256(account_id.encode()).hexdigest()[:24]
|
||||
db = pymysql.connect(
|
||||
host=Config.CRAWLER_DATABASE_HOST, port=Config.CRAWLER_DATABASE_PORT,
|
||||
user=Config.CRAWLER_DATABASE_USER, password=Config.CRAWLER_DATABASE_PASSWORD,
|
||||
database=Config.CRAWLER_DATABASE_NAME, charset="utf8mb4",
|
||||
cursorclass=pymysql.cursors.DictCursor, autocommit=True,
|
||||
)
|
||||
try:
|
||||
with db.cursor() as cursor:
|
||||
cursor.execute(
|
||||
"""INSERT INTO xhs_device_accounts
|
||||
(feishu_record_id,base_token,table_id,view_id,xhs_account_id,
|
||||
account_name,phone,device_code,text_value,active,raw_json,
|
||||
first_created_at,last_synced_at,updated_at)
|
||||
VALUES (%s,'manual','manual',NULL,%s,%s,%s,%s,'人工添加',1,%s,%s,%s,%s)
|
||||
ON DUPLICATE KEY UPDATE account_name=VALUES(account_name),
|
||||
phone=VALUES(phone),device_code=VALUES(device_code),active=1,
|
||||
raw_json=VALUES(raw_json),last_synced_at=VALUES(last_synced_at),
|
||||
updated_at=VALUES(updated_at)""",
|
||||
(record_id, account_id, str(data.get("account_name") or "").strip() or None,
|
||||
str(data.get("phone") or "").strip() or None,
|
||||
str(data.get("device_code") or "").strip() or None,
|
||||
json.dumps(data, ensure_ascii=False), now, now, now),
|
||||
)
|
||||
finally:
|
||||
db.close()
|
||||
return {"xhs_account_id": account_id, "active": True}
|
||||
|
||||
|
||||
def delete_xapi_device(account_id: str) -> dict[str, Any]:
|
||||
"""软删除设备;历史任务保留,后续下发不再选择该设备。"""
|
||||
import pymysql
|
||||
normalized = str(account_id or "").strip()
|
||||
if not normalized:
|
||||
raise CrawlerError("xhs_account_id 不能为空")
|
||||
db = pymysql.connect(
|
||||
host=Config.CRAWLER_DATABASE_HOST, port=Config.CRAWLER_DATABASE_PORT,
|
||||
user=Config.CRAWLER_DATABASE_USER, password=Config.CRAWLER_DATABASE_PASSWORD,
|
||||
database=Config.CRAWLER_DATABASE_NAME, charset="utf8mb4", autocommit=True,
|
||||
)
|
||||
try:
|
||||
with db.cursor() as cursor:
|
||||
cursor.execute(
|
||||
"UPDATE xhs_device_accounts SET active=0,updated_at=NOW() WHERE xhs_account_id=%s AND active=1",
|
||||
(normalized,),
|
||||
)
|
||||
if not cursor.rowcount:
|
||||
raise CrawlerError("设备不存在或已经删除")
|
||||
finally:
|
||||
db.close()
|
||||
return {"xhs_account_id": normalized, "active": False}
|
||||
|
||||
|
||||
def query_xapi_batch_runs(batch_id: str, *, refresh: bool = True) -> dict[str, Any]:
|
||||
"""读取某个 Framework 调度批次下的设备任务、截图与飞书结果。"""
|
||||
normalized = str(batch_id or "").strip()
|
||||
|
||||
Reference in New Issue
Block a user