Add API getallscadarecordsbydate
This commit is contained in:
@@ -1395,6 +1395,66 @@ def query_SCADA_data_by_device_ID_and_time_range(query_ids_list: List[str], star
|
|||||||
|
|
||||||
return SCADA_results
|
return SCADA_results
|
||||||
|
|
||||||
|
# DingZQ, 2025-03-08
|
||||||
|
def query_all_SCADA_records_by_date(query_date: str, bucket: str="SCADA_data", client: InfluxDBClient=client) -> list[dict[str, float]]:
|
||||||
|
"""
|
||||||
|
根据日期查询所有SCADA数据
|
||||||
|
|
||||||
|
:param query_date: 输入的日期,格式为 '2024-11-24', 日期是北京时间的日期
|
||||||
|
:param bucket: InfluxDB 的 bucket 名称,默认值为 "SCADA_data"。
|
||||||
|
:param client: 已初始化的 InfluxDBClient 实例。
|
||||||
|
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
if client.ping(): print("{} -- Successfully connected to InfluxDB.".format( datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
|
||||||
|
else: print("{} -- Failed to connect to InfluxDB.".format( datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
|
||||||
|
|
||||||
|
query_api = client.query_api()
|
||||||
|
# 将北京时间转换为 UTC 时间
|
||||||
|
|
||||||
|
bg_start_time, bg_end_time = time_api.parse_beijing_date_range(query_date)
|
||||||
|
utc_start_time = bg_start_time.astimezone(timezone.utc)
|
||||||
|
utc_end_time = bg_end_time.astimezone(timezone.utc)
|
||||||
|
|
||||||
|
print(f"utc_start_time: {utc_start_time}, utc_end_time: {utc_end_time}")
|
||||||
|
|
||||||
|
# 构建查询字典
|
||||||
|
SCADA_results = []
|
||||||
|
|
||||||
|
# 构建 Flux 查询语句
|
||||||
|
flux_query = f'''
|
||||||
|
from(bucket: "{bucket}")
|
||||||
|
|> range(start: {utc_start_time.isoformat()}, stop: {utc_end_time.isoformat()})
|
||||||
|
|> filter(fn: (r) => r["_field"] == "monitored_value")
|
||||||
|
|> sort(columns: ["_time"], desc: false)
|
||||||
|
'''
|
||||||
|
|
||||||
|
# 执行查询
|
||||||
|
try:
|
||||||
|
result = query_api.query(flux_query)
|
||||||
|
|
||||||
|
# 从查询结果中提取 monitored_value
|
||||||
|
if result:
|
||||||
|
# 假设返回的结果为一行数据
|
||||||
|
for table in result:
|
||||||
|
for record in table.records:
|
||||||
|
# 获取字段 "_value" 即为 monitored_value
|
||||||
|
monitored_value = record.get_value()
|
||||||
|
rec = {
|
||||||
|
"ID": record['device_ID'],
|
||||||
|
"time": record.get_time(),
|
||||||
|
"value": monitored_value
|
||||||
|
}
|
||||||
|
SCADA_results.append(rec)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error querying InfluxDB for date {query_date}: {e}")
|
||||||
|
|
||||||
|
return SCADA_results
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# DingZQ, 2025-02-15
|
# DingZQ, 2025-02-15
|
||||||
def query_SCADA_data_by_device_ID_and_date(query_ids_list: List[str], query_date: str, bucket: str="SCADA_data", client: InfluxDBClient=client) -> list[dict[str, float]]:
|
def query_SCADA_data_by_device_ID_and_date(query_ids_list: List[str], query_date: str, bucket: str="SCADA_data", client: InfluxDBClient=client) -> list[dict[str, float]]:
|
||||||
"""
|
"""
|
||||||
|
|||||||
5
main.py
5
main.py
@@ -2181,6 +2181,11 @@ async def fastapi_query_scada_data_by_device_id_and_date(ids: str, querydate: st
|
|||||||
query_ids = ids.split(',')
|
query_ids = ids.split(',')
|
||||||
return influxdb_api.query_SCADA_data_by_device_ID_and_date(query_ids_list=query_ids, query_date=querydate, client=influx_client)
|
return influxdb_api.query_SCADA_data_by_device_ID_and_date(query_ids_list=query_ids, query_date=querydate, client=influx_client)
|
||||||
|
|
||||||
|
# DingZQ, 2025-03-08
|
||||||
|
@app.get("/queryallscadarecordsbydate/")
|
||||||
|
async def fastapi_query_all_scada_records_by_date(querydate: str):
|
||||||
|
return influxdb_api.query_all_SCADA_records_by_date(query_date=querydate, client=influx_client)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@app.get("/queryinfluxdbbuckets/")
|
@app.get("/queryinfluxdbbuckets/")
|
||||||
|
|||||||
Reference in New Issue
Block a user