You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
2.0 KiB
60 lines
2.0 KiB
# -*- coding: utf-8 -*-
|
|
"""Alpha表达式服务"""
|
|
|
|
import time
|
|
import httpx
|
|
from typing import List, Dict, Any
|
|
from config.settings import settings
|
|
|
|
|
|
class AlphaService:
|
|
"""Alpha表达式服务类"""
|
|
|
|
def __init__(self, client: httpx.Client):
|
|
self.client = client
|
|
|
|
def simulate_alpha(self, expression: str) -> Dict[str, Any]:
|
|
"""模拟单个Alpha表达式"""
|
|
simulation_data = {
|
|
'type': 'REGULAR',
|
|
'settings': settings.SIMULATION_SETTINGS,
|
|
'regular': expression
|
|
}
|
|
|
|
sim_resp = self.client.post(f'{settings.BRAIN_API_URL}/simulations', json=simulation_data)
|
|
print(f"模拟提交状态: {sim_resp.status_code}")
|
|
|
|
if 'location' not in sim_resp.headers:
|
|
return {"status": "err", "message": "No location header in response"}
|
|
|
|
sim_progress_url = sim_resp.headers['location']
|
|
return self._wait_for_simulation_result(sim_progress_url)
|
|
|
|
def _wait_for_simulation_result(self, progress_url: str) -> Dict[str, Any]:
|
|
"""等待模拟结果"""
|
|
while True:
|
|
sim_progress_resp = self.client.get(progress_url)
|
|
retry_after_sec = float(sim_progress_resp.headers.get("Retry-After", 0))
|
|
|
|
if retry_after_sec == 0:
|
|
break
|
|
|
|
if sim_progress_resp.json():
|
|
result = sim_progress_resp.json()
|
|
progress = result.get('progress', 0)
|
|
print(f"模拟进度: {float(progress) * 100}%")
|
|
|
|
print(f"等待 {retry_after_sec} 秒...")
|
|
time.sleep(retry_after_sec)
|
|
|
|
result_data = sim_progress_resp.json()
|
|
|
|
if result_data.get("status") == "ERROR":
|
|
error_message = result_data.get("message", "未知错误")
|
|
print(f"因子模拟失败: {error_message}")
|
|
return {"status": "err", "message": error_message}
|
|
|
|
alpha_id = result_data.get("alpha")
|
|
print(f"生成的Alpha ID: {alpha_id}")
|
|
|
|
return {"status": "ok", "alpha_id": alpha_id} |