169 lines
5.5 KiB
Python
169 lines
5.5 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
验证load_status修正
|
|
"""
|
|
|
|
import json
|
|
import os
|
|
from influxdb_client import InfluxDBClient
|
|
|
|
def load_config():
|
|
"""从default.json加载配置"""
|
|
config_path = os.path.join(os.path.dirname(__file__), "default.json")
|
|
try:
|
|
with open(config_path, 'r', encoding='utf-8') as f:
|
|
config = json.load(f)
|
|
return config.get('influx', {})
|
|
except Exception as e:
|
|
print(f"加载配置文件失败: {e}")
|
|
return {}
|
|
|
|
def verify_new_measurement():
|
|
"""验证新的measurement中的数据"""
|
|
print("验证新measurement中的数据")
|
|
print("=" * 40)
|
|
|
|
influx_config = load_config()
|
|
|
|
try:
|
|
client = InfluxDBClient(
|
|
url=influx_config.get('url', 'http://127.0.0.1:8086'),
|
|
token=influx_config.get('token', ''),
|
|
org=influx_config.get('org', 'MEASCON')
|
|
)
|
|
|
|
bucket = influx_config.get('bucket', 'PCM')
|
|
measurement = 'PCM_Measurement' # 新的measurement名称
|
|
|
|
# 查询最近的数据
|
|
query = f'''
|
|
from(bucket: "{bucket}")
|
|
|> range(start: -10m)
|
|
|> filter(fn: (r) => r["_measurement"] == "{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(f"measurement '{measurement}' 中的load_status数据:")
|
|
for table in result:
|
|
for record in table.records:
|
|
value = record.get_value()
|
|
value_type = type(value).__name__
|
|
print(f" 时间: {record.get_time()}")
|
|
print(f" 值: {value} (类型: {value_type})")
|
|
|
|
if isinstance(value, int):
|
|
print(" ✅ 值是整数类型")
|
|
else:
|
|
print(f" ❌ 值不是整数类型: {value_type}")
|
|
print("-" * 20)
|
|
else:
|
|
print(f"没有在measurement '{measurement}' 中查询到load_status数据")
|
|
|
|
client.close()
|
|
|
|
except Exception as e:
|
|
print(f"ERROR 验证失败: {e}")
|
|
|
|
def check_old_vs_new():
|
|
"""对比旧measurement和新measurement"""
|
|
print("\n对比旧measurement和新measurement")
|
|
print("=" * 40)
|
|
|
|
influx_config = load_config()
|
|
|
|
try:
|
|
client = InfluxDBClient(
|
|
url=influx_config.get('url', 'http://127.0.0.1:8086'),
|
|
token=influx_config.get('token', ''),
|
|
org=influx_config.get('org', 'MEASCON')
|
|
)
|
|
|
|
bucket = influx_config.get('bucket', 'PCM')
|
|
|
|
# 检查旧measurement
|
|
old_measurement = 'PCM_Measurement'
|
|
query_old = f'''
|
|
from(bucket: "{bucket}")
|
|
|> range(start: -1h)
|
|
|> filter(fn: (r) => r["_measurement"] == "{old_measurement}")
|
|
|> filter(fn: (r) => r["_field"] == "load_status")
|
|
|> limit(n: 1)
|
|
'''
|
|
|
|
result_old = client.query_api().query(query_old)
|
|
|
|
if result_old:
|
|
for table in result_old:
|
|
for record in table.records:
|
|
value = record.get_value()
|
|
print(f"旧measurement '{old_measurement}':")
|
|
print(f" load_status类型: {type(value).__name__}")
|
|
break
|
|
else:
|
|
print(f"旧measurement '{old_measurement}' 中没有load_status数据")
|
|
|
|
# 检查新measurement
|
|
new_measurement = 'PCM_Measurement'
|
|
query_new = f'''
|
|
from(bucket: "{bucket}")
|
|
|> range(start: -1h)
|
|
|> filter(fn: (r) => r["_measurement"] == "{new_measurement}")
|
|
|> filter(fn: (r) => r["_field"] == "load_status")
|
|
|> limit(n: 1)
|
|
'''
|
|
|
|
result_new = client.query_api().query(query_new)
|
|
|
|
if result_new:
|
|
for table in result_new:
|
|
for record in table.records:
|
|
value = record.get_value()
|
|
print(f"新measurement '{new_measurement}':")
|
|
print(f" load_status类型: {type(value).__name__}")
|
|
break
|
|
else:
|
|
print(f"新measurement '{new_measurement}' 中没有load_status数据")
|
|
|
|
client.close()
|
|
|
|
except Exception as e:
|
|
print(f"ERROR 对比失败: {e}")
|
|
|
|
def main():
|
|
print("load_status修正验证")
|
|
print("=" * 50)
|
|
print("修正内容:")
|
|
print("- 字段名: load_status (保持原名)")
|
|
print("- 数据类型: 整数 (不是字符串)")
|
|
print("- measurement: PCM_Measurement (避免类型冲突)")
|
|
print("=" * 50)
|
|
|
|
# 1. 验证新measurement中的数据
|
|
verify_new_measurement()
|
|
|
|
# 2. 对比旧measurement和新measurement
|
|
check_old_vs_new()
|
|
|
|
print("\n" + "=" * 50)
|
|
print("修正总结:")
|
|
print("✅ 字段名: load_status (不是 load_status_int)")
|
|
print("✅ 数据类型: 整数 1 和 0")
|
|
print("✅ measurement: PCM_Measurement")
|
|
print("✅ 监控配置: 使用新的measurement")
|
|
print("✅ 测试脚本: 写入整数到load_status字段")
|
|
|
|
print("\n现在可以:")
|
|
print("1. 重启程序")
|
|
print("2. 创建工单并进入等待状态")
|
|
print("3. 使用测试脚本写入状态变化")
|
|
print("4. 监控器将正确检测整数值变化")
|
|
|
|
if __name__ == "__main__":
|
|
main()
|