Cache scada data

This commit is contained in:
DingZQ
2025-03-08 23:27:10 +08:00
parent ad029b1c92
commit fc2635ada7

View File

@@ -1395,6 +1395,7 @@ def query_SCADA_data_by_device_ID_and_time_range(query_ids_list: List[str], star
# 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数据
@@ -1404,6 +1405,13 @@ def query_all_SCADA_records_by_date(query_date: str, bucket: str="SCADA_data", c
:return:
"""
global influxdb_cache
cache_key = f"{query_date}"
if influxdb_cache.get(cache_key) is not None:
return influxdb_cache.get(cache_key)
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')))
@@ -1449,11 +1457,11 @@ def query_all_SCADA_records_by_date(query_date: str, bucket: str="SCADA_data", c
except Exception as e:
print(f"Error querying InfluxDB for date {query_date}: {e}")
influxdb_cache[cache_key] = SCADA_results
return SCADA_results
# 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]]:
"""
@@ -1839,8 +1847,6 @@ influxdb_cache = {}
# 2025/02/21 WMH
def query_all_record_by_date_property(query_date: str, type: str, property: str,
bucket: str="realtime_simulation_result", client: InfluxDBClient=client) -> list:
global influxdb_cache
"""
查询指定日期的nodelink的某一属性值的所有记录以指定的格式返回
:param query_date: 输入的日期格式为2025-02-14
@@ -1850,6 +1856,13 @@ def query_all_record_by_date_property(query_date: str, type: str, property: str,
:param client: 已初始化的InfluxDBClient 实例。
:return: list(dict): result_records
"""
global influxdb_cache
cache_key = f"{query_date}_{type}_{property}"
if influxdb_cache.get(cache_key) is not None:
return influxdb_cache.get(cache_key)
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')))
@@ -1862,11 +1875,6 @@ def query_all_record_by_date_property(query_date: str, type: str, property: str,
else:
raise ValueError(f"不支持的类型: {type}")
cache_key = f"{query_date}_{type}_{property}"
if influxdb_cache.get(cache_key) is not None:
return influxdb_cache.get(cache_key)
# 将 start_date 的北京时间转换为 UTC 时间
bg_start_time, bg_end_time = time_api.parse_beijing_date_range(query_date)
# bg_end_time = bg_start_time + timedelta(hours=2) # 服务器性能不行暂时返回2个小时的数据