Skip to content

Commit

Permalink
Add webjobs app insight package
Browse files Browse the repository at this point in the history
  • Loading branch information
cofaulco committed Jan 22, 2021
1 parent 7644bd1 commit 4ace1b9
Showing 1 changed file with 63 additions and 9 deletions.
72 changes: 63 additions & 9 deletions src/SFA.DAS.EmployerFinance/Services/PaymentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public PaymentService(IPaymentsEventsApiClient paymentsEventsApiClient,
_apprenticeshipInfoService = apprenticeshipInfoService;
_mapper = mapper;
_logger = logger;
_inProcessCache = inProcessCache;
_inProcessCache = inProcessCache;
_providerService = providerService;
}

Expand All @@ -56,17 +56,43 @@ public async Task<ICollection<PaymentDetails>> GetAccountPayments(string periodE

var paymentDetails = payments.Items.Select(x => _mapper.Map<PaymentDetails>(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);
Expand All @@ -77,6 +103,34 @@ public async Task<ICollection<PaymentDetails>> GetAccountPayments(string periodE
return populatedPayments;
}

private async Task<Dictionary<long, Models.ApprenticeshipProvider.Provider>> GetProviderDetailsDict(IEnumerable<long> ukprnList)
{
var resultProviders = new Dictionary<long, Models.ApprenticeshipProvider.Provider>();

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<Dictionary<long, Apprenticeship>> GetApprenticeshipDetailsDict(long employerAccountId, IEnumerable<long> apprenticeshipIdList)
{
var resultApprenticeships = new Dictionary<long, Apprenticeship>();

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<IEnumerable<AccountTransfer>> GetAccountTransfers(string periodEnd, long receiverAccountId, Guid correlationId)
{
var pageOfTransfers =
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -183,7 +237,7 @@ private async Task<PageOfResults<Payment>> GetPaymentsPage(long employerAccountI
}

return null;
}
}

private async Task<Standard> GetStandard(long standardCode)
{
Expand Down

0 comments on commit 4ace1b9

Please sign in to comment.