private async Task <string> GetCsv(IReportServiceContext reportServiceContext, CancellationToken cancellationToken) { Task <IMessage> ilrFileTask = _ilrProviderService.GetIlrFile(reportServiceContext, cancellationToken); Task <List <string> > validLearnersTask = _validLearnersService.GetLearnersAsync(reportServiceContext, cancellationToken); Task <FM36Global> fm36Task = _fm36ProviderService.GetFM36Data(reportServiceContext, cancellationToken); await Task.WhenAll(ilrFileTask, validLearnersTask, fm36Task); if (cancellationToken.IsCancellationRequested) { return(null); } List <AppsIndicativeEarningsModel> appsIndicativeEarningsModels = new List <AppsIndicativeEarningsModel>(); if (ilrFileTask.Result?.Learners != null) { await GenerateRowsAsync(ilrFileTask.Result, fm36Task.Result, validLearnersTask.Result, appsIndicativeEarningsModels, cancellationToken); } using (var ms = new MemoryStream()) { UTF8Encoding utF8Encoding = new UTF8Encoding(false, true); using (TextWriter textWriter = new StreamWriter(ms, utF8Encoding)) { using (CsvWriter csvWriter = new CsvWriter(textWriter)) { WriteCsvRecords <AppsIndicativeEarningsMapper, AppsIndicativeEarningsModel>(csvWriter, appsIndicativeEarningsModels); csvWriter.Flush(); textWriter.Flush(); return(Encoding.UTF8.GetString(ms.ToArray())); } } } }
public async Task GenerateReport(IReportServiceContext reportServiceContext, ZipArchive archive, bool isFis, CancellationToken cancellationToken) { FM36Global fm36Data = await _fm36ProviderService.GetFM36Data(reportServiceContext, cancellationToken).ConfigureAwait(false); if (fm36Data?.Learners == null) { _logger.LogWarning($"No FM36 learner data for {nameof(DataMatchReport)}. It will be empty."); } else { cancellationToken.ThrowIfCancellationRequested(); List <RawEarning> rawEarnings = new List <RawEarning>(); List <long> ulns = await GetUlnsForValidLearners( fm36Data, rawEarnings, reportServiceContext, cancellationToken); List <DasCommitment> commitments = await _dasCommitmentsService.GetCommitments( reportServiceContext.Ukprn, ulns, cancellationToken); _logger.LogWarning($"Found {commitments.Count} commitments against UKPRN {reportServiceContext.Ukprn} and {ulns.Count} ULNs."); cancellationToken.ThrowIfCancellationRequested(); BuildReportData(rawEarnings, commitments, reportServiceContext.Ukprn); _validationStageOutputCache.DataMatchProblemCount = dataMatchModels.Count; _validationStageOutputCache.DataMatchProblemLearnersCount = dataMatchModels.DistinctBy(x => x.LearnRefNumber).Count(); } var jobId = reportServiceContext.JobId; var externalFileName = GetExternalFilename(reportServiceContext.Ukprn.ToString(), jobId, reportServiceContext.SubmissionDateTimeUtc); var fileName = GetFilename(reportServiceContext.Ukprn.ToString(), jobId, reportServiceContext.SubmissionDateTimeUtc); string csv = WriteResults(); await _streamableKeyValuePersistenceService.SaveAsync($"{externalFileName}.csv", csv, cancellationToken); await WriteZipEntry(archive, $"{fileName}.csv", csv); }