/// <summary> /// 用于同步限时操作 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="timeoutMilliseconds"></param> /// <param name="action"></param> /// <param name="actionKey"></param> /// <returns></returns> public static T CallActionWithTimeoutSync <T>(int timeoutMilliseconds, Func <T> action, string actionKey, ClientInfo cinfo) { T local = default(T); var thread = new Thread(() => { var client = new FeedbackServiceInterfaceClient(); local = action(); }); thread.Start(); if (!thread.Join(timeoutMilliseconds)) { if (cinfo != null) { var client = new CollectorInterfaceClient(); Task.Run <bool>(() => { //Task.Delay(5000).Wait(); return(client.SetConfused(new CollectorEntity() { Content = actionKey, Type = (int)cinfo.Type, IP = cinfo.IP, UserID = cinfo.UserName })); }); } } return(local); }
/// <summary> /// 写内容收集日志 /// </summary> /// <param name="cinfo"></param> /// <param name="q"></param> /// <param name="logType"></param> public static bool WriteContentLog(ClientInfo cinfo, string q) { var client = new CollectorInterfaceClient(); Task.Run <bool>(() => { return(client.SetConfused(new CollectorEntity() { Content = q, Type = (int)cinfo.Type, IP = cinfo.IP, UserID = cinfo.UserName })); }); return(true); }
/// <summary> /// 用于异步限时操作 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="timeoutMilliseconds"></param> /// <param name="action"></param> /// <param name="q"></param> /// <returns></returns> public static async Task <T> CallWithTimeoutAsync <T>(int timeoutMilliseconds, Func <T> action, string actionKey, ClientInfo cinfo) { T local = default(T); return(await Task.Run(() => { Thread threadToKill = null; Func <T> wrappedAction = () => { threadToKill = Thread.CurrentThread; return action(); }; IAsyncResult result = wrappedAction.BeginInvoke(null, null); if (result.AsyncWaitHandle.WaitOne(timeoutMilliseconds)) { local = wrappedAction.EndInvoke(result); } else { if (cinfo != null) { var client = new CollectorInterfaceClient(); Task.Run <bool>(() => { return client.SetConfused(new CollectorEntity() { Content = actionKey, Type = (int)cinfo.Type, IP = cinfo.IP, UserID = cinfo.UserName }); }); } if (threadToKill != null) { threadToKill.Abort(); } } return local; })); }
//[System.Web.Http.HttpGet] //[ValidateInput(false)] //[System.Web.Http.Route("GetKBData")] private async Task <Object> GetKBData(string q, string domain = null) { try { List <FuncElapsed> listElaps = new List <FuncElapsed>(); CBase.Log.TimeWatchLog tlogTotal = new CBase.Log.TimeWatchLog(); CBase.Log.TimeWatchLog tlog_temp = new CBase.Log.TimeWatchLog(); if (string.IsNullOrEmpty(q)) { return(null); } //根据意图分析结果,提供准确答案与资源导航、问答对 Dictionary <string, QueryContainer <CMKArea> > navContent = null; Task <Dictionary <string, QueryContainer <CMKArea> > > tNavi = null; ClientInfo cinfo = new ClientInfo() { UserName = CommonHelper.GetUserName(), IP = CommonHelper.GetClientIP() }; //获取意图识别 tlog_temp.Start(); Domain.Intent intent = new Answer(domain).GetIntent(q, domain); listElaps.Add(new FuncElapsed("GetIntent", tlog_temp.Debug())); //内容收集 意图是否识别收集用于数据分析 1未识别 2已识别 ContentLogType collectType = ContentLogType.已识别; if (intent != null && intent.qt_parsed_rst == null) { collectType = ContentLogType.未识别; } var client = new CollectorInterfaceClient(); Task.Run <bool>(() => { return(client.SetConfused(new CollectorEntity() { Content = intent.RawInput, Type = (int)collectType, IP = cinfo.IP, UserID = cinfo.UserName })); }); cinfo.Type = ContentLogType.知识库超时; tNavi = TimedTask.CallWithTimeoutAsync(timeout, () => new KB(domain).Get(intent), q, cinfo); await Task.WhenAll(tNavi); navContent = tNavi == null ? null : tNavi.Result; tlog_temp.Start(); cinfo.Type = ContentLogType.问题排序超时; QueryDataContainer SortData = DataSort.SortKB(null, null, navContent, cinfo); listElaps.Add(new FuncElapsed("SortKB", tlog_temp.Debug())); AnswerResultKB result = new Models.AnswerResultKB() { intent = intent, SortData = SortData }; result.timeSpan = tlogTotal.Debug(); SortData.ElapsedList.AddRange(listElaps); if (SortData.MetaList.Count == 0) { return(new { result = false, msg = "未找到查询结果!" }); } for (int i = 0; i < SortData.MetaList.Count; i++) { SortData.MetaList[i].QExp = null; } return(new { result = true, SortData.MetaList }); } catch (System.Exception ex) { CBase.Log.Logger.Error(ex); return(new { result = false, msg = ex.ToString() }); } }
public async Task <Object> GetAnswer(string q, string appid, string aid) { try { CBase.Log.TimeWatchLog twTotal = new CBase.Log.TimeWatchLog(); if (string.IsNullOrEmpty(q)) { return new { result = false, msg = "问题不能为空!" } } ; //根据意图分析结果,提供准确答案与资源导航、问答对 Dictionary <string, QueryContainer <CMKArea> > navContent = null; QueryContainer <CMFAQ> faq = null; QueryContainer <CMFAQ_NET> faq_net = null; Task <Dictionary <string, QueryContainer <CMKArea> > > tNavi = null; List <Task <QueryContainer <CMFAQ> > > tFaqList = null; Task <QueryContainer <CMFAQ_NET> > tFaq_net = null; ClientInfo cinfo = new ClientInfo() { UserName = CommonHelper.GetUserName(), IP = CommonHelper.GetClientIP() }; //获取意图识别 Domain.Intent intent = new Answer().GetIntent(q); //内容收集 意图是否识别收集用于数据分析 1未识别 2已识别 ContentLogType collectType = ContentLogType.已识别; if (intent != null && intent.qt_parsed_rst == null) { collectType = ContentLogType.未识别; } var client = new CollectorInterfaceClient(); Task.Run <bool>(() => { return(client.SetConfused(new CollectorEntity() { Content = intent.RawInput, Type = (int)collectType, IP = cinfo.IP, UserID = cinfo.UserName })); }); List <string> filterDomain = new List <string>(); QueryConfig qconfig = new QueryConfig().GetQueryConfig(); if (qconfig != null && qconfig.DomainMap.ContainsKey("default")) { filterDomain = qconfig.DomainMap["default"].FAQ; } var tasks = new List <Task>(); cinfo.Type = ContentLogType.知识库超时; tNavi = TimedTask.CallWithTimeoutAsync(timeout, () => new KB().Get(intent), q, cinfo); cinfo.Type = ContentLogType.问答集超时; tFaqList = GetFAQTask(q, timeout, filterDomain); cinfo.Type = ContentLogType.用户问答集超时; tFaq_net = TimedTask.CallWithTimeoutAsync(timeout, () => new Faq_net().Get(q), q, cinfo); tasks.Add(tNavi); tasks.AddRange(tFaqList); tasks.Add(tFaq_net); await Task.WhenAll(tasks.ToArray()); navContent = tNavi == null ? null : tNavi.Result; faq = GetFAQAll(tFaqList); //如果faq和faqnet有内容就不返回知识库内容 faq_net = tFaq_net == null ? null : tFaq_net.Result; if ((faq != null && faq.Total > 0) || (faq_net != null && faq_net.Total > 0)) { navContent = null; } cinfo.Type = ContentLogType.问题排序超时; QueryDataContainer SortData = DataSort.SortKB(faq, faq_net, navContent, cinfo); twTotal.Write("GetAnswer"); if (SortData.MetaList.Count == 0) { return(new { result = false, msg = "未找到查询结果!" }); } return(new { result = true, SortData.MetaList }); } catch (System.Exception ex) { CBase.Log.Logger.Error(ex); return(new { result = false, msg = ex.ToString() }); } }