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); } }
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); }