93 lines
3.9 KiB
Python
93 lines
3.9 KiB
Python
from __future__ import annotations
|
|
|
|
from datetime import datetime, timezone
|
|
|
|
from sqlalchemy import JSON, Boolean, DateTime, 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)
|
|
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)
|
|
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 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)
|