tradingSystem/app/services/judge_async.py

153 lines
8.3 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 copy
import hashlib
import json
import logging
import re
import uuid
from datetime import timedelta
from app.core.execution_policy import local_now, timestamp
from app.repo import rotation_repo as repo
from app.services import param_store
logger = logging.getLogger('pms.judge_async')
KIND = 'judge_request'
def digest(value):
return hashlib.sha256(json.dumps(value, sort_keys=True, ensure_ascii=False,
separators=(',', ':'), allow_nan=False).encode()).hexdigest()
def identity(candidate, payload):
"""研究内容和实际持仓变更才换请求;价格、资金、候选排名每跳重核。"""
hard = candidate.get('hard_numbers') or {}
research = hard.get('plan_basis')
if research is None:
research = {k: v for k, v in payload.get('hard_numbers', {}).items()
if k in ('score', 'theme', 'tier', 'upside', 'heat', 'sector', 'verdict', 'reasons',
'missing', 'risk', 'events_text', 'pricing_text', 'news_text',
'company_review_text', 'intraday_buy_signal', 'intraday_buy_reason',
'logic_state', 'support', 'pressure', 'ref_source')}
position = (payload.get('context') or {}).get('position') or {}
material = {'research': research, 'position': {k:position.get(k) for k in
('total_qty', 'base_qty', 'add_qty', 'dca_qty', 'avg_cost', 'support_ref', 'pressure_ref', 'stop_ref')},
'book': candidate.get('book'), 'source': hard.get('candidate_source'),
'stage': {k: hard.get(k) for k in ('dca_stage', 'stage', 'dca_count', 'trigger', 'logic_state')},
# OPEN数量由PMS实时复核,持仓增持的模型证据还包含本次动作数量。
'action_qty': candidate.get('qty') if payload.get('action') != 'OPEN' else None}
event = (hard.get('timing') or {}).get('event')
if event:
material['timing_event'] = {k: event.get(k) for k in ('key', 'action', 'at', 'expires_at')}
return digest(material)
def unavailable(reason, **extra):
return {'verdict': 'UNAVAILABLE', 'reason': reason, 'degraded': True,
'confidence': None, 'raw': extra or None}
def pending(doc):
return {'verdict': 'PENDING', 'reason': '研判已登记,等待结果,其他执行继续',
'degraded': False, 'confidence': None,
'raw': {'request_id': doc['request_id'], 'deadline_at': doc['deadline_at']}}
def _result(doc, candidate):
from app.services.judge import _map_verdict
if doc.get('reask'):
candidate['reask'] = copy.deepcopy(doc['reask'])
return _map_verdict(doc['result'])
def request(candidate, payload, base, now=None):
"""同一逻辑请求重启后续查;网络故障重发原编号,过期不补发模型。"""
current = local_now(now)
code = str(payload.get('ts_code') or '').upper()
if not re.fullmatch(r'(?:60[0-9]{4}\.SH|68[0-9]{4}\.SH|00[0-9]{4}\.SZ|30[0-9]{4}\.SZ)', code):
return unavailable('研判证券不在沪深支持范围')
scope = {'day': str(current.date()), 'code': code, 'action': payload.get('action'),
'book': candidate.get('book_id') or candidate.get('book'), 'client_scope': payload.get('client_scope'),
'source': (candidate.get('hard_numbers') or {}).get('candidate_source')}
key = 'judge:' + digest(scope)
material = identity(candidate, payload)
saved = None
try:
with repo.locked(key, KIND, {}) as doc:
stale = (doc.get('result', {}).get('verdict') in ('PASS', 'REJECT')
and current >= timestamp(doc['cache_until']))
force_new = payload.get('force_refresh') and not doc.get('forced')
if not doc or doc.get('material') != material or stale or force_new:
doc.clear()
deadline = current + timedelta(seconds=param_store.get_int('PMS_JUDGE_ASYNC_DEADLINE_SEC', 120))
doc.update(scope=scope, request_id='PMS_'+uuid.uuid4().hex, payload=copy.deepcopy(payload),
material=material, fingerprint=digest(payload), deadline_at=deadline.isoformat(),
cache_until=(deadline+timedelta(seconds=param_store.get_int('PMS_JUDGE_ASYNC_CACHE_SEC',120))).isoformat(),
created_at=current.isoformat(), forced=bool(payload.get('force_refresh')),
reask=copy.deepcopy(candidate.get('reask')), submitted=False)
if doc.get('result'):
doc['delivered'] = True
return _result(doc, candidate)
if current >= timestamp(doc['deadline_at']):
doc['result'] = {'verdict':'UNAVAILABLE', 'reason':'研判总等待时间已到,转人工确认', 'error_code':'EXPIRED'}
doc['delivered'] = True
return _result(doc, candidate)
# 短租约避免同一请求被两个扫描同时轮询。网络不在数据库事务内。
lease = timestamp(doc.get('poll_until'))
if lease and current < lease:
return pending(doc)
doc['poll_until'] = (current+timedelta(seconds=param_store.get_int('PMS_JUDGE_ASYNC_HTTP_TIMEOUT',3)+2)).isoformat()
saved = copy.deepcopy(doc)
import requests
timeout = min(param_store.get_int('PMS_JUDGE_ASYNC_HTTP_TIMEOUT',3),
max(.1,(timestamp(saved['deadline_at'])-current).total_seconds()))
if saved['submitted']:
response = requests.get(base+'/api/intraday/pms_judge_result/'+saved['request_id'], timeout=timeout)
else:
response = requests.post(base+'/api/intraday/pms_judge_submit', timeout=timeout,
json=dict(saved['payload'], request_id=saved['request_id'], deadline_at=saved['deadline_at'],
evidence_fingerprint=saved['fingerprint']))
response.raise_for_status()
data = response.json()
finished = local_now() if now is None else current
with repo.locked(key, KIND, {}) as doc:
if doc.get('request_id') != saved['request_id']:
return unavailable('研究依据已变化,本次旧结果已作废', error_code='SUPERSEDED')
doc.pop('poll_until', None)
if finished >= timestamp(doc['deadline_at']):
data = {'verdict':'UNAVAILABLE', 'reason':'结果到达时已超过截止时间,转人工确认', 'error_code':'EXPIRED'}
elif (not isinstance(data,dict) or data.get('request_id') != doc['request_id']
or (data.get('verdict') in ('PASS','REJECT','PENDING') and
data.get('evidence_fingerprint') != doc['fingerprint'])):
data = {'verdict':'UNAVAILABLE', 'reason':'研判结果编号或证据不匹配,转人工确认', 'error_code':'INVALID_RESPONSE'}
if data.get('error_code') == 'NOT_FOUND':
doc['submitted'] = False # 同编号重试,不另造一个模型任务。
return pending(doc)
doc['submitted'] = True
if data.get('verdict') == 'PENDING':
return pending(doc)
doc['result'] = data
doc['completed_at'] = finished.isoformat()
doc['cache_until'] = (finished+timedelta(seconds=param_store.get_int('PMS_JUDGE_ASYNC_CACHE_SEC',120))).isoformat()
doc['delivered'] = True
return _result(doc, candidate)
except Exception as exc:
logger.warning('研判登记或查询暂不可用 [%s]: %s', code, type(exc).__name__)
# 已可靠登记且尚未过期,传输失败保留轮询;否则在途提议会挡住结果消费。
if saved is not None and local_now(now) < timestamp(saved['deadline_at']):
return pending(saved)
return unavailable('研判登记或查询暂不可用,转人工确认', error_code=type(exc).__name__)
def resumable_reasks(now=None):
"""仅让已经登记的重问穿过旧模型驳回,不越过用户拒绝、在途或规则闸。"""
today = str(local_now(now).date())
values = {}
for row in repo.recent(KIND,1000):
doc = row['value']
if doc.get('scope',{}).get('day') == today and doc.get('reask') and not doc.get('delivered'):
scope = doc['scope']
values[(scope['code'],scope['action'])] = copy.deepcopy(doc['reask'])
return values