feat: 工参每周一一份并提前缓存,登录后自动补缺;评估按周一取工参,指标等平台同步三天后再查
- 工参从 2024-01-01 起每周一一份,排队任务一次补一份,新的周先补;一个地市同时只取一份 - 大数据平台账号登录成功后排补取,排队进程每 10 分钟检查缺的周;排队进程重启时放开没取完的快照 - 拆除前工参用拆除日上一周的周一,拆除后用当周的周一;待拆小区不参与周边站和指标匹配 - 一周的指标等周日过后第 3 天才查,没到时间在结果说明里写明哪天之后有数据 - AI 工作区没就绪时开通接口返回 200,改为等待重试,最多 150 秒 - 排任务加锁,避免多个线程同时排时用到已关闭的连接池 - 运行面板显示各地市工参缓存进度
This commit is contained in:
+41
-21
@@ -23,6 +23,7 @@ from collections.abc import AsyncIterator
|
||||
from contextlib import asynccontextmanager
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import date
|
||||
from urllib.parse import urlsplit
|
||||
|
||||
import httpx
|
||||
|
||||
@@ -33,6 +34,7 @@ logger = logging.getLogger("decom")
|
||||
|
||||
OPENCODE = f"{ORIGIN}/pro-opencode"
|
||||
PROVISION_PID = "LXWXYWGZT"
|
||||
PROVISION_WAIT = 150.0
|
||||
WORKSPACE = "/workspace"
|
||||
TASKS_DIR = "tasks"
|
||||
MODEL_ID = "DeepSeek-V4-Flash-0731-w8a8"
|
||||
@@ -267,38 +269,56 @@ def ascending_id(prefix: str) -> str:
|
||||
|
||||
|
||||
class OpenCode:
|
||||
def __init__(self, nqi: NqiClient, *, model: str = MODEL_ID, poll_seconds: float = 3.0):
|
||||
def __init__(self, nqi: NqiClient, *, model: str = MODEL_ID, poll_seconds: float = 3.0, provision_wait: float = PROVISION_WAIT):
|
||||
self.nqi = nqi
|
||||
self.model = model
|
||||
self.poll_seconds = poll_seconds
|
||||
self.provision_wait = provision_wait
|
||||
self.ticket = nqi.ticket()
|
||||
self.base = ""
|
||||
self.headers = {"Accept": "*/*", "Origin": ORIGIN, "x-opencode-directory": "%2Fworkspace"}
|
||||
|
||||
async def provision(self) -> str:
|
||||
"""provision 会下发 opencode_route,会话页会下发 oc_castgc,之后接口才认。"""
|
||||
"""打开账号的 AI 工作区。provision 会下发 opencode_route,会话页会下发 oc_castgc,之后接口才认。
|
||||
|
||||
工作区没开过或被平台收回时,provision 返回的是 200 的页面而不是跳转,平台这时才开始准备工作区;
|
||||
所以没跳转就隔几秒再请求,直到跳转或超过 provision_wait。跳转到哪个目录都行,后面的接口一律指定 /workspace。
|
||||
"""
|
||||
if not self.ticket:
|
||||
raise NqiAuthError("登录已失效,请重新登录")
|
||||
page = {"Accept": "text/html,application/xhtml+xml,*/*;q=0.8", "Referer": f"{ORIGIN}/pro-portal/struts/portal/index.html"}
|
||||
response = await self.nqi._request(
|
||||
"GET",
|
||||
f"{OPENCODE}/api/provision",
|
||||
params={"random": str(random.random()), "__PID": PROVISION_PID, "token": self.ticket},
|
||||
headers=page,
|
||||
follow_redirects=False,
|
||||
)
|
||||
location = response.headers.get("location", "")
|
||||
if "/cas/login" in location:
|
||||
raise NqiAuthError("登录已失效,请重新登录")
|
||||
found = re.match(r"^/pro-opencode/([^/]+)/", location)
|
||||
if response.status_code not in (301, 302, 303, 307, 308) or not found:
|
||||
raise OpenCodeError("没有打开大数据平台的 AI 工作区")
|
||||
self.base = f"{OPENCODE}/{found.group(1)}"
|
||||
self.headers["Referer"] = f"{self.base}/L3dvcmtzcGFjZQ/session"
|
||||
response = await self.nqi._request("GET", f"{ORIGIN}{location}", headers=page, follow_redirects=False)
|
||||
if response.status_code != 200:
|
||||
raise OpenCodeError("没有打开大数据平台的 AI 工作区")
|
||||
return found.group(1)
|
||||
deadline = time.monotonic() + self.provision_wait
|
||||
seen = ""
|
||||
while True:
|
||||
response = await self.nqi._request(
|
||||
"GET",
|
||||
f"{OPENCODE}/api/provision",
|
||||
params={"random": str(random.random()), "__PID": PROVISION_PID, "token": self.ticket},
|
||||
headers=page,
|
||||
follow_redirects=False,
|
||||
)
|
||||
location = response.headers.get("location", "")
|
||||
if "/cas/login" in location:
|
||||
raise NqiAuthError("登录已失效,请重新登录")
|
||||
target = urlsplit(location)
|
||||
found = re.match(r"^/pro-opencode/([^/]+)/", target.path)
|
||||
if response.status_code in (301, 302, 303, 307, 308) and found and found.group(1) != "api":
|
||||
opened = await self.nqi._request(
|
||||
"GET", f"{ORIGIN}{target.path}" + (f"?{target.query}" if target.query else ""), headers=page, follow_redirects=False
|
||||
)
|
||||
if opened.status_code == 200:
|
||||
self.base = f"{OPENCODE}/{found.group(1)}"
|
||||
self.headers["Referer"] = f"{self.base}/L3dvcmtzcGFjZQ/session"
|
||||
return found.group(1)
|
||||
state = f"工作区页面返回 {opened.status_code}"
|
||||
else:
|
||||
state = f"开通接口返回 {response.status_code},没有跳转到工作区"
|
||||
if state != seen:
|
||||
seen = state
|
||||
logger.info("OpenCode 工作区还没就绪:%s", state)
|
||||
if time.monotonic() >= deadline:
|
||||
raise OpenCodeError(f"平台没有为这个账号打开 AI 工作区({state})")
|
||||
await asyncio.sleep(self.poll_seconds)
|
||||
|
||||
async def _call(self, method: str, path: str, *, params: dict | None = None, body: dict | None = None, tickets: bool = True) -> httpx.Response:
|
||||
if not self.base:
|
||||
|
||||
Reference in New Issue
Block a user