예제 #1
0
        public async Task RefreshIndexes()
        {
            try
            {
                var indexCodes = StaticData.Instruments.Where(x => x.CompanyCode == Constants.CompanyCodes.IDXS)
                                 .Select(x => x.InsCode)
                                 .ToArray();

                OnOperationStart?.Invoke(this, indexCodes.Length + 3);

                List <Index> indices          = new List <Index>();
                var          readWebSiteBlock = new ActionBlock <long>(
                    async inxCode =>
                {
                    indices.Add(await Online.GetIndex(inxCode, false));
                    OnOperationStep?.Invoke(this, EventArgs.Empty);
                }, new ExecutionDataflowBlockOptions
                {
                    MaxDegreeOfParallelism = 15
                });

                foreach (var code in indexCodes)
                {
                    readWebSiteBlock.Post(code);
                }

                readWebSiteBlock.Complete();
                await readWebSiteBlock.Completion;

                var indexLastValue = await Online.GetIndexLastValue();

                OnOperationStep?.Invoke(this, EventArgs.Empty);

                if (indices.Count > 0)
                {
                    var idxSrvf = StaticServiceFactory.Create <IIndexLastDayTimeValueService>();
                    var today   = DateTime.Now.Date;
                    var idxData = idxSrvf.GetDefaultQuery()
                                  .Where(x => x.Dt > today)
                                  .ToList();
                    OnOperationStep?.Invoke(this, EventArgs.Empty);

                    var newData = indices.Where(x => x != null).SelectMany(x => x.LastDayTimeValue)
                                  .Where(x => x.Dt >= today)
                                  .Where(x => !idxData.Any(y => y.InsCode == x.InsCode && x.Dt == y.Dt))
                                  .ToArray();

                    foreach (var dtv in newData)
                    {
                        var lv = indexLastValue.FirstOrDefault(x => x.Code == dtv.InsCode);
                        if (lv != null)
                        {
                            dtv.ChangePercent = lv.ChangePercent;
                            dtv.ChangeValue   = lv.ChangeValue;
                        }
                    }

                    await idxSrvf.SaveEntitiesAsync(newData);

                    OnOperationStep?.Invoke(this, EventArgs.Empty);
                }
                OnOperationCompleted?.Invoke(this, EventArgs.Empty);
            }
            catch (Exception exception)
            {
                _logger.Error("RefreshIndexes", exception);
                OnOperationBreak?.Invoke(this, exception);
            }
        }