feat: 工参按地市和日期入库,先查库再从大数据平台取

即席查询取工参属性,OpenCode 导出当天全市 CGI 和经纬度,按 CGI 合并后存成快照;即席查询失败或 CGI 对不上时改由 OpenCode 一并导出属性。拆除前后的工参日期按库优先、逐月回溯的规则选取,多个任务取同一天时只取一次。即席查询 getTableCount 出错不再被当成 0 行。
This commit is contained in:
2026-11-11 01:57:28 +08:00
parent 2d89d3fcd8
commit b5a59622a1
9 changed files with 1765 additions and 5 deletions
+85 -3
View File
@@ -2,7 +2,9 @@
from __future__ import annotations
import asyncio
import json
import re
from datetime import date, datetime, timedelta
from urllib.parse import urlencode
@@ -156,9 +158,7 @@ async def export_table(
"result": json.dumps({"result": selected}, ensure_ascii=False),
"where": json.dumps(where, ensure_ascii=False),
}
counted = await _post_json(client, f"{PAGE}/getTableCount", common)
total = int(counted.get("count") or 0) if isinstance(counted, dict) else 0
index_count = str(counted.get("indexcount") or "0") if isinstance(counted, dict) else "0"
total, index_count = await _count(client, common)
rows: list[dict] = []
start_at = 0
pages = 0
@@ -192,6 +192,88 @@ async def export_table(
return title, columns, rows
async def export_day(
client: NqiClient,
tablename: str,
day: date,
city: str,
fields: tuple[str, ...],
*,
page_size: int = PAGE_SIZE,
pause_seconds: float = 0.3,
) -> list[dict]:
"""取一张表某一天、某个地市的指定几列。
维度有多个可选值时(如“小区,网格,地市,分公司”“天粒度”),按页面默认取第一个并去掉“粒度”。
翻页按实际返回的行数推进,平台把每页压到更少时也不会漏行;取不全就报错。
"""
await ensure_session(client)
columns = await _columns(client, tablename)
selected = [column for column in columns if column.get("columnname") in fields]
missing = set(fields) - {str(column.get("columnname")) for column in selected}
if missing:
raise NqiError(f"{tablename} 缺少字段:{'、'.join(sorted(missing))}")
head = columns[0]
common = {
"geographicdimension": _first_choice(head.get("geographicdimension")),
"timedimension": _first_choice(head.get("timedimension")).removesuffix("粒度"),
"enodebField": head.get("enodeb_field") or "",
"cgiField": head.get("cgi_field") or "",
"timeField": head.get("time_field") or "starttime",
"cellField": head.get("cell_field") or "",
"cityField": head.get("city_field") or "city",
"result": json.dumps({"result": [_field(column) for column in selected]}, ensure_ascii=False),
"where": json.dumps(_where(head, _start(day), _end(day), city), ensure_ascii=False),
}
total, index_count = await _count(client, common)
rows: list[dict] = []
pages = 0
while len(rows) < total:
pages += 1
if pages > MAX_PAGES:
raise NqiError("数据行数超出单次导出上限")
page = await _post_json(
client,
f"{PAGE}/getTable",
{
**common,
**_column_form(selected),
"draw": pages,
"start": len(rows),
"length": page_size,
"indexcount": index_count,
"total": total,
"order[0][column]": 0,
"order[0][dir]": "desc",
"search[value]": "",
"search[regex]": "false",
},
)
data = page.get("data") if isinstance(page, dict) else None
batch = [item for item in data if isinstance(item, dict)] if isinstance(data, list) else []
if not batch:
break
rows.extend(batch)
if pause_seconds:
await asyncio.sleep(pause_seconds)
if len(rows) < total:
raise NqiError(f"{tablename} 只取到 {len(rows)} 行,应有 {total} 行")
return rows
def _first_choice(value: object) -> str:
return re.split(r"[,,.、]", str(value or ""))[0].strip()
async def _count(client: NqiClient, common: dict) -> tuple[int, str]:
"""平台出错时返回 {"stat":"0"},没有 count。不能当成 0 行。"""
counted = await _post_json(client, f"{PAGE}/getTableCount", common)
if not isinstance(counted, dict) or "count" not in counted:
message = counted.get("message") if isinstance(counted, dict) else ""
raise NqiError(f"即席查询没有返回行数{f':{message}' if message and message != 'null' else ''}")
return int(counted.get("count") or 0), str(counted.get("indexcount") or "0")
def _field(column: dict) -> dict:
return {
"feildtype": column.get("fieldtype") or "",