feat: 选表时可以查看这张表的字段
This commit is contained in:
+29
-1
@@ -2,6 +2,7 @@ from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import base64
|
||||
import re
|
||||
from datetime import date, datetime, timedelta, timezone
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Request
|
||||
@@ -16,7 +17,7 @@ from app.install import InstallError, perform_install
|
||||
from app.db import get_session, session_scope
|
||||
from app.models import AppSetting, CollectionSource, ExportTask, NqiAccount, User, utcnow
|
||||
from app.collect.schedule import next_run_at, parse_schedule
|
||||
from app.nqi.adhoc import TIME_MODES, catalog_tree, categories, tables, time_window
|
||||
from app.nqi.adhoc import TIME_MODES, catalog_tree, categories, table_fields, tables, time_window
|
||||
from app.nqi.client import NqiAuthError, NqiClient, NqiError
|
||||
from app.nqi.flows import drop, purge, remember, take
|
||||
from app.security import hash_password, issue_token, verify_password
|
||||
@@ -560,6 +561,33 @@ def nqi_catalog(code: int | None = None, tree: bool = False, actor: Actor = Depe
|
||||
raise _bad(str(exc)) from exc
|
||||
|
||||
|
||||
@router.get("/admin/nqi/columns")
|
||||
def nqi_columns(tablename: str, actor: Actor = Depends(current_actor), session: Session = db_session) -> dict:
|
||||
require_admin(actor)
|
||||
if not re.fullmatch(r"[A-Za-z0-9_.]{1,180}", tablename):
|
||||
raise _bad("请选择要查看的表")
|
||||
cookies = _pool_cookies(session)
|
||||
|
||||
async def run() -> dict:
|
||||
client = NqiClient()
|
||||
try:
|
||||
client.import_cookies(cookies)
|
||||
await client.current_user()
|
||||
try:
|
||||
return {"columns": await table_fields(client, tablename)}
|
||||
except NqiAuthError as exc:
|
||||
raise NqiError("门户登录还在,但进入即席查询没有接上,请用这个大数据账号重新登录一次") from exc
|
||||
finally:
|
||||
await client.aclose()
|
||||
|
||||
try:
|
||||
return asyncio.run(run())
|
||||
except NqiAuthError as exc:
|
||||
raise _bad("账号池凭证已失效") from exc
|
||||
except NqiError as exc:
|
||||
raise _bad(str(exc)) from exc
|
||||
|
||||
|
||||
def _pool_cookies(session: Session) -> list:
|
||||
account = session.scalar(select(NqiAccount).where(NqiAccount.credential_valid.is_(True)).order_by(NqiAccount.saved_at.desc()))
|
||||
cookies = list(account.cookies or []) if account is not None else []
|
||||
|
||||
@@ -259,6 +259,18 @@ def _csv(columns: list[dict], rows: list[dict]) -> bytes:
|
||||
return buffer.getvalue().encode("utf-8-sig")
|
||||
|
||||
|
||||
async def table_fields(client: NqiClient, tablename: str) -> list[dict]:
|
||||
await ensure_session(client)
|
||||
rows = await _columns(client, tablename)
|
||||
fields = []
|
||||
for row in rows:
|
||||
name = str(row.get("columnname_cn") or row.get("columnname") or "").strip()
|
||||
if not name:
|
||||
continue
|
||||
fields.append({"name": name, "type": str(row.get("datatype") or "").strip()})
|
||||
return fields
|
||||
|
||||
|
||||
async def _columns(client: NqiClient, tablename: str) -> list[dict]:
|
||||
payload = await _post_json(client, f"{PAGE}/getSelectTable", {"tablename": tablename})
|
||||
rows = payload.get("CFG_ADHOC_CONF_TABLE") if isinstance(payload, dict) else None
|
||||
|
||||
Reference in New Issue
Block a user