"""交易员只看到当前提醒能力的异常;历史准备状态保留在内部巡检。""" from datetime import datetime from zoneinfo import ZoneInfo def funds_warning(doc, codes, now=None, *, trade_day=True): now = now or datetime.now(ZoneInfo('Asia/Shanghai')) hm = now.strftime('%H:%M') active = trade_day and ('09:45' <= hm <= '11:31' or '13:01' <= hm <= '15:01') unavailable = '资金异动提醒可能延迟,系统正在重试。' try: at = datetime.fromisoformat(doc['at']) as_of = datetime.fromisoformat(doc['as_of']) if at.tzinfo is None or as_of.tzinfo is None: raise ValueError('missing timezone') if not 0 <= (now-at).total_seconds() <= 180 or as_of.date() != now.date(): return unavailable if active else '' if active and not 0 <= (now-as_of).total_seconds() <= 180: return unavailable rows = doc['priority'] if (not isinstance(rows, dict) or not all(isinstance(row, dict) for row in rows.values()) or doc.get('source') != 'redis_stock_flow_realtime'): raise ValueError('invalid health report') wanted = set(codes) | set(rows) if not wanted: return '' bad = sum(rows.get(c, {}).get('state') != 'available' for c in wanted) if bad: return '部分持仓及关注股的资金异动提醒暂不可用,系统正在重试。' return '' except (KeyError, TypeError, ValueError): return unavailable if active else ''