PCM_Report/diagnose_time_range.py

76 lines
2.2 KiB
Python
Raw Permalink Normal View History

2026-03-13 14:31:26 +08:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""诊断指定时间区间的InfluxDB数据"""
import json
import sys
from datetime import datetime, timezone, timedelta
def main():
# 写死配置
influx_config = {
"url": "http://10.0.5.232:8086",
"org": "MEASCON",
"token": "JPZMq2UP5ORhLq8CfsPbawl6k0MSDlJmEwMJ2uvR_TXqW5bUOWIYBQOSXkGNzDqOU3rnuGpIxGxrB_mlAF-EEw=="
}
bucket = "PCM"
measurement = "PCM_Measurement"
# 时间区间UTC+8转UTC
start_local = "2026-03-12T17:39:10"
end_local = "2026-03-12T21:09:17"
# 本地时间转UTC本地时间-8小时=UTC
start_time = datetime.strptime(start_local, "%Y-%m-%dT%H:%M:%S")
start_time = start_time - timedelta(hours=8)
start_time = start_time.replace(tzinfo=timezone.utc)
end_time = datetime.strptime(end_local, "%Y-%m-%dT%H:%M:%S")
end_time = end_time - timedelta(hours=8)
end_time = end_time.replace(tzinfo=timezone.utc)
print(f"查询时间: {start_time}{end_time}")
try:
from influxdb_client import InfluxDBClient
client = InfluxDBClient(
url=influx_config['url'],
org=influx_config['org'],
token=influx_config['token']
)
query_api = client.query_api()
# 查询主轴承#1数据
flux = f'''
from(bucket: "{bucket}")
|> range(start: {start_time.strftime('%Y-%m-%dT%H:%M:%SZ')}, stop: {end_time.strftime('%Y-%m-%dT%H:%M:%SZ')})
|> filter(fn: (r) => r["_measurement"] == "{measurement}")
|> filter(fn: (r) => r["data_type"] == "LSDAQ")
|> filter(fn: (r) => r["_field"] == "主轴承#1")
|> limit(n: 10)
'''
print(f"\nFlux查询:\n{flux}\n")
tables = query_api.query(flux)
count = 0
for table in tables:
for record in table.records:
count += 1
print(f"时间: {record.get_time()}, 值: {record.get_value()}")
print(f"\n共找到 {count} 条数据")
client.close()
except Exception as e:
print(f"错误: {e}", file=sys.stderr)
return 1
return 0
if __name__ == "__main__":
sys.exit(main())