Beispiel #1
0
        public async Task <JsonResult> SyncStatistics(int?from)
        {
            var stats = await _statisticsRepository.GetAllCompaniesStatistics();

            var radarioCompanies = mf.CompanyManager.GetAllCompanies();
            var updateCount      = 0;
            var insertCount      = 0;

            foreach (var radarioCompany in radarioCompanies.Where(x => x.Id > from.GetValueOrDefault()))
            {
                var entity       = stats.SingleOrDefault(x => x.CompanyId == radarioCompany.Id);
                var shouldInsert = entity == null;
                entity           = entity ?? new Statistics();
                entity.Hostname  = radarioCompany.Title;
                entity.CompanyId = radarioCompany.Id;
                entity.Logo      = GetLogo(radarioCompany.Logo);
                var aggregatedYearInfo = mf.CompanyManager.GetAggregatedYearInfoByCompany(radarioCompany.Id);
                if (aggregatedYearInfo.Any())
                {
                    entity.Year = MapYearInfo(aggregatedYearInfo);
                    var eventIdsWithSales = aggregatedYearInfo.Select(x => x.EventId);
                    entity.EventsCovers = ProcessEventCovers(eventIdsWithSales);
                    entity.BestChannel  = GetBestChannels(radarioCompany.Id);
                    var dateStats = mf.CompanyManager.GetAggregatedDateStatistics(radarioCompany.Id);
                    entity.BestMonth = GetBestMonth(dateStats);
                    entity.BestDay   = GetBestDayOfWeek(dateStats);
                    entity.BestTime  = GetBestTime(dateStats);
                }
                else
                {
                    _logger.LogInformation($"Host {radarioCompany.Id} {radarioCompany.Title} have sold nothing in 2018");
                }
                if (shouldInsert)
                {
                    await _statisticsRepository.Insert(entity);

                    insertCount++;
                }
                else
                {
                    await _statisticsRepository.Update(entity);

                    updateCount++;
                }

                if (updateCount % 10 == 0 || insertCount % 10 == 0)
                {
                    _logger.LogInformation($"[SyncStatistics] Already updated: {updateCount} | inserted: {insertCount}");
                }
            }
            return(Json(new {
                Updated = updateCount,
                CreatedNew = insertCount
            }, JsonSerializerSettings));
        }