Пример #1
0
        public ScenarioCampaignResultsFileProcessorTest()
        {
            _mockAuditEventRepository = new Mock <IAuditEventRepository>();
            _mockSalesAreaRepository  = new Mock <ISalesAreaRepository>();
            _context = new RunWithScenarioReference(Guid.NewGuid(), Guid.NewGuid());
            var dataSnapshot = new Mock <IOutputDataSnapshot>();

            int id = 1;

            _dummySalesArea = _fixture
                              .Build <SalesArea>()
                              .Do(s => s.CustomId = id++)
                              .CreateMany(5);

            _ = _mockSalesAreaRepository.Setup(m => m.GetAll()).Returns(_dummySalesArea);
            _ = dataSnapshot.Setup(x => x.AllSalesAreas)
                .Returns(new Lazy <IEnumerable <SalesArea> >(_mockSalesAreaRepository.Object.GetAll));

            var importSettings = CSVImportSettings.GetImportSettings(
                Path.Combine(TestRootDataFolder, "lmkii_scen_camp_reqm_summ.csv"),
                typeof(ScenarioCampaignResultHeaderMap),
                typeof(ScenarioCampaignResultIndexMap)
                );

            var scenarioCampaignResultsImportRepository = new CSVScenarioCampaignResultsImportRepository(importSettings);

            _expectedItems = scenarioCampaignResultsImportRepository.GetAll();

            _processor = new ScenarioCampaignResultsFileProcessor(dataSnapshot.Object, _mockAuditEventRepository.Object);
        }
        public CampaignsReqmOutput ProcessFile(Guid scenarioId, string folder)
        {
            string pathToFile = FileHelpers.GetPathToFileIfExists(folder, FileName);
            var    result     = new CampaignsReqmOutput();

            if (String.IsNullOrEmpty(pathToFile))
            {
                _audit.Insert(AuditEventFactory.CreateAuditEventForWarningMessage(0, 0, $"File {pathToFile} was not found."));

                return(result);
            }

            _audit.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0,
                                                                                  0, $"Processing output file {pathToFile}"));

            var importSettings = CSVImportSettings.GetImportSettings(pathToFile, typeof(CampaignsReqmHeaderMap), typeof(CampaignsReqmIndexMap));

            ICampaignsReqmImportRepository _fileReader = new CSVCampaignsReqmImportRepository(importSettings);

            var data = _fileReader.GetAll().ToList();

            result.Data = _mapper.Map <IEnumerable <CampaignsReqm> >(data);

            _audit.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0,
                                                                                  0, $"Processed output file {pathToFile}"));

            return(result);
        }
        public ScenarioCampaignLevelResult ProcessFile(Guid scenarioId, string folder)
        {
            string pathToFile = FileHelpers.GetPathToFileIfExists(folder, FileName);

            var result = new ScenarioCampaignLevelResult {
                Id = scenarioId
            };

            if (string.IsNullOrEmpty(pathToFile))
            {
                _audit.Insert(AuditEventFactory.CreateAuditEventForWarningMessage(0, 0, $"File {pathToFile} was not found."));

                return(result);
            }

            _audit.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0,
                                                                                  0, $"Processing output file {pathToFile}"));

            var importSettings = CSVImportSettings.GetImportSettings(pathToFile, typeof(ScenarioCampaignLevelResultHeaderMap), typeof(ScenarioCampaignLevelResultIndexMap));
            IScenarioCampaignLevelResultsImportRepository repository = new CSVScenarioCampaignLevelResultsImportRepository(importSettings);

            var data = repository.GetAll();

            result.Items = _mapper.Map <List <ScenarioCampaignLevelResultItem> >(data);

            _audit.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0,
                                                                                  0, $"Processed output file {pathToFile}"));

            return(result);
        }
        public IEnumerable <ConversionEfficiency> Process(string pathToFile)
        {
            if (!File.Exists(pathToFile))
            {
                _audit?.Insert(AuditEventFactory.CreateAuditEventForWarningMessage(0, 0, "File was not found."));

                return(Enumerable.Empty <ConversionEfficiency>());
            }

            _audit?.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0,
                                                                                   0, $"Processing output file {pathToFile}"));

            try
            {
                var importSettings = CSVImportSettings.GetImportSettings(pathToFile, typeof(ConversionEfficiencyHeaderMap), typeof(ConversionEfficiencyIndexMap));

                var conversionEfficiency = new CSVConversionEfficiencyImportRepository(importSettings);

                var data = conversionEfficiency.GetAll();

                var output = _mapper.Map <IEnumerable <ConversionEfficiency> >(data);

                _audit?.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0,
                                                                                       0, $"Processed output file {pathToFile}"));

                return(output);
            }
            catch (Exception exception)
            {
                _audit?.Insert(AuditEventFactory.CreateAuditEventForException(0, 0, $"Error processing file {pathToFile}", exception));

                return(Enumerable.Empty <ConversionEfficiency>());
            }
        }
Пример #5
0
        public Failures ProcessFile(Guid scenarioId, string folder)
        {
            string pathToFile = FileHelpers.GetPathToFileIfExists(folder, FileName);
            var    failures   = new Failures {
                Id = scenarioId
            };

            if (string.IsNullOrEmpty(pathToFile))
            {
                _audit.Insert(AuditEventFactory.CreateAuditEventForWarningMessage(0, 0, $"File {pathToFile} was not found."));

                return(failures);
            }

            _audit.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0,
                                                                                  0, $"Processing output file {pathToFile}"));

            var importSettings = CSVImportSettings.GetImportSettings(pathToFile, typeof(FailureHeaderMap), typeof(FailureIndexMap));

            var campaignsIndex = _dataSnapshot.AllCampaigns.Value.ToDictionary(c => (long)c.CustomId);
            var salesAreaIndex = _dataSnapshot.AllSalesAreas.Value.ToDictionary(c => c.CustomId);

            IFailureImportRepository failureRepository = new CSVFailureImportRepository(importSettings);

            foreach (var summary in failureRepository.GetAll())
            {
                var failure = new Failure();

                if (!campaignsIndex.TryGetValue(summary.Campaign, out var campaign))
                {
                    failure.ExternalId = failure.CampaignName = "Unknown";
                }
                else
                {
                    failure.ExternalId   = campaign.ExternalId;
                    failure.CampaignName = campaign.Name;
                }

                failure.Type     = summary.Type;
                failure.Failures = summary.Failures;
                failure.Campaign = summary.Campaign;

                failure.SalesAreaName = !salesAreaIndex.TryGetValue(summary.SalesAreaNumberOfBooking, out var salesArea)
                    ? "Unknown"
                    : salesArea.Name;

                failures.Items.Add(failure);
            }

            _audit.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0,
                                                                                  0, $"Processed output file {pathToFile}"));

            return(failures);
        }
Пример #6
0
        public void ProcessFile(string file, ScenarioResult scenarioResult)
        {
            if (!File.Exists(file))
            {
                return;
            }

            if (_outputFileConverter != null)
            {
                string convertedFile = $"{file}.tmp";
                _outputFileConverter.Convert(file, convertedFile);
                File.Delete(file);
                File.Move(convertedFile, file);
            }

            var kpiRepository = new CSVLmkKpiImportRepository(CSVImportSettings.GetImportSettings(file, typeof(LmkKpiHeaderMap), typeof(LmkKpiIndexMap)));
            var metrics       = kpiRepository.Get();

            var result = new List <KPI>();

            foreach (var metric in Metrics)
            {
                result.Add(new KPI
                {
                    Name          = metric.Name,
                    Displayformat = metric.DisplayFormat,
                    Value         = metric.Value(metrics),
                    ResultSource  = _resultSource
                });
            }

            if (_resultSource == KPISource.Gameplan)
            {
                scenarioResult.Metrics.AddRange(result);
            }
            else if (_resultSource == KPISource.Landmark)
            {
                scenarioResult.LandmarkMetrics.AddRange(result);
            }
            else
            {
                throw new InvalidOperationException($"Unknown result source {_resultSource}");
            }
        }
        public BaseRatingsOutput ProcessFile(Guid scenarioId, string folder)
        {
            string pathToFile = FileHelpers.GetPathToFileIfExists(folder, FileName);
            var    result     = new BaseRatingsOutput();

            if (String.IsNullOrEmpty(pathToFile))
            {
                _audit.Insert(AuditEventFactory.CreateAuditEventForWarningMessage(0, 0, $"File {pathToFile} was not found."));

                return(result);
            }

            _audit.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0,
                                                                                  0, $"Processing output file {pathToFile}"));

            try
            {
                var importSettings = CSVImportSettings.GetImportSettings(pathToFile, typeof(BaseRatingsHeaderMap), typeof(BaseRatingsIndexMap));

                var baseRatingsImportRepository = new CSVBaseRatingsImportRepository(importSettings);

                var data = baseRatingsImportRepository.GetAll();

                result.Data = _mapper.Map <IEnumerable <BaseRatings> >(data);

                _audit.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0,
                                                                                      0, $"Processed output file {pathToFile}"));

                return(result);
            }
            catch (Exception exception)
            {
                _audit.Insert(AuditEventFactory.CreateAuditEventForException(0, 0, "Error processing base ratings file", exception));

                return(result);
            }
        }
        public ScenarioCampaignResult ProcessFile(Guid scenarioId, string folder)
        {
            string pathToFile = FileHelpers.GetPathToFileIfExists(folder, FileName);
            var    result     = new ScenarioCampaignResult {
                Id = scenarioId
            };

            if (string.IsNullOrEmpty(pathToFile))
            {
                _audit.Insert(AuditEventFactory.CreateAuditEventForWarningMessage(0, 0, $"File {pathToFile} was not found."));

                return(result);
            }

            _audit.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0,
                                                                                  0, $"Processing output file {pathToFile}"));

            var importSettings = CSVImportSettings.GetImportSettings(pathToFile, typeof(ScenarioCampaignResultHeaderMap), typeof(ScenarioCampaignResultIndexMap));
            IScenarioCampaignResultsImportRepository repository = new CSVScenarioCampaignResultsImportRepository(importSettings);

            var data = repository.GetAll();

            var salesAreaIndex = _dataSnapshot.AllSalesAreas.Value.ToDictionary(c => c.CustomId);

            foreach (var importItem in data)
            {
                salesAreaIndex.TryGetValue(importItem.SalesAreaNo, out var salesArea);

                result.Items.Add(MapImportItem(importItem, salesArea));
            }

            _audit.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0,
                                                                                  0, $"Processed output file {pathToFile}"));

            return(result);
        }
Пример #9
0
 public CSVConversionEfficiencyImportRepository(CSVImportSettings importSettings)
 {
     _settings = importSettings;
 }
Пример #10
0
        public SpotsReqmOutput ProcessFile(Guid scenarioId, string folder)
        {
            string spotFile     = FileHelpers.GetPathToFileIfExists(folder, FileName);
            string mainSpotFile = FileHelpers.GetPathToFileIfExists(folder, OutputFileNames.MainSpotTable);
            var    output       = new SpotsReqmOutput
            {
                ScenarioId = scenarioId
            };

            if (String.IsNullOrEmpty(spotFile) || String.IsNullOrEmpty(mainSpotFile))
            {
                return(output);
            }

            _audit.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0,
                                                                                  0, $"Processing output file {spotFile}"));

            _audit.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0,
                                                                                  0, $"Processing output file {mainSpotFile}"));

            // Process file
            DateTime processorDateTime = DateTime.UtcNow;

            var importSettings = CSVImportSettings.GetImportSettings(mainSpotFile, typeof(SpotHeaderMap), typeof(SpotIndexMap));

            // Get main spots if path passed in
            ISpotImportRepository    spotImportRepository = new CSVSpotImportRepository(importSettings);
            IEnumerable <SpotImport> spotImports          = spotImportRepository.GetAll();

            importSettings = CSVImportSettings.GetImportSettings(spotFile, typeof(SpotReqmHeaderMap), typeof(SpotReqmIndexMap));

            ISpotReqmImportRepository spotReqmImportRepository = new CSVSpotReqmImportRepository(importSettings);
            var spotReqmImports = spotReqmImportRepository.GetAll();

            // Index for lookup performance
            var campaignsByCustomId       = CampaignReducedModel.IndexListByCustomId(_campaignRepository.GetAllFlat());
            var spotsByCustomId           = SpotHelper.IndexListById(_dataSnapshot.SpotsForRun.Value);
            var demographicsById          = Demographic.IndexListById(_dataSnapshot.AllDemographics.Value);
            var programmeDictionariesById = ProgrammeDictionary.IndexListById(_dataSnapshot.AllProgrammeDictionaries.Value);

            IEnumerable <Pass> scenarioPasses = _dataSnapshot.ScenarioPasses.Value;

            foreach (var currentSalesArea in _dataSnapshot.AllSalesAreas.Value)
            {
                foreach (SpotReqmImport spotReqmImport in spotReqmImports.Where(sri => sri.sare_no == currentSalesArea.CustomId))
                {
                    output.CountTotalSpots++;

                    try
                    {
                        Recommendation recommendation = _mapper.Map <Recommendation>(Tuple.Create(spotReqmImport, new List <ProgrammeDictionary>(), _dataSnapshot.BreakTypes.Value));
                        recommendation.ScenarioId        = scenarioId;
                        recommendation.ProcessorDateTime = processorDateTime;

                        bool isCancelledSpot = !String.IsNullOrEmpty(spotReqmImport.status) && spotReqmImport.status == "C";

                        if (isCancelledSpot)
                        {
                            output.CountCancelledSpots++;
                            recommendation.SpotSequenceNumber = default;

                            if (spotsByCustomId.TryGetValue((int)spotReqmImport.spot_no, out var spot))
                            {
                                recommendation.ExternalSpotRef       = spot.ExternalSpotRef;
                                recommendation.Sponsored             = spot.Sponsored;
                                recommendation.Preemptable           = spot.Preemptable;
                                recommendation.Preemptlevel          = spot.Preemptlevel;
                                recommendation.MultipartSpot         = spot.MultipartSpot;
                                recommendation.MultipartSpotPosition = spot.MultipartSpotPosition;
                                recommendation.MultipartSpotRef      = spot.MultipartSpotRef;
                            }
                            else
                            {
                                _audit.Insert(
                                    AuditEventFactory.CreateAuditEventForWarningMessage(
                                        0,
                                        0,
                                        $"Unable to set cancelled spot details for recommendations" +
                                        $" because spot {spotReqmImport.spot_no} does not exist")
                                    );
                            }
                        }
                        else
                        {
                            recommendation.SpotSequenceNumber = spotReqmImport.spot_no;
                        }

                        Pass pass;

                        if (scenarioPasses != null && scenarioPasses.Any() && (pass = scenarioPasses.FirstOrDefault(p => p.Id == spotReqmImport.abdn_no)) != null)
                        {
                            recommendation.PassName = pass.Name;
                            recommendation.OptimiserPassSequenceNumber = spotReqmImport.pass_sequence_no;
                        }

                        recommendation.CampaignPassPriority = spotReqmImport.campaign_pass_priority;
                        recommendation.RankOfEfficiency     = spotReqmImport.rank_of_efficiency;
                        recommendation.RankOfCampaign       = spotReqmImport.rank_of_campaign;
                        recommendation.CampaignWeighting    = spotReqmImport.campaign_weighting;

                        recommendation.SalesArea = recommendation.GroupCode = currentSalesArea.Name;

                        recommendation.NominalPrice = spotReqmImport.nominal_price;

                        recommendation.ExternalCampaignNumber = campaignsByCustomId.TryGetValue((int)spotReqmImport.camp_no, out var campaign)
                            ? campaign.ExternalId
                            : string.Empty;

                        recommendation.Demographic = demographicsById.TryGetValue(spotReqmImport.demo_no, out var demographic)
                            ? demographic.ExternalRef
                            : string.Empty;

                        if (programmeDictionariesById.TryGetValue((int)spotReqmImport.prog_no, out var programmeDictionary))
                        {
                            recommendation.ExternalProgrammeReference = programmeDictionary.ExternalReference;
                            recommendation.ProgrammeName = programmeDictionary.ProgrammeName;
                        }
                        else
                        {
                            recommendation.ExternalProgrammeReference = recommendation.ProgrammeName = string.Empty;
                        }

                        recommendation.ClientPicked    = spotReqmImport.client_picked.ToUpper() == "Y";
                        recommendation.ExternalBreakNo = spotReqmImport.brek_external_ref;

                        output.Recommendations.Add(recommendation);
                    }
                    catch (System.Exception exception)
                    {
                        // Log exception, continue
                        _audit.Insert(AuditEventFactory.CreateAuditEventForException(0, 0, string.Format("Error generating recommendation for spot no {0}", spotReqmImport.spot_no), exception));
                    }
                }
            }

            // TODO: Remove this when it has been confirmed that cancellations appear in Spot Req file
            // Process cancelled spots if they weren't present in the Spot Req
            // file above
            if (output.CountCancelledSpots == 0)
            {
                string scheduleIndexKey = string.Empty;

                var scheduleBreaksBySalesAreaAndDate = _dataSnapshot
                                                       .BreaksForRun
                                                       .Value
                                                       .GroupBy(k => GetScheduleIndexKey(k.SalesAreaName, k.ScheduleDate.Date))
                                                       .ToDictionary(c => c.Key, c => c.ToDictionary(v => v.CustomId));

                _audit.Insert(AuditEventFactory.CreateAuditEventForWarningMessage(0, 0, string.Format("Processing {0} for cancellations because no cancelled spots were found in {1}", Path.GetFileName(mainSpotFile), Path.GetFileName(spotFile))));

                foreach (var currentSalesArea in _dataSnapshot.AllSalesAreas.Value)
                {
                    foreach (SpotImport spotImport2 in spotImports.Where(s => s.status == "C" && s.sare_no == currentSalesArea.CustomId))
                    {
                        try
                        {
                            scheduleIndexKey = string.Empty;
                            bool isCancelledSpot = !String.IsNullOrEmpty(spotImport2.status) && spotImport2.status == "C";    // Should all be cancelled

                            // Update statistics for Spot Performance, Campaign
                            // Performance, New Efficiency, cancelled spots
                            // should be excluded
                            output.CountTotalSpots++;
                            if (isCancelledSpot)
                            {
                                output.CountCancelledSpots++;
                            }

                            // Create recommendation
                            Recommendation recommendation = _mapper.Map <Recommendation>(Tuple.Create(spotImport2, new List <ProgrammeDictionary>()));
                            recommendation.ScenarioId        = scenarioId;
                            recommendation.ProcessorDateTime = processorDateTime;

                            if (spotsByCustomId.TryGetValue((int)spotImport2.spot_no, out var spot))
                            {
                                recommendation.Sponsored             = spot.Sponsored;
                                recommendation.Preemptable           = spot.Preemptable;
                                recommendation.Preemptlevel          = spot.Preemptlevel;
                                recommendation.ExternalSpotRef       = spot.ExternalSpotRef;
                                recommendation.MultipartSpot         = spot.MultipartSpot;
                                recommendation.MultipartSpotPosition = spot.MultipartSpotPosition;
                                recommendation.MultipartSpotRef      = spot.MultipartSpotRef;
                            }
                            else
                            {
                                _audit.Insert(
                                    AuditEventFactory.CreateAuditEventForWarningMessage(
                                        0,
                                        0,
                                        $"Unable to set multipart spot details for recommendations because spot {spotImport2.spot_no} does not exist"
                                        )
                                    );
                            }

                            recommendation.SalesArea = recommendation.GroupCode = currentSalesArea.Name;

                            if (campaignsByCustomId.TryGetValue((int)spotImport2.camp_no, out CampaignReducedModel campaign))
                            {
                                recommendation.ExternalCampaignNumber = campaign.ExternalId;
                            }

                            if (demographicsById.TryGetValue(spotImport2.demo_no, out Demographic demographic))
                            {
                                recommendation.Demographic = demographic.ExternalRef;
                            }

                            scheduleIndexKey = GetScheduleIndexKey(recommendation.SalesArea, recommendation.StartDateTime.Date);

                            string breakNotSetReason = "Unknown reason";

                            if (spotImport2 == null)
                            {
                                breakNotSetReason = $"Spot not found in {Path.GetFileName(mainSpotFile)}";
                            }
                            else
                            {
                                if (!scheduleBreaksBySalesAreaAndDate.TryGetValue(scheduleIndexKey, out var scheduleBreakCache))
                                {
                                    breakNotSetReason = string.Format("Schedule document not found (SalesArea={0}, StartDateTime={1})", recommendation.SalesArea, recommendation.StartDateTime.ToString());
                                }
                                else
                                {
                                    recommendation.ExternalBreakNo = !scheduleBreakCache.TryGetValue(spotImport2.break_no, out var @break)
                                        ? recommendation.ExternalBreakNo
                                        : @break.ExternalBreakRef;

                                    // Set reason why ExternalBreakNo isn't set
                                    if (String.IsNullOrEmpty(recommendation.ExternalBreakNo))
                                    {
                                        if (!(scheduleBreakCache?.Any() ?? false))
                                        {
                                            breakNotSetReason = $"Schedule document exists but it contains no breakss (BreakNo={spotImport2.break_no}, Breaks=0)";
                                        }
                                        else if (@break == null)
                                        {
                                            breakNotSetReason = $"Schedule document exists but the break was not found (BreakNo={spotImport2.break_no}, Breaks={scheduleBreakCache.Count})";
                                        }
                                        else    // Break found but ExternalBreakRef is not set
                                        {
                                            breakNotSetReason = $"Schedule document exists and the break was found but the ExternalBreakRef is not set (BreakNo={spotImport2.break_no}, Breaks={scheduleBreakCache.Count})";
                                        }
                                    }
                                }

                                recommendation.ClientPicked = (spotImport2.client_picked.ToUpper() == "Y");
                            }

                            if (String.IsNullOrEmpty(recommendation.ExternalBreakNo))
                            {
                                _audit.Insert(
                                    AuditEventFactory.CreateAuditEventForWarningMessage(
                                        0,
                                        0,
                                        $"Unable to determine External Break No for recommendation for spot no {spotImport2.spot_no}: {breakNotSetReason}"
                                        )
                                    );
                            }

                            output.Recommendations.Add(recommendation);
                        }
                        catch (Exception exception)
                        {
                            // Log exception, continue
                            _audit.Insert(AuditEventFactory.CreateAuditEventForException(0, 0, string.Format("Error generating cancellation recommendation for spot no {0}", spotImport2.spot_no), exception));
                        }
                    }
                }
            }

            _audit.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0,
                                                                                  0, $"Processed output file {spotFile}"));

            _audit.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0,
                                                                                  0, $"Processed output file {mainSpotFile}"));

            return(output);
        }
Пример #11
0
 public CSVCampaignsReqmImportRepository(CSVImportSettings importSettings)
 {
     _settings = importSettings;
 }
 public CSVScenarioCampaignResultsImportRepository(CSVImportSettings importSettings)
 {
     _settings = importSettings;
 }
Пример #13
0
 public CSVKPIImportRepository(CSVImportSettings settings)
 {
     _settings = settings;
 }
        public ScenarioCampaignFailureOutput ProcessFile(Guid scenarioId, string folder)
        {
            string filename = FileHelpers.GetPathToFileIfExists(folder, FileName);
            var    result   = new ScenarioCampaignFailureOutput
            {
                ScenarioId = scenarioId,
                Data       = new List <ScenarioCampaignFailure>()
            };

            if (String.IsNullOrEmpty(filename))
            {
                return(result);
            }

            _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0,
                                                                                                 0, $"Processing output file {filename}"));

            var importSettings = CSVImportSettings.GetImportSettings(filename, typeof(ScenarioCampaignFailureHeaderMap), typeof(ScenarioCampaignFailureIndexMap));

            using var scenarioCampaignFailureImportRepository = new CSVScenarioCampaignFailureImportRepository(importSettings);

            var fileData = scenarioCampaignFailureImportRepository.GetAll();

            if (fileData is null || !fileData.Any())
            {
                _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForException(0, 0,
                                                                                            $"Error generating Scenario Campaign Failure, data not found", new ObjectNotFoundException()));

                return(result);
            }

            var salesAreas = _dataSnapshot.AllSalesAreas.Value.ToDictionary(s => s.CustomId);
            var campaigns  = _dataSnapshot.AllCampaigns.Value.ToDictionary(c => c.ExternalId);

            foreach (var data in fileData)
            {
                try
                {
                    _ = salesAreas.TryGetValue(data.SalesAreaNumber, out var salesArea);
                    _ = campaigns.TryGetValue(data.CampaignExternalId, out var campaign);
                    var salesAreaName      = salesArea?.Name ?? "Unknown";
                    var salesAreaGroupName = campaign?.SalesAreaCampaignTarget
                                             .Where(sact => !(sact.SalesAreaGroup is null))
                                             .Select(sact => sact.SalesAreaGroup)
                                             .FirstOrDefault(sag =>
                                                             sag.SalesAreas.Any(sa => sa.Equals(salesAreaName, StringComparison.OrdinalIgnoreCase)))
                                             ?.GroupName;

                    var scenarioCampaignFailure = new ScenarioCampaignFailure
                    {
                        ScenarioId                = scenarioId,
                        ExternalCampaignId        = data.CampaignExternalId,
                        SalesArea                 = salesAreaName,
                        SalesAreaGroup            = salesAreaGroupName,
                        Length                    = Duration.FromSeconds(data.SpotLength),
                        MultipartNo               = data.MultipartNumber,
                        StrikeWeightStartDate     = DateHelper.GetDate(data.StrikeWeightStartDate.ToString(), "yyyyMMdd"),
                        StrikeWeightEndDate       = DateHelper.GetDate(data.StrikeWeightEndDate.ToString(), "yyyyMMdd"),
                        DayPartStartTime          = ToTimeSpan(data.DayPartStartTime.ToString().PadLeft(6, '0')),
                        DayPartEndTime            = ToTimeSpan(data.DayPartEndTime.ToString().PadLeft(6, '0')),
                        DayPartDays               = data.DayPartDays,
                        FailureType               = data.FailureType,
                        FailureCount              = data.NumberOfFailures,
                        PassesEncounteringFailure = DecryptPassIds(data.PassesEncounteringFailure),
                    };

                    result.Data.Add(scenarioCampaignFailure);
                }
                catch (ObjectNotFoundException exception)
                {
                    _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForException(0, 0,
                                                                                                $"Error generating Scenario Campaign Failure for campaign id {data.CampaignExternalId}", exception));
                }
            }

            _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0,
                                                                                                 0, $"Processed output file {filename}"));

            return(result);
        }
Пример #15
0
 public CSVScenarioCampaignFailureImportRepository(CSVImportSettings importSettings)
 {
     _settings = importSettings;
 }
        private ProcessBreakEfficiencyOutput ProcessFileInternal(Guid scenarioId, string folder)
        {
            var    output = new ProcessBreakEfficiencyOutput();
            string breakEfficiencyFile = FileHelpers.GetPathToFileIfExists(folder, FileName);

            if (String.IsNullOrEmpty(breakEfficiencyFile))
            {
                return(output);
            }

            _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0,
                                                                                                 0, $"Processing output file {breakEfficiencyFile}"));

            var demographicsById  = Demographic.IndexListById(_dataSnapshot.AllDemographics.Value);
            var salesAreasIndexed = SalesArea.IndexListByCustomId(_dataSnapshot.AllSalesAreas.Value);

            // Create memory cache of Schedule documents
            var      schedules           = new List <ScheduleIndexed <Break, int> >();
            string   lastScheduleDateKey = "";
            int      count           = 0;
            int      countUpdated    = 0;
            int      countNotUpdated = 0;
            DateTime nextYield       = DateTime.MinValue;

            // Process CSV in order of sales area + schedule date + schedule
            // time, allows us to keep as few Break/Schedule documents in memory
            var importSettings = CSVImportSettings.GetImportSettings(breakEfficiencyFile, typeof(BreakEfficiencyHeaderMap), typeof(BreakEfficiencyIndexMap));

            IBreakEfficiencyImportRepository breakEfficiencyImportRepository = new CSVBreakEfficiencyImportRepository(importSettings);
            IScheduleRepository scheduleRepository = null;

            var lazyScopeCreator = new Func <Lazy <IRepositoryScope> >(() => new Lazy <IRepositoryScope>(() => _repositoryFactory.BeginRepositoryScope()));
            var lazyScope        = lazyScopeCreator();

            try
            {
                foreach (BreakEfficiencyImport breakEfficiencyImport in breakEfficiencyImportRepository.GetAll()
                         .OrderBy(be => be.sare_no).ThenBy(be => be.brek_sched_date).ThenBy(be => be.brek_nom_time))
                {
                    count++;
                    bool   breakUpdated    = false;
                    string scheduleDateKey = string.Format("{0}{1}{2}", breakEfficiencyImport.sare_no, (Char)9,
                                                           breakEfficiencyImport.brek_sched_date);

                    try
                    {
                        // Save changes if different sales area + schedule date
                        if (!String.IsNullOrEmpty(lastScheduleDateKey) && scheduleDateKey != lastScheduleDateKey) // No longer need previous Break/Schedule docs
                        {
                            if (scheduleRepository != null)
                            {
                                schedules.ForEach(scheduleIndexed => scheduleRepository.Add(scheduleIndexed.Schedule));
                                scheduleRepository.SaveChanges();
                            }

                            scheduleRepository = null;
                            lazyScope.Value.Dispose();
                            lazyScope = lazyScopeCreator();

                            // Clear data that we no longer need
                            schedules.Clear();
                        }

                        if (scheduleRepository == null)
                        {
                            scheduleRepository = lazyScope.Value.CreateRepository <IScheduleRepository>();
                        }

                        SalesArea salesArea = salesAreasIndexed.ContainsKey(breakEfficiencyImport.sare_no)
                            ? salesAreasIndexed[breakEfficiencyImport.sare_no]
                            : null;

                        // Load breaks if necessary
                        LoadBreaks(salesArea, breakEfficiencyImport, schedules, scheduleRepository);

                        // Update break efficiency
                        if (UpdateBreakEfficiency(salesArea, breakEfficiencyImport, schedules,
                                                  demographicsById))
                        {
                            breakUpdated = true;
                        }
                    }
                    catch (System.Exception exception)
                    {
                        // Log exception, continue
                        _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForException(0, 0,
                                                                                                    string.Format("Error processing break efficiency for break no {0} (Record {1})",
                                                                                                                  breakEfficiencyImport.break_no, count), exception));
                    }
                    finally
                    {
                        lastScheduleDateKey = scheduleDateKey;
                        if (breakUpdated)
                        {
                            countUpdated++;
                        }
                        else
                        {
                            countNotUpdated++;
                        }

                        DoYield(false, ref nextYield);
                    }
                }

                // Save changed
                if (scheduleRepository != null)
                {
                    schedules.ForEach(scheduleIndexed => scheduleRepository.Add(scheduleIndexed.Schedule));
                    scheduleRepository.SaveChanges();
                }
            }
            finally
            {
                if (scheduleRepository != null)
                {
                    lazyScope.Value.Dispose();
                }
            }

            if (countUpdated > 0 && countNotUpdated == 0)   // Success, log info event
            {
                _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0, 0, string.Format("Updated {0} break efficiencies (Not updated={1})", countUpdated, countNotUpdated)));
            }
            else          // Some updates failed, log warning event
            {
                _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForWarningMessage(0, 0, string.Format("Updated {0} break efficiencies (Not updated={1})", countUpdated, countNotUpdated)));
            }

            _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0,
                                                                                                 0, $"Processed output file {breakEfficiencyFile}"));

            return(output);
        }
Пример #17
0
 public CSVSpotImportRepository(CSVImportSettings importSettings)
 {
     _settings = importSettings;
 }
 public CSVBaseRatingsImportRepository(CSVImportSettings importSettings)
 {
     _settings = importSettings;
 }
Пример #19
0
 public CSVLmkKpiImportRepository(CSVImportSettings settings)
 {
     _settings = settings;
 }
Пример #20
0
 public CSVFailureImportRepository(CSVImportSettings settings)
 {
     _settings = settings;
 }
 public CSVBreakEfficiencyImportRepository(CSVImportSettings importSettings)
 {
     _settings = importSettings;
 }