-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: #1294: Process all sets imported found in a directory.
- Loading branch information
Showing
7 changed files
with
220 additions
and
112 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
....MatchPrediction.Test.Validation.Data/Repositories/Homework/HomeworkDeletionRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using Dapper; | ||
using Microsoft.Data.SqlClient; | ||
|
||
namespace Atlas.MatchPrediction.Test.Validation.Data.Repositories.Homework | ||
{ | ||
public interface IHomeworkDeletionRepository | ||
{ | ||
Task DeleteAll(); | ||
} | ||
|
||
public class HomeworkDeletionRepository : IHomeworkDeletionRepository | ||
{ | ||
private readonly string connectionString; | ||
|
||
public HomeworkDeletionRepository(string connectionString) | ||
{ | ||
this.connectionString = connectionString; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task DeleteAll() | ||
{ | ||
const string deleteAllPairs = "DELETE FROM PatientDonorPairs"; | ||
|
||
await using (var connection = new SqlConnection(connectionString)) | ||
{ | ||
await connection.ExecuteAsync(deleteAllPairs); | ||
} | ||
|
||
const string deleteAllSets = "DELETE FROM HomeworkSets"; | ||
|
||
await using (var connection = new SqlConnection(connectionString)) | ||
{ | ||
await connection.ExecuteAsync(deleteAllSets); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
Atlas.MatchPrediction.Test.Validation/Services/Exercise4/Homework/HomeworkCreator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Atlas.Common.Utils.Extensions; | ||
using Atlas.ManualTesting.Common.Services; | ||
using Atlas.MatchPrediction.Test.Validation.Data.Models.Homework; | ||
using Atlas.MatchPrediction.Test.Validation.Data.Repositories.Homework; | ||
using Atlas.MatchPrediction.Test.Validation.Models; | ||
|
||
namespace Atlas.MatchPrediction.Test.Validation.Services.Exercise4.Homework | ||
{ | ||
public interface IHomeworkCreator | ||
{ | ||
/// <summary> | ||
/// Creates new homework sets from the input files defined in the request. | ||
/// </summary> | ||
/// <returns>Homework Set Ids</returns> | ||
Task<IEnumerable<int>> CreateHomeworkSets(HomeworkRequest request); | ||
} | ||
|
||
internal class HomeworkCreator : IHomeworkCreator | ||
{ | ||
private const string FileDelimiter = ","; | ||
private readonly IHomeworkSetRepository setRepository; | ||
private readonly IFileReader<SubjectIdPair> fileReader; | ||
private readonly IPatientDonorPairRepository pdpRepository; | ||
|
||
public HomeworkCreator( | ||
IHomeworkSetRepository setRepository, | ||
IFileReader<SubjectIdPair> fileReader, | ||
IPatientDonorPairRepository pdpRepository) | ||
{ | ||
this.setRepository = setRepository; | ||
this.fileReader = fileReader; | ||
this.pdpRepository = pdpRepository; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task<IEnumerable<int>> CreateHomeworkSets(HomeworkRequest request) | ||
{ | ||
var setIds = new List<int?>(); | ||
|
||
var fileNames = GetCsvFileNames(request.InputPath); | ||
|
||
foreach (var file in fileNames) | ||
{ | ||
setIds.Add(await ImportPatientDonorPairs(request, file)); | ||
} | ||
|
||
return setIds.Where(id => id.HasValue).Select(id => id.Value); | ||
} | ||
|
||
private static IEnumerable<string> GetCsvFileNames(string path) | ||
{ | ||
return Directory.GetFiles(path) | ||
.Where(file => Path.GetExtension(file) == ".csv") | ||
.Select(Path.GetFileName); | ||
} | ||
|
||
private async Task<int?> ImportPatientDonorPairs(HomeworkRequest request, string setFileName) | ||
{ | ||
var subjectIdPairs = await fileReader.ReadAllLines( | ||
FileDelimiter, | ||
Path.Combine(request.InputPath, setFileName), | ||
hasHeaderRecord: false); | ||
|
||
if (subjectIdPairs.IsNullOrEmpty()) | ||
{ | ||
System.Diagnostics.Debug.WriteLine($"No patient-donor pairs found in file {setFileName}."); | ||
return null; | ||
} | ||
|
||
var setId = await setRepository.Add(setFileName, request.ResultsPath, request.MatchLoci.MatchLociToString()); | ||
await pdpRepository.BulkInsert(subjectIdPairs.Select(ids => MapToDatabaseModel(ids, setId)).ToList()); | ||
return setId; | ||
} | ||
|
||
private static PatientDonorPair MapToDatabaseModel(SubjectIdPair ids, int homeworkSetId) | ||
{ | ||
return new PatientDonorPair | ||
{ | ||
PatientId = ids.PatientId, | ||
DonorId = ids.DonorId, | ||
HomeworkSet_Id = homeworkSetId | ||
}; | ||
} | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
Atlas.MatchPrediction.Test.Validation/Services/Exercise4/Homework/HomeworkProcessor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Atlas.Common.Utils.Extensions; | ||
using Atlas.MatchPrediction.Test.Validation.Data.Repositories.Homework; | ||
using Atlas.MatchPrediction.Test.Validation.Models; | ||
|
||
namespace Atlas.MatchPrediction.Test.Validation.Services.Exercise4.Homework | ||
{ | ||
public interface IHomeworkProcessor | ||
{ | ||
Task StartOrContinueHomeworkSets(IEnumerable<int> homeworkSetIds); | ||
} | ||
|
||
internal class HomeworkProcessor : IHomeworkProcessor | ||
{ | ||
private readonly IHomeworkSetRepository setRepository; | ||
private readonly IPatientDonorPairRepository pdpRepository; | ||
private readonly IPatientDonorPairProcessor pdpProcessor; | ||
|
||
public HomeworkProcessor( | ||
IHomeworkSetRepository setRepository, | ||
IPatientDonorPairRepository pdpRepository, | ||
IPatientDonorPairProcessor pdpProcessor) | ||
{ | ||
this.setRepository = setRepository; | ||
this.pdpRepository = pdpRepository; | ||
this.pdpProcessor = pdpProcessor; | ||
} | ||
|
||
public async Task StartOrContinueHomeworkSets(IEnumerable<int> homeworkSetIds) | ||
{ | ||
foreach (var homeworkSetId in homeworkSetIds) | ||
{ | ||
await ProcessHomeworkSet(homeworkSetId); | ||
} | ||
} | ||
|
||
private async Task ProcessHomeworkSet(int homeworkSetId) | ||
{ | ||
var set = await setRepository.Get(homeworkSetId); | ||
|
||
if (set == null) | ||
{ | ||
throw new ArgumentException($"No homework set found with id {homeworkSetId}."); | ||
} | ||
|
||
var pdps = (await pdpRepository.GetUnprocessedPairs(homeworkSetId)).ToList(); | ||
|
||
if (pdps.IsNullOrEmpty()) | ||
{ | ||
System.Diagnostics.Debug.WriteLine($"No unprocessed patient-donor pairs found for homework set {homeworkSetId}."); | ||
return; | ||
} | ||
|
||
var matchLoci = set.MatchLoci.ToLociInfo(); | ||
|
||
foreach (var pdp in pdps) | ||
{ | ||
await pdpProcessor.Process(pdp, matchLoci); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.