54 lines
2.3 KiB
Python
54 lines
2.3 KiB
Python
"""Command-line entry for one-shot datahub operations."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
import sys
|
|
|
|
from datahub.hub import build_hub
|
|
from datahub.settings import load_settings
|
|
from datahub.timeutil import yyyymmdd
|
|
|
|
|
|
def main(argv: list[str] | None = None) -> int:
|
|
parser = argparse.ArgumentParser(description="xiaobai-datahub CLI")
|
|
sub = parser.add_subparsers(dest="command", required=True)
|
|
history = sub.add_parser("history-backfill", help="回补 2016 年起交易日历和网站所用指数日 K")
|
|
history.add_argument("--calendar-start", default=None, help="日历起点,默认配置 calendar_start")
|
|
history.add_argument("--index-days", type=int, default=None, help="指数回补交易日数量,默认 260")
|
|
history.add_argument("--force", action="store_true", help="覆盖已发布的指数日期")
|
|
refresh = sub.add_parser("eod-refresh", help="对指定交易日补跑盘后正式数据(跳过已发布数据集,仍走质量门禁)")
|
|
refresh.add_argument("--trade-date", default=None, help="交易日 YYYYMMDD,默认今天")
|
|
args = parser.parse_args(argv)
|
|
|
|
settings = load_settings()
|
|
hub = build_hub(settings)
|
|
if args.command == "history-backfill":
|
|
result = hub.pipeline.backfill_history(
|
|
calendar_start=args.calendar_start,
|
|
index_days=args.index_days,
|
|
force=args.force,
|
|
)
|
|
json.dump(result, sys.stdout, ensure_ascii=False, indent=2, default=str)
|
|
sys.stdout.write("\n")
|
|
return 0 if result.get("ok") else 1
|
|
if args.command == "eod-refresh":
|
|
day = yyyymmdd(args.trade_date) if args.trade_date else yyyymmdd()
|
|
result = hub.pipeline.run_eod_missing(day)
|
|
hub.pipeline.audit("cli", "eod-refresh", f"eod:{day}", json.dumps(
|
|
{name: item.get("state") for name, item in result.items() if isinstance(item, dict)},
|
|
ensure_ascii=False,
|
|
))
|
|
missing = hub.pipeline.missing_official_datasets(day)
|
|
payload = {"trade_date": day, "datasets": result, "missing_after": missing}
|
|
json.dump(payload, sys.stdout, ensure_ascii=False, indent=2, default=str)
|
|
sys.stdout.write("\n")
|
|
return 0 if not missing else 1
|
|
parser.error(f"unknown command: {args.command}")
|
|
return 2
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|