Exemple #1
0
        CollateThreadOutputToSmoothOutput(
            SmoothOutput smoothOutput,
            ConcurrentBag <SmoothBatchOutput> batchAllThreadOutputCollection)
        {
            var spotIdsUsed    = new HashSet <Guid>();
            var spotIdsNotUsed = new HashSet <Guid>();

            foreach (var batch in batchAllThreadOutputCollection)
            {
                batch.UnusedSpotIds.CopyDistinctTo(spotIdsNotUsed);
                batch.UsedSpotIds.CopyDistinctTo(spotIdsUsed);

                foreach (KeyValuePair <int, SmoothOutputForPass> item in batch.OutputByPass)
                {
                    var(smoothPassSequence, passOutput) = (item.Key, item.Value);

                    if (smoothOutput.OutputByPass.ContainsKey(smoothPassSequence))
                    {
                        smoothOutput.OutputByPass[smoothPassSequence].CountSpotsSet += passOutput.CountSpotsSet;
                    }
                    else
                    {
                        var smoothPassOutput = new SmoothOutputForPass(passOutput.PassSequence)
                        {
                            CountSpotsSet = passOutput.CountSpotsSet
                        };

                        smoothOutput.OutputByPass.Add(smoothPassSequence, smoothPassOutput);
                    }
                }

                foreach (KeyValuePair <int, int> item in batch.SpotsByFailureMessage)
                {
                    var(messageId, count) = (item.Key, item.Value);

                    if (smoothOutput.SpotsByFailureMessage.ContainsKey(messageId))
                    {
                        smoothOutput.SpotsByFailureMessage[messageId] += count;
                    }
                    else
                    {
                        smoothOutput.SpotsByFailureMessage.Add(messageId, count);
                    }
                }

                smoothOutput.BookedSpotsUnplacedDueToRestrictions += batch.BookedSpotsUnplacedDueToRestrictions;
                smoothOutput.Breaks          += batch.Breaks;
                smoothOutput.Failures        += batch.Failures;
                smoothOutput.Recommendations += batch.Recommendations;
                smoothOutput.SpotsNotSetDueToExternalCampaignRef += batch.SpotsNotSetDueToExternalCampaignRef;
                smoothOutput.SpotsSetAfterMovingOtherSpots       += batch.SpotsSetAfterMovingOtherSpots;
                smoothOutput.SpotsSet += batch.SpotsSet;
            }

            return(spotIdsUsed, spotIdsNotUsed);
        }
Exemple #2
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);
            }
        }
Exemple #3
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);
        }
Exemple #4
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());
            }
        }