69 lines
3.3 KiB
Python
69 lines
3.3 KiB
Python
|
|
"""在上游容器内只读导出;stdout为gzip JSONL,不写上游数据。"""
|
|||
|
|
import datetime as dt
|
|||
|
|
import gzip
|
|||
|
|
import json
|
|||
|
|
import re
|
|||
|
|
import sys
|
|||
|
|
from pathlib import Path
|
|||
|
|
|
|||
|
|
START = '2026-10-08'
|
|||
|
|
END = '2026-11-10'
|
|||
|
|
HS = re.compile(r'(?:(?:60|68)\d{4}\.SH|(?:00|30)\d{4}\.SZ|SH(?:60|68)\d{4}|SZ(?:00|30)\d{4})$')
|
|||
|
|
out = gzip.GzipFile(fileobj=sys.stdout.buffer, mode='wb', mtime=0)
|
|||
|
|
|
|||
|
|
def emit(kind, data):
|
|||
|
|
out.write((json.dumps({'kind': kind, **data}, ensure_ascii=False, default=str)+'\n').encode())
|
|||
|
|
|
|||
|
|
mode = sys.argv[1]
|
|||
|
|
if mode == 'itd':
|
|||
|
|
for p in sorted(Path('/app/data/alert_audit').glob('2026-*.jsonl')):
|
|||
|
|
if not '2026-07-22' <= p.stem <= END: continue
|
|||
|
|
for line in p.open():
|
|||
|
|
r = json.loads(line)
|
|||
|
|
if HS.fullmatch(str(r.get('ts_code', ''))): emit('itd', r)
|
|||
|
|
elif mode == 'bionic':
|
|||
|
|
from sqlalchemy import create_engine, text, event
|
|||
|
|
from sqlalchemy.engine import Engine
|
|||
|
|
@event.listens_for(Engine, 'before_cursor_execute')
|
|||
|
|
def guard(conn, cursor, statement, parameters, context, executemany):
|
|||
|
|
if not statement.lstrip().upper().startswith(('SELECT', 'SHOW', 'DESCRIBE')):
|
|||
|
|
raise RuntimeError('read-only export')
|
|||
|
|
from config.settings import settings
|
|||
|
|
eng = create_engine(settings.SOURCE_DB_EXT_DSN, pool_pre_ping=False)
|
|||
|
|
query = """SELECT stock_code,audit_date,strategy_date,verdict,reason,created_at
|
|||
|
|
FROM strategy_audit_log
|
|||
|
|
WHERE audit_date >= :start AND audit_date <= :end
|
|||
|
|
AND (stock_code LIKE 'SH60%' OR stock_code LIKE 'SH68%' OR stock_code LIKE 'SZ00%' OR stock_code LIKE 'SZ30%')
|
|||
|
|
AND verdict IN ('REVERSAL_BUY','REVERSAL_SELL','TAKE_PROFIT','MAINTAIN_HOLD','MAINTAIN')
|
|||
|
|
AND reason LIKE :reason
|
|||
|
|
ORDER BY audit_date,created_at,stock_code"""
|
|||
|
|
with eng.connect() as c:
|
|||
|
|
for r in c.execute(text(query), {'start':int(START.replace('-','')), 'end':int(END.replace('-','')), 'reason':'【盘中%'}):
|
|||
|
|
emit('bionic_audit', dict(r._mapping))
|
|||
|
|
eng.dispose()
|
|||
|
|
elif mode == 'streams':
|
|||
|
|
import redis
|
|||
|
|
from config.settings import settings
|
|||
|
|
c = redis.Redis(host=settings.SIGNAL_REDIS_HOST, port=settings.SIGNAL_REDIS_PORT,
|
|||
|
|
password=settings.SIGNAL_REDIS_PASSWORD, db=2, decode_responses=True,
|
|||
|
|
socket_timeout=8, socket_connect_timeout=5)
|
|||
|
|
keys = ['mtf:intraday:stream:metrics'] + sorted(c.scan_iter(match='intraday_signals:2026-11-*', count=100))
|
|||
|
|
for key in keys:
|
|||
|
|
if c.type(key) != 'stream': continue
|
|||
|
|
cursor = str(int(dt.datetime.fromisoformat(START).timestamp()*1000))+'-0'
|
|||
|
|
stop = str(int(dt.datetime.fromisoformat('2026-11-11').timestamp()*1000)-1)+'-999999'
|
|||
|
|
while True:
|
|||
|
|
rows = c.xrange(key, min=cursor, max=stop, count=500)
|
|||
|
|
if not rows: break
|
|||
|
|
for ident, raw in rows:
|
|||
|
|
r = {**raw, **json.loads(raw['data'])} if raw.get('data') else raw
|
|||
|
|
code = r.get('ts_code') or r.get('symbol') or r.get('stock_code')
|
|||
|
|
if HS.fullmatch(str(code)):
|
|||
|
|
r.pop('data', None)
|
|||
|
|
emit('mtf' if key.startswith('mtf:') else 'bionic_broadcast', {'stream_key':key,'stream_id':ident, **r})
|
|||
|
|
ms, seq = rows[-1][0].split('-'); cursor = ms+'-'+str(int(seq)+1)
|
|||
|
|
if len(rows) < 500: break
|
|||
|
|
else:
|
|||
|
|
raise ValueError('unsupported export mode')
|
|||
|
|
out.close()
|