private void PreGenerateReport(IReadOnlyCollection <Recommendation> data, string fileName, Guid scenarioId) { _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0, 0, $"Start recommendations report generation. ScenarioId: {scenarioId} Count: {data.Count}")); try { using (var reportStream = _recommendationsReportCreator.GenerateReport(data)) { _ = _cloudStorage.Upload(new S3UploadComment { BucketName = _awsSettings.S3Bucket, DestinationFilePath = fileName, FileStream = reportStream }); } } catch (Exception e) { _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForException(0, 0, $"Error while generating report for scenario recommendations. ScenarioId: {scenarioId}", e)); throw; } _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0, 0, $"End recommendations report generation. ScenarioId: {scenarioId}")); }
public void ProcessData(ScenarioCampaignFailureOutput data, Run run, Scenario scenario) { if (!_saveData) { PreGenerateReport(data.Data, run, scenario); return; } try { _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0, 0, $"Start insert of campaign failures. ScenarioId: {data.ScenarioId} Count: {data.Data.Count}")); using (var internalScope = _repositoryFactory.BeginRepositoryScope()) { var scenarioCampaignFailureRepository = internalScope.CreateRepository <IScenarioCampaignFailureRepository>(); scenarioCampaignFailureRepository.AddRange(data.Data, false); scenarioCampaignFailureRepository.SaveChanges(); } _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0, 0, $"Complete insert of campaign failures. ScenarioId: {data.ScenarioId} Count: {data.Data.Count}")); } catch (Exception e) { _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForException(0, 0, $"Error while processing scenario campaign failures. ScenarioId: {scenario.Id}", e)); throw; } PreGenerateReport(data.Data, run, scenario); }
public void ProcessScenarioCampaignMetrics(Guid runId, Guid scenarioId, IEnumerable <Recommendation> scenarioRecommendations) { try { var scenarioCampaignMetrics = new ScenarioCampaignMetric { Id = scenarioId, Metrics = GenerateCampaignKPIs(runId, scenarioId, scenarioRecommendations) }; _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0, 0, $"Start insert of scenario campaign metrics. ScenarioId: {scenarioId} Count: {scenarioCampaignMetrics.Metrics.Count}")); using (var innerScope = _repositoryFactory.BeginRepositoryScope()) { var scenarioCampaignMetricRepository = innerScope.CreateRepository <IScenarioCampaignMetricRepository>(); scenarioCampaignMetricRepository.AddOrUpdate(scenarioCampaignMetrics); scenarioCampaignMetricRepository.SaveChanges(); } _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0, 0, $"End insert of scenario campaign metrics. ScenarioId: {scenarioId} Count: {scenarioCampaignMetrics.Metrics.Count}")); } catch (Exception e) { _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForException(0, 0, $"Error while processing scenario campaign metrics. ScenarioId: {scenarioId}", e)); } }
/// <summary> /// It is called dynamically. /// </summary> public async Task Execute(CancellationToken cancellationToken, AutoBook autoBook) { try { try { _autoBooks.Create(autoBook); _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0, 0, $"Creating AutoBook instance (AutoBookID={autoBook.Id}): Waiting for Idle notification")); } catch (Exception ex) { _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForException(0, 0, $"Error creating AutoBook (AutoBookID={autoBook.Id})", ex)); autoBook.Status = AutoBookStatuses.Fatal_Error; _autoBookRepository.Update(autoBook); } } finally { foreach (var dbContext in _tenantDbContexts) { try { await dbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false); } catch //ignore exception { } } } }
private void PreGenerateReport(List <ScenarioCampaignLevelResultItem> data, Run run, Scenario scenario) { _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0, 0, $"Start scenario campaign level results report generation. ScenarioId: {scenario.Id} Count: {data.Count}")); try { _campaignResultReportCreator.EnablePerformanceKPIColumns(_includeScenarioPerformanceMeasurementKpIs); _campaignResultReportCreator.EnableCampaignLevel(!_saveData); string fileName = ReportFileNameHelper.ScenarioCampaignResult(scenario, run.ExecuteStartedDateTime.Value); using (var reportStream = _campaignResultReportCreator.GenerateReport(() => ScenarioCampaignResultReportHelper.MapToExtendedResults(data, scenario.CampaignPassPriorities, _mapper))) { _ = _cloudStorage.Upload(new S3UploadComment { BucketName = _awsSettings.S3Bucket, DestinationFilePath = fileName, FileStream = reportStream }); } } catch (Exception e) { _auditEventRepository.Insert( AuditEventFactory.CreateAuditEventForException( 0, 0, $"Error while generating report for scenario campaign level results. ScenarioId: {scenario.Id}", e ) ); throw; } _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0, 0, $"End scenario campaign level results report generation. ScenarioId: {scenario.Id}")); }
public void ProcessData(SpotsReqmOutput data, Run run, Scenario scenario) { try { _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0, 0, $"Start insert of recommendations. ScenarioId: {data.ScenarioId} Count: {data.Recommendations.Count}")); using (var innerScope = _repositoryFactory.BeginRepositoryScope()) { var recommendationBatchRepository = innerScope.CreateRepository <IRecommendationRepository>(); recommendationBatchRepository.Insert(data.Recommendations, false); recommendationBatchRepository.SaveChanges(); } _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0, 0, $"End insert of recommendations. ScenarioId: {data.ScenarioId}")); } catch (Exception e) { _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForException(0, 0, $"Error while processing scenario recommendations. ScenarioId: {scenario.Id}", e)); throw; } // do not generate report for scenario with smooth if (run.Smooth) { return; } var fileName = ReportFileNameHelper.RecommendationResult(run, scenario); PreGenerateReport(data.Recommendations, fileName, scenario.Id); }
public ProcessBreakEfficiencyOutput ProcessFile(Guid scenarioId, string folder) { var output = new ProcessBreakEfficiencyOutput(); var run = _dataSnapshot.Run.Value; var efficiencySettings = _efficiencySettingsRepository.GetDefault(); if (!run.Manual || run.Manual && efficiencySettings.PersistEfficiency == PersistEfficiency.AllRun) { if (scenarioId == run.Scenarios.OrderBy(c => c.Id).First().Id) { try { output = ProcessFileInternal(scenarioId, folder); } catch (Exception exception) { _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForException(0, 0, $"Error processing Break Efficiency", exception)); } } else { _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForWarningMessage(0, 0, $"Ignoring break efficiency because it should only be processed once for the run")); } } return(output); }
/// <summary> /// Executes systems task /// </summary> /// <param name="taskId"></param> /// <returns></returns> public List<SystemTaskResult> ExecuteTask(string taskId) { var allSystemTaskResults = new List<SystemTaskResult>(); _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0, 0, String.Format("Executing system task {0}", taskId))); try { // Get task to run var systemTask = GetSystemTasks().Where(mt => mt.Id == taskId).First(); var systemTaskResults = systemTask.Execute(); allSystemTaskResults.AddRange(systemTaskResults); // Log results foreach (var systemTaskResult in systemTaskResults) { _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0, 0, String.Format("System task result for {0}: {1}: {2}", taskId, systemTaskResult.ResultType, systemTaskResult.Message))); } _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0, 0, String.Format("Executed system task {0}", taskId))); } catch (System.Exception exception) { _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForException(0, 0, String.Format("Error executing system task {0}", taskId), exception)); throw; } return allSystemTaskResults; }
public IEnumerable <ConversionEfficiency> Process(string pathToFile) { if (!File.Exists(pathToFile)) { _audit?.Insert(AuditEventFactory.CreateAuditEventForWarningMessage(0, 0, "File was not found.")); return(Enumerable.Empty <ConversionEfficiency>()); } _audit?.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0, 0, $"Processing output file {pathToFile}")); try { var importSettings = CSVImportSettings.GetImportSettings(pathToFile, typeof(ConversionEfficiencyHeaderMap), typeof(ConversionEfficiencyIndexMap)); var conversionEfficiency = new CSVConversionEfficiencyImportRepository(importSettings); var data = conversionEfficiency.GetAll(); var output = _mapper.Map <IEnumerable <ConversionEfficiency> >(data); _audit?.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0, 0, $"Processed output file {pathToFile}")); return(output); } catch (Exception exception) { _audit?.Insert(AuditEventFactory.CreateAuditEventForException(0, 0, $"Error processing file {pathToFile}", exception)); return(Enumerable.Empty <ConversionEfficiency>()); } }
public override void Log(ExceptionLoggerContext context) { if (context.Exception == null) { return; } _logger.LogError(context.Exception, "application exception"); try { _auditEventRepository.Insert( AuditEventFactory.CreateAuditEventForException( tenantId: 0, userId: 0, message: GetAuditInfo(context), exception: context.Exception)); } catch (Exception ex) { _logger.LogError(ex, "auditevent insert exception"); // hope unreachable code }; base.Log(context); }
public async Task Execute(Guid runId) { var runCompleted = false; try { runCompleted = await WaitForRunCompletedAsync(runId).ConfigureAwait(false); } catch (Exception ex) { _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForException(0, 0, $"Completed run notification error (RunID={runId})", ex)); } finally { if (runCompleted) { _runManager.CreateNotificationForCompletedRun(_run); _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0, 0, $"Completed run notification has sent (RunID={runId})")); } else { _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForWarningMessage(0, 0, $"Completed run notification warning, Run not found (RunID={runId})")); } } }
/// <inheritdoc /> public List <KPI> CalculateKPIs(HashSet <string> kpiNamesToCalculate, Guid rawRunId, Guid rawScenarioId) { if (kpiNamesToCalculate is null) { throw new ArgumentNullException(nameof(kpiNamesToCalculate)); } var runId = rawRunId.ToString(); var scenarioId = rawScenarioId.ToString(); _audit?.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0, 0, $"Executing KPI processing (RunID={runId}, ScenarioID={scenarioId}) " + $"KPIs to calculate: [{string.Join(", ", kpiNamesToCalculate)}]")); var kpis = new List <KPI>(); foreach (var kpiName in kpiNamesToCalculate) { _audit?.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0, 0, $"Executing KPI calculation (KPI={kpiName}, RunID={runId}, ScenarioID={scenarioId})")); try { var kpiResult = _kpiResolver.Resolve(kpiName); if (kpiResult is null || !kpiResult.Any()) { _audit?.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0, 0, $"No result of {kpiName} KPI calculation (RunID={runId}, ScenarioID={scenarioId})")); continue; } kpis.AddRange(kpiResult); } catch (DependencyResolutionException exception) { _audit?.Insert(AuditEventFactory.CreateAuditEventForException(0, 0, $"Error resolving calculator for {kpiName} KPI for Scenario {scenarioId} of Run {runId}", exception)); } catch (Exception exception) { _audit?.Insert(AuditEventFactory.CreateAuditEventForException(0, 0, $"Error calculating {kpiName} KPI for Scenario {scenarioId} of Run {runId}", exception)); } _audit?.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0, 0, $"Executed KPI calculation (KPI={kpiName}, RunID={runId}, ScenarioID={scenarioId})")); } _audit?.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0, 0, $"Executed KPI processing (RunID={runId}, ScenarioID={scenarioId})")); return(kpis); }
private void PreGenerateReport(IReadOnlyCollection <ScenarioCampaignFailure> data, Run run, Scenario scenario) { _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0, 0, $"Start scenario campaign failures report generation. ScenarioId: {scenario.Id} Count: {data.Count}")); try { var reportService = new ScenarioCampaignFailuresReportCreator(); var reportData = new ScenarioCampaignFailuresDataSnapshot(); var fileName = ScenarioCampaignFailuresReportCreator.GetFilePath(scenario.Name, run.ExecuteStartedDateTime.Value, scenario.Id); var failureTypes = data.Select(c => c.FailureType) .Distinct() .ToList(); DayOfWeek tenantStartDayOfWeek; using (var internalScope = _repositoryFactory.BeginRepositoryScope()) { reportData.Campaigns = internalScope.CreateRepository <ICampaignRepository>().GetAllFlat(); reportData.FaultTypes = internalScope.CreateRepository <IFunctionalAreaRepository>().FindFaultTypes(failureTypes); tenantStartDayOfWeek = internalScope.CreateRepository <ITenantSettingsRepository>().GetStartDayOfWeek(); } using (var reportStream = reportService.GenerateReport(data, reportData, tenantStartDayOfWeek)) { _ = _cloudStorage.Upload(new S3UploadComment { BucketName = _awsSettings.S3Bucket, DestinationFilePath = fileName, FileStream = reportStream }); } } catch (Exception e) { _auditEventRepository.Insert( AuditEventFactory.CreateAuditEventForException( 0, 0, $"Error while generating report for scenario campaign failures. ScenarioId: {scenario.Id}", e ) ); throw; } _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0, 0, $"End scenario campaign failures report generation. ScenarioId: {scenario.Id}")); }
public void Handle(Run run, Guid scenarioId) { bool cleanup = true; try { // Check for scenario result, shouldn't exist ScenarioResult scenarioResult = _scenarioResultRepository.Find(scenarioId); if (scenarioResult is null) { scenarioResult = new ScenarioResult { Id = scenarioId, TimeCompleted = DateTime.UtcNow }; } // Download output files from AWS DownloadOutputFiles(run.Id, scenarioId, GetScenarioLocalFolder(scenarioId)); // Process output files ProcessOutputFiles(run.Id, scenarioId, scenarioResult); cleanup = false; } catch (Exception exception) { //adding the audit event to clear the warning of 'exception declared but never used' and preserves stack trace with the 'throw' _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForException(0, 0, String.Format("Error handling output file for scenario ID: {0}", scenarioId), exception)); throw; } finally { // Delete all output data if it failed if (cleanup) { try { DeleteOutputData(scenarioId); } catch { }; } } }
public TaskResult Execute(TaskInstance taskInstance) { var taskResult = new TaskResult(); string autoBookId = String.Empty; try { StartActiveNotifier(taskInstance.Id); using var scope = _repositoryFactory.BeginRepositoryScope(); TaskUtilities.UpdateTaskInstanceStatus(taskInstance.Id, _repositoryFactory, TaskInstanceStatues.InProgress, null); // Read parameters autoBookId = taskInstance.Parameters["AutoBookId"].ToString(); var autoBookRepository = scope.CreateRepository <IAutoBookRepository>(); var autoBook = autoBookRepository.Get(autoBookId); if (autoBook == null) { throw new Exception($"AutoBook {autoBookId} does not exist"); } else { _ = _runManager.NextScheduledScenarioStartRun(autoBook); } TaskUtilities.UpdateTaskInstanceStatus(taskInstance.Id, _repositoryFactory, TaskInstanceStatues.CompletedSuccess, DateTime.UtcNow); taskResult.Status = TaskInstanceStatues.CompletedSuccess; } catch (Exception exception) { _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForException(0, 0, String.Format("Error executing task for starting next scenario (TaskInstanceID={0}, AutoBookID={1})", taskInstance.Id, autoBookId), exception)); TaskUtilities.UpdateTaskInstanceStatus(taskInstance.Id, _repositoryFactory, TaskInstanceStatues.CompletedError, DateTime.UtcNow); taskResult.Status = TaskInstanceStatues.CompletedError; taskResult.Exception = exception; } finally { StopActiveNotifier(); } return(taskResult); }
public TaskResult Execute(TaskInstance taskInstance) { var taskResult = new TaskResult(); try { StartActiveNotifier(taskInstance.Id); TaskUtilities.UpdateTaskInstanceStatus(taskInstance.Id, _repositoryFactory, TaskInstanceStatues.InProgress, null); using (var scope = _repositoryFactory.BeginRepositoryScope()) { // Test repository, confirms that we're able to pick up config settings var campaignRepository = scope.CreateRepository <ICampaignRepository>(); var campaigns = campaignRepository.GetAll(); } // Do work int seconds = Convert.ToInt32(taskInstance.Parameters["Seconds"].ToString()); DoWork(seconds); TaskUtilities.UpdateTaskInstanceStatus(taskInstance.Id, _repositoryFactory, TaskInstanceStatues.CompletedSuccess, DateTime.UtcNow); taskResult.Status = TaskInstanceStatues.CompletedSuccess; } catch (Exception exception) { _auditEventRepository.Insert( AuditEventFactory.CreateAuditEventForException(0, 0, $"Error executing task for test (TaskInstanceID={taskInstance.Id.ToString()})", exception) ); TaskUtilities.UpdateTaskInstanceStatus(taskInstance.Id, _repositoryFactory, TaskInstanceStatues.CompletedError, DateTime.UtcNow); taskResult.Status = TaskInstanceStatues.CompletedError; taskResult.Exception = exception; } finally { StopActiveNotifier(); } return(taskResult); }
/// <summary> /// Gets collection of valid autopilot rules for all needed rule categories /// </summary> /// <param name="flexibilityLevelId"></param> /// <returns>ReadOnly collection of autopilot rules</returns> private IReadOnlyDictionary <int, List <AutopilotRule> > GetAutopilotRules(int flexibilityLevelId) { var autopilotRules = _autopilotRuleRepository.GetByFlexibilityLevelId(flexibilityLevelId).ToList(); string exceptionMessage = null; if (!autopilotRules.Any()) { exceptionMessage = $"Autopilot rules missing for '{flexibilityLevelId}' flexibility level"; } var rulesByType = autopilotRules .Where(ar => ar.Enabled || ar.RuleTypeId == (int)RuleCategory.SlottingLimits) .GroupBy(ar => ar.RuleTypeId) .ToDictionary(g => g.Key, g => g.ToList()); if (!rulesByType.ContainsKey((int)RuleCategory.SlottingLimits) || rulesByType[(int)RuleCategory.SlottingLimits].Count != 1) { exceptionMessage = "Missing default SlottingLimits Autopilot rule"; } if (exceptionMessage != null) { var exception = new Exception("Error while generating autopilot scenarios"); _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForException(0, 0, exceptionMessage, exception)); throw exception; } // default values for all rule categories foreach (var ruleCategory in Enum.GetValues(typeof(RuleCategory)).Cast <RuleCategory>()) { if (!rulesByType.ContainsKey((int)ruleCategory)) { rulesByType[(int)ruleCategory] = new List <AutopilotRule>(); } } return(rulesByType); }
public void ProcessData(ScenarioCampaignResult data, Run run, Scenario scenario) { var scenarioId = data.Id; if (!_saveData) { const string campaignLevelReportFacility = "XGSCRC"; var facility = _facilityRepository.GetByCode(campaignLevelReportFacility); if (facility is null || facility.Enabled == false) { PreGenerateReport(data.Items, run, scenario); } return; } try { _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0, 0, $"Start insert of scenario campaigns. ScenarioId: {scenarioId} Count: {data.Items.Count}")); using (var innerScope = _repositoryFactory.BeginRepositoryScope()) { var scenarioCampaignResultRepository = innerScope.CreateRepository <IScenarioCampaignResultRepository>(); scenarioCampaignResultRepository.AddOrUpdate(data); scenarioCampaignResultRepository.SaveChanges(); } _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0, 0, $"End insert of scenario campaigns. ScenarioId: {scenarioId} Count: {data.Items.Count}")); } catch (Exception e) { _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForException(0, 0, $"Error while processing scenario campaign results. ScenarioId: {scenarioId}", e)); throw; } PreGenerateReport(data.Items, run, scenario); }
/// <summary> /// It is called dynamically. /// </summary> public async Task Execute(CancellationToken cancellationToken, AutoBook autoBook) { try { _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0, 0, $"Deleting AutoBook (AutoBookID={autoBook.Id})")); var autobookDeleted = false; try { _autoBooks.Delete(autoBook); autobookDeleted = true; } catch (Exception ex) { _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForException(0, 0, $"Error deleting AutoBook (AutoBookID={autoBook.Id})", ex)); } if (autobookDeleted) { _auditEventRepository.Insert( AuditEventFactory.CreateAuditEventForInformationMessage(0, 0, "Deleted AutoBook")); } } finally { foreach (var dbContext in _tenantDbContexts) { try { await dbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false); } catch //ignore exception { } } } }
public void ProcessData(Failures data, Run run, Scenario scenario) { try { _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0, 0, $"Failures has been generated. ScenarioId: {data.Id}, Count: {data.Items.Count}")); using (var innerScope = _repositoryFactory.BeginRepositoryScope()) { var failuresRepository = innerScope.CreateRepository <IFailuresRepository>(); failuresRepository.Add(data); failuresRepository.SaveChanges(); } _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0, 0, $"Failures has been saved to the db. ScenarioId: {data.Id}")); } catch (Exception e) { _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForException(0, 0, "Error while processing failures", e)); throw; } }
public TaskResult Execute(TaskInstance taskInstance) { var taskResult = new TaskResult(); Guid runId = Guid.Empty; try { StartActiveNotifier(taskInstance.Id); TaskUtilities.UpdateTaskInstanceStatus(taskInstance.Id, _repositoryFactory, TaskInstanceStatues.InProgress, null); // Read parameters runId = new Guid(taskInstance.Parameters["RunId"].ToString()); var runInstances = _runManager.AllScenariosStartRun(runId); TaskUtilities.UpdateTaskInstanceStatus(taskInstance.Id, _repositoryFactory, TaskInstanceStatues.CompletedSuccess, DateTime.UtcNow); taskResult.Status = TaskInstanceStatues.CompletedSuccess; } catch (Exception exception) { _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0, 0, $"{exception.StackTrace}"));///testing _auditEventRepository.Insert( AuditEventFactory.CreateAuditEventForException(0, 0, $"Error executing task for start run (TaskInstanceID={taskInstance.Id.ToString()}, RunID={runId.ToString()})", exception) ); TaskUtilities.UpdateTaskInstanceStatus(taskInstance.Id, _repositoryFactory, TaskInstanceStatues.CompletedError, DateTime.UtcNow); taskResult.Status = TaskInstanceStatues.CompletedError; taskResult.Exception = exception; } finally { StopActiveNotifier(); } return(taskResult); }
public BaseRatingsOutput ProcessFile(Guid scenarioId, string folder) { string pathToFile = FileHelpers.GetPathToFileIfExists(folder, FileName); var result = new BaseRatingsOutput(); if (String.IsNullOrEmpty(pathToFile)) { _audit.Insert(AuditEventFactory.CreateAuditEventForWarningMessage(0, 0, $"File {pathToFile} was not found.")); return(result); } _audit.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0, 0, $"Processing output file {pathToFile}")); try { var importSettings = CSVImportSettings.GetImportSettings(pathToFile, typeof(BaseRatingsHeaderMap), typeof(BaseRatingsIndexMap)); var baseRatingsImportRepository = new CSVBaseRatingsImportRepository(importSettings); var data = baseRatingsImportRepository.GetAll(); result.Data = _mapper.Map <IEnumerable <BaseRatings> >(data); _audit.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0, 0, $"Processed output file {pathToFile}")); return(result); } catch (Exception exception) { _audit.Insert(AuditEventFactory.CreateAuditEventForException(0, 0, "Error processing base ratings file", exception)); return(result); } }
public void Execute( Run run, RunScenario scenario, IReadOnlyCollection <AutoBookInstanceConfiguration> autoBookInstanceConfigurationsForRun, double autoBookRequiredStorageGB, ConcurrentBag <RunInstance> runInstances, ConcurrentDictionary <Guid, ScenarioStatuses> newScenarioStatuses, ConcurrentDictionary <Guid, bool> scenarioSyncStatuses, bool autoDistributed) { AutoBookDomainObject autoBook = null; IAutoBook autoBookInterface = null; bool runStarted = false; RaiseInfo($"Begin Execute for ScenarioID: { scenario.Id}"); try { AutoBookInstanceConfiguration runAutoBookInstanceConfiguration = null; if (autoDistributed) { RaiseInfo($"AutoDistributed - RunScenarioTask Execute Starting ScenarioID ={ scenario.Id}, RunID ={ run.Id}"); //create instance for scenario RunInstance runInstance = _runInstanceCreator.Create(run.Id, scenario.Id); RaiseInfo($"AutoDistributed - about to enter: {nameof(runInstance.UploadInputFilesAndCreateAutoBookRequest)}"); runInstance.UploadInputFilesAndCreateAutoBookRequest(autoBookInstanceConfigurationsForRun, autoBookRequiredStorageGB); RaiseInfo($"AutoDistributed - returned from: {nameof(runInstance.UploadInputFilesAndCreateAutoBookRequest)}"); // Flag run as started runStarted = true; runInstances.Add(runInstance); _ = newScenarioStatuses.TryRemove(scenario.Id, out _); // Don't update scenario status at the end scenarioSyncStatuses[scenario.Id] = false; RaiseInfo($"AutoDistributed - RunScenarioTask Execute Started ScenarioID ={ scenario.Id}, RunID ={ run.Id}"); } else { try { using (MachineLock.Create("xggameplan.AWSAutoBooks.GetFreeAutoBook", new TimeSpan(0, 10, 0))) { foreach (var autoBookInstanceConfiguration in autoBookInstanceConfigurationsForRun) { autoBook = _autoBooks.GetFirstAdequateIdleAutoBook(autoBookInstanceConfiguration, autoBookRequiredStorageGB, true); if (autoBook != null) // Got free AutoBook { RaiseInfo($"Got Free AutoBook: {autoBook.Id} ConfigurationId: {autoBook.InstanceConfigurationId}"); runAutoBookInstanceConfiguration = autoBookInstanceConfiguration; break; } } } } catch (MachineLockTimeoutException) { RaiseInfo($"MachineLockTimeoutException in xggameplan.AWSAutoBooks.GetFreeAutoBook"); } // Get autobook interface autoBookInterface = (autoBook == null) ? null : _autoBooks.GetInterface(autoBook); // Get free AutoBook instance, will be locked so that it can't be used elsewhere if (autoBook != null) // Free AutoBook - start run { RaiseInfo($"Free Autobook - Starting ScenarioID ={ scenario.Id}, AutoBookID ={autoBook?.Id}, RunID ={ run.Id}, Instance Configuration = { runAutoBookInstanceConfiguration.Description }"); // Start run, exception will cause cleanup below RunInstance runInstance = _runInstanceCreator.Create(run.Id, scenario.Id); runInstance.UploadInputFilesStartAutoBookRun(autoBookInterface, autoBook); // Flag run as started runStarted = true; runInstances.Add(runInstance); _ = newScenarioStatuses.TryRemove(scenario.Id, out _); // Don't update scenario status at the end scenarioSyncStatuses[scenario.Id] = false; RaiseInfo($"Started ScenarioID ={ scenario.Id}, AutoBookID ={ autoBook?.Id}, RunID ={ run.Id}, Instance Configuration = { runAutoBookInstanceConfiguration?.Description }"); } else // No free AutoBook, awaiting for provisioning { _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForWarningMessage(0, 0, $"No free AutoBook, awaiting for provisioning, waiting for existing AutoBooks to be Idle (RunID={run.Id}, ScenarioID={scenario.Id})")); // Update scenario so that it can be retried later when an AutoBook becomes idle RunManager.UpdateScenarioStatuses(_repositoryFactory, _auditEventRepository, run.Id, new List <Guid> { scenario.Id }, new List <ScenarioStatuses> { ScenarioStatuses.Scheduled }, new List <DateTime?> { null }); _ = newScenarioStatuses.TryRemove(scenario.Id, out _); // Don't update scenario status at the end scenarioSyncStatuses[scenario.Id] = false; } } } catch (System.Exception exception) { // Log exception but don't throw it. We want to try and start other scenarios _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForException(0, 0, $"Error starting scenario (RunID={run.Id}, ScenarioID={scenario.Id}, AutoBookID={(autoBook == null ? "Unknown" : autoBook.Id)})", exception)); } finally { // If we locked a free AutoBook instance but didn't start the scenario then reset to free, unlocks it. if (!runStarted && autoBook != null) { autoBookInterface.ResetFree(); } } }
/// <summary> /// Processes files. For each file we pass it to a file processor which /// performs some action on it. /// </summary> /// <param name="runId">RunID</param> /// <param name="scenarioId">ScenarioId</param> /// <param name="scenarioResult"></param> private void ProcessOutputFiles(Guid runId, Guid scenarioId, ScenarioResult scenarioResult) { _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForGameplanPipelineStart(0, 0, PipelineEventIDs.STARTED_EXPORTING_DATA_TO_DATABASE, runId, scenarioId, null, null)); _pipelineAuditEventRepository.Add(PipelineEventHelper.CreatePipelineAuditEvent(AuditEventTypes.GamePlanRun, PipelineEventIDs.STARTED_EXPORTING_DATA_TO_DATABASE, runId, scenarioId, null)); string scenarioLocalFolder = GetScenarioLocalFolder(scenarioId); var dataProcessingExceptions = new List <Exception>(); using (var kpiCalculationScope = _kpiCalculationScopeFactory.CreateCalculationScope(runId, scenarioId)) { var kpiCalculationContext = kpiCalculationScope.Resolve <IKPICalculationContext>(); var kpiCalculationManager = kpiCalculationScope.Resolve <IKPICalculationManager>(); foreach (var dataProcessingAction in _dataProcessingActions) { try { dataProcessingAction(kpiCalculationScope, _featureManager, _auditEventRepository, scenarioLocalFolder); } catch (Exception e) { _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForException(0, 0, $"Error processing: {dataProcessingAction.Method.GetGenericArguments().First().Name}. ScenarioId: {scenarioId}", e)); dataProcessingExceptions.Add(e); } } if (_includeScenarioPerformanceMeasurementKPIs) { kpiCalculationContext.SetDefaultKpiDemos(); } _scenarioCampaignMetricsProcessor.ProcessScenarioCampaignMetrics(runId, scenarioId, kpiCalculationContext.Recommendations); scenarioResult.Metrics = kpiCalculationManager.CalculateKPIs(runId, scenarioId); scenarioResult.AnalysisGroupMetrics = kpiCalculationManager.CalculateAnalysisGroupKPIs(runId); _scenarioResultRepository.Add(scenarioResult); _scenarioResultRepository.SaveChanges(); } // Exceptions handling if (dataProcessingExceptions.Count == 1) { _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForGameplanPipelineEnd(0, 0, PipelineEventIDs.FINISHED_EXPORTING_DATA_TO_DATABASE, runId, scenarioId, null, null, dataProcessingExceptions[0].Message, dataProcessingExceptions[0])); throw dataProcessingExceptions[0]; } else if (dataProcessingExceptions.Count > 1) { _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForGameplanPipelineEnd(0, 0, PipelineEventIDs.FINISHED_EXPORTING_DATA_TO_DATABASE, runId, scenarioId, null, null, dataProcessingExceptions[0].Message, dataProcessingExceptions[0])); throw new AggregateException(dataProcessingExceptions); } _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForGameplanPipelineEnd(0, 0, PipelineEventIDs.FINISHED_EXPORTING_DATA_TO_DATABASE, runId, scenarioId, null, "Import has been finished", null, null)); _pipelineAuditEventRepository.Add(PipelineEventHelper.CreatePipelineAuditEvent(AuditEventTypes.GamePlanRun, PipelineEventIDs.FINISHED_EXPORTING_DATA_TO_DATABASE, runId, scenarioId, null)); _pipelineAuditEventRepository.SaveChanges(); }
/// <summary> /// Downloads output files /// </summary> /// <param name="runId"></param> /// <param name="scenarioId"></param> /// <param name="remoteFolder"></param> /// <param name="localFolder"></param> private void DownloadOutputFiles(Guid runId, Guid scenarioId, string localFolder) { // For some reason the output goes to \Output\[ScenarioId]\output string zipFileName = $"{scenarioId}.zip"; string localZipFile = Path.Combine(localFolder + @"\output", zipFileName); try { _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForGameplanPipelineStart(0, 0, PipelineEventIDs.STARTED_DOWNLOADING_ZIP_ARCHIVE_FROM_CLOUD_STORAGE, runId, scenarioId, null, null)); _pipelineAuditEventRepository.Add(PipelineEventHelper.CreatePipelineAuditEvent(AuditEventTypes.GamePlanRun, PipelineEventIDs.STARTED_DOWNLOADING_ZIP_ARCHIVE_FROM_CLOUD_STORAGE, runId, scenarioId, null)); // For some reason the output goes to \Output\[ScenarioId]\output if (!Directory.Exists(Path.GetDirectoryName(localZipFile))) { _ = Directory.CreateDirectory(Path.GetDirectoryName(localZipFile)); } if (File.Exists(localZipFile)) { File.Delete(localZipFile); } var filename = $@"output/{zipFileName}"; _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0, 0, $"Downloading scenario output from cloud provider ( Bucket={_awsSettings.S3Bucket},filename={filename} Local Folder={localFolder})")); _ = _cloudStorage.Download(new S3DownloadComment { BucketName = _awsSettings.S3Bucket, FileName = filename, LocalFileFolder = localFolder }); _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForGameplanPipelineEnd(0, 0, PipelineEventIDs.FINISHED_DOWNLOADING_ZIP_ARCHIVE_FROM_CLOUD_STORAGE, runId, scenarioId, null, null, null, null)); _pipelineAuditEventRepository.Add(PipelineEventHelper.CreatePipelineAuditEvent(AuditEventTypes.GamePlanRun, PipelineEventIDs.FINISHED_DOWNLOADING_ZIP_ARCHIVE_FROM_CLOUD_STORAGE, runId, scenarioId, null)); } catch (Exception exception) { _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForGameplanPipelineEnd(0, 0, PipelineEventIDs.FINISHED_DOWNLOADING_ZIP_ARCHIVE_FROM_CLOUD_STORAGE, runId, scenarioId, null, null, exception.Message, exception)); _pipelineAuditEventRepository.Add(PipelineEventHelper.CreatePipelineAuditEvent(AuditEventTypes.GamePlanRun, PipelineEventIDs.FINISHED_DOWNLOADING_ZIP_ARCHIVE_FROM_CLOUD_STORAGE, runId, scenarioId, exception.Message)); throw; } finally { _pipelineAuditEventRepository.SaveChanges(); } try { // Clear files in destination folder before we unzip ClearFolder(localFolder); ZipConversion.DeserializeFromZip(localZipFile, localFolder); } catch (Exception exception) { _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForException(0, 0, String.Format("Error unzipping {0}", localZipFile), exception)); throw; } }
/// <summary> /// Builds autopilot scenario of the specified type by putting newly generated passes into the correct positions /// </summary> /// <param name="scenarioType"></param> /// <param name="baseScenario"></param> /// <param name="autopilotPasses">Autopilot passes, grouped by pass type</param> /// <returns>Generated scenario with the expanded list of passes</returns> private AutopilotScenarioModel GenerateAutopilotScenarioByType(AutopilotScenarioType scenarioType, AutopilotScenarioEngageModel baseScenario, IReadOnlyDictionary <AutopilotPassType, AutopilotPassModel> autopilotPasses) { var newScenarioModel = (AutopilotScenarioEngageModel)baseScenario.Clone(); newScenarioModel.Id = Guid.Empty; newScenarioModel.Name += $", #{(int)scenarioType + 1} AP type"; newScenarioModel.IsAutopilot = true; var(tightenPassIndex, loosenPassIndex) = baseScenario.GetAutopilotPassIndexes(); loosenPassIndex++; var tightenALotPass = (AutopilotPassModel)autopilotPasses[AutopilotPassType.TightenALot].Clone(); var tightenABitPass = (AutopilotPassModel)autopilotPasses[AutopilotPassType.TightenABit].Clone(); var loosenABitPass = (AutopilotPassModel)autopilotPasses[AutopilotPassType.LoosenABit].Clone(); var loosenALotPass = (AutopilotPassModel)autopilotPasses[AutopilotPassType.LoosenALot].Clone(); switch (scenarioType) { case AutopilotScenarioType.First: newScenarioModel.Passes.Insert(tightenPassIndex, tightenABitPass); break; case AutopilotScenarioType.Second: newScenarioModel.Passes.Insert(loosenPassIndex, loosenABitPass); break; case AutopilotScenarioType.Third: newScenarioModel.Passes.Insert(loosenPassIndex, loosenABitPass); newScenarioModel.Passes.Insert(tightenPassIndex, tightenABitPass); break; case AutopilotScenarioType.Fourth: newScenarioModel.Passes.Insert(tightenPassIndex, tightenABitPass); newScenarioModel.Passes.Insert(tightenPassIndex, tightenALotPass); break; case AutopilotScenarioType.Fifth: newScenarioModel.Passes.Insert(loosenPassIndex, loosenALotPass); newScenarioModel.Passes.Insert(loosenPassIndex, loosenABitPass); break; case AutopilotScenarioType.Sixth: newScenarioModel.Passes.Insert(loosenPassIndex, loosenABitPass); newScenarioModel.Passes.Insert(tightenPassIndex, tightenABitPass); newScenarioModel.Passes.Insert(tightenPassIndex, tightenALotPass); break; case AutopilotScenarioType.Seventh: newScenarioModel.Passes.Insert(loosenPassIndex, loosenALotPass); newScenarioModel.Passes.Insert(loosenPassIndex, loosenABitPass); newScenarioModel.Passes.Insert(tightenPassIndex, tightenABitPass); break; case AutopilotScenarioType.Eighth: newScenarioModel.Passes.Insert(loosenPassIndex, loosenALotPass); newScenarioModel.Passes.Insert(loosenPassIndex, loosenABitPass); newScenarioModel.Passes.Insert(tightenPassIndex, tightenABitPass); newScenarioModel.Passes.Insert(tightenPassIndex, tightenALotPass); break; default: var exception = new Exception("Error while generating autopilot scenario"); _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForException(0, 0, "Attempt to process unknown autopilot scenario type", exception)); throw exception; } var originalPassNames = newScenarioModel.AddUniquePassNames(); newScenarioModel.UpdateCampaignPassPriorities(originalPassNames); return(_mapper.Map <AutopilotScenarioModel>(newScenarioModel)); }
private void LogError(Exception exception, string message) { var auditEvent = AuditEventFactory.CreateAuditEventForException(0, 0, message, exception); _auditEventRepository.Insert(auditEvent); }
public SpotsReqmOutput ProcessFile(Guid scenarioId, string folder) { string spotFile = FileHelpers.GetPathToFileIfExists(folder, FileName); string mainSpotFile = FileHelpers.GetPathToFileIfExists(folder, OutputFileNames.MainSpotTable); var output = new SpotsReqmOutput { ScenarioId = scenarioId }; if (String.IsNullOrEmpty(spotFile) || String.IsNullOrEmpty(mainSpotFile)) { return(output); } _audit.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0, 0, $"Processing output file {spotFile}")); _audit.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0, 0, $"Processing output file {mainSpotFile}")); // Process file DateTime processorDateTime = DateTime.UtcNow; var importSettings = CSVImportSettings.GetImportSettings(mainSpotFile, typeof(SpotHeaderMap), typeof(SpotIndexMap)); // Get main spots if path passed in ISpotImportRepository spotImportRepository = new CSVSpotImportRepository(importSettings); IEnumerable <SpotImport> spotImports = spotImportRepository.GetAll(); importSettings = CSVImportSettings.GetImportSettings(spotFile, typeof(SpotReqmHeaderMap), typeof(SpotReqmIndexMap)); ISpotReqmImportRepository spotReqmImportRepository = new CSVSpotReqmImportRepository(importSettings); var spotReqmImports = spotReqmImportRepository.GetAll(); // Index for lookup performance var campaignsByCustomId = CampaignReducedModel.IndexListByCustomId(_campaignRepository.GetAllFlat()); var spotsByCustomId = SpotHelper.IndexListById(_dataSnapshot.SpotsForRun.Value); var demographicsById = Demographic.IndexListById(_dataSnapshot.AllDemographics.Value); var programmeDictionariesById = ProgrammeDictionary.IndexListById(_dataSnapshot.AllProgrammeDictionaries.Value); IEnumerable <Pass> scenarioPasses = _dataSnapshot.ScenarioPasses.Value; foreach (var currentSalesArea in _dataSnapshot.AllSalesAreas.Value) { foreach (SpotReqmImport spotReqmImport in spotReqmImports.Where(sri => sri.sare_no == currentSalesArea.CustomId)) { output.CountTotalSpots++; try { Recommendation recommendation = _mapper.Map <Recommendation>(Tuple.Create(spotReqmImport, new List <ProgrammeDictionary>(), _dataSnapshot.BreakTypes.Value)); recommendation.ScenarioId = scenarioId; recommendation.ProcessorDateTime = processorDateTime; bool isCancelledSpot = !String.IsNullOrEmpty(spotReqmImport.status) && spotReqmImport.status == "C"; if (isCancelledSpot) { output.CountCancelledSpots++; recommendation.SpotSequenceNumber = default; if (spotsByCustomId.TryGetValue((int)spotReqmImport.spot_no, out var spot)) { recommendation.ExternalSpotRef = spot.ExternalSpotRef; recommendation.Sponsored = spot.Sponsored; recommendation.Preemptable = spot.Preemptable; recommendation.Preemptlevel = spot.Preemptlevel; recommendation.MultipartSpot = spot.MultipartSpot; recommendation.MultipartSpotPosition = spot.MultipartSpotPosition; recommendation.MultipartSpotRef = spot.MultipartSpotRef; } else { _audit.Insert( AuditEventFactory.CreateAuditEventForWarningMessage( 0, 0, $"Unable to set cancelled spot details for recommendations" + $" because spot {spotReqmImport.spot_no} does not exist") ); } } else { recommendation.SpotSequenceNumber = spotReqmImport.spot_no; } Pass pass; if (scenarioPasses != null && scenarioPasses.Any() && (pass = scenarioPasses.FirstOrDefault(p => p.Id == spotReqmImport.abdn_no)) != null) { recommendation.PassName = pass.Name; recommendation.OptimiserPassSequenceNumber = spotReqmImport.pass_sequence_no; } recommendation.CampaignPassPriority = spotReqmImport.campaign_pass_priority; recommendation.RankOfEfficiency = spotReqmImport.rank_of_efficiency; recommendation.RankOfCampaign = spotReqmImport.rank_of_campaign; recommendation.CampaignWeighting = spotReqmImport.campaign_weighting; recommendation.SalesArea = recommendation.GroupCode = currentSalesArea.Name; recommendation.NominalPrice = spotReqmImport.nominal_price; recommendation.ExternalCampaignNumber = campaignsByCustomId.TryGetValue((int)spotReqmImport.camp_no, out var campaign) ? campaign.ExternalId : string.Empty; recommendation.Demographic = demographicsById.TryGetValue(spotReqmImport.demo_no, out var demographic) ? demographic.ExternalRef : string.Empty; if (programmeDictionariesById.TryGetValue((int)spotReqmImport.prog_no, out var programmeDictionary)) { recommendation.ExternalProgrammeReference = programmeDictionary.ExternalReference; recommendation.ProgrammeName = programmeDictionary.ProgrammeName; } else { recommendation.ExternalProgrammeReference = recommendation.ProgrammeName = string.Empty; } recommendation.ClientPicked = spotReqmImport.client_picked.ToUpper() == "Y"; recommendation.ExternalBreakNo = spotReqmImport.brek_external_ref; output.Recommendations.Add(recommendation); } catch (System.Exception exception) { // Log exception, continue _audit.Insert(AuditEventFactory.CreateAuditEventForException(0, 0, string.Format("Error generating recommendation for spot no {0}", spotReqmImport.spot_no), exception)); } } } // TODO: Remove this when it has been confirmed that cancellations appear in Spot Req file // Process cancelled spots if they weren't present in the Spot Req // file above if (output.CountCancelledSpots == 0) { string scheduleIndexKey = string.Empty; var scheduleBreaksBySalesAreaAndDate = _dataSnapshot .BreaksForRun .Value .GroupBy(k => GetScheduleIndexKey(k.SalesAreaName, k.ScheduleDate.Date)) .ToDictionary(c => c.Key, c => c.ToDictionary(v => v.CustomId)); _audit.Insert(AuditEventFactory.CreateAuditEventForWarningMessage(0, 0, string.Format("Processing {0} for cancellations because no cancelled spots were found in {1}", Path.GetFileName(mainSpotFile), Path.GetFileName(spotFile)))); foreach (var currentSalesArea in _dataSnapshot.AllSalesAreas.Value) { foreach (SpotImport spotImport2 in spotImports.Where(s => s.status == "C" && s.sare_no == currentSalesArea.CustomId)) { try { scheduleIndexKey = string.Empty; bool isCancelledSpot = !String.IsNullOrEmpty(spotImport2.status) && spotImport2.status == "C"; // Should all be cancelled // Update statistics for Spot Performance, Campaign // Performance, New Efficiency, cancelled spots // should be excluded output.CountTotalSpots++; if (isCancelledSpot) { output.CountCancelledSpots++; } // Create recommendation Recommendation recommendation = _mapper.Map <Recommendation>(Tuple.Create(spotImport2, new List <ProgrammeDictionary>())); recommendation.ScenarioId = scenarioId; recommendation.ProcessorDateTime = processorDateTime; if (spotsByCustomId.TryGetValue((int)spotImport2.spot_no, out var spot)) { recommendation.Sponsored = spot.Sponsored; recommendation.Preemptable = spot.Preemptable; recommendation.Preemptlevel = spot.Preemptlevel; recommendation.ExternalSpotRef = spot.ExternalSpotRef; recommendation.MultipartSpot = spot.MultipartSpot; recommendation.MultipartSpotPosition = spot.MultipartSpotPosition; recommendation.MultipartSpotRef = spot.MultipartSpotRef; } else { _audit.Insert( AuditEventFactory.CreateAuditEventForWarningMessage( 0, 0, $"Unable to set multipart spot details for recommendations because spot {spotImport2.spot_no} does not exist" ) ); } recommendation.SalesArea = recommendation.GroupCode = currentSalesArea.Name; if (campaignsByCustomId.TryGetValue((int)spotImport2.camp_no, out CampaignReducedModel campaign)) { recommendation.ExternalCampaignNumber = campaign.ExternalId; } if (demographicsById.TryGetValue(spotImport2.demo_no, out Demographic demographic)) { recommendation.Demographic = demographic.ExternalRef; } scheduleIndexKey = GetScheduleIndexKey(recommendation.SalesArea, recommendation.StartDateTime.Date); string breakNotSetReason = "Unknown reason"; if (spotImport2 == null) { breakNotSetReason = $"Spot not found in {Path.GetFileName(mainSpotFile)}"; } else { if (!scheduleBreaksBySalesAreaAndDate.TryGetValue(scheduleIndexKey, out var scheduleBreakCache)) { breakNotSetReason = string.Format("Schedule document not found (SalesArea={0}, StartDateTime={1})", recommendation.SalesArea, recommendation.StartDateTime.ToString()); } else { recommendation.ExternalBreakNo = !scheduleBreakCache.TryGetValue(spotImport2.break_no, out var @break) ? recommendation.ExternalBreakNo : @break.ExternalBreakRef; // Set reason why ExternalBreakNo isn't set if (String.IsNullOrEmpty(recommendation.ExternalBreakNo)) { if (!(scheduleBreakCache?.Any() ?? false)) { breakNotSetReason = $"Schedule document exists but it contains no breakss (BreakNo={spotImport2.break_no}, Breaks=0)"; } else if (@break == null) { breakNotSetReason = $"Schedule document exists but the break was not found (BreakNo={spotImport2.break_no}, Breaks={scheduleBreakCache.Count})"; } else // Break found but ExternalBreakRef is not set { breakNotSetReason = $"Schedule document exists and the break was found but the ExternalBreakRef is not set (BreakNo={spotImport2.break_no}, Breaks={scheduleBreakCache.Count})"; } } } recommendation.ClientPicked = (spotImport2.client_picked.ToUpper() == "Y"); } if (String.IsNullOrEmpty(recommendation.ExternalBreakNo)) { _audit.Insert( AuditEventFactory.CreateAuditEventForWarningMessage( 0, 0, $"Unable to determine External Break No for recommendation for spot no {spotImport2.spot_no}: {breakNotSetReason}" ) ); } output.Recommendations.Add(recommendation); } catch (Exception exception) { // Log exception, continue _audit.Insert(AuditEventFactory.CreateAuditEventForException(0, 0, string.Format("Error generating cancellation recommendation for spot no {0}", spotImport2.spot_no), exception)); } } } } _audit.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0, 0, $"Processed output file {spotFile}")); _audit.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0, 0, $"Processed output file {mainSpotFile}")); return(output); }
public TaskResult Execute(TaskInstance taskInstance) { var taskResult = new TaskResult(); Guid runId = Guid.Empty; Guid scenarioId = Guid.Empty; string autoBookId = String.Empty; try { StartActiveNotifier(taskInstance.Id); TaskUtilities.UpdateTaskInstanceStatus(taskInstance.Id, _repositoryFactory, TaskInstanceStatues.InProgress, null); // Read parameters runId = new Guid(taskInstance.Parameters["RunId"]); scenarioId = new Guid(taskInstance.Parameters["ScenarioId"]); autoBookId = taskInstance.Parameters["AutoBookId"]; AutoBookStatuses autoBookStatus = (AutoBookStatuses)Enum.Parse(typeof(AutoBookStatuses), taskInstance.Parameters["AutoBookStatus"], true); using var scope = _repositoryFactory.BeginRepositoryScope(); // Get AutoBook instance var autoBookRepository = scope.CreateRepository <IAutoBookRepository>(); var autoBook = autoBookRepository.Get(autoBookId); RunInstance runInstance = _runInstanceCreator.Create(runId, scenarioId); // Check AutoBook status switch (autoBookStatus) { case AutoBookStatuses.Task_Completed: runInstance.HandleCompletedSuccess(); break; case AutoBookStatuses.Task_Error: runInstance.HandleCompletedTaskError(); break; case AutoBookStatuses.Fatal_Error: runInstance.HandleCompletedFatalError(autoBook); break; } TaskUtilities.UpdateTaskInstanceStatus(taskInstance.Id, _repositoryFactory, TaskInstanceStatues.CompletedSuccess, DateTime.UtcNow); taskResult.Status = TaskInstanceStatues.CompletedSuccess; } catch (Exception exception) { _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForException(0, 0, string.Format("Error executing task for scenario completed (TaskInstanceID={0}, RunID={1}, ScenarioID={2}, AutoBookID={3})", taskInstance.Id, runId, scenarioId, autoBookId), exception)); TaskUtilities.UpdateTaskInstanceStatus(taskInstance.Id, _repositoryFactory, TaskInstanceStatues.CompletedError, DateTime.UtcNow); taskResult.Status = TaskInstanceStatues.CompletedError; taskResult.Exception = exception; } finally { StopActiveNotifier(); } return(taskResult); }
private ProcessBreakEfficiencyOutput ProcessFileInternal(Guid scenarioId, string folder) { var output = new ProcessBreakEfficiencyOutput(); string breakEfficiencyFile = FileHelpers.GetPathToFileIfExists(folder, FileName); if (String.IsNullOrEmpty(breakEfficiencyFile)) { return(output); } _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0, 0, $"Processing output file {breakEfficiencyFile}")); var demographicsById = Demographic.IndexListById(_dataSnapshot.AllDemographics.Value); var salesAreasIndexed = SalesArea.IndexListByCustomId(_dataSnapshot.AllSalesAreas.Value); // Create memory cache of Schedule documents var schedules = new List <ScheduleIndexed <Break, int> >(); string lastScheduleDateKey = ""; int count = 0; int countUpdated = 0; int countNotUpdated = 0; DateTime nextYield = DateTime.MinValue; // Process CSV in order of sales area + schedule date + schedule // time, allows us to keep as few Break/Schedule documents in memory var importSettings = CSVImportSettings.GetImportSettings(breakEfficiencyFile, typeof(BreakEfficiencyHeaderMap), typeof(BreakEfficiencyIndexMap)); IBreakEfficiencyImportRepository breakEfficiencyImportRepository = new CSVBreakEfficiencyImportRepository(importSettings); IScheduleRepository scheduleRepository = null; var lazyScopeCreator = new Func <Lazy <IRepositoryScope> >(() => new Lazy <IRepositoryScope>(() => _repositoryFactory.BeginRepositoryScope())); var lazyScope = lazyScopeCreator(); try { foreach (BreakEfficiencyImport breakEfficiencyImport in breakEfficiencyImportRepository.GetAll() .OrderBy(be => be.sare_no).ThenBy(be => be.brek_sched_date).ThenBy(be => be.brek_nom_time)) { count++; bool breakUpdated = false; string scheduleDateKey = string.Format("{0}{1}{2}", breakEfficiencyImport.sare_no, (Char)9, breakEfficiencyImport.brek_sched_date); try { // Save changes if different sales area + schedule date if (!String.IsNullOrEmpty(lastScheduleDateKey) && scheduleDateKey != lastScheduleDateKey) // No longer need previous Break/Schedule docs { if (scheduleRepository != null) { schedules.ForEach(scheduleIndexed => scheduleRepository.Add(scheduleIndexed.Schedule)); scheduleRepository.SaveChanges(); } scheduleRepository = null; lazyScope.Value.Dispose(); lazyScope = lazyScopeCreator(); // Clear data that we no longer need schedules.Clear(); } if (scheduleRepository == null) { scheduleRepository = lazyScope.Value.CreateRepository <IScheduleRepository>(); } SalesArea salesArea = salesAreasIndexed.ContainsKey(breakEfficiencyImport.sare_no) ? salesAreasIndexed[breakEfficiencyImport.sare_no] : null; // Load breaks if necessary LoadBreaks(salesArea, breakEfficiencyImport, schedules, scheduleRepository); // Update break efficiency if (UpdateBreakEfficiency(salesArea, breakEfficiencyImport, schedules, demographicsById)) { breakUpdated = true; } } catch (System.Exception exception) { // Log exception, continue _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForException(0, 0, string.Format("Error processing break efficiency for break no {0} (Record {1})", breakEfficiencyImport.break_no, count), exception)); } finally { lastScheduleDateKey = scheduleDateKey; if (breakUpdated) { countUpdated++; } else { countNotUpdated++; } DoYield(false, ref nextYield); } } // Save changed if (scheduleRepository != null) { schedules.ForEach(scheduleIndexed => scheduleRepository.Add(scheduleIndexed.Schedule)); scheduleRepository.SaveChanges(); } } finally { if (scheduleRepository != null) { lazyScope.Value.Dispose(); } } if (countUpdated > 0 && countNotUpdated == 0) // Success, log info event { _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0, 0, string.Format("Updated {0} break efficiencies (Not updated={1})", countUpdated, countNotUpdated))); } else // Some updates failed, log warning event { _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForWarningMessage(0, 0, string.Format("Updated {0} break efficiencies (Not updated={1})", countUpdated, countNotUpdated))); } _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0, 0, $"Processed output file {breakEfficiencyFile}")); return(output); }