diff --git a/influxdb_api.py b/influxdb_api.py index 73e3ce4..a2ce38e 100644 --- a/influxdb_api.py +++ b/influxdb_api.py @@ -1754,6 +1754,7 @@ def query_all_record_by_time_property(query_time: str, type: str, property: str, # 执行查询 tables = query_api.query(flux_query) result_records = [] + # 解析查询结果 for table in tables: for record in table.records: @@ -1833,10 +1834,13 @@ def query_all_record_by_date(query_date: str, bucket: str="realtime_simulation_r }) return node_records, link_records +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 + """ 查询指定日期的‘node’或‘link’的某一属性值的所有记录,以指定的格式返回 :param query_date: 输入的日期,格式为‘2025-02-14’ @@ -1857,11 +1861,15 @@ def query_all_record_by_date_property(query_date: str, type: str, property: str, measurement = "link" else: raise ValueError(f"不支持的类型: {type}") - # 将 start_date 的北京时间转换为 UTC 时间 - # start_time = (datetime.strptime(query_date, "%Y-%m-%d") - timedelta(days=1)).replace(hour=16, minute=0, second=0, tzinfo=timezone.utc).isoformat() + 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个小时的数据 + # bg_end_time = bg_start_time + timedelta(hours=2) # 服务器性能不行,暂时返回2个小时的数据 utc_start_time = time_api.to_utc_time(bg_start_time) utc_end_time = time_api.to_utc_time(bg_end_time) @@ -1896,6 +1904,9 @@ def query_all_record_by_date_property(query_date: str, type: str, property: str, "ID": record["ID"], property: record["_value"] }) + + influxdb_cache[cache_key] = result_records + return result_records diff --git a/main.py b/main.py index 2fe6ff0..7b3fff1 100644 --- a/main.py +++ b/main.py @@ -2182,6 +2182,7 @@ async def fastapi_query_scada_data_by_device_id_and_date(ids: str, querydate: st 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 +# 返回所有SCADA设备在指定日期的所有记录 @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)