示例#1
0
        public async Task RecordValueHistory(VooperContext context, string type, DateTime time)
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();

            // First clear outdated records
            if (type == "MINUTE")
            {
                context.RemoveRange(context.ValueHistory.AsQueryable().Where(v => v.Type == "MINUTE" && EF.Functions.DateDiffMinute(v.Time, time) > 2000));
            }
            else if (type == "HOUR")
            {
                context.RemoveRange(context.ValueHistory.AsQueryable().Where(v => v.Type == "HOUR" && EF.Functions.DateDiffHour(v.Time, time) > 2000));
            }
            else if (type == "DAY")
            {
                context.RemoveRange(context.ValueHistory.AsQueryable().Where(v => v.Type == "DAY" && EF.Functions.DateDiffDay(v.Time, time) > 2000));
            }

            // Add new records

            List <ValueHistory> additions = new List <ValueHistory>();

            // User Portfolios

            foreach (Entity user in context.Users)
            {
                decimal portValue = await user.GetPortfolioValue();

                if (portValue > 0)
                {
                    ValueHistory hist = new ValueHistory()
                    {
                        Id         = Guid.NewGuid().ToString(),
                        Account_Id = user.Id,
                        Time       = time,
                        Type       = type,
                        Value      = Math.Min(portValue, 9999999999M)
                    };

                    additions.Add(hist);
                }
            }

            // Group Portfolios

            foreach (Entity group in context.Groups)
            {
                decimal portValue = await group.GetPortfolioValue();

                if (portValue > 0)
                {
                    ValueHistory hist = new ValueHistory()
                    {
                        Id         = Guid.NewGuid().ToString(),
                        Account_Id = group.Id,
                        Time       = time,
                        Type       = type,
                        Value      = Math.Min(portValue, 9999999999M)
                    };

                    additions.Add(hist);
                }
            }

            // Stocks

            foreach (StockDefinition stock in context.StockDefinitions)
            {
                decimal value = stock.Current_Value;

                int volume = 0;

                Dictionary <string, int> volumesDict = null;

                if (type == "MINUTE")
                {
                    volumesDict = ExchangeManager.VolumesMinute;
                }
                else if (type == "HOUR")
                {
                    volumesDict = ExchangeManager.VolumesHour;
                }
                else
                {
                    volumesDict = ExchangeManager.VolumesDay;
                }

                if (volumesDict.ContainsKey(stock.Ticker))
                {
                    volume = volumesDict[stock.Ticker];
                    volumesDict[stock.Ticker] = 0;
                }

                ValueHistory hist = new ValueHistory()
                {
                    Id         = Guid.NewGuid().ToString(),
                    Account_Id = stock.Ticker,
                    Time       = time,
                    Type       = type,
                    Value      = Math.Min(value, 9999999999M),
                    Volume     = volume
                };

                additions.Add(hist);
            }

            await context.AddRangeAsync(additions);

            await context.SaveChangesAsync();

            sw.Stop();

            _logger.LogInformation($"Added {additions.Count} financial records in {sw.Elapsed.Seconds} seconds.");
        }