def fundrisk(innercode, checkdate): """ innercode from url:param innercode: checkdate from url:param checkdate: reult fundrisk with json type:return: """ innercodeCalc, result = ca.reshape_innercode(innercode=innercode, checkdate=checkdate, cache=ca.fundRiskCache) """ check the cache for the expected short fall already existed """ #innercodeCalc=innercode result = df(result,columns=('innercode', 'es')) if len(innercodeCalc) != 0: startdate = datetime.strptime(checkdate, '%Y-%m-%d') - timedelta(days=730) query = """select financedata.ana_fnd_nav_calc.TRADEDATE, financedata.ana_fnd_nav_calc.fac_unit_net, financedata.ana_fnd_nav_calc.INNER_CODE from financedata.ana_fnd_nav_calc where financedata.ana_fnd_nav_calc.inner_code in ( %s ) and financedata.ana_fnd_nav_calc.tradedate<= '%s' and financedata.ana_fnd_nav_calc.tradedate>= '%s' """ % (','.join(innercodeCalc), checkdate, startdate.strftime('%Y-%m-%d')) """ asseemble the query """ indexPrice = GetDataFromDB(config, query) indexPrice.columns = ['tradedate', 'close', 'innercode'] """ get date from db """ grouped = indexPrice.groupby('innercode') result_new = df(columns=('innercode', 'es')) for i in innercodeCalc: try: dataForCalc = grouped.get_group(int(i)) es = cr.es(dataForCalc['tradedate'], dataForCalc['close']) result_new.loc[len(result_new.index)]=[i,es] except KeyError: es = '' result_new.loc[len(result_new.index)]=[i,es] """ constract the result maxtrix which innercode cannot be found in database with empty es """ #save cache for new data ca.set_cache(result_new=result_new, checkdate=checkdate,cache=ca.fundRiskCache) """ save the result in cache """ result = result.append(result_new,ignore_index=1) result['checkdate'] = checkdate else: result['checkdate'] = checkdate return (result.to_json(orient='index'))