192 lines
6.3 KiB
Python
192 lines
6.3 KiB
Python
"""Request/response models. Responses stay plain dicts straight from SQLite so the
|
|
API surface does not drift from what the frontend already receives."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Literal
|
|
|
|
from pydantic import BaseModel, Field, field_validator
|
|
|
|
StatusKey = Literal["running", "improving", "dev", "unknown"]
|
|
Direction = Literal["forward", "backward", "bidirectional"]
|
|
Reliability = Literal["sync", "async"]
|
|
|
|
|
|
def _clean(value: str | None, limit: int = 500) -> str:
|
|
return (value or "").strip()[:limit]
|
|
|
|
|
|
class LayerIn(BaseModel):
|
|
id: str = Field(min_length=1, max_length=40)
|
|
name: str = Field(min_length=1, max_length=80)
|
|
short_name: str = ""
|
|
color: str = "#0d9488"
|
|
description: str = ""
|
|
sort_order: int = 0
|
|
|
|
@field_validator("id")
|
|
@classmethod
|
|
def _norm_id(cls, v: str) -> str:
|
|
return _clean(v, 40)
|
|
|
|
@field_validator("name")
|
|
@classmethod
|
|
def _norm_name(cls, v: str) -> str:
|
|
return _clean(v, 80)
|
|
|
|
|
|
class SystemIn(BaseModel):
|
|
key: str = Field(min_length=1, max_length=60)
|
|
name: str = Field(min_length=1, max_length=80)
|
|
layer_id: str | None = None
|
|
dept: str = ""
|
|
status: StatusKey = "unknown"
|
|
tech: str = ""
|
|
url: str = ""
|
|
domain: str = ""
|
|
owner: str = ""
|
|
impl_time: str = ""
|
|
entry_enabled: bool = True
|
|
description: str = ""
|
|
tier: int = Field(default=2, ge=0, le=9)
|
|
flow_x: float | None = Field(default=None, ge=-4000, le=8000)
|
|
flow_y: float | None = Field(default=None, ge=-4000, le=8000)
|
|
sort_order: int = 0
|
|
|
|
@field_validator("key")
|
|
@classmethod
|
|
def _norm_key(cls, v: str) -> str:
|
|
return _clean(v, 60)
|
|
|
|
@field_validator("name")
|
|
@classmethod
|
|
def _norm_name(cls, v: str) -> str:
|
|
return _clean(v, 80)
|
|
|
|
|
|
class SystemPatch(BaseModel):
|
|
name: str | None = None
|
|
layer_id: str | None = None
|
|
dept: str | None = None
|
|
status: StatusKey | None = None
|
|
tech: str | None = None
|
|
url: str | None = None
|
|
domain: str | None = None
|
|
owner: str | None = None
|
|
impl_time: str | None = None
|
|
entry_enabled: bool | None = None
|
|
description: str | None = None
|
|
tier: int | None = Field(default=None, ge=0, le=9)
|
|
flow_x: float | None = Field(default=None, ge=-4000, le=8000)
|
|
flow_y: float | None = Field(default=None, ge=-4000, le=8000)
|
|
sort_order: int | None = None
|
|
|
|
@field_validator("name")
|
|
@classmethod
|
|
def _norm_name(cls, v: str | None) -> str | None:
|
|
# Renaming later is an ordinary edit, but an empty name would blank out every card.
|
|
if v is None:
|
|
return None
|
|
cleaned = _clean(v, 80)
|
|
if not cleaned:
|
|
raise ValueError("系统名称不能为空")
|
|
return cleaned
|
|
|
|
|
|
class LayoutItem(BaseModel):
|
|
# One card on the data-flow canvas; a null coordinate means place it automatically.
|
|
key: str = Field(min_length=1, max_length=60)
|
|
x: float | None = Field(default=None, ge=-4000, le=8000)
|
|
y: float | None = Field(default=None, ge=-4000, le=8000)
|
|
tier: int | None = Field(default=None, ge=0, le=9)
|
|
|
|
|
|
class LayoutIn(BaseModel):
|
|
items: list[LayoutItem] = Field(default_factory=list, max_length=400)
|
|
|
|
|
|
class FlowTypeIn(BaseModel):
|
|
id: str = Field(min_length=1, max_length=40)
|
|
name: str = Field(min_length=1, max_length=60)
|
|
color: str = "#64748b"
|
|
# 线型是节奏的编码: 实线同步、虚线异步、点划线定时/人工、点线低频参考。
|
|
line_style: Literal["solid", "dashed", "dash-dot", "dotted"] = "dashed"
|
|
default_interval: int = Field(default=3600, ge=10, le=86400)
|
|
description: str = ""
|
|
sort_order: int = 0
|
|
|
|
|
|
class FlowIn(BaseModel):
|
|
source_key: str = Field(min_length=1, max_length=60)
|
|
target_key: str = Field(min_length=1, max_length=60)
|
|
flow_type_id: str
|
|
label: str = Field(min_length=1, max_length=80)
|
|
payload: str = ""
|
|
direction: Direction = "forward"
|
|
interval_sec: int = Field(default=3600, ge=10, le=86400)
|
|
records_per_batch: int = Field(default=1, ge=0, le=1_000_000)
|
|
avg_payload_kb: float = Field(default=1.0, ge=0, le=10_000_000)
|
|
latency_ms: int = Field(default=120, ge=0, le=600_000)
|
|
reliability: Reliability = "async"
|
|
peak_hours: list[int] = Field(default_factory=lambda: [9, 10, 11, 14, 15, 16, 17])
|
|
enabled: bool = True
|
|
note: str = ""
|
|
sort_order: int = 0
|
|
|
|
@field_validator("peak_hours")
|
|
@classmethod
|
|
def _hours(cls, v: list[int]) -> list[int]:
|
|
clean = sorted({int(h) for h in v if 0 <= int(h) <= 23})
|
|
return clean
|
|
|
|
@field_validator("label")
|
|
@classmethod
|
|
def _label(cls, v: str) -> str:
|
|
return _clean(v, 80)
|
|
|
|
|
|
class FlowPatch(BaseModel):
|
|
source_key: str | None = None
|
|
target_key: str | None = None
|
|
flow_type_id: str | None = None
|
|
label: str | None = None
|
|
payload: str | None = None
|
|
direction: Direction | None = None
|
|
interval_sec: int | None = Field(default=None, ge=10, le=86400)
|
|
records_per_batch: int | None = Field(default=None, ge=0, le=1_000_000)
|
|
avg_payload_kb: float | None = Field(default=None, ge=0, le=10_000_000)
|
|
latency_ms: int | None = Field(default=None, ge=0, le=600_000)
|
|
reliability: Reliability | None = None
|
|
peak_hours: list[int] | None = None
|
|
enabled: bool | None = None
|
|
note: str | None = None
|
|
sort_order: int | None = None
|
|
|
|
|
|
class Actor(BaseModel):
|
|
"""Optional editing identity carried on write requests."""
|
|
|
|
actor: str = "匿名"
|
|
|
|
|
|
class SettingsIn(BaseModel):
|
|
site_title: str | None = None
|
|
site_subtitle: str | None = None
|
|
editing_enabled: bool | None = None
|
|
animation_enabled: bool | None = None
|
|
animation_speed: float | None = Field(default=None, ge=0.1, le=5)
|
|
live_mode: bool | None = None
|
|
live_interval_sec: int | None = Field(default=None, ge=5, le=600)
|
|
default_range_days: int | None = Field(default=None, ge=1, le=90)
|
|
#: How links are drawn on the data-flow canvas: orthogonal, curved, or plain segments.
|
|
flow_route_style: Literal["elbow", "curve", "straight"] | None = None
|
|
|
|
|
|
class BackupIn(BaseModel):
|
|
layers: list[dict] = Field(default_factory=list)
|
|
systems: list[dict] = Field(default_factory=list)
|
|
flow_types: list[dict] = Field(default_factory=list)
|
|
data_flows: list[dict] = Field(default_factory=list)
|
|
settings: dict = Field(default_factory=dict)
|
|
mode: Literal["replace", "merge"] = "replace"
|