示例#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;
        }
示例#2
0
 public SaveSmoothChanges(
     IRepositoryFactory repositoryFactory,
     ImmutableSmoothData immutableLookupCollection,
     Action <string> raiseInfo)
 {
     RaiseInfo = raiseInfo ?? throw new ArgumentNullException(nameof(raiseInfo));
     ImmutableLookupCollection = immutableLookupCollection ?? throw new ArgumentNullException(nameof(immutableLookupCollection));
     RepositoryFactory         = repositoryFactory ?? throw new ArgumentNullException(nameof(repositoryFactory));
 }
示例#3
0
        public void SmoothSalesAreaForDateTimePeriod(
            Guid runId,
            Guid firstScenarioId,
            SalesArea salesArea,
            DateTime processorDateTime,
            DateTimeRange smoothPeriod,
            ImmutableSmoothData threadSafeCollections,
            Action <string> raiseInfo,
            Action <string> raiseWarning,
            Action <string, Exception> raiseException
            )
        {
            SmoothOutput smoothOutput    = null;
            Exception    caughtException = null;

            try
            {
                using (MachineLock.Create($"SmoothEngine.Smooth.{salesArea.Name}", new TimeSpan(1, 0, 0)))
                {
                    var worker = new SmoothWorkerForSalesAreaDuringDateTimePeriod(
                        _repositoryFactory,
                        _smoothLogFileFolder,
                        threadSafeCollections,
                        _clashExposureCountService,
                        raiseInfo,
                        raiseWarning,
                        raiseException
                        );

                    // Define handler for worker notification of day complete
                    worker.OnSmoothBatchComplete += (sender, currentFromDateTime, currentToDateTime, recommendations, smoothFailures) =>
                    {
                        // Notify parent
                        OnSmoothBatchComplete?.Invoke(this, salesArea, currentFromDateTime, currentToDateTime, recommendations, smoothFailures);
                    };

                    smoothOutput = worker.ActuallyStartSmoothing(
                        runId,
                        firstScenarioId,
                        processorDateTime,
                        smoothPeriod,
                        salesArea);
                }
            }
            catch (Exception ex)
            {
                caughtException = ex;
                Debug.WriteLine(ex.ToString());
            }
            finally
            {
                OnSmoothComplete?.Invoke(this, salesArea, caughtException, smoothOutput);
            }
        }
示例#4
0
 public static ImmutableSmoothData LoadSmoothData(
     IModelLoaders modelLoadingService,
     IReadOnlyCollection <string> salesAreaNames,
     DateTimeRange smoothPeriod,
     Action <string> raiseInfo
     ) => ImmutableSmoothData.Create(
     modelLoadingService,
     salesAreaNames,
     smoothPeriod,
     raiseInfo
     );
示例#5
0
        public SmoothWorkerForSalesAreaDuringDateTimePeriod(
            IRepositoryFactory repositoryFactory,
            string smoothLogFileFolder,
            ImmutableSmoothData threadSafeCollections,
            IClashExposureCountService clashExposureCountService,
            Action <string> raiseInfo,
            Action <string> raiseWarning,
            Action <string, Exception> raiseException
            )
        {
            _smoothLogFileFolder       = smoothLogFileFolder;
            _clashExposureCountService = clashExposureCountService;
            _repositoryFactory         = repositoryFactory;
            _smoothConfiguration       = threadSafeCollections.SmoothConfigurationReader;
            _threadSafeCollections     = threadSafeCollections;

            RaiseInfo      = raiseInfo;
            RaiseWarning   = raiseWarning;
            RaiseException = raiseException;
        }
示例#6
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);
        }
示例#7
0
        public void Execute(
            Run run,
            IReadOnlyList <SalesArea> salesAreas,
            Action <string> raiseInfo,
            Action <string> raiseWarning,
            Action <string, Exception> raiseException
            )
        {
            if (raiseInfo is null)
            {
                throw new ArgumentNullException(nameof(raiseInfo));
            }

            if (raiseWarning is null)
            {
                throw new ArgumentNullException(nameof(raiseWarning));
            }

            if (raiseException is null)
            {
                throw new ArgumentNullException(nameof(raiseException));
            }

            if (run is null)
            {
                raiseWarning("Run passed to Smooth as null");
                return;
            }

            if (salesAreas is null)
            {
                raiseWarning($"Sales area list passed to Smooth as null for (RunID={Log(run.Id)})");
                return;
            }

            if (salesAreas.Count == 0)
            {
                raiseWarning($"No sales areas to execute Smooth for (RunID={Log(run.Id)})");
                return;
            }

            IReadOnlyCollection <SmoothStatistics> smoothStatistics = PrepareSmoothStatisticsForSalesAreas(salesAreas);

            var smoothOutputs         = new List <SmoothOutput>();
            int smoothInstancesFailed = 0;

            var smoothStopWatch = new Stopwatch();

            smoothStopWatch.Start();

            try
            {
                DateTimeRange runPeriod = (
                    DateHelper.CreateStartDateTime(run.StartDate, run.StartTime),
                    DateHelper.CreateEndDateTime(run.EndDate, run.EndTime)
                    );

                DateTime processorDateTime = DateTime.UtcNow;

                DateTimeRange smoothPeriod = (
                    DateHelper.CreateStartDateTime(run.SmoothDateRange.Start, run.StartTime),
                    DateHelper.CreateEndDateTime(run.SmoothDateRange.End, run.EndTime)
                    );

                IReadOnlyCollection <string> salesAreaNames = new List <string>(
                    salesAreas.Select(s => s.Name)
                    );

                ImmutableSmoothData threadSafeCollections = LoadSmoothData(
                    _modelLoaders,
                    salesAreaNames,
                    smoothPeriod,
                    raiseInfo);

                raiseInfo($"Smoothing {Log(salesAreas.Count)} sales areas (RunID={Log(run.Id)})");

                LogSmoothConfiguration(
                    run.Id,
                    threadSafeCollections.SmoothConfiguration,
                    _rootFolder,
                    processorDateTime,
                    runPeriod,
                    smoothPeriod,
                    raiseInfo,
                    raiseWarning
                    );

                using (var processSmoothOutputMutex = new Mutex())
                {
                    _smoothEngine.OnSmoothBatchComplete += (
                        sender,
                        salesArea,
                        currentFromDateTime,
                        currentToDateTime,
                        dayRecommendations,
                        smoothFailures) =>
                    {
                        _ = processSmoothOutputMutex.WaitOne();

                        var timestamp = $"{Log(currentFromDateTime)} - {Log(currentToDateTime)} (RunID={Log(run.Id)})";

                        try
                        {
                            raiseInfo($"Processed Smooth output for sales area {salesArea.Name} for {timestamp}");
                        }
                        catch (Exception exception)
                        {
                            raiseException(
                                $"Error processing Smooth output for sales area {salesArea.Name} for {timestamp}",
                                exception
                                );
                        }
                        finally
                        {
                            processSmoothOutputMutex.ReleaseMutex();
                        }
                    };

                    // Define handler for Smooth output.
                    _smoothEngine.OnSmoothComplete += (
                        sender,
                        salesArea,
                        exception,
                        smoothOutput) =>
                    {
                        smoothOutputs.Add(smoothOutput);

                        var salesAreaName = salesArea.Name;

                        if (exception is null)
                        {
                            SmoothStatistics smoothStatistic = smoothStatistics.First(ss => ss.SalesAreaName == salesAreaName);

                            smoothStatistic.TimeEnded = DateTime.UtcNow;
                            TimeSpan elapsedTime = smoothStatistic.TimeEnded - smoothStatistic.TimeStarted;

                            var info = new StringBuilder(320);
                            _ = info
                                .Append("Completed Smooth processing")
                                .Append(": RunID=")
                                .Append(Log(run.Id))
                                .Append(": Sales Area=")
                                .Append(salesAreaName)
                                .Append(": Started=")
                                .Append(Log(smoothStatistic.TimeStarted))
                                .Append(": Ended=")
                                .Append(Log(smoothStatistic.TimeEnded))
                                .Append(": Elapsed=")
                                .Append(Log((long)elapsedTime.TotalSeconds) + "s")
                                .Append(": Breaks=")
                                .Append(smoothOutput.Breaks.ToString())
                                .Append(": Spots set=")
                                .Append(smoothOutput.SpotsSet.ToString())
                                .Append(": Spots not set=")
                                .Append(smoothOutput.SpotsNotSet.ToString())
                                .Append(": Recommendations=")
                                .Append(smoothOutput.Recommendations.ToString())
                            ;

                            raiseInfo(info.ToString());

                            return;
                        }

                        try
                        {
                            _ = processSmoothOutputMutex.WaitOne();

                            smoothInstancesFailed++;

                            raiseException(
                                $"Error executing Smooth for sales area {salesAreaName} (RunID={Log(run.Id)})",
                                exception);

                            if (exception is AggregateException aggEx)
                            {
                                AggregateException flatEx = aggEx.Flatten();

                                raiseException(
                                    $"Guru meditation: Exception during Smooth for sales area {salesAreaName} (RunID={Log(run.Id)})",
                                    flatEx
                                    );
                            }
                            else
                            {
                                raiseException(
                                    $"Guru meditation: Exception during Smooth for sales area {salesAreaName} (RunID={Log(run.Id)})",
                                    exception
                                    );

                                if (exception.InnerException != null)
                                {
                                    raiseException(
                                        $"Guru meditation: Exception during Smooth for sales area {salesAreaName} (RunID={Log(run.Id)})",
                                        exception
                                        );
                                }
                            }
                        }
                        catch (Exception exception2)
                        {
                            raiseException(
                                $"Guru meditation: Exception during outputting Smooth exception for sales area {salesArea.Name} (RunID={Log(run.Id)})",
                                exception2
                                );
                        }
                        finally
                        {
                            processSmoothOutputMutex.ReleaseMutex();
                        }
                    };

                    _ = Parallel.ForEach(salesAreas, salesArea =>
                    {
                        string salesAreaName = salesArea.Name;

                        raiseInfo($"Executing Smooth for sales area {salesAreaName} (RunID={Log(run.Id)})");

                        SmoothStatistics smoothStatistic = smoothStatistics.First(ss => ss.SalesAreaName == salesAreaName);
                        smoothStatistic.TimeStarted      = DateTime.UtcNow;

                        _smoothEngine.SmoothSalesAreaForDateTimePeriod(
                            run.Id,
                            run.Scenarios[0].Id,
                            salesArea,
                            processorDateTime,
                            smoothPeriod,
                            threadSafeCollections,
                            raiseInfo,
                            raiseWarning,
                            raiseException
                            );
                    });
                }
            }
            finally
            {
                smoothStopWatch.Stop();

                var allSmoothOutput = new SmoothOutput()
                {
                    SpotsByFailureMessage = new Dictionary <int, int>()
                };

                foreach (var smoothOutput in smoothOutputs.Where(o => o != null))
                {
                    allSmoothOutput.Append(smoothOutput);

                    SmoothStatistics smoothStatistic      = smoothStatistics.First(ss => ss.SalesAreaName == smoothOutput.SalesAreaName);
                    TimeSpan         elapsedTimeSalesArea = smoothStatistic.TimeEnded - smoothStatistic.TimeStarted;

                    var passDetails = new StringBuilder();

                    foreach (var passSequence in smoothOutput.OutputByPass.Keys)
                    {
                        _ = passDetails
                            .AppendFormat("(Pass={0}, ", smoothOutput.OutputByPass[passSequence].PassSequence.ToString())
                            .AppendFormat("Spots set={0}); ", smoothOutput.OutputByPass[passSequence].CountSpotsSet.ToString());
                    }

                    // Remove the trailing space from the string.
                    passDetails.Length--;

                    // Log final statistics
                    var finalStats = new StringBuilder(800);
                    _ = finalStats
                        .Append("Smooth results")
                        .Append(": RunID=" + run.Id.ToString())
                        .Append(": Sales area=" + smoothOutput.SalesAreaName)
                        .Append(": Started=" + Log(smoothStatistic.TimeStarted))
                        .Append(": Ended=" + Log(smoothStatistic.TimeEnded))
                        .Append(": Elapsed=" + Log((long)elapsedTimeSalesArea.TotalSeconds) + "s")
                        .Append(": Breaks=" + smoothOutput.Breaks.ToString())
                        .Append(": Breaks with reduced Optimizer availability for unplaced spots=" + smoothOutput.BreaksWithReducedOptimizerAvailForUnplacedSpots.ToString())
                        .Append(": Spots set=" + smoothOutput.SpotsSet.ToString())
                        .Append(": Spots set after moving other spots=" + smoothOutput.SpotsSetAfterMovingOtherSpots.ToString())
                        .Append(": Spots not set=" + smoothOutput.SpotsNotSet.ToString())
                        .Append(": Spots not set due to excluded campaign=" + smoothOutput.SpotsNotSetDueToExternalCampaignRef.ToString())
                        .Append(": Booked spots unplaced due to restrictions=" + smoothOutput.BookedSpotsUnplacedDueToRestrictions.ToString())
                        .Append(": Recommendations=" + smoothOutput.Recommendations.ToString())
                        .Append(": Failures=" + smoothOutput.Failures.ToString())
                        .Append(": Passes Results=" + passDetails.ToString())
                    ;

                    raiseInfo(finalStats.ToString());
                }

                LogSmoothFailureMessages(allSmoothOutput.SpotsByFailureMessage, raiseInfo);

                var completedSmoothStats = new StringBuilder(512);
                _ = completedSmoothStats
                    .Append("Completed Smooth")
                    .Append(": RunID=" + Log(run.Id))
                    .Append(": Elapsed=" + Log(smoothStopWatch.ElapsedDuration()) + "s")
                    .Append(": Breaks=" + allSmoothOutput.Breaks.ToString())
                    .Append(": Breaks with reduced Optimizer availability for unplaced spots=" + allSmoothOutput.BreaksWithReducedOptimizerAvailForUnplacedSpots.ToString())
                    .Append(": Spots set=" + allSmoothOutput.SpotsSet.ToString())
                    .Append(": Spots set after moving other spots=" + allSmoothOutput.SpotsSetAfterMovingOtherSpots.ToString())
                    .Append(": Spots not set=" + allSmoothOutput.SpotsNotSet.ToString())
                    .Append(": Spots not set due to excluded campaign=" + allSmoothOutput.SpotsNotSetDueToExternalCampaignRef.ToString())
                    .Append(": Booked spots unplaced due to restrictions=" + allSmoothOutput.BookedSpotsUnplacedDueToRestrictions.ToString())
                    .Append(": Recommendations=" + allSmoothOutput.Recommendations.ToString())
                    .Append(": Failures=" + allSmoothOutput.Failures.ToString())
                    .Append(": Fail Smooth instance=" + smoothInstancesFailed.ToString())
                ;

                raiseInfo(completedSmoothStats.ToString());
            }
        }