public async Task <OperationStatus> RunReport(string reportRequestIdString, CancellationToken token, IProgress <OperationStatus> progress = null) { if (HasPermission(Permission.ViewAllReporting)) { BaseReport report = null; ReportRequest _request = null; if (!int.TryParse(reportRequestIdString, out int reportRequestId)) { _logger.LogError($"Couldn't covert report request id {reportRequestIdString} to a number."); return(new OperationStatus { PercentComplete = 100, Status = $"Could not find report request {reportRequestIdString}.", Error = true, Complete = false }); } token.Register(() => { string duration = ""; if (report != null && report.Elapsed != null) { duration = $" after {((TimeSpan)report.Elapsed).TotalSeconds.ToString("N2")} seconds"; } if (_request != null) { _logger.LogWarning($"Report {reportRequestId} for user {_request.CreatedBy} was cancelled{duration}."); } else { _logger.LogWarning($"Report {reportRequestId} was cancelled{duration}."); } }); try { _request = await _reportRequestRepository.GetByIdAsync(reportRequestId) ?? throw new GraException($"Cannot find report request id {reportRequestId}"); } catch (Exception ex) { _logger.LogError($"Could not find report request {reportRequestId}: {ex.Message}"); return(new OperationStatus { PercentComplete = 0, Status = "Could not find the report request.", Error = true }); } var reportDetails = new Catalog().Get() .Where(_ => _.Id == _request.ReportId) .SingleOrDefault(); if (reportDetails == null) { _logger.LogError($"Cannot find report id {_request.ReportId} requested by request {reportRequestId}"); return(new OperationStatus { PercentComplete = 0, Status = "Could not find the requested report.", Error = true }); } try { report = _serviceProvider.GetService(reportDetails.ReportType) as BaseReport; } catch (Exception ex) { _logger.LogCritical($"Couldn't instantiate report: {ex.Message}"); return(new OperationStatus { PercentComplete = 100, Status = "Unable to run report.", Error = true }); } try { await report.ExecuteAsync(_request, token, progress); } catch (Exception ex) { return(new OperationStatus { PercentComplete = 100, Status = $"A software error occurred: {ex.Message}.", Error = true }); } if (!token.IsCancellationRequested) { return(new OperationStatus { PercentComplete = 100, Status = "Report processing complete.", }); } else { return(new OperationStatus { PercentComplete = 100, }); } } else { var requestingUser = GetClaimId(ClaimType.UserId); _logger.LogError($"User {requestingUser} doesn't have permission to view all reporting."); return(new OperationStatus { PercentComplete = 0, Status = "Permission denied.", Error = true }); } }
public async Task <JobStatus> RunReportJobAsync(int jobId, CancellationToken token, IProgress <JobStatus> progress = null) { var job = await _jobRepository.GetByIdAsync(jobId); var jobDetails = JsonConvert .DeserializeObject <JobDetailsRunReport>(job.SerializedParameters); int reportRequestId = jobDetails.ReportRequestId; if (HasPermission(Permission.ViewAllReporting)) { BaseReport report = null; ReportRequest _request = null; token.Register(() => { string duration = "immediately"; if (report?.Elapsed != null) { duration = $"after {((TimeSpan)report.Elapsed).TotalSeconds.ToString("N2")} seconds"; } if (_request != null) { _logger.LogWarning("Report {ReportRequestId} for user {UserId} was cancelled {Duration}.", reportRequestId, _request.CreatedBy, duration); } else { _logger.LogWarning("Report {ReportRequestId} was cancelled {Duration}.", reportRequestId, duration); } }); try { _request = await _reportRequestRepository.GetByIdAsync(reportRequestId) ?? throw new GraException($"Cannot find report request id {reportRequestId}"); } catch (Exception ex) { _logger.LogError(ex, "Could not find report request {ReportRequestId}: {Message}", reportRequestId, ex.Message); return(new JobStatus { Status = "Could not find the report request.", Error = true, Complete = true }); } var reportDetails = new Catalog().Get() .SingleOrDefault(_ => _.Id == _request.ReportId); using (Serilog.Context.LogContext.PushProperty(LoggingEnrichment.ReportName, reportDetails.Name)) { if (reportDetails == null) { _logger.LogError("Cannot find report id {ReportId} requested by request {ReportRequestId}", _request.ReportId, reportRequestId); return(new JobStatus { Status = "Could not find the requested report.", Error = true, Complete = true }); } try { report = _serviceProvider.GetService(reportDetails.ReportType) as BaseReport; } catch (Exception ex) { _logger.LogCritical(ex, "Couldn't instantiate report: {Message}", ex.Message); return(new JobStatus { Status = "Unable to run report.", Error = true, Complete = true }); } try { await report.ExecuteAsync(_request, token, progress); } catch (Exception ex) { await _jobRepository.UpdateStatusAsync(jobId, $"An error occurred: {ex.Message}"); return(new JobStatus { Status = $"A software error occurred: {ex.Message}.", Complete = true, Error = true }); } if (token.IsCancellationRequested) { await _jobRepository.UpdateStatusAsync(jobId, $"Report request id {reportRequestId} cancelled."); return(new JobStatus { Status = "Report processing cancelled.", Complete = true, Error = true }); } else { await _jobRepository.UpdateStatusAsync(jobId, $"Report request id {reportRequestId} completed."); return(new JobStatus { PercentComplete = 100, Complete = true, Status = "Report processing complete.", }); } } } else { var requestingUser = GetClaimId(ClaimType.UserId); _logger.LogError("User {UserId} doesn't have permission to view all reporting.", requestingUser); return(new JobStatus { Status = "Permission denied.", Complete = true, Error = true }); } }