切换监测 V2 配置及已发布图片接口
This commit is contained in:
@@ -416,9 +416,10 @@ def delete_xapi_device(account_id: str) -> dict[str, Any]:
|
||||
def sync_dashboard_acceptance_project(
|
||||
batch_key: str, name: str, keywords: list[str], match_keywords: list[str],
|
||||
) -> None:
|
||||
"""同步验收项目名称和问题配置到数据看板同一 batch_key。"""
|
||||
"""把验收项目配置同步到 V2;问题和匹配词均按主体隔离。"""
|
||||
import pymysql
|
||||
normalized_key = str(batch_key or "").strip()
|
||||
subject_name = str(name or "").strip() or "未命名监测项目"
|
||||
if not normalized_key:
|
||||
return
|
||||
db = pymysql.connect(
|
||||
@@ -429,44 +430,33 @@ def sync_dashboard_acceptance_project(
|
||||
)
|
||||
try:
|
||||
with db.cursor() as cursor:
|
||||
cursor.execute(
|
||||
"UPDATE xhs_ocr_monitor_batches SET subject_name=%s,updated_at=NOW() WHERE batch_key=%s",
|
||||
(name, normalized_key),
|
||||
)
|
||||
cursor.execute(
|
||||
"UPDATE xhs_ocr_batch_questions SET active=0,updated_at=NOW() WHERE batch_key=%s",
|
||||
(normalized_key,),
|
||||
)
|
||||
for index, keyword in enumerate(keywords):
|
||||
cursor.execute(
|
||||
"SELECT question_code FROM xhs_ocr_questions WHERE search_keyword=%s LIMIT 1",
|
||||
(keyword,),
|
||||
)
|
||||
row = cursor.fetchone()
|
||||
code = row["question_code"] if row else "X" + hashlib.sha256(keyword.encode()).hexdigest()[:15]
|
||||
if not row:
|
||||
cursor.execute(
|
||||
"""INSERT INTO xhs_ocr_questions
|
||||
(question_code,search_keyword,question_direction,display_order,active,created_at,updated_at)
|
||||
VALUES (%s,%s,%s,%s,1,NOW(),NOW())""",
|
||||
(code, keyword, name, index + 1),
|
||||
)
|
||||
cursor.execute(
|
||||
"""INSERT INTO xhs_ocr_batch_questions(batch_key,question_code,active,created_at,updated_at)
|
||||
VALUES (%s,%s,1,NOW(),NOW()) ON DUPLICATE KEY UPDATE active=1,updated_at=NOW()""",
|
||||
(normalized_key, code),
|
||||
)
|
||||
for keyword in match_keywords:
|
||||
keyword_key = hashlib.sha256(keyword.encode()).hexdigest()
|
||||
department_key = hashlib.sha256(b"").hexdigest()
|
||||
cursor.execute(
|
||||
"""INSERT INTO xhs_ocr_match_keywords
|
||||
(keyword_key,department_key,keyword,department_name,case_sensitive,
|
||||
active,remark,first_created_at,updated_at)
|
||||
VALUES (%s,%s,%s,'',0,1,'验收项目同步',NOW(),NOW())
|
||||
ON DUPLICATE KEY UPDATE active=1,updated_at=NOW()""",
|
||||
(keyword_key, department_key, keyword),
|
||||
)
|
||||
cursor.execute("INSERT INTO monitor_subjects(name) VALUES(%s) ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id),status='active'",(subject_name,))
|
||||
subject_id = cursor.lastrowid
|
||||
cursor.execute("SELECT id FROM monitor_subjects WHERE id=%s FOR UPDATE",(subject_id,))
|
||||
cursor.execute("SELECT id FROM monitor_batches WHERE legacy_batch_key=%s AND subject_id=%s AND status<>'archived' ORDER BY collected_at DESC LIMIT 1 FOR UPDATE",(normalized_key,subject_id))
|
||||
batch = cursor.fetchone()
|
||||
if batch:
|
||||
batch_id=batch['id']
|
||||
else:
|
||||
collection_key='acceptance-'+hashlib.sha256((normalized_key+subject_name).encode()).hexdigest()[:24]
|
||||
cursor.execute("INSERT INTO monitor_batches(subject_id,batch_key,legacy_batch_key,name,collected_at,status) VALUES(%s,%s,%s,%s,NOW(),'completed')",(subject_id,collection_key,normalized_key,subject_name))
|
||||
batch_id=cursor.lastrowid
|
||||
cursor.execute("UPDATE monitor_batch_questions SET status='archived' WHERE batch_id=%s",(batch_id,))
|
||||
scopes=[]
|
||||
for index, keyword in enumerate(dict.fromkeys(str(v).strip() for v in keywords if str(v).strip()),1):
|
||||
cursor.execute("INSERT INTO monitor_questions(subject_id,question_text,display_order) VALUES(%s,%s,%s) ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id),display_order=VALUES(display_order),status='active'",(subject_id,keyword,index))
|
||||
question_id=cursor.lastrowid
|
||||
cursor.execute("""INSERT INTO monitor_batch_questions(subject_id,batch_id,question_id,question_text_snapshot,display_order,status)
|
||||
VALUES(%s,%s,%s,%s,%s,'active') ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id),question_text_snapshot=VALUES(question_text_snapshot),display_order=VALUES(display_order),status='active'""",(subject_id,batch_id,question_id,keyword,index))
|
||||
scopes.append(cursor.lastrowid)
|
||||
normalized_matches=[str(v).strip() for v in match_keywords if str(v).strip()]
|
||||
for scope_id in scopes:
|
||||
cursor.execute("UPDATE monitor_batch_question_keywords SET status='archived' WHERE batch_question_id=%s",(scope_id,))
|
||||
for order,keyword in enumerate(dict.fromkeys(normalized_matches),1):
|
||||
cursor.execute("INSERT INTO monitor_keywords(subject_id,keyword_text) VALUES(%s,%s) ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id),status='active'",(subject_id,keyword))
|
||||
keyword_id=cursor.lastrowid
|
||||
cursor.execute("""INSERT INTO monitor_batch_question_keywords(subject_id,batch_question_id,keyword_id,keyword_text_snapshot,match_method,display_order,status)
|
||||
VALUES(%s,%s,%s,%s,'substring',%s,'active') ON DUPLICATE KEY UPDATE keyword_text_snapshot=VALUES(keyword_text_snapshot),match_method='substring',display_order=VALUES(display_order),status='active'""",(subject_id,scope_id,keyword_id,keyword,order))
|
||||
db.commit()
|
||||
except Exception:
|
||||
db.rollback()
|
||||
|
||||
Reference in New Issue
Block a user