public override async Task ExecuteAsync(ReportRequest request, CancellationToken token, IProgress <JobStatus> progress = null) { #region Reporting initialization request = await StartRequestAsync(request); var criterion = await _serviceFacade.ReportCriterionRepository.GetByIdAsync(request.ReportCriteriaId) ?? throw new GraException($"Report criteria {request.ReportCriteriaId} for report request id {request.Id} could not be found."); if (criterion.SiteId == null) { throw new ArgumentException(nameof(criterion.SiteId)); } string title = ""; if (criterion.BranchId.HasValue) { criterion.SystemId = null; title = (await _branchRepository.GetByIdAsync(criterion.BranchId.Value)).Name; } else if (criterion.SystemId.HasValue) { title = (await _systemRepository.GetByIdAsync(criterion.SystemId.Value)).Name; } else { throw new GraException("No system or branch selected."); } var report = new StoredReport { Title = title, AsOf = _serviceFacade.DateTimeProvider.Now }; var reportData = new List <object[]>(); #endregion Reporting initialization #region Collect data UpdateProgress(progress, 1, "Starting report...", request.Name); // header row report.HeaderRow = new object[] { "Prize Name", "# Redeemed" }; int count = 0; var prizes = await _prizeWinnerRepository.GetRedemptionsAsync(criterion); foreach (var prize in prizes .Where(_ => string.IsNullOrEmpty(_.PrizeName) && _.DrawingId == null && _.TriggerId == null)) { prize.PrizeName = "Free Book"; } var prizeGroups = prizes.GroupBy(_ => new { _.PrizeName, _.DrawingId, _.TriggerId }) .Select(_ => new { Prize = _.Key, Count = _.Count() }) .OrderBy(_ => _.Prize.PrizeName); int prizeGroupsCount = prizeGroups.Count(); foreach (var group in prizeGroups) { UpdateProgress(progress, ++count * 100 / prizeGroupsCount, $"Processing: {count}/{prizeGroupsCount}", request.Name); if (token.IsCancellationRequested) { break; } reportData.Add(new object[] { group.Prize.PrizeName, group.Count }); } report.Data = reportData.ToArray(); #endregion Collect data #region Finish up reporting if (!token.IsCancellationRequested) { ReportSet.Reports.Add(report); } await FinishRequestAsync(request, !token.IsCancellationRequested); #endregion Finish up reporting }
public override async Task ExecuteAsync(ReportRequest request, CancellationToken token, IProgress <OperationStatus> progress = null) { #region Reporting initialization if (request == null) { throw new ArgumentNullException(nameof(request)); } request = await StartRequestAsync(request); var criterion = await _serviceFacade.ReportCriterionRepository.GetByIdAsync(request.ReportCriteriaId) ?? throw new GraException($"Report criteria {request.ReportCriteriaId} for report request id {request.Id} could not be found."); if (criterion.SiteId == null) { throw new ArgumentNullException(nameof(criterion.SiteId)); } string title = ""; if (criterion.BranchId.HasValue) { criterion.SystemId = null; title = (await _branchRepository.GetByIdAsync(criterion.BranchId.Value)).Name; } else if (criterion.SystemId.HasValue) { title = (await _systemRepository.GetByIdAsync(criterion.SystemId.Value)).Name; } else { throw new GraException("No system or branch selected."); } var report = new StoredReport { Title = title, AsOf = _serviceFacade.DateTimeProvider.Now }; var reportData = new List <object[]>(); #endregion Reporting initialization #region Collect data UpdateProgress(progress, 1, "Starting report...", request.Name); // header row report.HeaderRow = new object[] { "Prize Name", "# Redeemed" }; int count = 0; var prizes = await _prizeWinnterRepository.GetRedemptionsAsync(criterion); var prizeGroups = prizes.GroupBy(_ => new { PrizeName = _.PrizeName, DrawingId = _.DrawingId, TriggerId = _.TriggerId }) .Select(_ => new { Prize = _.Key, Count = _.Count() }) .OrderBy(_ => _.Prize.PrizeName); int prizeGroupsCount = prizeGroups.Count(); foreach (var group in prizeGroups) { UpdateProgress(progress, ++count * 100 / prizeGroupsCount, $"Processing: {count}/{prizeGroupsCount}", request.Name); if (token.IsCancellationRequested) { break; } reportData.Add(new object[] { group.Prize.PrizeName, group.Count }); } report.Data = reportData.ToArray(); #endregion Collect data #region Finish up reporting _logger.LogInformation($"Report {GetType().Name} with criterion {criterion.Id} ran in {StopTimer()}"); request.Success = !token.IsCancellationRequested; if (request.Success == true) { ReportSet.Reports.Add(report); request.Finished = _serviceFacade.DateTimeProvider.Now; request.ResultJson = Newtonsoft.Json.JsonConvert.SerializeObject(ReportSet); } await _serviceFacade.ReportRequestRepository.UpdateSaveNoAuditAsync(request); #endregion Finish up reporting }