tradingSystem/research/heat_recurrence_20261029/check_source.py

34 lines
2.8 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.

"""155模拟PMS只读核对:使用既有连接注入守卫,不导出凭证、不改变历史缓存。"""
import gzip, importlib.util, json, shlex, subprocess
from pathlib import Path
ROOT=Path(__file__).resolve().parent
OLD=ROOT.parent/'heat_calibration_20261028'
def main():
spec=importlib.util.spec_from_file_location('old_fetch',OLD/'fetch.py');m=importlib.util.module_from_spec(spec);spec.loader.exec_module(m)
code=(OLD/'readonly_support.py').read_text()+'''
import gzip,hashlib,inspect
from app.services import strategy_advisor,param_store
signal.alarm(100)
data={}
data['latest_date']=read('proxy','SELECT MAX(trade_date) AS trade_date FROM stock_fund_heat_scores')
assert data['latest_date']['ok']
day=data['latest_date']['rows'][0]['trade_date']
data['latest_batch']=read('proxy','SELECT MAX(batch_no) AS batch FROM stock_fund_heat_scores WHERE trade_date=%s',(day,))
assert data['latest_batch']['ok']
batch=data['latest_batch']['rows'][0]['batch']
data['latest_summary']=read('proxy','SELECT COUNT(*) AS rows_count,MIN(score) AS min_score,MAX(score) AS max_score,SUM(score>=0.8) AS above_08,MIN(create_time) AS first_create,MAX(create_time) AS last_create FROM stock_fund_heat_scores WHERE trade_date=%s AND batch_no=%s',(day,batch))
data['heat_threshold_paramstore']=param_store.get_float('PMS_AUTO_HEAT_TH',.8)
source=inspect.getsource(strategy_advisor.heat_of)
data['heat_of_source']=source
data['heat_of_sha256']=hashlib.sha256(source.encode()).hexdigest()
out={'checked_at_beijing':dt.datetime.now(dt.timezone(dt.timedelta(hours=8))).isoformat(),'host':'155','prefix':settings.PMS_TABLE_PREFIX,'data':data}
sys.stdout.buffer.write(gzip.compress(json.dumps(out,ensure_ascii=False,default=encode).encode()))
for conn in CONNECTIONS.values():conn.close()
'''
wrapper=m.WRAPPER.replace("'pms-heat-research'","'pms-heat-recurrence-source'").replace("'role': 'readonly-research'","'role': 'readonly-research', 'mounts': [{'destination':x['Destination'],'type':x['Type']} for x in info.get('Mounts',[])]")
r=subprocess.run(['ssh','-o','BatchMode=yes','-o','ConnectTimeout=8','-p','2280','factor@192.168.16.155','python3 -c '+shlex.quote(wrapper)],input=json.dumps({'mode':'probe','code':code}).encode(),stdout=subprocess.PIPE,stderr=subprocess.PIPE,timeout=140)
if r.returncode:raise RuntimeError('只读核对失败,退出码'+str(r.returncode)+';不回显可能含配置的异常正文')
out=json.loads(gzip.decompress(r.stdout));out['instance']=next(json.loads(line) for line in r.stderr.decode().splitlines() if line.startswith('{') and '"source_image"' in line);(ROOT/'source_check.json').write_text(json.dumps(out,ensure_ascii=False,indent=2)+'\n')
print({k:v for k,v in out.items() if k!='data'});print({k:v for k,v in out['data'].items() if 'source' not in k})
if __name__=='__main__':main()