153 lines
4.7 KiB
Python
153 lines
4.7 KiB
Python
|
|
#!/usr/bin/env python3
|
|||
|
|
"""
|
|||
|
|
调试数据写入问题
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
import datetime
|
|||
|
|
import json
|
|||
|
|
import os
|
|||
|
|
from influxdb_client import InfluxDBClient, Point
|
|||
|
|
from influxdb_client.client.write_api import SYNCHRONOUS
|
|||
|
|
|
|||
|
|
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 debug_write_and_read():
|
|||
|
|
"""调试写入和读取过程"""
|
|||
|
|
config = load_config()
|
|||
|
|
|
|||
|
|
url = config.get('url', '')
|
|||
|
|
org = config.get('org', '')
|
|||
|
|
token = config.get('token', '')
|
|||
|
|
bucket = config.get('bucket', 'PCM')
|
|||
|
|
measurement = config.get('measurement', 'experiment_status')
|
|||
|
|
|
|||
|
|
print("🔧 调试写入和读取")
|
|||
|
|
print(f"URL: {url}")
|
|||
|
|
print(f"Org: {org}")
|
|||
|
|
print(f"Token: {token[:20]}...")
|
|||
|
|
print(f"Bucket: {bucket}")
|
|||
|
|
print(f"Measurement: {measurement}")
|
|||
|
|
print()
|
|||
|
|
|
|||
|
|
try:
|
|||
|
|
client = InfluxDBClient(url=url, token=token, org=org)
|
|||
|
|
|
|||
|
|
# 1. 写入测试数据
|
|||
|
|
print("1️⃣ 写入测试数据...")
|
|||
|
|
write_api = client.write_api(write_options=SYNCHRONOUS)
|
|||
|
|
|
|||
|
|
now = datetime.datetime.now()
|
|||
|
|
print(f"当前时间: {now}")
|
|||
|
|
|
|||
|
|
# 创建数据点
|
|||
|
|
point = Point(measurement) \
|
|||
|
|
.field("status", "1") \
|
|||
|
|
.field("temperature", 25.5) \
|
|||
|
|
.field("debug_timestamp", now.isoformat()) \
|
|||
|
|
.time(now)
|
|||
|
|
|
|||
|
|
print(f"数据点: {point}")
|
|||
|
|
|
|||
|
|
# 写入数据
|
|||
|
|
write_api.write(bucket=bucket, record=point)
|
|||
|
|
print("✅ 数据写入成功")
|
|||
|
|
|
|||
|
|
# 2. 立即读取数据
|
|||
|
|
print("\n2️⃣ 立即读取数据...")
|
|||
|
|
query_api = client.query_api()
|
|||
|
|
|
|||
|
|
# 查询最近5分钟的数据
|
|||
|
|
query = f'''
|
|||
|
|
from(bucket: "{bucket}")
|
|||
|
|
|> range(start: -5m)
|
|||
|
|
|> filter(fn: (r) => r["_measurement"] == "{measurement}")
|
|||
|
|
|> sort(columns: ["_time"], desc: true)
|
|||
|
|
|> limit(n: 5)
|
|||
|
|
'''
|
|||
|
|
|
|||
|
|
print("查询语句:")
|
|||
|
|
print(query)
|
|||
|
|
print()
|
|||
|
|
|
|||
|
|
result = query_api.query(query)
|
|||
|
|
|
|||
|
|
data_found = False
|
|||
|
|
for table in result:
|
|||
|
|
for record in table.records:
|
|||
|
|
data_found = True
|
|||
|
|
print(f"时间: {record.get_time()}")
|
|||
|
|
print(f"字段: {record.get_field()}")
|
|||
|
|
print(f"值: {record.get_value()}")
|
|||
|
|
print("---")
|
|||
|
|
|
|||
|
|
if not data_found:
|
|||
|
|
print("❌ 没有找到刚写入的数据")
|
|||
|
|
|
|||
|
|
# 3. 尝试查询更大的时间范围
|
|||
|
|
print("\n3️⃣ 扩大时间范围查询...")
|
|||
|
|
query2 = f'''
|
|||
|
|
from(bucket: "{bucket}")
|
|||
|
|
|> range(start: -1h)
|
|||
|
|
|> filter(fn: (r) => r["_measurement"] == "{measurement}")
|
|||
|
|
|> sort(columns: ["_time"], desc: true)
|
|||
|
|
|> limit(n: 10)
|
|||
|
|
'''
|
|||
|
|
|
|||
|
|
result2 = query_api.query(query2)
|
|||
|
|
|
|||
|
|
data_found2 = False
|
|||
|
|
for table in result2:
|
|||
|
|
for record in table.records:
|
|||
|
|
data_found2 = True
|
|||
|
|
print(f"时间: {record.get_time()}")
|
|||
|
|
print(f"字段: {record.get_field()}")
|
|||
|
|
print(f"值: {record.get_value()}")
|
|||
|
|
print("---")
|
|||
|
|
|
|||
|
|
if not data_found2:
|
|||
|
|
print("❌ 1小时内也没有找到数据")
|
|||
|
|
|
|||
|
|
# 4. 检查bucket中是否有任何experiment_status数据
|
|||
|
|
print("\n4️⃣ 检查所有experiment_status数据...")
|
|||
|
|
query3 = f'''
|
|||
|
|
from(bucket: "{bucket}")
|
|||
|
|
|> range(start: -24h)
|
|||
|
|
|> filter(fn: (r) => r["_measurement"] == "{measurement}")
|
|||
|
|
|> count()
|
|||
|
|
'''
|
|||
|
|
|
|||
|
|
result3 = query_api.query(query3)
|
|||
|
|
|
|||
|
|
total_count = 0
|
|||
|
|
for table in result3:
|
|||
|
|
for record in table.records:
|
|||
|
|
total_count += record.get_value()
|
|||
|
|
|
|||
|
|
print(f"24小时内总数据量: {total_count}")
|
|||
|
|
else:
|
|||
|
|
print("✅ 成功找到刚写入的数据")
|
|||
|
|
|
|||
|
|
client.close()
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"❌ 操作失败: {e}")
|
|||
|
|
import traceback
|
|||
|
|
traceback.print_exc()
|
|||
|
|
|
|||
|
|
def main():
|
|||
|
|
print("数据写入调试工具")
|
|||
|
|
print("=" * 50)
|
|||
|
|
debug_write_and_read()
|
|||
|
|
|
|||
|
|
if __name__ == "__main__":
|
|||
|
|
main()
|