Exemple #1
0
        private void DoUpdate()
        {
            var date = DateTime.Now.Date;

            using (var dbContext = _contextFactory.Create())
            {
                var dailyRates = _bankService.GetDailyRates(DateTime.Now).Result;
                if (dailyRates.Count == 0)
                {
                    return;
                }
                var ratesInDb = dbContext.DailyCurrencyRates.Where(x => x.Date == date).ToList();
                if (ratesInDb.Count != 0)
                {
                    var ratesInDbByCurrency = ratesInDb.ToDictionary(x => x.CurrencyCode);
                    foreach (var dailyRate in dailyRates)
                    {
                        var rateInDb = ratesInDbByCurrency[dailyRate.CurrencyCode];
                        rateInDb.OriginalRate = dailyRate.OriginalRate;
                        rateInDb.Amount       = dailyRate.Amount;
                        rateInDb.FinalRate    = dailyRate.FinalRate;
                    }
                    _logger.LogInformation($"Updated {ratesInDbByCurrency.Count} records");
                }
                else
                {
                    dbContext.AddRange(dailyRates);
                    _logger.LogInformation($"Inserted {dailyRates.Count} records");
                }

                dbContext.SaveChanges();
            }
        }
Exemple #2
0
        /// <summary>
        /// Starts writing data to the database.
        /// </summary>
        public void StartDataGeneration()
        {
            if (_timer != null)
            {
                return;
            }

            bool writingData = false;

            _timer = new Timer(state =>
            {
                if (writingData)
                {
                    return;
                }
                var sw = new Stopwatch();
                try
                {
                    sw.Reset();
                    sw.Start();
                    using (var dbContext = _dbContextFactory.Create())
                    {
                        var set = dbContext.Set <T>();
                        set.AddRange(_datagenerator.GetRecords(RecordsPerSecond));
                        dbContext.SaveChanges();
                        _recordsCount = set.Count();
                    }
                    sw.Stop();
                    _throttling = (float)sw.ElapsedMilliseconds / PERIOD;
                } finally
                {
                    writingData = false;
                }
            }, null, 0, PERIOD);
        }
        public void LoadData(int year)
        {
            try
            {
                using (var dbContext = _contextFactory.Create())
                {
                    if (dbContext.DailyCurrencyRates.Any(x => x.Date.Year == year))
                    {
                        _logger.LogWarning($"Current year {year} already in db");
                    }

                    var yearRates = _bankService.GetYearRates(year).Result;
                    var bulkSize  = 500;
                    for (int i = 0; i < yearRates.Count; i += bulkSize)
                    {
                        var forInsert = yearRates.Skip(i).Take(bulkSize);
                        dbContext.DailyCurrencyRates.AddRange(forInsert);
                        dbContext.SaveChanges();
                    }
                }
            }
            catch (Exception error)
            {
                _logger.LogError($"Error while LoadData for year {year} {error}");
            }
        }
Exemple #4
0
 /// <summary>
 /// Constructor which creates new DatabaseWriter instance.
 /// </summary>
 /// <param name="datagenerator">Instance of generator.</param>
 /// <param name="dbContextFactory">The factory object which creates <see cref="DbContext"/></param>
 public DatabaseWriter(IDatagenerator <T> datagenerator, IDBContextFactory dbContextFactory)
 {
     _datagenerator    = datagenerator;
     _dbContextFactory = dbContextFactory;
     RecordsPerSecond  = 150;
     using (var dbContext = _dbContextFactory.Create())
     {
         _recordsCount = dbContext.Set <T>().Count();
     }
 }
Exemple #5
0
 private List <IGrouping <DateTime, ReportCurrencyRate> > GetRatesFromDb(int year, int month, string[] currencies)
 {
     //https://docs.microsoft.com/ru-ru/ef/core/what-is-new/ef-core-2.1
     //печально что в данном случае не поддерживается groupBy
     //to-do переделать в plain и уже псевдо группировать при переборе
     using (var context = _contextFactory.Create())
     {
         return(context.DailyCurrencyRates
                .Where(x => x.Date.Year == year && x.Date.Month == month && currencies.Contains(x.CurrencyCode))
                .Select(x => new ReportCurrencyRate {
             CurrencyCode = x.CurrencyCode, Date = x.Date, FinalRate = x.FinalRate
         })
                .GroupBy(x => x.Date)
                .OrderBy(x => x.Key).ToList());
     }
 }