PCM_Report/diagnose_experiment_list.py

141 lines
4.7 KiB
Python
Raw Normal View History

2026-03-10 17:03:31 +08:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
诊断实验列表显示问题
"""
import sqlite3
from pathlib import Path
from logger import get_logger
logger = get_logger()
def diagnose():
"""诊断实验列表问题"""
print("\n" + "="*80)
print("诊断实验列表显示问题")
print("="*80 + "\n")
try:
db_path = Path(__file__).parent / "experiments.db"
if not db_path.exists():
print(f"❌ 数据库文件不存在: {db_path}")
return
print(f"✅ 数据库文件存在: {db_path}\n")
db = sqlite3.connect(str(db_path))
cur = db.cursor()
# 1. 检查表结构
print("1. 检查 experiments 表结构")
print("-" * 80)
cur.execute("PRAGMA table_info(experiments)")
columns = cur.fetchall()
print(f"表字段数量: {len(columns)}")
print(f"{'序号':<5} {'字段名':<25} {'类型':<15} {'非空':<5} {'默认值'}")
print("-" * 80)
for col in columns:
cid, name, type_, notnull, default, pk = col
print(f"{cid:<5} {name:<25} {type_:<15} {notnull:<5} {str(default)}")
# 检查关键字段
column_names = [col[1] for col in columns]
has_save_status = 'save_status' in column_names
has_save_error = 'save_error' in column_names
print(f"\n✅ save_status 字段: {'存在' if has_save_status else '❌ 缺失'}")
print(f"✅ save_error 字段: {'存在' if has_save_error else '❌ 缺失'}")
# 2. 检查实验记录
print("\n2. 检查实验记录")
print("-" * 80)
cur.execute("SELECT COUNT(*) FROM experiments")
count = cur.fetchone()[0]
print(f"实验记录总数: {count}")
if count > 0:
# 显示最近的记录
print("\n最近5条实验记录:")
print("-" * 80)
if has_save_status and has_save_error:
cur.execute("""
SELECT id, work_order_no, start_ts, end_ts, save_status, save_error
FROM experiments
ORDER BY id DESC
LIMIT 5
""")
else:
cur.execute("""
SELECT id, work_order_no, start_ts, end_ts
FROM experiments
ORDER BY id DESC
LIMIT 5
""")
rows = cur.fetchall()
for row in rows:
if has_save_status and has_save_error:
eid, wo, st, et, save_status, save_error = row
print(f"ID: {eid}, 工单: {wo or 'N/A'}, 开始: {st or 'N/A'}, 结束: {et or 'N/A'}")
print(f" 保存状态: {save_status or 'NULL'}, 错误: {save_error or 'NULL'}")
else:
eid, wo, st, et = row
print(f"ID: {eid}, 工单: {wo or 'N/A'}, 开始: {st or 'N/A'}, 结束: {et or 'N/A'}")
# 3. 测试查询语句
print("\n3. 测试UI查询语句")
print("-" * 80)
try:
if has_save_status and has_save_error:
test_sql = """
SELECT id, start_ts, end_ts, work_order_no, process_name, part_no,
executor, remark, sqlserver_status, is_paused, is_terminated,
save_status, save_error
FROM experiments
ORDER BY id DESC
LIMIT 1
"""
else:
test_sql = """
SELECT id, start_ts, end_ts, work_order_no, process_name, part_no,
executor, remark, sqlserver_status, is_paused, is_terminated
FROM experiments
ORDER BY id DESC
LIMIT 1
"""
cur.execute(test_sql)
result = cur.fetchone()
if result:
print("✅ 查询成功")
print(f"返回字段数: {len(result)}")
else:
print("⚠️ 查询成功但无数据")
except Exception as e:
print(f"❌ 查询失败: {e}")
db.close()
print("\n" + "="*80)
print("诊断完成")
print("="*80)
# 给出建议
if not has_save_status or not has_save_error:
print("\n⚠️ 建议:运行 python add_save_status_columns.py 添加缺失的字段")
except Exception as e:
print(f"\n❌ 诊断过程出错: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
diagnose()