From 4ace1b987f3bdf45f7bfaaf68be5294fe3401899 Mon Sep 17 00:00:00 2001 From: Corey Date: Fri, 22 Jan 2021 11:10:40 +0000 Subject: [PATCH] Add webjobs app insight package --- .../Services/PaymentService.cs | 72 ++++++++++++++++--- 1 file changed, 63 insertions(+), 9 deletions(-) diff --git a/src/SFA.DAS.EmployerFinance/Services/PaymentService.cs b/src/SFA.DAS.EmployerFinance/Services/PaymentService.cs index 680fcdfbab..3f1e293a4c 100644 --- a/src/SFA.DAS.EmployerFinance/Services/PaymentService.cs +++ b/src/SFA.DAS.EmployerFinance/Services/PaymentService.cs @@ -36,7 +36,7 @@ public PaymentService(IPaymentsEventsApiClient paymentsEventsApiClient, _apprenticeshipInfoService = apprenticeshipInfoService; _mapper = mapper; _logger = logger; - _inProcessCache = inProcessCache; + _inProcessCache = inProcessCache; _providerService = providerService; } @@ -56,17 +56,43 @@ public async Task> GetAccountPayments(string periodE var paymentDetails = payments.Items.Select(x => _mapper.Map(x)).ToArray(); - int paymentDetailsCount = 0; + //int paymentDetailsCount = 0; + + _logger.Info($"Fetching provider and apprenticeship for AccountId = {employerAccountId}, periodEnd={periodEnd}, correlationId = {correlationId}"); + + var ukprnList = paymentDetails.Select(pd => pd.Ukprn).Distinct(); + var apprenticeshipIdList = paymentDetails.Select(pd => pd.ApprenticeshipId).Distinct(); + + var getProviderDetailsTask = GetProviderDetailsDict(ukprnList); + var getApprenticeDetailsTask = GetApprenticeshipDetailsDict(employerAccountId, apprenticeshipIdList); + + await Task.WhenAll(getProviderDetailsTask, getApprenticeDetailsTask); + + var apprenticeshipDetails = getApprenticeDetailsTask.Result; + var providerDetails = getProviderDetailsTask.Result; + + _logger.Info($"Fetching {providerDetails.Count} providers and {apprenticeshipDetails.Count} apprenticeship details for AccountId = {employerAccountId}, periodEnd={periodEnd}, correlationId = {correlationId}"); + foreach (var details in paymentDetails) { details.PeriodEnd = periodEnd; - - var getProviderDetailsTask = GetProviderDetails(details); - var getApprenticeshipDetailsTask = GetApprenticeshipDetails(employerAccountId, details); var getCourseDetailsTask = GetCourseDetails(details); - await Task.WhenAll(getProviderDetailsTask, getApprenticeshipDetailsTask, getCourseDetailsTask); - _logger.Info($"Metadata retrieved for payment {details.Id}, count: {++paymentDetailsCount}, correlationId = {correlationId}"); + var provider = providerDetails[details.Ukprn]; + details.ProviderName = provider?.Name; + details.IsHistoricProviderName = provider?.IsHistoricProviderName ?? false; + + var apprenticeship = apprenticeshipDetails[details.ApprenticeshipId]; + + if (apprenticeship != null) + { + details.ApprenticeName = $"{apprenticeship.FirstName} {apprenticeship.LastName}"; + details.ApprenticeNINumber = apprenticeship.NINumber; + details.CourseStartDate = apprenticeship.StartDate; + } + + await getCourseDetailsTask; + //_logger.Info($"Metadata retrieved for payment {details.Id}, count: {++paymentDetailsCount}, correlationId = {correlationId}"); } populatedPayments.AddRange(paymentDetails); @@ -77,6 +103,34 @@ public async Task> GetAccountPayments(string periodE return populatedPayments; } + private async Task> GetProviderDetailsDict(IEnumerable ukprnList) + { + var resultProviders = new Dictionary(); + + foreach (var ukprn in ukprnList) + { + if (resultProviders.ContainsKey(ukprn)) continue; + var provider = await _providerService.Get(ukprn); + resultProviders.Add(ukprn, provider); + } + + return resultProviders; + } + + private async Task> GetApprenticeshipDetailsDict(long employerAccountId, IEnumerable apprenticeshipIdList) + { + var resultApprenticeships = new Dictionary(); + + foreach (var apprenticeshipId in apprenticeshipIdList) + { + if (resultApprenticeships.ContainsKey(apprenticeshipId)) continue; + var apprenticeship = await GetApprenticeship(employerAccountId, apprenticeshipId); + resultApprenticeships.Add(apprenticeshipId, apprenticeship); + } + + return resultApprenticeships; + } + public async Task> GetAccountTransfers(string periodEnd, long receiverAccountId, Guid correlationId) { var pageOfTransfers = @@ -112,7 +166,7 @@ private async Task GetCourseDetails(PaymentDetails payment) payment.CourseName = standard?.CourseName; payment.CourseLevel = standard?.Level; } - else if(payment.FrameworkCode.HasValue && payment.FrameworkCode > 0) + else if (payment.FrameworkCode.HasValue && payment.FrameworkCode > 0) { await GetFrameworkCourseDetails(payment); } @@ -183,7 +237,7 @@ private async Task> GetPaymentsPage(long employerAccountI } return null; - } + } private async Task GetStandard(long standardCode) {