akg-factor-bridge/build_readiness.py

49 lines
1.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""自动日更的日期核验。只读三路来源,不重建计划、不调用模型。"""
import datetime as dt
from zoneinfo import ZoneInfo
import pandas as pd
import db
def expected_data_day(now=None):
from chinese_calendar import is_holiday
local = (now or dt.datetime.now(dt.timezone.utc)).astimezone(ZoneInfo('Asia/Shanghai'))
day = local.date() - dt.timedelta(days=1)
while day.weekday() >= 5 or is_holiday(day):
day -= dt.timedelta(days=1)
return day
def check(now=None):
"""数据未齐则在任何写入前失败;不能用缺失数据推断今天休市。"""
expected = expected_data_day(now)
dates = {}
failures = []
for name, source, sql in (
('传导', 'pg', 'SELECT MAX(scan_date) d FROM v_factor_transmission'),
('收盘行情', 'price', 'SELECT MAX(`timestamp`) d FROM gp_day_data'),
('资金热度', 'heat', 'SELECT MAX(trade_date) d FROM stock_fund_heat_scores'),
):
try:
frame = db.read_pg(sql) if source == 'pg' else db.read_mysql(source, sql)
value = None if frame.empty else frame.iloc[0, 0]
day = None if value is None or pd.isna(value) else pd.Timestamp(value).date()
except Exception as exc:
failures.append(f'{name}读取失败({type(exc).__name__})')
continue
dates[name] = day
if day != expected:
failures.append(f'{name}日期 {day or "缺失"}')
if failures:
raise RuntimeError(f'日更输入未就绪:应为 {expected};' + ';'.join(failures)
+ '。保留上次完整计划,停止构建及入池。')
return expected.isoformat()
if __name__ == '__main__':
try:
print('日更输入日期已对齐:' + check())
except RuntimeError as exc:
raise SystemExit(str(exc))