- 저장(`POST /save`) 6.8초 → 0.8초 — 고친 줄과 그 줄을 부르던 줄만 검사 (안 보는 줄이 부르는 키는 목록이 미리 세어 둔 것을 씀 · 돌고 도는 길은 전체 그림으로) - 로직 줄만 고쳤으면 품셈재료 연결 · 인력 준용 검사(`check_links`)도 건너뜀 - 로직 밖 파일(요소 · 표)이 섞인 저장은 예전처럼 전부 검사 — 전체 검사는 `check_master` 몫 - 켠 뒤 첫 목록 3.4초 → 0.03초 — M01 라우터가 뜰 때 뒤에서 한 번 읽어 둠(`cache.warm`) - 저장 전 검사(`check_saved`)를 M01 쪽(`_Store_Cache`)으로 옮김 — 좁히는 값이 있는 자리 · `check_master` 가 700줄에 닿음 - 시험 `test_m01_cache.py` 에 넷 더함 — 없는 로직 키 · 없는 요소 키 · 돌고 도는 참조 · 다른 파일에서 그 줄을 부르던 줄이 깨짐(모두 422) · 미리 읽은 뒤 첫 목록 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01JgUhN55Z3BCYhUJTjwTNfj
335 lines
9.5 KiB
Python
335 lines
9.5 KiB
Python
"""M01 마스터 데이터 관리자 API — 계약 `resources/master_data/_화면_계약.md`.
|
|
|
|
⚠ 권한은 등록하는 쪽(`main.py`)이 `system_admin_only` 로 붙임.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import sys
|
|
from decimal import Decimal
|
|
from typing import Any, Literal
|
|
|
|
from fastapi import APIRouter, HTTPException
|
|
from pydantic import BaseModel
|
|
|
|
from M01_MasterData import M01_MasterData_Store as store
|
|
from M01_MasterData import M01_MasterData_Store_Combo as combo_store
|
|
from M01_MasterData import M01_MasterData_Store_Make as make
|
|
|
|
router = APIRouter(prefix="/api/m01", tags=["M01 MasterData"])
|
|
|
|
if "pytest" not in sys.modules: # 시험은 사본 폴더를 써서 정본을 미리 읽을 까닭이 없음
|
|
store.warm() # 서버가 뜰 때 뒤에서 마스터를 한 번 읽어 둠 — 첫 목록을 기다리지 않게
|
|
|
|
|
|
class CalcBody(BaseModel):
|
|
key: str
|
|
inputs: dict[str, Any] = {}
|
|
row: dict[str, Any] | None = None # 저장 전 고친 로직 줄(없으면 저장된 파일로)
|
|
file: str | None = None # 새 로직이면 더할 로직 파일
|
|
|
|
|
|
class Change(BaseModel):
|
|
op: Literal["edit", "add", "delete"]
|
|
key: str | None = None
|
|
row: dict[str, Any] | None = None
|
|
|
|
|
|
class FileChanges(BaseModel):
|
|
file: str
|
|
version: str
|
|
changes: list[Change]
|
|
|
|
|
|
class SaveBody(BaseModel):
|
|
files: list[FileChanges]
|
|
|
|
|
|
class NewLogic(BaseModel):
|
|
logic: dict[str, Any]
|
|
owner: str = make.OWNERS[0]
|
|
|
|
|
|
class CopyLogic(BaseModel):
|
|
key: str
|
|
이름: str = ""
|
|
owner: str = make.OWNERS[0]
|
|
|
|
|
|
class EditLogic(BaseModel):
|
|
key: str
|
|
version: str
|
|
logic: dict[str, Any]
|
|
owner: str = ""
|
|
|
|
|
|
class DeleteLogic(BaseModel):
|
|
key: str
|
|
version: str
|
|
|
|
|
|
class FormulaLogic(BaseModel):
|
|
"""복사해서 만들기 — 별칭 꼴 식이 든 로직 줄과 그 별칭표(되돌릴 원문)."""
|
|
|
|
로직: dict[str, Any]
|
|
별칭: list[dict[str, Any]] = []
|
|
owner: str = make.OWNERS[0]
|
|
이름: str = ""
|
|
본뜬키: str = ""
|
|
|
|
|
|
class FormulaPreview(BaseModel):
|
|
"""저장 전 미리보기 — 별칭을 원문으로 되돌린 로직 줄만 받고 싶을 때(시험 계산 전)."""
|
|
|
|
로직: dict[str, Any]
|
|
별칭: list[dict[str, Any]] = []
|
|
|
|
|
|
class NewCombo(BaseModel):
|
|
"""일위대가 조합 — 담은 단가산출 로직 목록만(수량·비율·계산은 안 둠)."""
|
|
|
|
combo: dict[str, Any]
|
|
owner: str = combo_store.OWNERS[0]
|
|
|
|
|
|
class EditCombo(BaseModel):
|
|
key: str
|
|
version: str
|
|
combo: dict[str, Any]
|
|
owner: str = ""
|
|
|
|
|
|
class DeleteCombo(BaseModel):
|
|
key: str
|
|
version: str
|
|
|
|
|
|
class PreviewCombo(BaseModel):
|
|
"""저장 전 미리 보기 — `combo` 는 조합 줄 통째 또는 저장된 키 · `inputs` 는 로직마다."""
|
|
|
|
combo: dict[str, Any] | str
|
|
inputs: dict[str, dict[str, Any]] = {}
|
|
|
|
|
|
class DraftLogic(BaseModel):
|
|
text: str # 줄로 적은 식 — 줄마다 「이름 = 식」
|
|
decided: dict[str, Any] = {} # 변수마다 정한 것 {이름: {무엇, 단위, 비목, 요소·찾기·값}}
|
|
head: dict[str, Any] | None = None # 로직 머리(이름 · 결과단위 …) — 주면 로직 줄까지
|
|
save: bool = False
|
|
owner: str = make.OWNERS[0]
|
|
|
|
|
|
def _num(v):
|
|
"""Decimal → JSON 수(그냥 두면 글자로 나감)."""
|
|
if isinstance(v, Decimal):
|
|
return int(v) if v == v.to_integral_value() else float(v)
|
|
if isinstance(v, list):
|
|
return [_num(x) for x in v]
|
|
if isinstance(v, dict):
|
|
return {k: _num(x) for k, x in v.items()}
|
|
return v
|
|
|
|
|
|
def _call(fn, *args):
|
|
try:
|
|
return _num(fn(*args))
|
|
except store.StoreError as e:
|
|
raise HTTPException(status_code=e.status, detail=e.detail) from e
|
|
|
|
|
|
@router.get("/groups")
|
|
def get_groups() -> dict:
|
|
return {"groups": _call(store.groups)}
|
|
|
|
|
|
@router.get("/groups/{group}/files")
|
|
def get_files(group: str) -> dict:
|
|
return {"files": _call(store.files_of, group)}
|
|
|
|
|
|
@router.get("/subs")
|
|
def get_subs(file: str = "", group: str = "") -> dict:
|
|
return _call(store.subs, file, group)
|
|
|
|
|
|
@router.get("/rows")
|
|
def get_rows(
|
|
file: str,
|
|
page: int = 1,
|
|
size: int = 50,
|
|
q: str = "",
|
|
unlinked: bool = False,
|
|
sub: str = "",
|
|
detail: str = "",
|
|
state: str = "",
|
|
) -> dict:
|
|
return _call(store.rows, file, page, size, q, unlinked, sub, detail, state)
|
|
|
|
|
|
@router.get("/tables")
|
|
def get_tables(
|
|
file: str = "",
|
|
group: str = "",
|
|
sub: str = "",
|
|
detail: str = "",
|
|
q: str = "",
|
|
page: int = 1,
|
|
size: int = 30,
|
|
usage: str = "",
|
|
section: str = "",
|
|
) -> dict:
|
|
return _call(store.tables, file, group, sub, detail, q, page, size, usage, section)
|
|
|
|
|
|
@router.get("/table")
|
|
def get_table(file: str, key: str) -> dict:
|
|
return _call(store.table, file, key)
|
|
|
|
|
|
@router.get("/table/options")
|
|
def get_table_options(file: str, key: str) -> dict:
|
|
"""표 조건 칸의 값만 — 줄을 통째로 받지 않음."""
|
|
return _call(make.table_options, file, key)
|
|
|
|
|
|
@router.get("/sections")
|
|
def get_sections(
|
|
book: str, division: str = "", chapter: str = "", q: str = "", limit: int = 200
|
|
) -> dict:
|
|
"""원문 절 목록 — 만들기 첫 걸음."""
|
|
return _call(make.sections, book, division, chapter, q, limit)
|
|
|
|
|
|
@router.get("/logics")
|
|
def get_logics(
|
|
sub: str = "", detail: str = "", q: str = "", blocked: int | None = None, owner: str = ""
|
|
) -> dict:
|
|
return {"logics": _call(store.logics, sub, detail, q, blocked, owner)}
|
|
|
|
|
|
@router.get("/logic")
|
|
def get_logic(key: str) -> dict:
|
|
return _call(store.logic, key)
|
|
|
|
|
|
@router.get("/elements")
|
|
def get_elements(group: str, q: str = "", limit: int = 50) -> dict:
|
|
return _call(store.elements, group, q, limit)
|
|
|
|
|
|
@router.get("/pick")
|
|
def get_pick(kind: str, q: str = "", limit: int = 50) -> dict:
|
|
return _call(store.pick, kind, q, limit)
|
|
|
|
|
|
@router.get("/materials")
|
|
def get_materials(
|
|
sub: str,
|
|
detail: str = "",
|
|
spec: str = "",
|
|
region: str = "",
|
|
unit: str = "",
|
|
limit: int = 50,
|
|
) -> dict:
|
|
"""재료 고르기 조건 안 후보 — sub = 구분 · detail = 상세구분 · spec = 규격 · region = 자재지역
|
|
· unit = 호표 줄 단위(주면 그 단위 줄만)."""
|
|
return _call(store.materials, sub, detail, spec, region, unit, limit)
|
|
|
|
|
|
@router.post("/calc")
|
|
def post_calc(body: CalcBody) -> dict:
|
|
return _call(store.calc, body.key, body.inputs, body.row, body.file)
|
|
|
|
|
|
@router.post("/text")
|
|
def post_text(body: CalcBody) -> dict:
|
|
"""읽는 식 줄 — 몸은 `/calc` 와 같음."""
|
|
return _call(store.text, body.key, body.inputs, body.row, body.file)
|
|
|
|
|
|
@router.post("/save")
|
|
def post_save(body: SaveBody) -> dict:
|
|
return {"files": _call(store.save, [f.model_dump() for f in body.files])}
|
|
|
|
|
|
@router.post("/logic/new")
|
|
def post_logic_new(body: NewLogic) -> dict:
|
|
return _call(make.logic_new, body.logic, body.owner)
|
|
|
|
|
|
@router.post("/logic/copy")
|
|
def post_logic_copy(body: CopyLogic) -> dict:
|
|
return _call(make.logic_copy, body.key, body.이름, body.owner)
|
|
|
|
|
|
@router.post("/logic/edit")
|
|
def post_logic_edit(body: EditLogic) -> dict:
|
|
return _call(make.logic_edit, body.key, body.version, body.logic, body.owner)
|
|
|
|
|
|
@router.post("/logic/delete")
|
|
def post_logic_delete(body: DeleteLogic) -> dict:
|
|
return _call(make.logic_delete, body.key, body.version)
|
|
|
|
|
|
@router.get("/logic/formula")
|
|
def get_logic_formula(key: str) -> dict:
|
|
"""복사해서 만들기 — 로직 한 줄을 별칭 꼴 식 묶음으로."""
|
|
return _call(make.logic_formula, key)
|
|
|
|
|
|
@router.post("/logic/formula/save")
|
|
def post_logic_formula_save(body: FormulaLogic) -> dict:
|
|
"""고친 식 묶음을 자체 로직(`GX`)으로 — 별칭은 원문으로 되돌려 저장."""
|
|
return _call(make.logic_from_formula, body.로직, body.별칭, body.owner, body.이름, body.본뜬키)
|
|
|
|
|
|
@router.post("/logic/formula/preview")
|
|
def post_logic_formula_preview(body: FormulaPreview) -> dict:
|
|
"""저장 전 시험 계산용 — 별칭을 원문으로 되돌린 로직 줄만(저장 안 함)."""
|
|
return _call(make.logic_formula_preview, body.로직, body.별칭)
|
|
|
|
|
|
@router.post("/logic/draft")
|
|
def post_logic_draft(body: DraftLogic) -> dict:
|
|
"""식으로 새 로직 만들기 — 변수 뽑기 · 검사 · 로직 줄로 옮기기(`save` 면 저장까지)."""
|
|
return _call(make.logic_draft, body.text, body.decided, body.head, body.save, body.owner)
|
|
|
|
|
|
# ── 일위대가 조합(계약 9장) ────────────────────────────────────────────
|
|
@router.get("/combos")
|
|
def get_combos(sub: str = "", detail: str = "", q: str = "", owner: str = "") -> dict:
|
|
return {"combos": _call(combo_store.combos, sub, detail, q, owner)}
|
|
|
|
|
|
@router.get("/combo")
|
|
def get_combo(key: str) -> dict:
|
|
return _call(combo_store.combo, key)
|
|
|
|
|
|
@router.get("/logic/combos")
|
|
def get_logic_combos(key: str) -> dict:
|
|
"""이 단가산출 로직을 담은 조합 — 로직 쪽에서 거꾸로 봄."""
|
|
return _call(combo_store.combos_of_logic, key)
|
|
|
|
|
|
@router.post("/combo/new")
|
|
def post_combo_new(body: NewCombo) -> dict:
|
|
return _call(combo_store.combo_new, body.combo, body.owner)
|
|
|
|
|
|
@router.post("/combo/edit")
|
|
def post_combo_edit(body: EditCombo) -> dict:
|
|
return _call(combo_store.combo_edit, body.key, body.version, body.combo, body.owner)
|
|
|
|
|
|
@router.post("/combo/delete")
|
|
def post_combo_delete(body: DeleteCombo) -> dict:
|
|
return _call(combo_store.combo_delete, body.key, body.version)
|
|
|
|
|
|
@router.post("/combo/preview")
|
|
def post_combo_preview(body: PreviewCombo) -> dict:
|
|
"""담은 로직마다 값을 뽑아 비목별로 갈라 합계 — 저장하지 않음."""
|
|
return _call(combo_store.combo_preview, body.combo, body.inputs)
|