93 lines
3.7 KiB
Python
93 lines
3.7 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
批量替换 analytics.py 中所有 app_collect_summary 查询为新架构
|
|
|
|
策略:
|
|
1. 简单的 SELECT * FROM app_collect_summary → LEFT JOIN app_catalog + v_collection_latest
|
|
2. UPDATE app_collect_summary → UPDATE app_catalog
|
|
3. INSERT INTO app_collect_summary → INSERT INTO app_catalog + collection_task
|
|
"""
|
|
|
|
import re
|
|
|
|
def generate_new_query_patterns():
|
|
"""生成新架构的查询模式"""
|
|
|
|
# 基础 JOIN 模式
|
|
BASE_JOIN = """
|
|
FROM app_catalog ac
|
|
LEFT JOIN v_collection_latest vl ON ac.package_name = vl.package_name
|
|
"""
|
|
|
|
# 字段映射
|
|
FIELD_MAPPING = {
|
|
'catalog_active': 'ac.is_active',
|
|
'incremental_batch_tag': 'ac.batch_tags',
|
|
'latest_status': 'vl.execution_status',
|
|
'latest_test_time': "CAST(strftime('%s', vl.completed_at) AS REAL)",
|
|
'latest_worker_id': 'vl.worker_id',
|
|
'latest_task_key': 'vl.task_key',
|
|
'latest_failure_type': "COALESCE(vl.error_category, '') || '/' || COALESCE(vl.error_code, 0)",
|
|
'restriction_status': """
|
|
CASE
|
|
WHEN vl.execution_status = 'success' THEN
|
|
CASE WHEN COALESCE(vl.num_nodes, 0) >= 5 AND COALESCE(vl.self_ratio, 0) >= 30
|
|
THEN 'success'
|
|
ELSE 'light_restricted' END
|
|
ELSE 'severe_restricted'
|
|
END""",
|
|
'retryability': """
|
|
CASE
|
|
WHEN vl.execution_status = 'success' THEN 'retryable'
|
|
WHEN vl.error_category = 'DOWNLOAD_ERROR'
|
|
AND vl.error_code IN (1,5,6,10,403,404) THEN 'non_retryable'
|
|
ELSE 'retryable'
|
|
END""",
|
|
'collection_status': """
|
|
CASE
|
|
WHEN vl.execution_status = 'success'
|
|
AND COALESCE(vl.num_nodes, 0) >= 5
|
|
AND COALESCE(vl.self_ratio, 0) >= 30 THEN 'qualified'
|
|
ELSE 'restricted'
|
|
END""",
|
|
}
|
|
|
|
return BASE_JOIN, FIELD_MAPPING
|
|
|
|
|
|
# 需要改写的函数列表(按优先级)
|
|
FUNCTIONS_TO_REWRITE = [
|
|
# 批量查询
|
|
'list_pending_collection_tasks', # SELECT FROM app_collect_summary WHERE catalog_active=1
|
|
'list_qualified_apps', # SELECT FROM app_collect_summary WHERE restriction_status='success'
|
|
'list_all_catalog_apps', # SELECT FROM app_collect_summary
|
|
'list_model_eligible_apps', # SELECT FROM app_collect_summary WHERE model_eligible=1
|
|
'has_successful_related_magic_label', # SELECT FROM app_collect_summary WHERE app_magic_label=?
|
|
|
|
# 目录管理
|
|
'upsert_catalog_entries', # INSERT INTO app_collect_summary
|
|
'replace_catalog_entries', # INSERT + DELETE FROM app_collect_summary
|
|
'update_collection_statuses', # UPDATE app_collect_summary SET collection_status
|
|
'upsert_high_priority_app', # INSERT INTO app_collect_summary
|
|
|
|
# 批次管理
|
|
'clear_incremental_batch_tags', # UPDATE app_collect_summary SET incremental_batch_tag
|
|
'set_incremental_batch_tag_for_packages', # UPDATE app_collect_summary
|
|
|
|
# 复杂聚合
|
|
'get_overview', # SELECT COUNT(*) FROM app_collect_summary GROUP BY
|
|
'list_apps', # SELECT FROM app_collect_summary LIMIT OFFSET
|
|
'get_app_detail', # SELECT FROM app_collect_summary WHERE package_name=?
|
|
|
|
# 清理操作
|
|
'delete_snapshots_not_in', # DELETE FROM app_collect_summary
|
|
'clear_all_snapshots', # UPDATE app_collect_summary SET latest_*=NULL
|
|
]
|
|
|
|
print("=" * 60)
|
|
print("需要改写的函数:")
|
|
for i, func in enumerate(FUNCTIONS_TO_REWRITE, 1):
|
|
print(f"{i:2d}. {func}")
|
|
print(f"\n总计:{len(FUNCTIONS_TO_REWRITE)} 个函数")
|
|
print("=" * 60)
|