from typing import List from fastapi.logger import logger from timescaledb.schemas.scheme import SchemeRepository from timescaledb.schemas.realtime import RealtimeRepository import timescaledb.timescaledb_info as timescaledb_info import psycopg import time # 内部使用存储类 class InternalStorage: @staticmethod def store_realtime_simulation( node_result_list: List[dict], link_result_list: List[dict], result_start_time: str, db_name: str = None, max_retries: int = 3, ): """存储实时模拟结果""" for attempt in range(max_retries): try: conn_string = ( timescaledb_info.get_pgconn_string(db_name=db_name) if db_name else timescaledb_info.get_pgconn_string() ) with psycopg.Connection.connect(conn_string) as conn: starttime = time.time() RealtimeRepository.store_realtime_simulation_result_sync( conn, node_result_list, link_result_list, result_start_time ) endtime = time.time() logger.info(f"存储实时模拟结果耗时: {endtime - starttime} 秒") break # 成功 except Exception as e: logger.error(f"存储尝试 {attempt + 1} 失败: {e}") if attempt < max_retries - 1: time.sleep(1) # 重试前等待 else: raise # 达到最大重试次数后抛出异常 @staticmethod def store_scheme_simulation( scheme_type: str, scheme_name: str, node_result_list: List[dict], link_result_list: List[dict], result_start_time: str, num_periods: int = 1, db_name: str = None, max_retries: int = 3, ): """存储方案模拟结果""" for attempt in range(max_retries): try: conn_string = ( timescaledb_info.get_pgconn_string(db_name=db_name) if db_name else timescaledb_info.get_pgconn_string() ) with psycopg.Connection.connect(conn_string) as conn: SchemeRepository.store_scheme_simulation_result_sync( conn, scheme_type, scheme_name, node_result_list, link_result_list, result_start_time, num_periods, ) break # 成功 except Exception as e: logger.error(f"存储尝试 {attempt + 1} 失败: {e}") if attempt < max_retries - 1: time.sleep(1) # 重试前等待 else: raise # 达到最大重试次数后抛出异常