Skip to content

Commit

Permalink
enable scenario run in runLIbrary
Browse files Browse the repository at this point in the history
  • Loading branch information
yellowbean committed Jul 15, 2023
1 parent 94d9c5d commit ea0294f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 21 deletions.
32 changes: 24 additions & 8 deletions absbox/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
from absbox.local.base import *
from absbox.validation import valReq,valAssumption

from absbox.local.china import SPV
from absbox.local.generic import Generic


VERSION_NUM = version("absbox")
urllib3.disable_warnings()
console = Console()
Expand Down Expand Up @@ -51,7 +55,7 @@ def __post_init__(self):
def build_req(self, deal, assumptions=None, pricing=None) -> str:
r = None
_assump = None
_deal = deal.json
_deal = deal.json if not isinstance(deal,str) else deal
_pricing = mkPricingAssump(pricing) if pricing else None
if isinstance(assumptions, dict):
_assump = mapValsBy(assumptions, mkAssumption2)
Expand Down Expand Up @@ -112,9 +116,9 @@ def run(self, deal,
console.print("❌[bold red]Failed to get response from run")
return None
if read and multi_run_flag:
return {n:deal.read(_r,position=position) for (n,_r) in result.items()}
return {n:deal.read(_r) for (n,_r) in result.items()}
elif read :
return deal.read(result,position=position)
return deal.read(result)
else:
return result

Expand Down Expand Up @@ -209,11 +213,23 @@ def runLibrary(self,_id,**p):
read = p.get("read",True)
pricingAssump = p.get("pricing",None)
dealAssump = p.get("assump",None)
d = {'user':None, 'dealid':_id, 'assump':dealAssump, 'pricing':pricingAssump} | p

result = self._send_req(json.dumps(d), deal_library_url,headers={"Authorization":f"Bearer {self.token}"})
classReader = p['reader']

#d = {'dealid':_id, 'assump':dealAssump, 'pricing':pricingAssump} | p
runReq = self.build_req(_id, dealAssump, pricingAssump)
result = self._send_req(runReq, deal_library_url, headers={"Authorization":f"Bearer {self.token}"})
try:
result = json.loads(result)
except Exception as e:
console.print(f"error parsing resp from engine:{result}")
def lookupReader(x):
match x:
case "china.SPV":
return SPV
case "generic.Generic":
return Generic
case _:
raise RuntimeError(f"Failed to match reader:{x}")

classReader = lookupReader(p['reader'])
if read and isinstance(result,list):
return classReader.read(result)
elif read and isinstance(result, dict):
Expand Down
15 changes: 3 additions & 12 deletions absbox/local/china.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from pyspecter import query

from absbox import *
from absbox.local.util import mkTag,DC,mkTs,consolStmtByDate,aggStmtByDate,subMap,subMap2,mapValsBy,mapListValBy,renameKs2
from absbox.local.util import *
from absbox.local.component import *


Expand Down Expand Up @@ -108,13 +108,14 @@ def read_pricing(self, pricing):
return None

@staticmethod
def read(resp, position=None):
def read(resp):
read_paths = {'bonds': ('bndStmt', china_bondflow_fields, "债券")
, 'fees': ('feeStmt', china_fee_flow_fields_d, "费用")
, 'accounts': ('accStmt', china_acc_flow_fields_d , "账户")
, 'liqProvider': ('liqStmt', china_liq_flow_fields_d, "流动性支持")
, 'rateSwap': ('rsStmt', china_rs_flow_fields_d, "")
}
assert isinstance(resp,list),f"<read>:resp should be list,but it is {type(resp)} => {resp}"
deal_content = resp[0]['contents']
output = {}
for comp_name, comp_v in read_paths.items():
Expand Down Expand Up @@ -143,16 +144,6 @@ def read(resp, position=None):
output['pool']['flow'].index.rename(pool_idx, inplace=True)

output['pricing'] = readPricingResult(resp[3], 'cn')
if position:
output['position'] = {}
for k,v in position.items():
if k in output['bonds']:
b = self._get_bond(k)
factor = v / b["初始余额"] / 100
if factor > 1.0:
raise RuntimeError("持仓系数大于1.0")
output['position'][k] = output['bonds'][k][china_bond_cashflow].apply(lambda x:x*factor).round(4)

output['result'] = readRunSummary(resp[2], 'cn')
return output

Expand Down
2 changes: 2 additions & 0 deletions absbox/local/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,8 @@ def mkAction(x):
case ["更新事件", idx] | ["runTrigger", idx]:
dealCycleM = chinaDealCycle | englishDealCycle
return mkTag(("RunTrigger", ["InWF",idx]))
case ["查看",*ds] | ["inspect",*ds]:
return mkTag(("WatchVal",[None,[mkDs(_) for _ in ds]]))
case _:
raise RuntimeError(f"Failed to match :{x}:mkAction")

Expand Down
2 changes: 1 addition & 1 deletion absbox/local/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def read_pricing(self, pricing):
return None

@staticmethod
def read(resp, position=None):
def read(resp):
read_paths = {'bonds': ('bndStmt' , english_bondflow_fields , "bond")
, 'fees': ('feeStmt' , english_fee_flow_fields_d , "fee")
, 'accounts': ('accStmt' , english_acc_flow_fields_d , "account")
Expand Down

0 comments on commit ea0294f

Please sign in to comment.