167 lines
5.0 KiB
Python
167 lines
5.0 KiB
Python
#!/usr/bin/env python3
|
||
"""
|
||
调试监控器运行状态
|
||
"""
|
||
|
||
import psutil
|
||
import sqlite3
|
||
from pathlib import Path
|
||
|
||
def check_main_process():
|
||
"""检查主程序进程"""
|
||
print("检查主程序进程")
|
||
print("=" * 40)
|
||
|
||
main_processes = []
|
||
|
||
try:
|
||
for proc in psutil.process_iter(['pid', 'name', 'cmdline']):
|
||
try:
|
||
if proc.info['name'] and 'python' in proc.info['name'].lower():
|
||
cmdline = proc.info['cmdline']
|
||
if cmdline and any('main.py' in arg for arg in cmdline):
|
||
main_processes.append(proc)
|
||
print(f"找到主程序: PID {proc.info['pid']}")
|
||
print(f"命令行: {' '.join(cmdline)}")
|
||
except (psutil.NoSuchProcess, psutil.AccessDenied):
|
||
continue
|
||
|
||
if not main_processes:
|
||
print("ERROR 没有找到运行中的main.py进程")
|
||
return False
|
||
|
||
return True
|
||
|
||
except Exception as e:
|
||
print(f"ERROR 检查进程失败: {e}")
|
||
return False
|
||
|
||
def check_waiting_experiments():
|
||
"""检查等待中的实验"""
|
||
print("\n检查等待中的实验")
|
||
print("=" * 40)
|
||
|
||
try:
|
||
db_path = Path(__file__).parent / "experiments.db"
|
||
db = sqlite3.connect(str(db_path))
|
||
cur = db.cursor()
|
||
|
||
# 查询等待开始的实验
|
||
cur.execute("""
|
||
SELECT id, work_order_no, created_at, remark
|
||
FROM experiments
|
||
WHERE start_ts IS NULL
|
||
ORDER BY id DESC
|
||
LIMIT 10
|
||
""")
|
||
|
||
results = cur.fetchall()
|
||
|
||
if results:
|
||
print("等待开始的实验:")
|
||
for row in results:
|
||
exp_id, work_order, created_at, remark = row
|
||
print(f" ID: {exp_id} - 工单: {work_order} - 创建: {created_at}")
|
||
else:
|
||
print("没有等待开始的实验")
|
||
|
||
db.close()
|
||
return results
|
||
|
||
except Exception as e:
|
||
print(f"ERROR 检查失败: {e}")
|
||
return []
|
||
|
||
def check_recent_influx_data():
|
||
"""检查最近的InfluxDB数据"""
|
||
print("\n检查最近的InfluxDB数据")
|
||
print("=" * 40)
|
||
|
||
try:
|
||
import json
|
||
from influxdb_client import InfluxDBClient
|
||
|
||
# 加载配置
|
||
with open("default.json", 'r', encoding='utf-8') as f:
|
||
config = json.load(f)
|
||
|
||
influx_config = config.get('influx', {})
|
||
|
||
client = InfluxDBClient(
|
||
url=influx_config.get('url', 'http://127.0.0.1:8086'),
|
||
token=influx_config.get('token', ''),
|
||
org=influx_config.get('org', 'MEASCON')
|
||
)
|
||
|
||
# 查询最近的数据
|
||
query = '''
|
||
from(bucket: "PCM")
|
||
|> range(start: -30m)
|
||
|> filter(fn: (r) => r["_measurement"] == "PCM_Measurement")
|
||
|> filter(fn: (r) => r["data_type"] == "Breaker")
|
||
|> filter(fn: (r) => r["_field"] == "load_status")
|
||
|> sort(columns: ["_time"], desc: true)
|
||
|> limit(n: 5)
|
||
'''
|
||
|
||
result = client.query_api().query(query)
|
||
|
||
if result:
|
||
print("最近30分钟的load_status数据:")
|
||
for table in result:
|
||
for record in table.records:
|
||
print(f" 时间: {record.get_time()}")
|
||
print(f" 值: {record.get_value()}")
|
||
print("-" * 20)
|
||
else:
|
||
print("没有找到最近的数据")
|
||
|
||
client.close()
|
||
|
||
except Exception as e:
|
||
print(f"ERROR 检查InfluxDB失败: {e}")
|
||
|
||
def main():
|
||
print("监控器运行状态调试")
|
||
print("=" * 50)
|
||
|
||
# 1. 检查主程序是否运行
|
||
is_running = check_main_process()
|
||
|
||
# 2. 检查等待中的实验
|
||
waiting_experiments = check_waiting_experiments()
|
||
|
||
# 3. 检查最近的数据
|
||
check_recent_influx_data()
|
||
|
||
print("\n" + "=" * 50)
|
||
print("诊断结果:")
|
||
|
||
if not is_running:
|
||
print("❌ 主程序未运行")
|
||
print("解决方案: 启动主程序 python main.py")
|
||
else:
|
||
print("✅ 主程序正在运行")
|
||
|
||
if not waiting_experiments:
|
||
print("❌ 没有等待开始的实验")
|
||
print("解决方案: 在UI中创建新工单并进入等待状态")
|
||
else:
|
||
print(f"✅ 有 {len(waiting_experiments)} 个等待开始的实验")
|
||
print("监控器应该正在监控这些实验")
|
||
|
||
if is_running and waiting_experiments:
|
||
print("\n可能的问题:")
|
||
print("1. 监控器配置错误(measurement、field、data_type不匹配)")
|
||
print("2. 监控器没有正确启动")
|
||
print("3. 监控器查询逻辑有问题")
|
||
print("4. 时间戳问题导致查询不到数据")
|
||
|
||
print("\n建议:")
|
||
print("1. 检查监控器日志输出")
|
||
print("2. 在UI中手动刷新实验列表")
|
||
print("3. 重启程序并重新测试")
|
||
|
||
if __name__ == "__main__":
|
||
main()
|