feat: 工参属性全部由 OpenCode 同一条 SQL 导出;即席查询表没有时间列时不加时间条件

即席查询的 4G/5G 小区工参表没有日期列、4G 只有当前记录,取不到历史日工参,工参不再走即席查询。即席查询的表配置写了 time_field 而表里没有这一列时,按页面做法不带时间条件。
This commit is contained in:
2026-11-11 03:02:05 +08:00
parent 33d132d7b6
commit 688441f4a7
5 changed files with 101 additions and 230 deletions
+28 -61
View File
@@ -15,7 +15,6 @@ from app.collect.gongcan import (
build_cells,
claim_snapshot,
fetch_snapshot,
key_overlap,
month_start,
nearest_sites,
pick_after,
@@ -26,7 +25,6 @@ from app.collect.gongcan import (
target_center,
)
from app.models import Base, CellData4G, CellData5G, GongcanSnapshot
from app.nqi.client import NqiError
from app.nqi.opencode import CsvTable, OpenCode
CITY = "江门"
@@ -73,7 +71,7 @@ class Platform:
await asyncio.sleep(self.delay)
if day not in self.days:
raise GongcanError("平台没有这一天")
return self.days[day], "opencode"
return self.days[day]
def make_store(platform, stored=None, clock=None):
@@ -82,7 +80,7 @@ def make_store(platform, stored=None, clock=None):
clock = clock or Clock()
for day, cells in (stored or {}).items():
snapshot_id = claim_snapshot(db, CITY, day, clock())
save_snapshot(db, snapshot_id, cells, "adhoc", clock())
save_snapshot(db, snapshot_id, cells, clock())
return GongcanStore(db, platform, poll_seconds=0.01, clock=clock)
@@ -95,44 +93,34 @@ def test_month_start_crosses_year():
def test_platform_files_use_make_date_and_keep_commas_out_of_text_columns():
task = snapshot_task_name(CITY, date(2026, 11, 1))
assert re.fullmatch(r"gc_[0-9a-f]{12}", task) and task != snapshot_task_name(CITY, date(2026, 10, 1))
lte, nr = platform_files(task, CITY, date(2026, 11, 1), attributes=False)
assert lte.sql.startswith("select cgi, longitude, latitude, eff_from_date, eff_to_date from appdbv3.a_common_cfg_lte_cellant_d where city = '江门'")
lte, nr = platform_files(task, CITY, date(2026, 11, 1))
assert lte.sql.startswith("select cgi, longitude, latitude, eff_from_date, eff_to_date, replace(cell_name::text, ',', ',') as cell_name")
assert lte.sql.endswith("from appdbv3.a_common_cfg_lte_cellant_d where city = '江门' and eff_from_date <= make_date(2026, 11, 1) and eff_to_date > make_date(2026, 11, 1)")
assert nr.sql.startswith("select ncgi, cgi, longitude, latitude,")
assert "make_date(2026, 11, 1)" in lte.sql and not re.search(r"\d{4}-\d{2}-\d{2}", lte.sql + nr.sql)
assert (lte.phase, lte.key, nr.key) == ("xy", "xy/lte", "xy/nr")
lte_attr, nr_attr = platform_files(task, CITY, date(2026, 11, 1), attributes=True)
assert "replace(station_name::text, ',', ',') as station_name" in lte_attr.sql
assert "replace(gnodeb_name::text, ',', ',') as gnodeb_name" in nr_attr.sql
assert {"station_name", "enodeb_id", "azimuth"} <= set(lte_attr.required) and lte_attr.phase == "attr"
assert "replace(gnodeb_name::text, ',', ',') as gnodeb_name" in nr.sql
assert not re.search(r"\d{4}-\d{2}-\d{2}", lte.sql + nr.sql)
assert (lte.phase, lte.key, nr.key) == ("gc", "gc/lte", "gc/nr")
assert {"cgi", "latitude", "station_name", "enodeb_id", "azimuth"} <= set(lte.required)
assert {"ncgi", "nrcell_name", "gnodeb_id"} <= set(nr.required)
def test_build_cells_merges_coordinates_by_cgi():
attrs = [
{"cgi": "c1", "cell_name": "A", "station_name": "S1", "enodeb_id": "1", "network_type": "LTE"},
{"cgi": "c1", "cell_name": "A-dup", "station_name": "S1"},
{"cgi": "c2", "cell_name": "B", "station_name": "S2"},
def test_build_cells_merges_antenna_rows_by_cgi():
rows = [
{"cgi": "c1", "cell_name": "A", "station_name": "S1", "enodeb_id": "1", "network_type": "LTE", "longitude": "", "latitude": ""},
{"cgi": "c1", "cell_name": "A-dup", "station_name": "S1", "longitude": "113.1", "latitude": "22.5"},
{"cgi": "c2", "cell_name": "B", "station_name": "S2", "longitude": "0", "latitude": "0"},
{"cgi": "", "cell_name": "no key"},
]
coords = [
{"cgi": "c1", "longitude": "", "latitude": ""},
{"cgi": "c1", "longitude": "113.1", "latitude": "22.5"},
{"cgi": "c2", "longitude": "0", "latitude": "0"},
]
cells = build_cells(attrs, coords, "4G")
cells = build_cells(rows, "4G")
assert [(item["cgi"], item["cell_name"], item["station_name"], item["latitude"], item["longitude"]) for item in cells] == [
("c1", "A", "S1", 22.5, 113.1),
("c2", "B", "S2", None, None),
]
assert cells[0]["enodeb_id"] == "1" and cells[1]["network_type"] == ""
assert set(cells[0]) == {"cgi", "latitude", "longitude"} | set(gongcan.ATTRIBUTES["4G"])
rows = build_cells([{"ncgi": "", "cgi": "n1", "nrcell_name": "N", "gnodeb_id": "9"}], [{"ncgi": "n1", "longitude": "113", "latitude": "22"}], "5G")
assert rows == [{**rows[0], "ncgi": "n1", "nrcell_name": "N", "gnodeb_id": "9", "latitude": 22.0, "longitude": 113.0}]
assert "cgi" not in rows[0] and "enodeb_id" not in rows[0]
def test_key_overlap():
assert key_overlap([{"cgi": "a"}, {"cgi": "b"}, {"cgi": "c"}], [{"cgi": "a"}, {"cgi": "b"}, {"cgi": "d"}], "4G") == 0.5
assert key_overlap([], [{"cgi": "a"}], "4G") == 0.0
nr_rows = build_cells([{"ncgi": "", "cgi": "n1", "nrcell_name": "N", "gnodeb_id": "9", "longitude": "113", "latitude": "22"}], "5G")
assert nr_rows == [{**nr_rows[0], "ncgi": "n1", "nrcell_name": "N", "gnodeb_id": "9", "latitude": 22.0, "longitude": 113.0}]
assert "cgi" not in nr_rows[0] and "enodeb_id" not in nr_rows[0]
class SnapshotOpenCode(OpenCode):
@@ -163,40 +151,19 @@ PLATFORM_ROWS = {
}
def adhoc_returns(monkeypatch, result):
async def export_day(client, tablename, day, city, fields, **kwargs):
if isinstance(result, Exception):
raise result
return result["4G" if "lte" in tablename else "5G"]
monkeypatch.setattr(gongcan.adhoc, "export_day", export_day)
def test_snapshot_takes_attributes_from_adhoc_and_coordinates_from_opencode(monkeypatch):
adhoc_returns(monkeypatch, {"4G": [{"cgi": "c1", "station_name": "即席站"}], "5G": [{"ncgi": "n1", "station_name": "即席站"}]})
def test_snapshot_comes_from_one_opencode_export():
oc = SnapshotOpenCode(PLATFORM_ROWS)
cells, source = asyncio.run(fetch_snapshot(None, oc, CITY, date(2026, 11, 1)))
assert source == "adhoc" and oc.phases == ["xy"] and oc.cleaned == [snapshot_task_name(CITY, date(2026, 11, 1))]
assert [(row["cgi"], row["station_name"], row["latitude"]) for row in cells["4G"]] == [("c1", "即席站", 22.0)]
assert [(row["ncgi"], row["station_name"], row["latitude"]) for row in cells["5G"]] == [("n1", "即席站", 22.0)]
cells = asyncio.run(fetch_snapshot(oc, CITY, date(2026, 11, 1)))
assert oc.phases == ["gc"] and oc.cleaned == [snapshot_task_name(CITY, date(2026, 11, 1))]
assert [(row["cgi"], row["station_name"], row["latitude"]) for row in cells["4G"]] == [("c1", "OC站", 22.0)]
assert [(row["ncgi"], row["station_name"], row["latitude"]) for row in cells["5G"]] == [("n1", "OC站", 22.0)]
def test_snapshot_falls_back_to_opencode_attributes(monkeypatch):
adhoc_returns(monkeypatch, NqiError("即席查询没有返回行数"))
oc = SnapshotOpenCode(PLATFORM_ROWS)
cells, source = asyncio.run(fetch_snapshot(None, oc, CITY, date(2026, 11, 1)))
assert source == "opencode" and oc.phases == ["attr"] and cells["4G"][0]["station_name"] == "OC站"
adhoc_returns(monkeypatch, {"4G": [{"cgi": "other"}], "5G": [{"ncgi": "n1"}]})
mismatch = SnapshotOpenCode(PLATFORM_ROWS)
cells, source = asyncio.run(fetch_snapshot(None, mismatch, CITY, date(2026, 11, 1)))
assert source == "opencode" and mismatch.phases == ["xy", "attr"] and cells["4G"][0]["cgi"] == "c1"
def test_snapshot_rejects_empty_city(monkeypatch):
adhoc_returns(monkeypatch, NqiError("x"))
def test_snapshot_rejects_empty_city():
oc = SnapshotOpenCode({"4G": [], "5G": PLATFORM_ROWS["5G"]})
with pytest.raises(GongcanError):
asyncio.run(fetch_snapshot(None, SnapshotOpenCode({"4G": [], "5G": []}), CITY, date(2026, 11, 1)))
asyncio.run(fetch_snapshot(oc, CITY, date(2026, 11, 1)))
assert oc.cleaned == [snapshot_task_name(CITY, date(2026, 11, 1))]
def test_store_answers_presence_queries():