即席查询取工参属性,OpenCode 导出当天全市 CGI 和经纬度,按 CGI 合并后存成快照;即席查询失败或 CGI 对不上时改由 OpenCode 一并导出属性。拆除前后的工参日期按库优先、逐月回溯的规则选取,多个任务取同一天时只取一次。即席查询 getTableCount 出错不再被当成 0 行。
144 lines
6.7 KiB
Python
144 lines
6.7 KiB
Python
from __future__ import annotations
|
|
|
|
from datetime import date, datetime, timezone
|
|
|
|
from sqlalchemy import JSON, BigInteger, Boolean, Date, DateTime, Float, ForeignKey, Index, Integer, String, Text, UniqueConstraint
|
|
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
|
|
|
|
|
|
def utcnow() -> datetime:
|
|
return datetime.now(timezone.utc)
|
|
|
|
|
|
class Base(DeclarativeBase):
|
|
pass
|
|
|
|
|
|
class AppSetting(Base):
|
|
__tablename__ = "app_setting"
|
|
|
|
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
|
login_mode: Mapped[str] = mapped_column(String(16), default="local")
|
|
credential_refresh_seconds: Mapped[int] = mapped_column(Integer, default=300)
|
|
worker_concurrency: Mapped[int] = mapped_column(Integer, default=4)
|
|
db_host: Mapped[str] = mapped_column(String(255), default="")
|
|
db_port: Mapped[int] = mapped_column(Integer, default=5432)
|
|
db_name: Mapped[str] = mapped_column(String(128), default="")
|
|
db_user: Mapped[str] = mapped_column(String(128), default="")
|
|
db_password: Mapped[str] = mapped_column(Text, default="")
|
|
jwt_secret: Mapped[str] = mapped_column(String(128), default="")
|
|
|
|
|
|
class User(Base):
|
|
__tablename__ = "app_user"
|
|
|
|
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
|
username: Mapped[str] = mapped_column(String(64), unique=True, index=True)
|
|
password_hash: Mapped[str] = mapped_column(String(200))
|
|
role: Mapped[str] = mapped_column(String(16), default="user")
|
|
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utcnow)
|
|
|
|
|
|
class NqiAccount(Base):
|
|
__tablename__ = "nqi_account"
|
|
|
|
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
|
login_id: Mapped[str] = mapped_column(String(64), unique=True, index=True)
|
|
user_name: Mapped[str] = mapped_column(String(128), default="")
|
|
city_name: Mapped[str] = mapped_column(String(64), default="")
|
|
org_name: Mapped[str] = mapped_column(String(128), default="")
|
|
cookies: Mapped[list] = mapped_column(JSON, default=list)
|
|
role: Mapped[str] = mapped_column(String(16), default="user")
|
|
session_open: Mapped[bool] = mapped_column(Boolean, default=True)
|
|
credential_valid: Mapped[bool] = mapped_column(Boolean, default=True)
|
|
active_tasks: Mapped[int] = mapped_column(Integer, default=0)
|
|
task_runs: Mapped[int] = mapped_column(Integer, default=0)
|
|
last_task_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|
|
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utcnow)
|
|
saved_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utcnow)
|
|
|
|
|
|
class CellData(Base):
|
|
__tablename__ = "CellData"
|
|
__table_args__ = (UniqueConstraint("cell_key", "observed_at", "source", name="uq_cell_time_source"),)
|
|
|
|
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
|
cell_key: Mapped[str] = mapped_column(String(128), index=True)
|
|
observed_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), index=True)
|
|
source: Mapped[str] = mapped_column(String(16))
|
|
metrics: Mapped[dict] = mapped_column(JSON, default=dict)
|
|
|
|
|
|
class CollectionSource(Base):
|
|
__tablename__ = "collection_source"
|
|
|
|
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
|
name: Mapped[str] = mapped_column(String(128))
|
|
kind: Mapped[str] = mapped_column(String(16))
|
|
period_days: Mapped[int] = mapped_column(Integer, default=1)
|
|
config: Mapped[dict] = mapped_column(JSON, default=dict)
|
|
enabled: Mapped[bool] = mapped_column(Boolean, default=True)
|
|
next_run_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|
|
last_error: Mapped[str] = mapped_column(Text, default="")
|
|
|
|
|
|
class GongcanSnapshot(Base):
|
|
"""某地市某一天的全量 4G/5G 小区工参。status 兼作取数锁:loading 时别的任务等它。"""
|
|
|
|
__tablename__ = "gongcan_snapshot"
|
|
__table_args__ = (UniqueConstraint("city", "snapshot_date", name="uq_gongcan_city_date"),)
|
|
|
|
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
|
city: Mapped[str] = mapped_column(String(32))
|
|
snapshot_date: Mapped[date] = mapped_column(Date)
|
|
status: Mapped[str] = mapped_column(String(16), default="loading")
|
|
attr_source: Mapped[str] = mapped_column(String(16), default="")
|
|
lte_cells: Mapped[int] = mapped_column(Integer, default=0)
|
|
nr_cells: Mapped[int] = mapped_column(Integer, default=0)
|
|
located_cells: Mapped[int] = mapped_column(Integer, default=0)
|
|
error: Mapped[str] = mapped_column(Text, default="")
|
|
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utcnow)
|
|
updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utcnow)
|
|
|
|
|
|
class GongcanCell(Base):
|
|
__tablename__ = "gongcan_cell"
|
|
__table_args__ = (
|
|
UniqueConstraint("snapshot_id", "cgi", "tech", name="uq_gongcan_cell"),
|
|
Index("ix_gongcan_cell_site", "snapshot_id", "site_name"),
|
|
)
|
|
|
|
id: Mapped[int] = mapped_column(BigInteger().with_variant(Integer, "sqlite"), primary_key=True)
|
|
snapshot_id: Mapped[int] = mapped_column(ForeignKey("gongcan_snapshot.id", ondelete="CASCADE"))
|
|
tech: Mapped[str] = mapped_column(String(4))
|
|
cgi: Mapped[str] = mapped_column(String(64))
|
|
cell_name: Mapped[str] = mapped_column(Text, default="")
|
|
site_name: Mapped[str] = mapped_column(Text, default="")
|
|
latitude: Mapped[float | None] = mapped_column(Float, nullable=True)
|
|
longitude: Mapped[float | None] = mapped_column(Float, nullable=True)
|
|
network_type: Mapped[str] = mapped_column(Text, default="")
|
|
node_id: Mapped[str] = mapped_column(Text, default="")
|
|
node_name: Mapped[str] = mapped_column(Text, default="")
|
|
area: Mapped[str] = mapped_column(Text, default="")
|
|
town: Mapped[str] = mapped_column(Text, default="")
|
|
state: Mapped[str] = mapped_column(Text, default="")
|
|
cover_type: Mapped[str] = mapped_column(Text, default="")
|
|
frequency_band: Mapped[str] = mapped_column(Text, default="")
|
|
vendor: Mapped[str] = mapped_column(Text, default="")
|
|
azimuth: Mapped[str] = mapped_column(Text, default="")
|
|
|
|
|
|
class ExportTask(Base):
|
|
__tablename__ = "export_task"
|
|
|
|
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
|
owner_kind: Mapped[str] = mapped_column(String(16))
|
|
owner_id: Mapped[str] = mapped_column(String(64), index=True)
|
|
status: Mapped[str] = mapped_column(String(16), default="queued", index=True)
|
|
file_format: Mapped[str] = mapped_column(String(8))
|
|
params: Mapped[dict] = mapped_column(JSON, default=dict)
|
|
result_name: Mapped[str] = mapped_column(String(255), default="")
|
|
error: Mapped[str] = mapped_column(Text, default="")
|
|
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utcnow)
|
|
finished_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|