146 lines
4.4 KiB
Python
146 lines
4.4 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
测试最终配置
|
|
根据最新的InfluxDB Data Explorer截图
|
|
"""
|
|
|
|
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 test_final_config():
|
|
"""测试最终配置"""
|
|
print("测试最终配置")
|
|
print("=" * 40)
|
|
|
|
influx_config = load_config()
|
|
|
|
print("最终配置:")
|
|
print(f" Bucket: {influx_config.get('bucket', 'PCM')}")
|
|
print(f" Measurement: {influx_config.get('measurement', 'PCM_Measurement')}")
|
|
print(f" data_type: Breaker (tag)")
|
|
print(f" _field: load_status")
|
|
|
|
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')
|
|
|
|
# 写入测试数据
|
|
point = Point(measurement) \
|
|
.tag("data_type", "Breaker") \
|
|
.field("load_status", "1") \
|
|
.time(datetime.datetime.now(datetime.timezone.utc))
|
|
|
|
write_api.write(bucket=bucket, record=point)
|
|
print("OK 写入测试数据成功:")
|
|
print(f" {measurement}.load_status = 1")
|
|
print(f" data_type = Breaker")
|
|
|
|
# 查询测试数据
|
|
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")
|
|
|> last()
|
|
'''
|
|
|
|
result = client.query_api().query(query)
|
|
|
|
if result:
|
|
for table in result:
|
|
for record in table.records:
|
|
print("OK 查询验证成功:")
|
|
print(f" 时间: {record.get_time()}")
|
|
print(f" 值: {record.get_value()}")
|
|
print(f" 字段: {record.get_field()}")
|
|
print(f" data_type: {record.values.get('data_type', 'N/A')}")
|
|
else:
|
|
print("WARNING 查询无结果")
|
|
|
|
client.close()
|
|
|
|
except Exception as e:
|
|
print(f"ERROR 测试失败: {e}")
|
|
|
|
def test_quick_script():
|
|
"""测试quick_test_data.py脚本"""
|
|
print("\n测试quick_test_data.py脚本")
|
|
print("=" * 40)
|
|
|
|
import subprocess
|
|
|
|
try:
|
|
# 测试写入状态1
|
|
result = subprocess.run(
|
|
['python', 'quick_test_data.py'],
|
|
input='3\n',
|
|
text=True,
|
|
capture_output=True,
|
|
timeout=10
|
|
)
|
|
|
|
if result.returncode == 0:
|
|
print("OK 状态1写入成功")
|
|
print(f"输出: {result.stdout.strip()}")
|
|
else:
|
|
print(f"ERROR 状态1写入失败: {result.stderr}")
|
|
|
|
except Exception as e:
|
|
print(f"ERROR 脚本测试失败: {e}")
|
|
|
|
def main():
|
|
print("最终配置测试")
|
|
print("基于最新的InfluxDB Data Explorer截图")
|
|
print("=" * 50)
|
|
print("数据结构:")
|
|
print(" Bucket: PCM")
|
|
print(" _measurement: PCM_Measurement")
|
|
print(" data_type: Breaker (tag)")
|
|
print(" _field: load_status")
|
|
print("=" * 50)
|
|
|
|
# 测试配置
|
|
test_final_config()
|
|
|
|
# 测试脚本
|
|
test_quick_script()
|
|
|
|
print("\n" + "=" * 50)
|
|
print("最终配置总结:")
|
|
print("1. Measurement: PCM_Measurement")
|
|
print("2. Field: load_status")
|
|
print("3. Tag Filter: data_type = Breaker")
|
|
print("4. 监控器将监控 load_status 字段的变化")
|
|
|
|
print("\n准备测试:")
|
|
print("1. 重启程序: python main.py")
|
|
print("2. 创建工单并进入等待状态")
|
|
print("3. 执行: echo 3 | python quick_test_data.py")
|
|
print("4. 执行: echo 2 | python quick_test_data.py")
|
|
print("5. 观察监控器检测 load_status 变化")
|
|
|
|
if __name__ == "__main__":
|
|
main()
|