274 lines
12 KiB
Python
274 lines
12 KiB
Python
|
|
"""人工关注的独立评析状态:先持久化受理凭据,再投基座;查询绝不派任务。
|
|||
|
|
|
|||
|
|
计划的 evaluated 只表示已收录,不能代替买方评议完成。网络结果不明时不重复
|
|||
|
|
投递,后续只读核验真实报告,并由原评析目标名单/批任务继续补齐。
|
|||
|
|
"""
|
|||
|
|
from __future__ import annotations
|
|||
|
|
|
|||
|
|
import contextlib
|
|||
|
|
import datetime as dt
|
|||
|
|
import fcntl
|
|||
|
|
import json
|
|||
|
|
import logging
|
|||
|
|
from pathlib import Path
|
|||
|
|
import re
|
|||
|
|
import urllib.error
|
|||
|
|
import urllib.parse
|
|||
|
|
import urllib.request
|
|||
|
|
import uuid
|
|||
|
|
from zoneinfo import ZoneInfo
|
|||
|
|
|
|||
|
|
import plan_versions as pv
|
|||
|
|
|
|||
|
|
BJ = ZoneInfo('Asia/Shanghai')
|
|||
|
|
UTC = dt.timezone.utc
|
|||
|
|
_FILE = 'user_research.json'
|
|||
|
|
log = logging.getLogger(__name__)
|
|||
|
|
|
|||
|
|
|
|||
|
|
def _time(value):
|
|||
|
|
try:
|
|||
|
|
value = value if isinstance(value, dt.datetime) else dt.datetime.fromisoformat(str(value).replace('Z', '+00:00'))
|
|||
|
|
return value.replace(tzinfo=UTC) if value.tzinfo is None else value
|
|||
|
|
except (TypeError, ValueError):
|
|||
|
|
return None
|
|||
|
|
|
|||
|
|
|
|||
|
|
def _now(now=None):
|
|||
|
|
return _time(now) or dt.datetime.now(UTC)
|
|||
|
|
|
|||
|
|
|
|||
|
|
@contextlib.contextmanager
|
|||
|
|
def _writer(root):
|
|||
|
|
Path(root).mkdir(parents=True, exist_ok=True)
|
|||
|
|
with open(Path(root) / '.user-research.lock', 'a') as lock:
|
|||
|
|
fcntl.flock(lock, fcntl.LOCK_EX)
|
|||
|
|
yield
|
|||
|
|
|
|||
|
|
|
|||
|
|
def read(root):
|
|||
|
|
path = Path(root) / _FILE
|
|||
|
|
if not path.exists():
|
|||
|
|
return {'version': 1, 'rows': {}}
|
|||
|
|
value = json.loads(path.read_text())
|
|||
|
|
if value.get('version') != 1 or not isinstance(value.get('rows'), dict):
|
|||
|
|
raise ValueError('人工关注评析状态暂不可用')
|
|||
|
|
return value
|
|||
|
|
|
|||
|
|
|
|||
|
|
def _save(root, state):
|
|||
|
|
pv.atomic(Path(root) / _FILE, state)
|
|||
|
|
|
|||
|
|
|
|||
|
|
def _active(root, now):
|
|||
|
|
import user_candidates as uc
|
|||
|
|
rows = []
|
|||
|
|
for row in uc.active(root, now):
|
|||
|
|
try:
|
|||
|
|
rows.append(dict(row, code=uc.normalize(row['code'])))
|
|||
|
|
except (ValueError, KeyError):
|
|||
|
|
# 持久化来源也在入口守沪深范围,不能因旧文件或外部写入越界。
|
|||
|
|
continue
|
|||
|
|
return rows
|
|||
|
|
|
|||
|
|
|
|||
|
|
def report_state(report, now=None, fresh_days=7):
|
|||
|
|
"""只判报告完成事实;财报期是否落后仍由基座原有批任务判断。"""
|
|||
|
|
now = _now(now)
|
|||
|
|
report = report if isinstance(report, dict) else {}
|
|||
|
|
generated = _time(report.get('report_ran_at'))
|
|||
|
|
checked = _time(report.get('checked_at'))
|
|||
|
|
if not report:
|
|||
|
|
return False, '尚无买方评析报告', None
|
|||
|
|
if generated is None or generated > now:
|
|||
|
|
return False, '评析生成时间缺失或晚于当前时刻,待核实', None
|
|||
|
|
# 旧报告命中指纹时只更新 checked_at;不能把复核当新报告,也不能永远判旧。
|
|||
|
|
verified = max(t for t in (generated, checked) if t is not None and t <= now)
|
|||
|
|
if not report.get('brain_used') or report.get('brain_status') != 'ok':
|
|||
|
|
return False, '买方评议尚未完整完成', verified
|
|||
|
|
if not re.fullmatch(r'[0-9a-f]{64}', str(report.get('review_ref') or '')):
|
|||
|
|
return False, '报告缺少可核验的固定版本,等待重新归档', verified
|
|||
|
|
if not str(report.get('thesis') or '').strip():
|
|||
|
|
return False, '报告尚缺买方论点', verified
|
|||
|
|
if now - verified > dt.timedelta(days=fresh_days):
|
|||
|
|
return False, '评析需要重新核对资料', verified
|
|||
|
|
return True, None, verified
|
|||
|
|
|
|||
|
|
|
|||
|
|
def load_reports(codes, now=None):
|
|||
|
|
import sources
|
|||
|
|
now = _now(now)
|
|||
|
|
if not codes:
|
|||
|
|
return {}
|
|||
|
|
return sources.company_reviews(codes, now.astimezone(BJ).date().isoformat(),
|
|||
|
|
cutoff_at=now.isoformat(), strict=True, include_check=True)
|
|||
|
|
|
|||
|
|
|
|||
|
|
def _public(report, record, now, fresh_days):
|
|||
|
|
import card
|
|||
|
|
report = report if isinstance(report, dict) else None
|
|||
|
|
complete, reason, verified = report_state(report, now, fresh_days)
|
|||
|
|
status = (record or {}).get('status', 'missing')
|
|||
|
|
if status == 'completed' and not complete: # 上次完成不能掩盖这次缺报或陈旧。
|
|||
|
|
status = 'missing'
|
|||
|
|
if complete:
|
|||
|
|
status = 'completed'
|
|||
|
|
generated = _time((report or {}).get('report_ran_at'))
|
|||
|
|
error = None if complete else (record or {}).get('error') or reason
|
|||
|
|
return {'review_status': status, 'review_updated_at': (verified.isoformat() if complete and verified else (record or {}).get('updated_at')),
|
|||
|
|
'review_error': error,
|
|||
|
|
'review_data_date': generated.astimezone(BJ).date().isoformat() if generated and generated <= now else None,
|
|||
|
|
'company_review': report, 'company_review_text': card.company_review_view(report),
|
|||
|
|
'review_ref': (report or {}).get('review_ref')}
|
|||
|
|
|
|||
|
|
|
|||
|
|
def views(root, codes, *, now=None, reader=None):
|
|||
|
|
"""实时只读视图;既不落状态文件,也不触发请求或写研究目标表。"""
|
|||
|
|
import config
|
|||
|
|
import user_candidates as uc
|
|||
|
|
now = _now(now)
|
|||
|
|
safe = []
|
|||
|
|
for code in codes:
|
|||
|
|
try:
|
|||
|
|
safe.append(uc.normalize(code))
|
|||
|
|
except ValueError:
|
|||
|
|
continue
|
|||
|
|
safe = sorted(set(safe))
|
|||
|
|
state = read(root)
|
|||
|
|
try:
|
|||
|
|
reports = (reader or load_reports)(safe, now)
|
|||
|
|
except Exception:
|
|||
|
|
log.exception('人工关注评析读取失败')
|
|||
|
|
return {code: {'review_status': 'failed', 'review_updated_at': None,
|
|||
|
|
'review_error': '评析资料暂不可读取,请稍后核实', 'review_data_date': None,
|
|||
|
|
'company_review': None, 'company_review_text': '', 'review_ref': None}
|
|||
|
|
for code in safe}
|
|||
|
|
return {code: _public(reports.get(code), state['rows'].get(code), now,
|
|||
|
|
config.USER_RESEARCH_FRESH_DAYS) for code in safe}
|
|||
|
|
|
|||
|
|
|
|||
|
|
class _NoRedirect(urllib.request.HTTPRedirectHandler):
|
|||
|
|
def redirect_request(self, req, fp, code, msg, headers, newurl):
|
|||
|
|
raise urllib.error.HTTPError(req.full_url, code, '评析提交不接受跳转', headers, fp)
|
|||
|
|
|
|||
|
|
|
|||
|
|
def submit_review(code):
|
|||
|
|
"""唯一请求地址来自受管配置,代码参数无法改变域名、路径或强制重跑。"""
|
|||
|
|
import config
|
|||
|
|
import user_candidates as uc
|
|||
|
|
code = uc.normalize(code)
|
|||
|
|
base = urllib.parse.urlsplit(config.AKG_API_BASE)
|
|||
|
|
if (base.scheme not in ('http', 'https') or not base.hostname or base.username
|
|||
|
|
or base.password or base.query or base.fragment):
|
|||
|
|
raise ValueError('评析服务地址配置无效')
|
|||
|
|
endpoint = config.AKG_API_BASE.rstrip('/') + '/admin/company_review'
|
|||
|
|
query = urllib.parse.urlencode({'ts_code': code[2:] + '.' + code[:2],
|
|||
|
|
'force': 'false', 'sync': 'false',
|
|||
|
|
'with_brain': 'true', 'with_qual': 'true'})
|
|||
|
|
request = urllib.request.Request(endpoint + '?' + query, data=b'', method='POST')
|
|||
|
|
timeout = max(1.0, min(float(config.USER_RESEARCH_REQUEST_TIMEOUT), 10.0))
|
|||
|
|
opener = urllib.request.build_opener(_NoRedirect())
|
|||
|
|
with opener.open(request, timeout=timeout) as response:
|
|||
|
|
return json.loads(response.read(1024 * 1024).decode('utf-8'))
|
|||
|
|
|
|||
|
|
|
|||
|
|
def _targets(root, active, state, write=None):
|
|||
|
|
"""给原自然批任务补齐全部有效人工关注,不改变已发布计划内容。"""
|
|||
|
|
import review_targets
|
|||
|
|
if not active and not state.get('targets_key'):
|
|||
|
|
return
|
|||
|
|
day = pv.latest_date(root)
|
|||
|
|
if not day:
|
|||
|
|
return
|
|||
|
|
published = pv.load(root, day)
|
|||
|
|
key = pv.digest({'day': day, 'decision': published.get('decision'), 'active': active})
|
|||
|
|
if state.get('targets_key') == key:
|
|||
|
|
return
|
|||
|
|
review_targets.persist(day, published, write=write, user_rows=active)
|
|||
|
|
state['targets_key'] = key
|
|||
|
|
state.pop('targets_error', None)
|
|||
|
|
|
|||
|
|
|
|||
|
|
def sync(root=None, *, now=None, reader=None, submit=None, target_write=None):
|
|||
|
|
"""后台/自然调度入口。锁只护本地状态;网络前 claim 已经原子落盘。"""
|
|||
|
|
import config
|
|||
|
|
root = root or config.PLAN_SNAPSHOT_DIR
|
|||
|
|
now = _now(now)
|
|||
|
|
active = _active(root, now)
|
|||
|
|
codes = sorted({r['code'] for r in active})
|
|||
|
|
with _writer(root):
|
|||
|
|
state = read(root)
|
|||
|
|
try:
|
|||
|
|
_targets(root, active, state, target_write)
|
|||
|
|
except Exception:
|
|||
|
|
state['targets_error'] = '研究目标名单同步失败,后台会继续核对'
|
|||
|
|
log.exception('人工关注研究目标名单同步失败')
|
|||
|
|
_save(root, state)
|
|||
|
|
try:
|
|||
|
|
reports = (reader or load_reports)(codes, now)
|
|||
|
|
except Exception:
|
|||
|
|
log.exception('人工关注评析同步读取失败,本轮不投递')
|
|||
|
|
return {'status': 'failed', 'error': '评析资料暂不可读取,本轮未重复提交', 'submitted': 0}
|
|||
|
|
submitted = 0
|
|||
|
|
for code in codes:
|
|||
|
|
report = reports.get(code)
|
|||
|
|
complete, reason, verified = report_state(report, now, config.USER_RESEARCH_FRESH_DAYS)
|
|||
|
|
with _writer(root):
|
|||
|
|
state = read(root)
|
|||
|
|
previous = state['rows'].get(code, {})
|
|||
|
|
# 提交前再核对删除/到期,避免旧后台任务给已移除关注派新任务。
|
|||
|
|
if code not in {r['code'] for r in _active(root, now)}:
|
|||
|
|
continue
|
|||
|
|
if complete:
|
|||
|
|
state['rows'][code] = dict(previous, status='completed', error=None,
|
|||
|
|
updated_at=verified.isoformat(), observed_at=now.isoformat(),
|
|||
|
|
report_ref=report['review_ref'])
|
|||
|
|
_save(root, state)
|
|||
|
|
continue
|
|||
|
|
if previous.get('status') == 'completed' and (_time(previous.get('observed_at')) or dt.datetime.min.replace(tzinfo=UTC)) >= now:
|
|||
|
|
continue # 较早开始的并发读取不能把刚核实的完成状态反向改成待投递。
|
|||
|
|
claimed = _time(previous.get('claimed_at'))
|
|||
|
|
if previous.get('status') in ('pending', 'queued', 'failed') and claimed:
|
|||
|
|
# 已投过的旧无 ref/不完整报告,核对更新后仍不合格,明确留待补齐;不假造完成。
|
|||
|
|
if verified and verified > claimed and previous.get('status') != 'failed':
|
|||
|
|
state['rows'][code] = dict(previous, status='failed', error=reason,
|
|||
|
|
updated_at=now.isoformat())
|
|||
|
|
_save(root, state)
|
|||
|
|
continue
|
|||
|
|
if submitted >= max(1, config.USER_RESEARCH_SUBMIT_LIMIT):
|
|||
|
|
continue
|
|||
|
|
claim = uuid.uuid4().hex
|
|||
|
|
state['rows'][code] = dict(previous, status='pending', claim_id=claim,
|
|||
|
|
claimed_at=now.isoformat(), updated_at=now.isoformat(),
|
|||
|
|
error='评析请求正在受理,结果待核实')
|
|||
|
|
_save(root, state)
|
|||
|
|
submitted += 1
|
|||
|
|
status, error, task_id = 'pending', '评析请求的受理结果待核实,暂不重复提交', None
|
|||
|
|
try:
|
|||
|
|
result = (submit or submit_review)(code)
|
|||
|
|
if (isinstance(result, dict) and result.get('status') == 'queued'
|
|||
|
|
and isinstance(result.get('task_id'), str) and result['task_id'].strip()):
|
|||
|
|
status, error, task_id = 'queued', None, str(result['task_id'])
|
|||
|
|
elif isinstance(result, dict) and result.get('status') == 'done':
|
|||
|
|
result_status = (result.get('result') or {}).get('status')
|
|||
|
|
if result_status not in ('ok', 'skipped_fresh'):
|
|||
|
|
status, error = 'failed', '评析尚未生成完整报告,等待资料补齐'
|
|||
|
|
else:
|
|||
|
|
error = '评析已返回,等待核验固定报告'
|
|||
|
|
except urllib.error.HTTPError as exc:
|
|||
|
|
if 400 <= exc.code < 500:
|
|||
|
|
status, error = 'failed', '评析请求未被接受,请核对服务后重试'
|
|||
|
|
log.warning('人工关注评析请求失败 code=%s http=%s', code, exc.code)
|
|||
|
|
except ValueError:
|
|||
|
|
log.exception('人工关注评析响应或配置不可核验 code=%s', code)
|
|||
|
|
except Exception:
|
|||
|
|
log.exception('人工关注评析受理结果不明 code=%s', code)
|
|||
|
|
with _writer(root):
|
|||
|
|
state = read(root)
|
|||
|
|
previous = state['rows'].get(code, {})
|
|||
|
|
if previous.get('claim_id') == claim and previous.get('status') != 'completed':
|
|||
|
|
state['rows'][code] = dict(previous, status=status, error=error,
|
|||
|
|
task_id=task_id, updated_at=now.isoformat())
|
|||
|
|
_save(root, state)
|
|||
|
|
return {'status': 'checked', 'active': len(codes), 'submitted': submitted}
|