diff --git a/app/core/signal_health.py b/app/core/signal_health.py index 0fb0f7f..8bd0d1f 100644 --- a/app/core/signal_health.py +++ b/app/core/signal_health.py @@ -1,4 +1,4 @@ -"""把资金监测的数据状态译成交易员提示;不生成交易信号。""" +"""交易员只看到当前提醒能力的异常;历史准备状态保留在内部巡检。""" from datetime import datetime from zoneinfo import ZoneInfo @@ -7,7 +7,7 @@ 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 = '资金监测状态暂不可用,不能据此判断持仓和关注股票没有资金异动。' + unavailable = '资金异动提醒可能延迟,系统正在重试。' try: at = datetime.fromisoformat(doc['at']) as_of = datetime.fromisoformat(doc['as_of']) @@ -18,18 +18,15 @@ def funds_warning(doc, codes, now=None, *, trade_day=True): if active and not 0 <= (now-as_of).total_seconds() <= 180: return unavailable rows = doc['priority'] - if not isinstance(rows, dict) or doc.get('source') != 'redis_stock_flow_realtime': + 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) - incomplete = sum(rows.get(c, {}).get('money_flow_baseline_ready') is False for c in wanted) - messages = [] if bad: - messages.append(f'持仓及关注中有{bad}只股票资金数据暂不可用,暂无提示不代表没有资金异动。') - if incomplete: - messages.append('部分股票的资金流强度仍缺历史数据,该类提示暂不可用;其他信号继续监测。') - return ''.join(messages) + return '部分持仓及关注股的资金异动提醒暂不可用,系统正在重试。' + return '' except (KeyError, TypeError, ValueError): return unavailable if active else '' diff --git a/scripts/test_signal_health.py b/scripts/test_signal_health.py index b2f1a53..ef459d2 100644 --- a/scripts/test_signal_health.py +++ b/scripts/test_signal_health.py @@ -22,20 +22,29 @@ class FundsHealthTests(unittest.TestCase): def test_frozen_reader_even_live_heartbeat_warns(self): self.doc['as_of']=(self.now-timedelta(hours=3)).isoformat() - self.assertIn('不能据此',self.check()) + self.assertIn('提醒可能延迟',self.check()) def test_missing_stale_and_future_status_visible(self): - self.assertIn('不能据此',funds_warning(None,['300133.SZ'],self.now)) + self.assertIn('提醒可能延迟',funds_warning(None,['300133.SZ'],self.now)) for delta in (-181,1): self.doc['at']=(self.now+timedelta(seconds=delta)).isoformat() - self.assertIn('不能据此',self.check()) + self.assertIn('提醒可能延迟',self.check()) def test_missing_focus_detected(self): - self.assertIn('1只',self.check(codes=('300133.SZ','601126.SH'))) + self.assertIn('提醒暂不可用',self.check(codes=('300133.SZ','601126.SH'))) def test_bad_funds_and_baseline_separate(self): self.doc['priority']['300133.SZ']={'state':'stale','money_flow_baseline_ready':False} - text=self.check();self.assertIn('1只',text);self.assertIn('仍缺历史数据',text) + text=self.check();self.assertIn('提醒暂不可用',text);self.assertNotIn('历史',text) + + def test_history_preparation_is_not_a_trader_diagnostic_banner(self): + self.doc['priority']['300133.SZ']['money_flow_baseline_ready']=False + self.assertEqual(self.check(),'') + self.assertFalse(self.doc['priority']['300133.SZ']['money_flow_baseline_ready']) + + def test_malformed_internal_status_does_not_break_signal_page(self): + self.doc['priority']['300133.SZ']=None + self.assertIn('提醒可能延迟',self.check()) def test_closed_and_lunch_do_not_expect_new_ticks(self): for hour,minute in [(12,0),(18,0),(9,0)]: @@ -45,7 +54,7 @@ class FundsHealthTests(unittest.TestCase): def test_no_timezone_or_wrong_source_not_healthy(self): self.doc['at']='2026-11-16T14:10:00' - self.assertIn('不能据此',self.check()) + self.assertIn('提醒可能延迟',self.check()) if __name__=='__main__': unittest.main()