feat: 工参每周一一份并提前缓存,登录后自动补缺;评估按周一取工参,指标等平台同步三天后再查
- 工参从 2024-01-01 起每周一一份,排队任务一次补一份,新的周先补;一个地市同时只取一份 - 大数据平台账号登录成功后排补取,排队进程每 10 分钟检查缺的周;排队进程重启时放开没取完的快照 - 拆除前工参用拆除日上一周的周一,拆除后用当周的周一;待拆小区不参与周边站和指标匹配 - 一周的指标等周日过后第 3 天才查,没到时间在结果说明里写明哪天之后有数据 - AI 工作区没就绪时开通接口返回 200,改为等待重试,最多 150 秒 - 排任务加锁,避免多个线程同时排时用到已关闭的连接池 - 运行面板显示各地市工参缓存进度
This commit is contained in:
@@ -12,6 +12,7 @@ from sqlalchemy import delete, select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.api.deps import Actor, current_actor
|
||||
from app.collect.gongcan import short_city
|
||||
from app.db import get_session
|
||||
from app.evaluate.compute import NEIGHBOR_SITES
|
||||
from app.evaluate.demand import (
|
||||
@@ -49,27 +50,19 @@ def _user(actor: Actor) -> Actor:
|
||||
return actor
|
||||
|
||||
|
||||
def _short_city(name: str) -> str:
|
||||
text = (name or "").strip()
|
||||
for suffix in ("分公司", "市"):
|
||||
if text.endswith(suffix) and len(text) > len(suffix):
|
||||
text = text[: -len(suffix)]
|
||||
return text
|
||||
|
||||
|
||||
def _default_city(session: Session, actor: Actor) -> str:
|
||||
if actor.kind == "nqi":
|
||||
account = session.scalar(select(NqiAccount).where(NqiAccount.login_id == actor.id))
|
||||
if account is not None and account.city_name:
|
||||
return _short_city(account.city_name)
|
||||
return short_city(account.city_name)
|
||||
pooled = session.scalar(select(NqiAccount.city_name).where(NqiAccount.city_name != "").order_by(NqiAccount.saved_at.desc()))
|
||||
if pooled:
|
||||
return _short_city(pooled)
|
||||
return short_city(pooled)
|
||||
return session.scalar(select(GongcanSnapshot.city).order_by(GongcanSnapshot.id.desc())) or ""
|
||||
|
||||
|
||||
def _check_city(value: object) -> str:
|
||||
city = _short_city(str(value or ""))
|
||||
city = short_city(value)
|
||||
if not city:
|
||||
raise _bad("请填写地市")
|
||||
if len(city) > 32:
|
||||
|
||||
+15
-1
@@ -2,6 +2,7 @@ from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import base64
|
||||
import logging
|
||||
import re
|
||||
from datetime import date, datetime, timedelta, timezone
|
||||
|
||||
@@ -15,6 +16,7 @@ from app.config import env_database, hosted, installed
|
||||
from app.install import InstallError, perform_install
|
||||
from app.db import get_session, session_scope
|
||||
from app.models import AppSetting, CollectionSource, NqiAccount, User, utcnow
|
||||
from app.collect.gongcan_sync import cache_status, request_sync
|
||||
from app.collect.schedule import next_run_at, parse_schedule
|
||||
from app.nqi.adhoc import TIME_MODES, catalog_tree, categories, search_by_field, table_fields, tables, time_window
|
||||
from app.nqi.client import NqiAuthError, NqiClient, NqiError
|
||||
@@ -22,6 +24,7 @@ from app.nqi.flows import drop, purge, remember, take
|
||||
from app.security import hash_password, issue_token, verify_password
|
||||
from app.tasks.runner import check_concurrency, check_refresh, enqueue_collection
|
||||
|
||||
logger = logging.getLogger("decom")
|
||||
router = APIRouter(prefix="/api")
|
||||
# 在响应发出前提交,客户端紧接着发出的请求才能读到刚写入的内容。
|
||||
db_session = Depends(get_session, scope="function")
|
||||
@@ -282,6 +285,9 @@ async def nqi_finish(body: dict) -> dict:
|
||||
account.credential_valid = adhoc_ready
|
||||
login_id = account.login_id
|
||||
name = account.user_name or account.login_id
|
||||
city = account.city_name
|
||||
if adhoc_ready:
|
||||
await asyncio.to_thread(_cache_gongcan, city)
|
||||
if purpose == "pool":
|
||||
return {"status": "ok"}
|
||||
return _token_for("nqi", login_id, "user", name)
|
||||
@@ -400,7 +406,7 @@ def update_user_password(user_id: int, body: dict, actor: Actor = Depends(curren
|
||||
def admin_dashboard(actor: Actor = Depends(current_actor), session: Session = db_session) -> dict:
|
||||
require_admin(actor)
|
||||
rows = session.scalars(select(NqiAccount)).all()
|
||||
return {"pool": _pool_dashboard(rows)}
|
||||
return {"pool": _pool_dashboard(rows), "gongcan": cache_status()}
|
||||
|
||||
|
||||
@router.get("/admin/pool")
|
||||
@@ -723,6 +729,14 @@ def _pool_dashboard(rows: list[NqiAccount]) -> dict:
|
||||
}
|
||||
|
||||
|
||||
def _cache_gongcan(city: str) -> None:
|
||||
"""有新凭证了,补取这个地市缺的工参。排不上只记日志,不影响登录。"""
|
||||
try:
|
||||
request_sync(city)
|
||||
except Exception as exc:
|
||||
logger.info("登录后排工参补取未完成: %s", exc)
|
||||
|
||||
|
||||
def _save_pool(session: Session, user: dict, cookies: list) -> NqiAccount:
|
||||
login_id = str(user.get("loginId") or "").strip()
|
||||
if not login_id:
|
||||
|
||||
Reference in New Issue
Block a user