섹터 플로우 진척도 추적 추가
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
"""build_sector_flow_history_progress_v1.py — SECTOR_FLOW_HISTORY_PROGRESS_V1
|
||||
|
||||
WBS-2.5 진척도를 실데이터로 요약한다.
|
||||
sector_flow_history 누적 일수와 Flow_Credit 커버리지를 정직하게 노출한다.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
from collections import defaultdict
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
DEFAULT_JSON = ROOT / "GatherTradingData.json"
|
||||
DEFAULT_OUT = ROOT / "Temp" / "sector_flow_history_progress_v1.json"
|
||||
FORMULA_ID = "SECTOR_FLOW_HISTORY_PROGRESS_V1"
|
||||
|
||||
|
||||
def _load(path: Path) -> dict[str, Any]:
|
||||
if not path.exists():
|
||||
return {}
|
||||
try:
|
||||
obj = json.loads(path.read_text(encoding="utf-8"))
|
||||
except Exception:
|
||||
return {}
|
||||
return obj if isinstance(obj, dict) else {}
|
||||
|
||||
|
||||
def _rows(data: dict[str, Any], key: str) -> list[dict[str, Any]]:
|
||||
rows = (data.get("data") or {}).get(key) or []
|
||||
return [r for r in rows if isinstance(r, dict)]
|
||||
|
||||
|
||||
def main() -> int:
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument("--json", default=str(DEFAULT_JSON))
|
||||
ap.add_argument("--out", default=str(DEFAULT_OUT))
|
||||
args = ap.parse_args()
|
||||
|
||||
json_path = Path(args.json)
|
||||
json_path = json_path if json_path.is_absolute() else ROOT / json_path
|
||||
out_path = Path(args.out)
|
||||
out_path = out_path if out_path.is_absolute() else ROOT / out_path
|
||||
|
||||
payload = _load(json_path)
|
||||
sector_rows = _rows(payload, "sector_flow_history")
|
||||
data_feed_rows = _rows(payload, "data_feed")
|
||||
|
||||
by_date: dict[str, int] = defaultdict(int)
|
||||
for row in sector_rows:
|
||||
snap = row.get("Snapshot_Date") or row.get("snapshot_date") or ""
|
||||
day = str(snap)[:10]
|
||||
if day:
|
||||
by_date[day] += 1
|
||||
|
||||
distinct_dates = len(by_date)
|
||||
row_count = len(sector_rows)
|
||||
target_dates = 30
|
||||
status = "DONE" if distinct_dates >= target_dates else "DATA_GATED"
|
||||
coverage = round(min(100.0, distinct_dates / target_dates * 100.0), 2)
|
||||
|
||||
flow_credit_values = [row.get("Flow_Credit") for row in data_feed_rows]
|
||||
nonnull_flow_credit = sum(1 for v in flow_credit_values if v is not None)
|
||||
flow_credit_coverage = round((nonnull_flow_credit / len(flow_credit_values) * 100.0), 2) if flow_credit_values else 0.0
|
||||
|
||||
result = {
|
||||
"formula_id": FORMULA_ID,
|
||||
"status": status,
|
||||
"current_dates": distinct_dates,
|
||||
"target_dates": target_dates,
|
||||
"coverage_pct": coverage,
|
||||
"row_count": row_count,
|
||||
"rows_per_date": sorted(by_date.items()),
|
||||
"flow_credit_coverage_pct": flow_credit_coverage,
|
||||
"flow_credit_nonnull_count": nonnull_flow_credit,
|
||||
"flow_credit_total_count": len(flow_credit_values),
|
||||
"source": "GatherTradingData.json:data.sector_flow_history + data.data_feed",
|
||||
}
|
||||
|
||||
out_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
out_path.write_text(json.dumps(result, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
|
||||
print(
|
||||
f"[{FORMULA_ID}] status={status} dates={distinct_dates}/{target_dates} "
|
||||
f"rows={row_count} flow_credit={nonnull_flow_credit}/{len(flow_credit_values)}"
|
||||
)
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
@@ -0,0 +1,79 @@
|
||||
"""validate_sector_flow_history_progress_v1.py — SECTOR_FLOW_HISTORY_PROGRESS_VALIDATE_V1
|
||||
|
||||
sector_flow_history 진척도 산출물의 기본 계약을 검증한다.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
DEFAULT_INPUT = ROOT / "Temp" / "sector_flow_history_progress_v1.json"
|
||||
DEFAULT_OUT = ROOT / "Temp" / "validate_sector_flow_history_progress_v1.json"
|
||||
FORMULA_ID = "SECTOR_FLOW_HISTORY_PROGRESS_VALIDATE_V1"
|
||||
|
||||
|
||||
def _load(path: Path) -> Any:
|
||||
if not path.exists():
|
||||
return {}
|
||||
try:
|
||||
return json.loads(path.read_text(encoding="utf-8"))
|
||||
except Exception:
|
||||
return {}
|
||||
|
||||
|
||||
def main() -> int:
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument("--input", default=str(DEFAULT_INPUT))
|
||||
ap.add_argument("--out", default=str(DEFAULT_OUT))
|
||||
args = ap.parse_args()
|
||||
|
||||
input_path = Path(args.input)
|
||||
input_path = input_path if input_path.is_absolute() else ROOT / input_path
|
||||
out_path = Path(args.out)
|
||||
out_path = out_path if out_path.is_absolute() else ROOT / out_path
|
||||
|
||||
payload = _load(input_path)
|
||||
errors: list[str] = []
|
||||
|
||||
if not isinstance(payload, dict) or not payload:
|
||||
errors.append("payload must be object")
|
||||
else:
|
||||
if payload.get("formula_id") != "SECTOR_FLOW_HISTORY_PROGRESS_V1":
|
||||
errors.append("formula_id mismatch")
|
||||
if payload.get("status") not in {"DONE", "DATA_GATED"}:
|
||||
errors.append(f"status={payload.get('status')}")
|
||||
for key in [
|
||||
"current_dates",
|
||||
"target_dates",
|
||||
"coverage_pct",
|
||||
"row_count",
|
||||
"rows_per_date",
|
||||
"flow_credit_coverage_pct",
|
||||
]:
|
||||
if key not in payload:
|
||||
errors.append(f"missing field: {key}")
|
||||
|
||||
current_dates = payload.get("current_dates")
|
||||
target_dates = payload.get("target_dates")
|
||||
if isinstance(current_dates, int) and isinstance(target_dates, int):
|
||||
if current_dates < target_dates and payload.get("status") != "DATA_GATED":
|
||||
errors.append("current_dates < target_dates requires DATA_GATED")
|
||||
if current_dates >= target_dates and payload.get("status") != "DONE":
|
||||
errors.append("current_dates >= target_dates requires DONE")
|
||||
|
||||
result = {
|
||||
"formula_id": FORMULA_ID,
|
||||
"gate": "PASS" if not errors else "FAIL",
|
||||
"checked_file": str(Path(args.input).as_posix()),
|
||||
"errors": errors,
|
||||
}
|
||||
out_path.write_text(json.dumps(result, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
|
||||
print(json.dumps(result, ensure_ascii=False, indent=2))
|
||||
return 0 if not errors else 1
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in New Issue
Block a user