스냅샷 어드민 웹 UI + WBS-7.10 Tabler 테이블 그리드 조회

settings/account_snapshot SQLite를 직접 편집하는 잠금/승인/변경이력
기반 웹 에디터를 추가하고, 2026-06-21 비판적 리뷰에서 요청된 테이블별
그리드 조회 기능(Tabler CDN)을 /tables 경로로 덧붙인다.

- 잠금(lock)·승인(approval)·undo·변경로그 전체 감사 추적
- KIS Collection 대시보드 통합(별도 SQLite, 워크스페이스 DB와 분리)
- WBS-7.10: 워크스페이스/KIS수집/정성매도전략 3개 SQLite, 11개 테이블을
  Tabler 그리드로 조회 — 테이블명은 고정 화이트리스트와 정확히 일치할
  때만 SQL에 사용(SQL 인젝션 방지, 단위테스트로 검증)
This commit is contained in:
2026-06-21 20:06:55 +09:00
parent da0e1b0f7e
commit f99f9821d2
8 changed files with 4889 additions and 0 deletions
+164
View File
@@ -0,0 +1,164 @@
#!/usr/bin/env python3
from __future__ import annotations
import argparse
import os
import subprocess
import sys
import time
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
SERVER_MODULE = "src.quant_engine.snapshot_admin_server_v1"
WATCH_DIRS = (
ROOT / "src",
ROOT / "tools",
ROOT / "spec",
ROOT / "governance",
ROOT / "docs",
ROOT / ".gitea",
)
WATCH_FILES = (
ROOT / "package.json",
ROOT / "AGENTS.md",
ROOT / "GatherTradingData.json",
)
WATCH_EXTENSIONS = {".py", ".yaml", ".yml", ".json", ".md", ".gs"}
IGNORED_DIR_NAMES = {"Temp", "outputs", ".git", "__pycache__", ".pytest_cache"}
def _server_cmd(args: argparse.Namespace) -> list[str]:
cmd = [
sys.executable,
"-m",
SERVER_MODULE,
"--host",
args.host,
"--port",
str(args.port),
"--db",
args.db,
"--seed",
args.seed,
]
if args.no_bootstrap:
cmd.append("--no-bootstrap")
return cmd
def _iter_watch_files() -> list[Path]:
seen: set[Path] = set()
files: list[Path] = []
for path in WATCH_FILES:
if path.exists() and path.is_file():
resolved = path.resolve()
if resolved not in seen:
seen.add(resolved)
files.append(resolved)
for root in WATCH_DIRS:
if not root.exists():
continue
for path in root.rglob("*"):
if not path.is_file():
continue
if any(part in IGNORED_DIR_NAMES for part in path.parts):
continue
if path.suffix.lower() not in WATCH_EXTENSIONS:
continue
resolved = path.resolve()
if resolved not in seen:
seen.add(resolved)
files.append(resolved)
return files
def _snapshot_mtimes() -> dict[Path, float]:
mtimes: dict[Path, float] = {}
for path in _iter_watch_files():
try:
mtimes[path] = path.stat().st_mtime
except FileNotFoundError:
continue
return mtimes
def _changed_files(previous: dict[Path, float]) -> list[Path]:
current = _snapshot_mtimes()
changed: list[Path] = []
for path, mtime in current.items():
if previous.get(path) != mtime:
changed.append(path)
for path in previous:
if path not in current:
changed.append(path)
return changed
def _run_once(args: argparse.Namespace) -> int:
proc = subprocess.Popen(_server_cmd(args), cwd=str(ROOT), env=os.environ.copy())
try:
return proc.wait()
except KeyboardInterrupt:
proc.terminate()
try:
return proc.wait(timeout=5)
except subprocess.TimeoutExpired:
proc.kill()
return proc.wait()
def _run_reload(args: argparse.Namespace, interval: float) -> int:
last_mtimes = _snapshot_mtimes()
child: subprocess.Popen[str] | None = None
try:
while True:
if child is None or child.poll() is not None:
if child is not None:
code = child.returncode or 0
print(f"[snapshot-admin] server exited with code {code}; restarting...")
child = subprocess.Popen(_server_cmd(args), cwd=str(ROOT), env=os.environ.copy())
print("[snapshot-admin] hot reload watcher active")
print("[snapshot-admin] watching:", ", ".join(str(path) for path in WATCH_DIRS))
time.sleep(interval)
changed = _changed_files(last_mtimes)
if changed:
print("[snapshot-admin] changes detected:")
for path in changed[:20]:
print(f" - {path}")
last_mtimes = _snapshot_mtimes()
if child is not None and child.poll() is None:
child.terminate()
try:
child.wait(timeout=10)
except subprocess.TimeoutExpired:
child.kill()
child.wait()
child = None
except KeyboardInterrupt:
if child is not None and child.poll() is None:
child.terminate()
try:
child.wait(timeout=5)
except subprocess.TimeoutExpired:
child.kill()
child.wait()
return 0
def main() -> int:
parser = argparse.ArgumentParser(description="Run the snapshot admin web server.")
parser.add_argument("--host", default="127.0.0.1")
parser.add_argument("--port", type=int, default=8787)
parser.add_argument("--db", default=str(ROOT / "outputs" / "snapshot_admin" / "snapshot_admin.db"))
parser.add_argument("--seed", default=str(ROOT / "GatherTradingData.json"))
parser.add_argument("--no-bootstrap", action="store_true")
parser.add_argument("--reload", action="store_true", help="Restart the server when watched files change.")
parser.add_argument("--reload-interval", type=float, default=1.0, help="Seconds between file-system polls.")
args = parser.parse_args()
if args.reload:
return _run_reload(args, max(0.25, args.reload_interval))
return _run_once(args)
if __name__ == "__main__":
raise SystemExit(main())
+222
View File
@@ -0,0 +1,222 @@
#!/usr/bin/env python3
from __future__ import annotations
import json
import socket
import subprocess
import sys
import time
import urllib.error
import urllib.request
from pathlib import Path
from typing import Any
ROOT = Path(__file__).resolve().parents[1]
if str(ROOT) not in sys.path:
sys.path.insert(0, str(ROOT))
OUT = ROOT / "Temp" / "snapshot_admin_web_validation_v1.json"
def _read_json(url: str) -> dict[str, Any]:
with urllib.request.urlopen(url, timeout=5) as response:
payload = response.read().decode("utf-8")
data = json.loads(payload)
return data if isinstance(data, dict) else {}
def _read_text(url: str) -> str:
with urllib.request.urlopen(url, timeout=5) as response:
return response.read().decode("utf-8")
def _post_json(url: str, payload: dict[str, Any]) -> dict[str, Any]:
data = json.dumps(payload, ensure_ascii=False).encode("utf-8")
request = urllib.request.Request(
url,
data=data,
headers={"Content-Type": "application/json"},
method="POST",
)
with urllib.request.urlopen(request, timeout=5) as response:
return json.loads(response.read().decode("utf-8"))
def _wait_for_server(url: str, timeout_s: float = 15.0) -> None:
deadline = time.time() + timeout_s
last_error: Exception | None = None
while time.time() < deadline:
try:
_read_text(url)
return
except Exception as exc: # noqa: BLE001
last_error = exc
time.sleep(0.25)
raise RuntimeError(f"server did not start: {last_error}")
def _pick_free_port() -> int:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.bind(("127.0.0.1", 0))
return int(sock.getsockname()[1])
def main() -> int:
port = _pick_free_port()
db_path = ROOT / "Temp" / "snapshot_admin_web_validation.db"
seed_path = ROOT / "GatherTradingData.json"
server_cmd = [
sys.executable,
str(ROOT / "tools" / "run_snapshot_admin_server_v1.py"),
"--host",
"127.0.0.1",
"--port",
str(port),
"--db",
str(db_path),
"--seed",
str(seed_path),
]
proc = subprocess.Popen(
server_cmd,
cwd=ROOT,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
encoding="utf-8",
)
base_url = f"http://127.0.0.1:{port}"
errors: list[str] = []
html = ""
state: dict[str, Any] = {}
try:
_wait_for_server(base_url)
html = _read_text(f"{base_url}/")
state = _read_json(f"{base_url}/api/state")
export_payload = _read_json(f"{base_url}/api/export")
approval_packet = {
"formula_id": "SNAPSHOT_ADMIN_APPROVAL_PACKET_V1",
"generated_at": state.get("generated_at") or "",
"summary": {
"settings_changed": 0,
"account_snapshot_changed": 0,
"pending_target_count": 0,
},
"pending_targets": [],
"diff_preview": {"settings": {"added": [], "removed": [], "changed": []}, "account_snapshot": {"added": [], "removed": [], "changed": []}},
"approvals": state.get("approval_rows", []),
"locks": state.get("locks", []),
"workspace": state.get("summary", {}),
}
packet_response = _post_json(f"{base_url}/api/approval_packet", {"packet": approval_packet})
if "Snapshot Admin" not in html:
errors.append("html_title_missing")
if "contenteditable" not in html:
errors.append("sheet_editor_missing")
if "settings" not in html or "Account Snapshot" not in html:
errors.append("section_missing")
if "/api/settings/save" not in html or "/api/account_snapshot/save" not in html:
errors.append("api_binding_missing")
if "Approve pending" not in html or "Refresh diff" not in html:
errors.append("diff_or_approval_ui_missing")
if "Export approval packet" not in html:
errors.append("approval_packet_ui_missing")
if "Selection Inspector" not in html or "Apply TSV to selection" not in html or "Save view" not in html:
errors.append("sheet_facade_ui_missing")
if "Recent row history" not in html or "Ctrl+S" not in html:
errors.append("sheet_shortcuts_ui_missing")
if "KIS Collection" not in html or "collector:" not in html:
errors.append("collection_dashboard_ui_missing")
if "Recent collector snapshots" not in html or "Collection detail" not in html or "Filter runs / snapshots / errors" not in html:
errors.append("collection_detail_ui_missing")
if "Filter change log" not in html:
errors.append("change_log_filter_ui_missing")
if "Timeline" not in html or "/collection" not in html or "Open collection dashboard" not in html:
errors.append("collection_page_link_missing")
if "Open collection dashboard" not in html:
errors.append("collection_dashboard_link_missing")
collection_html = _read_text(f"{base_url}/collection")
if "KIS Collection Dashboard" not in collection_html or "Download CSV" not in collection_html or "Ticker quick search" not in collection_html or "Date quick search" not in collection_html:
errors.append("collection_dashboard_page_missing")
if int(state.get("summary", {}).get("settings_rows") or 0) <= 0:
errors.append("settings_rows_missing")
if int(state.get("summary", {}).get("account_snapshot_rows") or 0) <= 0:
errors.append("account_snapshot_rows_missing")
topology = state.get("summary", {}).get("topology", {})
if not isinstance(topology, dict):
errors.append("topology_missing")
else:
if topology.get("mode") != "single_workspace_sqlite":
errors.append("topology_mode_invalid")
if not topology.get("settings_and_snapshot_share_db"):
errors.append("topology_workspace_split_invalid")
if not topology.get("collector_separate_db"):
errors.append("topology_collector_split_invalid")
if not isinstance(state.get("version"), dict) or not state.get("version", {}).get("app"):
errors.append("version_metadata_missing")
if not isinstance(state.get("collection"), dict):
errors.append("collection_state_missing")
collection = state.get("collection", {})
if not isinstance(collection.get("counts"), dict):
errors.append("collection_counts_missing")
if "latest_report" not in collection:
errors.append("collection_latest_report_missing")
if "data" not in export_payload:
errors.append("export_missing_data")
if packet_response.get("gate") != "PASS":
errors.append("approval_packet_export_failed")
packet_path = Path(packet_response.get("packet_path") or "")
md_path = Path(packet_response.get("md_path") or "")
if not packet_path.exists():
errors.append("approval_packet_json_missing")
if not md_path.exists():
errors.append("approval_packet_md_missing")
payload = {
"formula_id": "SNAPSHOT_ADMIN_WEB_VALIDATION_V1",
"gate": "PASS" if not errors else "FAIL",
"port": port,
"db_path": str(db_path),
"base_url": base_url,
"errors": errors,
"summary": state.get("summary", {}),
"version": state.get("version", {}),
"settings_rows": int(state.get("summary", {}).get("settings_rows") or 0),
"account_snapshot_rows": int(state.get("summary", {}).get("account_snapshot_rows") or 0),
"approval_packet_path": str(packet_path),
}
OUT.write_text(json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8")
print(json.dumps(payload, ensure_ascii=False, indent=2))
return 0 if payload["gate"] == "PASS" else 1
except urllib.error.URLError as exc:
errors.append(str(exc))
payload = {
"formula_id": "SNAPSHOT_ADMIN_WEB_VALIDATION_V1",
"gate": "FAIL",
"port": port,
"db_path": str(db_path),
"base_url": base_url,
"errors": errors,
"summary": state.get("summary", {}),
"version": state.get("version", {}),
}
OUT.write_text(json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8")
print(json.dumps(payload, ensure_ascii=False, indent=2))
return 1
finally:
if proc.poll() is None:
proc.terminate()
try:
proc.wait(timeout=5)
except subprocess.TimeoutExpired:
proc.kill()
proc.wait(timeout=5)
if proc.stdout is not None:
proc.stdout.close()
if __name__ == "__main__":
raise SystemExit(main())
@@ -0,0 +1,66 @@
from __future__ import annotations
import json
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
if str(ROOT) not in sys.path:
sys.path.insert(0, str(ROOT))
from src.quant_engine.snapshot_admin_store_v1 import (
DEFAULT_DB,
DEFAULT_SEED_JSON,
import_seed_json,
load_account_snapshot_rows,
load_settings_rows,
parse_account_snapshot_tsv,
validate_account_snapshot_rows,
validate_settings_rows,
write_export_json,
)
OUT = ROOT / "Temp" / "snapshot_admin_workflow_v1.json"
def main() -> int:
db_path = DEFAULT_DB
seed_path = DEFAULT_SEED_JSON
summary = import_seed_json(db_path, seed_path)
settings_rows = load_settings_rows(db_path)
snapshot_rows = load_account_snapshot_rows(db_path)
settings_errors = validate_settings_rows(settings_rows)
snapshot_errors = validate_account_snapshot_rows(snapshot_rows)
exported = write_export_json(db_path, ROOT / "Temp" / "snapshot_admin_export_v1.json")
tsv_rows = parse_account_snapshot_tsv(
"\n".join(
[
"captured_at\taccount\taccount_type\tticker\tname\tholding_quantity\tavailable_quantity\taverage_cost\ttotal_cost\tcurrent_price\tmarket_value\tprofit_loss\treturn_pct\timmediate_cash\tsettlement_cash_d2\tavailable_cash\topen_order_amount\tmonthly_contribution_limit\tmonthly_contribution_used\tparse_status\tuser_confirmed\tstop_price\thighest_price_since_entry\tentry_date\tentry_stage\tposition_type\tlast_updated",
"2026-06-21T09:00:00+09:00\treal\t일반계좌\t005930\t삼성전자\t10\t10\t70000\t700000\t71000\t710000\t10000\t1.43\t1000000\t1000000\t1000000\t0\t\t\tCAPTURE_READ_OK\tY\t65000\t72000\t2026-06-01\tstage_1\tcore\t2026-06-21T09:05:00+09:00",
]
)
)
payload = {
"status": "PASS",
"db_path": str(db_path),
"seed_path": str(seed_path),
"summary": summary,
"settings_rows": len(settings_rows),
"account_snapshot_rows": len(snapshot_rows),
"settings_errors": settings_errors,
"snapshot_errors": snapshot_errors,
"export_path": str(exported),
"tsv_parse_rows": len(tsv_rows),
}
OUT.parent.mkdir(parents=True, exist_ok=True)
OUT.write_text(json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8")
print(json.dumps(payload, ensure_ascii=False, indent=2))
if settings_errors or snapshot_errors:
print("FAIL")
return 1
print("PASS")
return 0
if __name__ == "__main__":
raise SystemExit(main())