124 lines
3.8 KiB
Python
124 lines
3.8 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_time_range_formats():
|
|||
|
|
"""测试不同的时间范围格式"""
|
|||
|
|
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 = {}
|
|||
|
|
|
|||
|
|
# 测试不同的时间范围格式
|
|||
|
|
test_cases = [
|
|||
|
|
("-1h", "相对时间(1小时前)"),
|
|||
|
|
("-30m", "相对时间(30分钟前)"),
|
|||
|
|
('"2025-11-25T15:00:00Z"', "绝对时间(带引号和时区)"),
|
|||
|
|
('2025-11-25T15:00:00Z', "绝对时间(无引号)- 应该失败"),
|
|||
|
|
]
|
|||
|
|
|
|||
|
|
for time_range, description in test_cases:
|
|||
|
|
print(f"\n🔍 测试: {description}")
|
|||
|
|
print(f" 时间范围: {time_range}")
|
|||
|
|
|
|||
|
|
try:
|
|||
|
|
# 生成查询
|
|||
|
|
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}")
|
|||
|
|
|
|||
|
|
# 执行查询
|
|||
|
|
df = service.query(
|
|||
|
|
bucket=bucket,
|
|||
|
|
measurement=measurement,
|
|||
|
|
fields=fields,
|
|||
|
|
filters=filters,
|
|||
|
|
time_range=time_range
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
print(f" ✅ 查询成功! 返回 {len(df)} 行数据")
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f" ❌ 查询失败: {e}")
|
|||
|
|
|
|||
|
|
def test_monitor_time_logic():
|
|||
|
|
"""测试监控器的时间逻辑"""
|
|||
|
|
print("\n🕐 测试监控器时间逻辑")
|
|||
|
|
|
|||
|
|
# 模拟不同的start_time情况
|
|||
|
|
test_times = [
|
|||
|
|
(datetime.datetime.now(), "当前时间(无时区)"),
|
|||
|
|
(datetime.datetime.now(datetime.timezone.utc), "当前时间(UTC时区)"),
|
|||
|
|
("2025-11-25T15:00:00", "字符串时间"),
|
|||
|
|
(None, "None值"),
|
|||
|
|
]
|
|||
|
|
|
|||
|
|
for start_time, description in test_times:
|
|||
|
|
print(f"\n🔍 测试: {description}")
|
|||
|
|
print(f" start_time: {start_time}")
|
|||
|
|
|
|||
|
|
try:
|
|||
|
|
# 模拟监控器的时间处理逻辑
|
|||
|
|
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"
|
|||
|
|
|
|||
|
|
if start_iso.startswith('-'):
|
|||
|
|
time_range = start_iso
|
|||
|
|
else:
|
|||
|
|
time_range = f'"{start_iso}"'
|
|||
|
|
|
|||
|
|
print(f" 处理后的时间范围: {time_range}")
|
|||
|
|
print(" ✅ 时间处理成功")
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f" ❌ 时间处理失败: {e}")
|
|||
|
|
|
|||
|
|
def main():
|
|||
|
|
print("监控器修复测试工具")
|
|||
|
|
print("=" * 40)
|
|||
|
|
test_time_range_formats()
|
|||
|
|
test_monitor_time_logic()
|
|||
|
|
|
|||
|
|
if __name__ == "__main__":
|
|||
|
|
main()
|