PCM_Report/diagnose_time_range.py

76 lines
2.2 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/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())