194 lines
6.4 KiB
Python
194 lines
6.4 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
测试load_status字段写入整数数据
|
|
"""
|
|
|
|
import json
|
|
import os
|
|
from influxdb_client import InfluxDBClient, Point
|
|
from influxdb_client.client.write_api import SYNCHRONOUS
|
|
import datetime
|
|
|
|
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 clear_old_load_status_data():
|
|
"""清理旧的load_status数据"""
|
|
print("清理旧的load_status数据")
|
|
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 = influx_config.get('measurement', 'PCM_Measurement')
|
|
|
|
# 删除最近1小时的load_status数据
|
|
from influxdb_client.client.delete_api import DeleteApi
|
|
delete_api = client.delete_api()
|
|
|
|
start = datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(hours=1)
|
|
stop = datetime.datetime.now(datetime.timezone.utc)
|
|
|
|
# 删除指定measurement和field的数据
|
|
predicate = f'_measurement="{measurement}" AND _field="load_status" AND data_type="Breaker"'
|
|
|
|
try:
|
|
delete_api.delete(
|
|
start=start,
|
|
stop=stop,
|
|
predicate=predicate,
|
|
bucket=bucket,
|
|
org=influx_config.get('org', 'MEASCON')
|
|
)
|
|
print("OK 已清理旧的load_status数据")
|
|
except Exception as e:
|
|
print(f"WARNING 清理失败 (可能不支持删除): {e}")
|
|
|
|
client.close()
|
|
|
|
except Exception as e:
|
|
print(f"ERROR 清理失败: {e}")
|
|
|
|
def write_integer_load_status():
|
|
"""写入整数类型的load_status数据"""
|
|
print("\n写入整数类型的load_status数据")
|
|
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')
|
|
)
|
|
write_api = client.write_api(write_options=SYNCHRONOUS)
|
|
|
|
measurement = influx_config.get('measurement', 'PCM_Measurement')
|
|
bucket = influx_config.get('bucket', 'PCM')
|
|
|
|
now = datetime.datetime.now(datetime.timezone.utc)
|
|
|
|
# 写入测试数据序列
|
|
test_data = [
|
|
(now - datetime.timedelta(minutes=5), 0, "5分钟前: 状态0"),
|
|
(now - datetime.timedelta(minutes=3), 1, "3分钟前: 状态1"),
|
|
(now - datetime.timedelta(minutes=1), 0, "1分钟前: 状态0"),
|
|
(now, 1, "现在: 状态1"),
|
|
]
|
|
|
|
for timestamp, value, description in test_data:
|
|
point = Point(measurement) \
|
|
.tag("data_type", "Breaker") \
|
|
.field("load_status", value) \
|
|
.time(timestamp)
|
|
|
|
write_api.write(bucket=bucket, record=point)
|
|
print(f"OK {description} - load_status = {value} (整数)")
|
|
|
|
client.close()
|
|
|
|
except Exception as e:
|
|
print(f"ERROR 写入失败: {e}")
|
|
|
|
def verify_integer_data():
|
|
"""验证写入的数据类型"""
|
|
print("\n验证写入的数据类型")
|
|
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')
|
|
)
|
|
|
|
measurement = influx_config.get('measurement', 'PCM_Measurement')
|
|
bucket = influx_config.get('bucket', 'PCM')
|
|
|
|
# 查询最近的数据
|
|
query = f'''
|
|
from(bucket: "{bucket}")
|
|
|> range(start: -1h)
|
|
|> 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: 10)
|
|
'''
|
|
|
|
result = client.query_api().query(query)
|
|
|
|
if result:
|
|
print("最新的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("没有查询到load_status数据")
|
|
|
|
client.close()
|
|
|
|
except Exception as e:
|
|
print(f"ERROR 验证失败: {e}")
|
|
|
|
def main():
|
|
print("load_status字段整数数据测试")
|
|
print("=" * 50)
|
|
print("修正说明:")
|
|
print("- 字段名: load_status (不是 load_status_int)")
|
|
print("- 数据类型: 整数 (不是字符串)")
|
|
print("- 监控配置: 使用 load_status 字段")
|
|
print("=" * 50)
|
|
|
|
# 1. 清理旧数据 (可选)
|
|
clear_old_load_status_data()
|
|
|
|
# 2. 写入整数数据
|
|
write_integer_load_status()
|
|
|
|
# 3. 验证数据类型
|
|
verify_integer_data()
|
|
|
|
print("\n" + "=" * 50)
|
|
print("修正总结:")
|
|
print("✅ UI监控配置: fields=['load_status']")
|
|
print("✅ 测试脚本: .field('load_status', int(value))")
|
|
print("✅ 数据类型: 整数 1 和 0")
|
|
print("✅ 字段名称: load_status (保持原名)")
|
|
|
|
print("\n现在可以:")
|
|
print("1. 使用 echo 3 | python quick_test_data.py 写入状态1")
|
|
print("2. 使用 echo 2 | python quick_test_data.py 写入状态0")
|
|
print("3. 监控器将正确检测到整数值变化")
|
|
print("4. 字段名保持为 load_status")
|
|
|
|
if __name__ == "__main__":
|
|
main()
|