109 lines
2.9 KiB
Python
109 lines
2.9 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
测试最终修复的监控器查询
|
|
"""
|
|
|
|
import sys
|
|
import os
|
|
import datetime
|
|
sys.path.append(os.path.dirname(__file__))
|
|
|
|
from influx_service import InfluxService, InfluxConnectionParams
|
|
from config_model import AppConfig
|
|
from pathlib import Path
|
|
|
|
def test_current_time_query():
|
|
"""测试当前时间的查询"""
|
|
print("🧪 测试当前时间查询")
|
|
|
|
# 加载配置
|
|
config_path = Path("default.json")
|
|
config = AppConfig.load(config_path)
|
|
|
|
# 创建InfluxDB服务
|
|
params = InfluxConnectionParams(
|
|
url=config.influx.url,
|
|
org=config.influx.org,
|
|
token=config.influx.token
|
|
)
|
|
|
|
service = InfluxService(params)
|
|
|
|
bucket = getattr(config.influx, 'bucket', 'PCM')
|
|
measurement = getattr(config.influx, 'measurement', 'experiment_status')
|
|
fields = ['status']
|
|
filters = {}
|
|
|
|
# 模拟监控器的时间处理(当前时间)
|
|
start_time = datetime.datetime.now() # 无时区的当前时间
|
|
|
|
print(f"原始时间: {start_time}")
|
|
|
|
# 应用监控器的时间处理逻辑
|
|
if hasattr(start_time, 'isoformat'):
|
|
if start_time.tzinfo is None:
|
|
start_time_utc = start_time.replace(tzinfo=datetime.timezone.utc)
|
|
else:
|
|
start_time_utc = start_time.astimezone(datetime.timezone.utc)
|
|
start_iso = start_time_utc.isoformat()
|
|
else:
|
|
start_iso = "-1h"
|
|
|
|
time_range = start_iso
|
|
|
|
print(f"处理后时间范围: {time_range}")
|
|
|
|
# 生成查询
|
|
flux = service.build_flux(
|
|
bucket=bucket,
|
|
measurement=measurement,
|
|
fields=fields,
|
|
filters=filters,
|
|
time_range=time_range
|
|
)
|
|
|
|
print("生成的查询:")
|
|
for i, line in enumerate(flux.split('\n'), 1):
|
|
if line.strip():
|
|
print(f" {i}: {line}")
|
|
|
|
# 执行查询
|
|
try:
|
|
df = service.query(
|
|
bucket=bucket,
|
|
measurement=measurement,
|
|
fields=fields,
|
|
filters=filters,
|
|
time_range=time_range
|
|
)
|
|
|
|
print(f"✅ 查询成功! 返回 {len(df)} 行数据")
|
|
if not df.empty:
|
|
print("数据预览:")
|
|
print(df.head())
|
|
|
|
except Exception as e:
|
|
print(f"❌ 查询失败: {e}")
|
|
|
|
# 尝试使用相对时间作为备选
|
|
print("\n🔄 尝试使用相对时间...")
|
|
try:
|
|
df_fallback = service.query(
|
|
bucket=bucket,
|
|
measurement=measurement,
|
|
fields=fields,
|
|
filters=filters,
|
|
time_range="-1h"
|
|
)
|
|
print(f"✅ 相对时间查询成功! 返回 {len(df_fallback)} 行数据")
|
|
except Exception as e2:
|
|
print(f"❌ 相对时间查询也失败: {e2}")
|
|
|
|
def main():
|
|
print("监控器最终修复测试")
|
|
print("=" * 30)
|
|
test_current_time_query()
|
|
|
|
if __name__ == "__main__":
|
|
main()
|