This commit is contained in:
DingZQ
2025-02-14 22:25:55 +08:00
parent 21a6c54f58
commit 528c660ddc

View File

@@ -1095,7 +1095,7 @@ def query_latest_record_by_ID(ID: str, type: str, bucket: str="realtime_simulati
"""
查询指定ID的最新的一条记录
:param ID: (str): 要查询的 ID。
:param type: (str): "node"或“link”
:param type: (str): "node"或“link”'scada'
:param bucket: (str): 数据存储的 bucket 名称。
:param client: (InfluxDBClient): 已初始化的 InfluxDB 客户端实例。
:return: dict: 最新记录的数据,如果没有找到则返回 None。
@@ -1166,6 +1166,30 @@ def query_latest_record_by_ID(ID: str, type: str, bucket: str="realtime_simulati
"reaction": record["reaction"],
"friction": record["friction"]
}
elif type == "scada":
flux_query = f'''
from(bucket: "SCADA_data")
|> range(start: -30d) // 查找最近一月的记录
|> filter(fn: (r) => r["_measurement"] == "reservoir_liquid_level_realtime")
|> filter(fn: (r) => r["device_ID"] == "{ID}")
|> pivot(
rowKey:["_time"],
columnKey:["_field"],
valueColumn:"_value"
)
|> sort(columns: ["_time"], desc: true)
|> limit(n: 1)
'''
tables = query_api.query(flux_query)
# 解析查询结果
for table in tables:
for record in table.records:
return {
"time": record["_time"],
"device_ID": ID,
"value": record["_value"],
}
return None # 如果没有找到记录