Example #1
0
        public SmoothDateRange(
            Guid runId,
            Guid firstScenarioId,
            DateTime processorDateTime,
            SalesArea salesArea,
            ISmoothConfiguration smoothConfiguration,
            ISmoothDiagnostics smoothDiagnostics,
            ImmutableSmoothData threadSafeCollections,
            IClashExposureCountService clashExposureCountService,
            SaveSmoothChanges saveSmoothChanges,
            IRepositoryFactory repositoryFactory,
            Action <string> raiseInfo,
            Action <string> raiseWarning,
            Action <string, Exception> raiseException)
        {
            RaiseInfo                  = raiseInfo;
            RaiseWarning               = raiseWarning;
            RaiseException             = raiseException;
            _threadSafeCollections     = threadSafeCollections;
            _clashExposureCountService = clashExposureCountService;
            _saveSmoothChanges         = saveSmoothChanges;
            _repositoryFactory         = repositoryFactory;
            _runId                        = runId;
            _firstScenarioId              = firstScenarioId;
            _processorDateTime            = processorDateTime;
            _salesArea                    = salesArea;
            _smoothConfiguration          = smoothConfiguration;
            _smoothDiagnostics            = smoothDiagnostics;
            _smoothFailuresFactory        = new SmoothFailuresFactory(_smoothConfiguration);
            _smoothRecommendationsFactory = new SmoothRecommendationsFactory(_smoothConfiguration);

            _smoothPasses = _smoothConfiguration.SortedSmoothPasses;
        }
Example #2
0
        public SmoothOutput ActuallyStartSmoothing(
            Guid runId,
            Guid firstScenarioId,
            DateTime processorDateTime,
            DateTimeRange smoothPeriod,
            SalesArea salesArea)
        {
            ISmoothDiagnostics smoothDiagnostics = new FileSmoothDiagnostics(
                runId,
                salesArea.Name,
                processorDateTime,
                _smoothLogFileFolder,
                _threadSafeCollections.SmoothConfigurationReader);

            var smoothOutput = new SmoothOutput {
                SalesAreaName = salesArea.Name
            };

            foreach (var item in PrepareSmoothFailureMessageCollection(
                         _threadSafeCollections.SmoothFailureMessages
                         ))
            {
                smoothOutput.SpotsByFailureMessage.Add(item);
            }

            IImmutableList <SmoothPass> smoothPasses = _smoothConfiguration.SortedSmoothPasses;

            foreach (var item in PrepareSmoothOutputWithSmoothPasses(smoothPasses))
            {
                smoothOutput.OutputByPass.Add(item);
            }

            var(fromDateTime, toDateTime) = smoothPeriod;
            if (toDateTime.TimeOfDay.Ticks == 0)
            {
                // No time part, include whole day
                toDateTime = toDateTime.AddDays(1).AddTicks(-1);
            }

            var smoothFailuresFactory          = new SmoothFailuresFactory(_smoothConfiguration);
            var smoothRecommendationsFactory   = new SmoothRecommendationsFactory(_smoothConfiguration);
            var batchAllThreadOutputCollection = new ConcurrentBag <SmoothBatchOutput>();

            var _saveSmoothChanges = new SaveSmoothChanges(
                _repositoryFactory,
                _threadSafeCollections,
                RaiseInfo);

            var smoothDateRange = new SmoothDateRange(
                runId,
                firstScenarioId,
                processorDateTime,
                salesArea,
                _smoothConfiguration,
                smoothDiagnostics,
                _threadSafeCollections,
                _clashExposureCountService,
                _saveSmoothChanges,
                _repositoryFactory,
                RaiseInfo,
                RaiseWarning,
                RaiseException);

            _ = Parallel.ForEach(
                DateHelper.SplitUTCDateRange((fromDateTime, toDateTime), 7),
                _weekBatchesToProcessInParallel,
                dateRangeToSmooth =>
            {
                var result = smoothDateRange.Execute(dateRangeToSmooth);

                OnSmoothBatchComplete?.Invoke(
                    this,
                    dateRangeToSmooth.Start,
                    dateRangeToSmooth.End,
                    result.recommendations,
                    result.smoothFailures
                    );

                batchAllThreadOutputCollection.Add(result.smoothBatchOutput);
            });

            var(spotIdsUsed, spotIdsNotUsed) = CollateThreadOutputToSmoothOutput(
                smoothOutput,
                batchAllThreadOutputCollection
                );

            _saveSmoothChanges.SaveUnplacedSpots(
                smoothOutput,
                spotIdsNotUsed,
                spotIdsUsed,
                smoothDiagnostics
                );

            return(smoothOutput);
        }