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);
        }
Example #3
0
        /// <summary>
        /// Create a new instance of this class for each programme to smooth.
        /// </summary>
        public SmoothOneProgramme(
            Programme programme,
            IEnumerable <Break> programmeBreaks,
            IReadOnlyCollection <Spot> programmeSpots,
            IReadOnlyDictionary <Guid, SpotInfo> spotInfos,
            Guid runId,
            SalesArea salesArea,
            DateTime processorDateTime,
            ISmoothDiagnostics smoothDiagnostics,
            IImmutableList <RatingsPredictionSchedule> ratingsPredictionSchedules,
            ImmutableSmoothData threadSafeCollections,
            IClashExposureCountService clashExposureCountService,
            SponsorshipRestrictionService sponsorshipRestrictionService,
            IReadOnlyCollection <Product> products,
            IReadOnlyCollection <Clash> clashes,
            IReadOnlyCollection <Programme> allProgrammesForPeriodAndSalesArea,
            Action <string> raiseInfo,
            Action <string, Exception> raiseException)
        {
            RaiseInfo      = raiseInfo;
            RaiseException = raiseException;

            if (programme is null)
            {
                var guruMeditation = new ArgumentNullException(nameof(programme));
                RaiseException("The programme to Smooth is null", guruMeditation);

                throw guruMeditation;
            }

            _smoothProgramme = new SmoothProgramme(salesArea, programme);
            _smoothProgramme.InitialiseSmoothBreaks(programmeBreaks);

            _processorDateTime             = processorDateTime;
            _programmeSpots                = programmeSpots;
            _spotInfos                     = spotInfos;
            _runId                         = runId;
            _threadSafeCollections         = threadSafeCollections;
            _sponsorshipRestrictionService = sponsorshipRestrictionService;
            _smoothConfiguration           = threadSafeCollections.SmoothConfigurationReader;
            _smoothDiagnostics             = smoothDiagnostics;

            _smoothPassBookedExecuter = new SmoothPassBookedExecuter(
                _smoothDiagnostics,
                _smoothResources,
                _smoothProgramme,
                sponsorshipRestrictionService,
                allProgrammesForPeriodAndSalesArea,
                RaiseInfo,
                RaiseException);

            _smoothPassDefaultExecuter = new SmoothPassDefaultExecuter(
                _smoothDiagnostics,
                _smoothResources,
                _smoothProgramme,
                sponsorshipRestrictionService,
                allProgrammesForPeriodAndSalesArea,
                _smoothConfiguration,
                clashExposureCountService,
                _threadSafeCollections.ClashesByExternalRef,
                _threadSafeCollections.ProductsByExternalRef,
                RaiseException);

            _smoothPassUnplacedExecuter = new SmoothPassUnplacedExecuter(
                _smoothDiagnostics,
                _smoothResources,
                _smoothProgramme,
                sponsorshipRestrictionService,
                allProgrammesForPeriodAndSalesArea,
                _smoothConfiguration,
                clashExposureCountService,
                _threadSafeCollections.ClashesByExternalRef,
                RaiseException);

            if (_smoothConfiguration.ClashExceptionCheckEnabled)
            {
                _smoothResources.ClashExceptionChecker = new ClashExceptionChecker(
                    threadSafeCollections.ClashExceptions,
                    products,
                    clashes
                    );
            }

            if (_smoothConfiguration.RestrictionCheckEnabled)
            {
                _smoothResources.RestrictionChecker = new RestrictionChecker(
                    threadSafeCollections.Restrictions,
                    products,
                    clashes,
                    threadSafeCollections.IndexTypes,
                    threadSafeCollections.Universes,
                    ratingsPredictionSchedules);
            }

            _smoothFailuresFactory        = new SmoothFailuresFactory(_smoothConfiguration);
            _smoothRecommendationsFactory = new SmoothRecommendationsFactory(_smoothConfiguration);
        }