using CsvHelper; using Microsoft.Extensions.DependencyInjection; using Qdp.Foundation.Utilities; using System.Diagnostics; using YieldChain.Commons; using YLErp.Cache; using YLErp.DBModels; using YLErp.Models; namespace YLErp.Modules.TradeRiskCalcModule.TaskRunner { /// /// 场外交易风险计算运行 /// class OtcTradeRiskRunner : InnerRunnerBase { readonly static IYLCache ylCache; static OtcTradeRiskRunner() { ylCache = YLServiceLocator.ServiceProvider.GetService(); } public OtcTradeRiskRunner(string volTpe) { VolType = volTpe ?? throw new ArgumentNullException(nameof(volTpe)); } /// /// 波动率类型 /// public string VolType { get; } /// /// 风险计算结果 /// public TradingRiskResult Result { get; set; } public override void StartTask(InnerRunTaskData taskData) { var valueDate = taskData.ValueDate; AppManager.SetSysInfo("实时风险-" + VolType + "-计算开始", "开始"); InnerStartTask(() => { var calcContext = new TradeRiskCalcContext(VolType, false, valueDate, taskData.PreSettleDate, taskData.DataProvider); var list = new TradeRiskCalcService(calcContext, skipTradeTypes: PS.Config.ErpElement.RealtimeRisk_SkipTradeTypes) .Calculate(taskData.TradeDataSource, VolType == "对冲" ? taskData.ManualRiskProviderHedge : taskData.ManualRiskProvider); var result = GetResult(); if (result == null || result.CalcStartTime > DateTime.Now || result.CalcStartTime < StartTime) { result = new TradingRiskResult { ValueDate = valueDate, PreDate = calcContext.PreSettleDate, TradeRiskList = list, CalcStartTime = StartTime, CalcEndTime = DateTime.Now }; SaveResult(result); } else { LogFactory.GetLogger("实时风险").Info($"[voltype:{VolType},valueDate:{valueDate}]抛弃一条过期数据"); } var totalMilliSeconds = (DateTime.Now - StartTime).TotalSeconds.ToString("F"); AppManager.SetSysInfo("实时风险-" + VolType + "-计算完成", $"开始时间:{StartTime},耗时{totalMilliSeconds}毫秒"); }).ContinueWith(t => { if (t.Exception != null) { LogFactory.GetLogger("实时风险").Error(t.Exception, $"[voltype:{VolType},valueDate:{valueDate}]计算出错"); } else { LogFactory.GetLogger("实时风险").Debug($"[voltype:{VolType},valueDate:{valueDate}]计算完成"); } InnerCompleteTask(); }); } private const string TradingRiskResultRedisKey = "TradeRisk"; /// /// 将结果保存 /// private void SaveResult(TradingRiskResult result) { //if (ylCache.CacheEnable()) //{ // var riskList = result.TradeRiskList.ToList(); // //// 测试数据上线前暂时保存 // //var baserisk = result.TradeRiskList.FirstOrDefault().Clone(); // //for (int i = 0; i < 5000; i++) // //{ // // var risk = baserisk.Clone(); // // risk.BookId = i; // // riskList.Add(risk); // //} // Stopwatch stopwatch= Stopwatch.StartNew(); // // batch delete from redis // ylCache.BatchDelete($"{TradingRiskResultRedisKey}:{VolType}:*"); // stopwatch.Stop(); // Console.WriteLine($"删除耗时:{stopwatch.ElapsedMilliseconds}"); // stopwatch.Restart(); // // batch add to redis // ylCache.BatchAdd($"{TradingRiskResultRedisKey}:{VolType}", riskList,1000).Wait(); // stopwatch.Stop(); // Console.WriteLine($"新增耗时:{stopwatch.ElapsedMilliseconds}"); // // save Result Objec without TradeRiskList // result.TradeRiskList = null; // ylCache.StringSet($"{TradingRiskResultRedisKey}_Result:{VolType}", result); //} //else { // save result to memory Result = result; } } /// /// 查询结果 /// /// public TradingRiskResult GetResult() { //if (ylCache.CacheEnable()) //{ // // get result object from redis // var result= ylCache.StringGet($"{TradingRiskResultRedisKey}_Result:{VolType}"); // if (result == null) // { // return null; // } // // get risk list from redis // var risks= ylCache.BatchQuery($"{TradingRiskResultRedisKey}:{VolType}:*"); // result.TradeRiskList= risks.ToList(); // return result; //} //else { // get result from memory return Result; } } public override string ToString() { return VolType; } } }