Skip to content

Commit

Permalink
chore: #869: Add new debug endpoint for Atlas donor store to test don…
Browse files Browse the repository at this point in the history
…or import.
  • Loading branch information
zabeen committed Jan 31, 2023
1 parent db75b86 commit 58dc568
Show file tree
Hide file tree
Showing 6 changed files with 51,158 additions and 56,999 deletions.
30 changes: 30 additions & 0 deletions Atlas.Common/Debugging/Donors/DebugDonorsHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace Atlas.Common.Debugging.Donors
{
public static class DebugDonorsHelper
{
public static DebugDonorsResult<TDonor, TDonorId> BuildDebugDonorsResult<TDonor, TDonorId>(
IReadOnlyCollection<TDonorId> allDonorIds,
IReadOnlyCollection<TDonor> presentDonors,
Func<TDonor, TDonorId> idSelector)
{
var absentDonorIds = allDonorIds.Except(presentDonors.Select(idSelector)).ToList();

return new DebugDonorsResult<TDonor, TDonorId>
{
DonorCounts = new DebugDonorsResult<TDonor, TDonorId>.Counts
{
Absent = absentDonorIds.Count,
Present = presentDonors.Count,
Received = allDonorIds.Count
},
AbsentDonorIds = absentDonorIds,
PresentDonors = presentDonors,
ReceivedDonorIds = allDonorIds
};
}
}
}
42 changes: 42 additions & 0 deletions Atlas.Common/Debugging/Donors/DebugDonorsResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Collections.Generic;

namespace Atlas.Common.Debugging.Donors
{
public class DebugDonorsResult<TDonor, TDonorId>
{
public Counts DonorCounts { get; set; }

/// <summary>
/// Ids of donors that were NOT found in the donor store
/// </summary>
public IEnumerable<TDonorId> AbsentDonorIds { get; set; }

/// <summary>
/// Info of donors that were found in the donor store
/// </summary>
public IEnumerable<TDonor> PresentDonors { get; set; }

/// <summary>
/// List of all ids provided in the debug request
/// </summary>
public IEnumerable<TDonorId> ReceivedDonorIds { get; set; }

public class Counts
{
/// <summary>
/// Count of donors NOT found in the donor store
/// </summary>
public int Absent { get; set; }

/// <summary>
/// Count of donors that were found in the donor store
/// </summary>
public int Present { get; set; }

/// <summary>
/// Count of all donor Ids provided in the debug request
/// </summary>
public int Received { get; set; }
}
}
}
42 changes: 42 additions & 0 deletions Atlas.DonorImport.Functions/Functions/Debug/DonorFunctions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Atlas.Common.Debugging.Donors;
using Atlas.Common.Utils.Http;
using Atlas.DonorImport.ExternalInterface;
using Atlas.DonorImport.ExternalInterface.Models;
using AzureFunctions.Extensions.Swashbuckle.Attribute;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Newtonsoft.Json;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;

namespace Atlas.DonorImport.Functions.Functions.Debug
{
public class DonorFunctions
{
private readonly IDonorReader donorReader;

public DonorFunctions(IDonorReader donorReader)
{
this.donorReader = donorReader;
}

[FunctionName(nameof(GetDonors))]
[ProducesResponseType(typeof(DebugDonorsResult<Donor, string>), (int)HttpStatusCode.OK)]
public async Task<IActionResult> GetDonors(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = $"{RouteConstants.DebugRoutePrefix}/donors")]
[RequestBodyType(typeof(string[]), "Donor Record Ids")]
HttpRequest request)
{
var recordIds = JsonConvert.DeserializeObject<string[]>(await new StreamReader(request.Body).ReadToEndAsync());

var donors = await donorReader.GetDonorsByExternalDonorCodes(recordIds);

return new JsonResult(
DebugDonorsHelper.BuildDebugDonorsResult(recordIds, donors.Values.ToList(), d => d.ExternalDonorCode));
}
}
}
2 changes: 2 additions & 0 deletions Atlas.DonorImport.Functions/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public override void Configure(IFunctionsHostBuilder builder)
OptionsReaderFor<PublishDonorUpdatesSettings>(),
ConnectionStringReader("DonorStoreSql")
);

builder.Services.RegisterDonorReader(ConnectionStringReader("DonorStoreSql"));
}

private static void RegisterSettings(IServiceCollection services)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using Atlas.Common.Utils.Http;
using Atlas.MatchingAlgorithm.Data.Models.DonorInfo;
using Atlas.MatchingAlgorithm.Data.Repositories.DonorRetrieval;
using Atlas.MatchingAlgorithm.Services.ConfigurationProviders.TransientSqlDatabase.RepositoryFactories;
using AzureFunctions.Extensions.Swashbuckle.Attribute;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using System.Linq;
using Atlas.Common.Debugging.Donors;

namespace Atlas.MatchingAlgorithm.Functions.Functions.Debug
{
public class DonorFunctions
{
private readonly IDonorInspectionRepository inspectionRepository;

public DonorFunctions(IActiveRepositoryFactory activeRepositoryFactory)
{
inspectionRepository = activeRepositoryFactory.GetDonorInspectionRepository();
}

[FunctionName(nameof(GetDonorsFromActiveDb))]
[ProducesResponseType(typeof(IEnumerable<DebugDonorsResult<DonorInfo, int>>), (int)HttpStatusCode.OK)]
public async Task<IActionResult> GetDonorsFromActiveDb(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = $"{RouteConstants.DebugRoutePrefix}/donors/active")]
[RequestBodyType(typeof(int[]), "Atlas Donor Ids")]
HttpRequest request)
{
var donorIds = JsonConvert.DeserializeObject<int[]>(await new StreamReader(request.Body).ReadToEndAsync());

var donors = await inspectionRepository.GetDonors(donorIds);

return new JsonResult(
DebugDonorsHelper.BuildDebugDonorsResult(donorIds, donors.Values.ToList(), d => d.DonorId));
}
}
}
Loading

0 comments on commit 58dc568

Please sign in to comment.