private void InitialReceives() { Receive <DownloadFinished_Research_Step1>(par => { Log.Information("Research started"); List <ResearchResult> researchResults = new List <ResearchResult>(); foreach (FundResponce fr in par.Responces) { ResearchResult rr = CheckFundResponse(fr); if (rr.status) { researchResults.Add(rr); } } FundGroup fg = FundGroups.Groups?.FirstOrDefault(x => x.Name == ResearchResultIdent); if (fg != null) { FundGroups.Groups.Remove(fg); } fg = new FundGroup { Name = ResearchResultIdent, Funds = researchResults.OrderBy(x => x.periodChange).Take(20) .Select(x => new Fund { Id = x.FundId, GroupName = ResearchResultIdent, }) .ToList() }; FundGroups.Groups.Add(fg); Context.Parent.Tell(new StartDownload_Research { FundGroupName = ResearchResultIdent }); }); }
public static List <FundGroup> LoadFundGroups(string NeFileName, out string ErrMes) { ErrMes = null; List <FundGroup> res = null; if (!File.Exists(NeFileName)) { ErrMes = "File '" + NeFileName + "' not found"; return(res); } string[] lines = File.ReadAllLines(NeFileName); char[] splitters = new char[] { ',' }; if (lines.Length == 0) { return(res); } res = new List <FundGroup>(); FundGroup allGroup = new FundGroup() { Name = Fund.AllGroupName }; res.Add(allGroup); for (int i = 1; i < lines.Length; i++) { Fund curFund = Fund.CreateFromLine(lines[i]); if (curFund != null) { if (allGroup.Funds.FirstOrDefault(x => x.Id == curFund.Id) != null) { continue; } FundGroup curGroup = res.FirstOrDefault(x => x.Name == curFund.GroupName); if (curGroup == null) { res.Add((curGroup = new FundGroup() { Name = curFund.GroupName })); } curGroup.Funds.Add(curFund); allGroup.Funds.Add(curFund); } } FundGroup CommonGroup = res.FirstOrDefault(x => x.Name == Fund.CommonGroupName); if (CommonGroup != null) { //split the "Common" group int CommonGroupSize = 8; //!!! CommonGroup.Funds = CommonGroup.Funds.OrderBy(x => x.Id).ToList(); // int CurSubGroupIndex = 0; // while(CommonGroup.Funds.Count>0) // { // FundGroup curGroup = new FundGroup() { Name = Fund.CommonGroupName +"_"+CurSubGroupIndex.ToString()}; // curGroup.Funds = CommonGroup.Funds.Take(CommonGroupSize).ToList(); // res.Add(curGroup); // foreach (Fund f in curGroup.Funds) // { // CommonGroup.Funds.Remove(f); // f.GroupName = curGroup.Name; // } // CurSubGroupIndex++; // } for (int CurSubGroupIndex = 0; CurSubGroupIndex <= CommonGroup.Funds.Count / CommonGroupSize; CurSubGroupIndex++) { FundGroup curGroup = new FundGroup() { Name = Fund.CommonGroupName + "_" + CurSubGroupIndex.ToString(), Funds = new List <Fund>(), }; res.Add(curGroup); for (int i = CurSubGroupIndex * CommonGroupSize; i < CommonGroup.Funds.Count && i < (CurSubGroupIndex + 1) * CommonGroupSize; i++) { curGroup.Funds.Add(CommonGroup.Funds[i].Clone(curGroup.Name)); } } } //res.SaveToFile(); return(res); }
private void InitialReceives() { Receive <StartDownload>(par => { try { _stopCommand = false; _requests.Clear(); DF = new DownloadFinished(); DownloadManager.status = Status.InProcess; FundGroup fg = FundGroups.Groups?.FirstOrDefault(x => x.Name == (par.FundGroupName ?? "nosale")); if (fg == null) { Sender.Tell(new FundGroupNotFound()); } else { DF.From = par.From ?? new DateTime(2020, 03, 29); DF.To = par.To ?? DateTime.Now; DF.FundGroupName = fg.Name; FundRequest cur; foreach (Fund f in fg.Funds) { _requests.Add((cur = new FundRequest { FundId = f.Id, From = DF.From, To = DF.To, Tag = f, status = Status.InProcess, retry = 1, })); if (_downloaderRef != null) { _downloaderRef.Tell(cur); } } } } catch (Exception ex) { Log.Error("DownloadManager:StartDownload " + ex.Message); } }); Receive <Stop>(job => { _stopCommand = true; }); Receive <FundResponce>(responce => { try { if (responce != null) { DF.Responces.Add(responce); FundRequest req = _requests.FirstOrDefault(x => x.FundId == responce.FundId); if (req != null) { req.status = Status.Successfuly; } if (_requests.Count(x => x.status == Status.Successfuly) == _requests.Count) { status = Status.Successfuly; Context.Parent.Tell(DF); } else if (_requests.Count(x => x.status == Status.InProcess) + _requests.Count(x => x.status == Status.Failed && x.retry < 3) == 0) { status = Status.Failed; } else { status = Status.InProcess; } } // BusinesLogic.Log.Logging(BusinesLogic.OP, "FundResponce"); // responce.SaveToFile(); // MainWindowViewModel.VP.AddNewSerie(responce); } catch (Exception ex) { Log.Error("DownloadManager:FundResponce " + ex.Message); } }); Receive <FailedFun>(res => { try { if (res != null) { FundRequest req = _requests.FirstOrDefault(x => x.FundId == res.FundId); if (req != null) { req.status = Status.Failed; req.retry++; if (!_stopCommand && req.retry <= 3) { _downloaderRef.Tell(req); } } } } catch (Exception ex) { Log.Error("DownloadManager:FailedFun " + ex.Message); } }); Receive <StartResearch_Step1>(par => { try { _stopCommand = false; _requests.Clear(); DF = new DownloadFinished_Research_Step1(); DownloadManager.status = Status.InProcess; FundGroup fg = FundGroups.Groups?.FirstOrDefault(x => x.Name == Fund.AllGroupName); if (fg == null) { Sender.Tell(new FundGroupNotFound()); } else { DF.From = DateTime.Now.AddDays(-Research.CheckPeriod_Days - 2); DF.To = DateTime.Now; DF.FundGroupName = fg.Name; FundRequest cur; foreach (Fund f in fg.Funds) { _requests.Add((cur = new FundRequest { FundId = f.Id, From = DF.From, To = DF.To, Tag = f, status = Status.InProcess, retry = 1, })); if (_downloaderRef != null) { _downloaderRef.Tell(cur); } } } } catch (Exception ex) { Log.Error("DownloadManager:StartResearch_Step1 " + ex.Message); } }); }