Skip to content

Commit

Permalink
refactor(dto): simplify data transfer logic for diabetes risk predict…
Browse files Browse the repository at this point in the history
…ion request

- refactor data transfer logic by:
  - removing DiabetesRiskModel as it's no longer needed
  - replacing with more focused PatientRiskRequest DTO
- rename PatientRiskInfo to PatientRiskRequest:
  - improve naming clarity for request/response pattern
  - ensure consistency across Frontend and Backend services
  - follow REST API conventions

Notes are now fetched directly by backend from elasticsearch, eliminating the need to transfer them through Frontend. Indeed only Id, DateOfBirth and Gender are needed. Id is needed to fetch notes, and Gender and DateOfBirth are needed to calculate diabetes risk.
  • Loading branch information
EveCrystali committed Nov 21, 2024
1 parent 0c23ff4 commit 3c5ff20
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ public class BackendDiabetesRiskPredictionsController(DiabetesRiskNotePrediction


[HttpGet]
public async Task<ActionResult<DiabetesRisk>> GetDiabetesRisk([FromBody] DiabetesRiskRequest diabetesRiskRequest)
public async Task<ActionResult<DiabetesRisk>> GetDiabetesRisk([FromBody] PatientRiskRequest patientRiskRequest)
{
logger.LogDebug("GetDiabetesRisk called");
logger.LogDebug($"Diabetes risk request : {diabetesRiskRequest}");
DiabetesRiskPrediction diabetesRisk = await diabetesRiskNotePredictionService.DiabetesRiskPrediction(diabetesRiskRequest.NotesRiskInfo, diabetesRiskRequest.PatientRiskInfo);
logger.LogDebug($"Diabetes risk request : {patientRiskRequest}");

Check notice on line 18 in BackendDiabetesRiskPrediction/Controllers/DiabetesRiskPredictionsController.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

RoslynAnalyzers Template should be a static expression

The logging message template should not vary between calls to 'LoggerExtensions.LogDebug(ILogger, string?, params object?\[\])'
DiabetesRiskPrediction diabetesRisk = await diabetesRiskNotePredictionService.DiabetesRiskPrediction(patientRiskRequest);

Check notice on line 19 in BackendDiabetesRiskPrediction/Controllers/DiabetesRiskPredictionsController.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Use preferred 'var' style (when type is simple)

Use 'var' (simple types)
logger.LogInformation($"Diabetes risk is : {diabetesRisk.DiabetesRisk}");

Check notice on line 20 in BackendDiabetesRiskPrediction/Controllers/DiabetesRiskPredictionsController.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

RoslynAnalyzers Template should be a static expression

The logging message template should not vary between calls to 'LoggerExtensions.LogInformation(ILogger, string?, params object?\[\])'
return Ok(diabetesRisk);
}
Expand Down
7 changes: 0 additions & 7 deletions BackendDiabetesRiskPrediction/Models/DiabetesRiskRequest.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace BackendDiabetesRiskPrediction.Models;

public class PatientRiskInfo
public class PatientRiskRequest
{
public int Id { get; set; }

Check warning on line 5 in BackendDiabetesRiskPrediction/Models/PatientRiskRequest.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Auto-property accessor is never used (non-private accessibility)

Auto-property accessor 'Id.set' is never used
public required DateOnly DateOfBirth { get; set; }

Check warning on line 6 in BackendDiabetesRiskPrediction/Models/PatientRiskRequest.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Auto-property accessor is never used (non-private accessibility)

Auto-property accessor 'DateOfBirth.set' is never used
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ public class DiabetesRiskNotePredictionService(ElasticsearchService elasticsearc
"Anticorps"
];

public async Task<DiabetesRiskPrediction> DiabetesRiskPrediction(List<NoteRiskInfo>? notes, PatientRiskInfo? patientRiskInfo)
public async Task<DiabetesRiskPrediction> DiabetesRiskPrediction(PatientRiskRequest? patientRiskInfo)
{
DiabetesRiskPrediction diabetesRiskPrediction = new();
if (notes is null || patientRiskInfo is null)
if (patientRiskInfo is null)
{
diabetesRiskPrediction.DiabetesRisk = DiabetesRisk.None;
return diabetesRiskPrediction;
Expand All @@ -38,9 +38,9 @@ public async Task<DiabetesRiskPrediction> DiabetesRiskPrediction(List<NoteRiskIn

private async Task<int> DiabetesRiskPredictionNotesAnalysis(int patientId, HashSet<string> hashSetofTriggerWords) => await elasticsearchService.CountUniqueWordsInNotes(patientId, hashSetofTriggerWords);

private DiabetesRisk DiabetesRiskPredictionCalculator(PatientRiskInfo patientRiskInfo, int triggersDiabetesRiskFromNotes)
private DiabetesRisk DiabetesRiskPredictionCalculator(PatientRiskRequest patientRiskRequest, int triggersDiabetesRiskFromNotes)
{
int age = PatientAgeCalculator(patientRiskInfo);
int age = PatientAgeCalculator(patientRiskRequest);

Check notice on line 43 in BackendDiabetesRiskPrediction/Services/DiabetesRiskNotePredictionService.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Use preferred 'var' style (for built-in types)

Use 'var' (built-in types)

// Do not need to consider if Female or Male or Age
if (triggersDiabetesRiskFromNotes == 0)
Expand All @@ -50,17 +50,17 @@ private DiabetesRisk DiabetesRiskPredictionCalculator(PatientRiskInfo patientRis

// Patient is younger than 30 (exclusive)
return age < 30
? DiabetesRiskPredictionForUnder30(patientRiskInfo, triggersDiabetesRiskFromNotes)
? DiabetesRiskPredictionForUnder30(patientRiskRequest, triggersDiabetesRiskFromNotes)
:
// Patient is older (or equal) than 30 (inclusive)
DiabetesRiskPredictionFor30AndOlder(triggersDiabetesRiskFromNotes);
}

private DiabetesRisk DiabetesRiskPredictionForUnder30(PatientRiskInfo patientRiskInfo, int triggersDiabetesRiskFromNotes)
private DiabetesRisk DiabetesRiskPredictionForUnder30(PatientRiskRequest patientRiskRequest, int triggersDiabetesRiskFromNotes)
{
logger.LogInformation($"Patient gender is : {patientRiskInfo.Gender}");
logger.LogInformation($"Patient gender is : {patientRiskRequest.Gender}");

Check notice on line 61 in BackendDiabetesRiskPrediction/Services/DiabetesRiskNotePredictionService.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

RoslynAnalyzers Template should be a static expression

The logging message template should not vary between calls to 'LoggerExtensions.LogInformation(ILogger, string?, params object?\[\])'

return patientRiskInfo.Gender switch
return patientRiskRequest.Gender switch
{
// Patient is a male
// Let's consider triggers from notes
Expand Down Expand Up @@ -89,10 +89,10 @@ private static DiabetesRisk DiabetesRiskPredictionFor30AndOlder(int triggersDiab

}

private int PatientAgeCalculator(PatientRiskInfo patientRiskInfo)
private int PatientAgeCalculator(PatientRiskRequest patientRiskRequest)
{
DateTime currentDate = DateTime.Now;

Check notice on line 94 in BackendDiabetesRiskPrediction/Services/DiabetesRiskNotePredictionService.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Use preferred 'var' style (when type is simple)

Use 'var' (simple types)
DateOnly birthDate = patientRiskInfo.DateOfBirth;
DateOnly birthDate = patientRiskRequest.DateOfBirth;

Check notice on line 95 in BackendDiabetesRiskPrediction/Services/DiabetesRiskNotePredictionService.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Use preferred 'var' style (when type is simple)

Use 'var' (simple types)
int age = currentDate.Year - birthDate.Year;

Check notice on line 96 in BackendDiabetesRiskPrediction/Services/DiabetesRiskNotePredictionService.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Use preferred 'var' style (for built-in types)

Use 'var' (built-in types)
logger.LogInformation("Patient age is : {Age}", age);
return age;
Expand Down
10 changes: 4 additions & 6 deletions Frontend/Controllers/PatientsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,15 @@ public async Task<IActionResult> Details(int id)

// First let's construct our needs
PatientViewModel patientViewModel = PatientService.MapPatientNoteToPatientNotesViewModel(patient, notes);

Check notice on line 65 in Frontend/Controllers/PatientsController.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Use preferred 'var' style (when type is simple)

Use 'var' (simple types)
DiabetesRiskRequestModel diabetesRiskRequestModel = DiabetesRiskPredictionService.MapPatientViewModelAndNoteToDiabetesRiskRequestModel(patientViewModel);
PatientRiskRequest patientRiskRequest = DiabetesRiskPredictionService.MapPatientViewModelToPatientRiskInfo(patientViewModel);

Check notice on line 66 in Frontend/Controllers/PatientsController.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Use preferred 'var' style (when type is simple)

Use 'var' (simple types)

// Secondly let's ask DiabetesRiskPredictionService
HttpRequestMessage requestForDiabetesRiskPredictionService = new(HttpMethod.Get, $"{_diabetesRiskPredictionServiceUrl}/")
{
Content = JsonContent.Create(diabetesRiskRequestModel)
Content = JsonContent.Create(patientRiskRequest)
};

_logger.LogInformation("Requesting Diabetes Risk Prediction for patient with id {PatientId}", id);
_logger.LogInformation("Request body: {RequestBody}", diabetesRiskRequestModel);

_logger.LogInformation("Requesting Diabetes Risk Prediction for patient with id {PatientId}, Date of birth: {DateOfBirth}, Gender: {Gender}", patientRiskRequest.Id.ToString(), patientRiskRequest.DateOfBirth.ToString(), patientRiskRequest.Gender);

// Finally let's manage the answer for DiabetesRiskPredictionService
HttpResponseMessage responseFromDiabetesRiskService = await _httpClientService.SendAsync(requestForDiabetesRiskPredictionService);

Check notice on line 76 in Frontend/Controllers/PatientsController.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Use preferred 'var' style (when type is simple)

Use 'var' (simple types)

Expand Down
7 changes: 0 additions & 7 deletions Frontend/Models/DiabetesRiskRequestModel.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Frontend.Models;

public class PatientRiskInfo
public class PatientRiskRequest
{
public int Id { get; set; }

Check notice on line 5 in Frontend/Models/PatientRiskRequest.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Property can be made init-only (non-private accessibility)

Property can be made init-only
public required DateOnly DateOfBirth { get; set; }

Check notice on line 6 in Frontend/Models/PatientRiskRequest.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Property can be made init-only (non-private accessibility)

Property can be made init-only
Expand Down
30 changes: 1 addition & 29 deletions Frontend/Services/DiabetesRiskPredictionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,10 @@ namespace Frontend.Services;

public static class DiabetesRiskPredictionService
{
private static NoteRiskInfo MapPatientViewModelToNoteRiskInfo(Note note) => new()
{
Id = note.Id,
PatientId = note.PatientId,
Title = note.Title,
Body = note.Body
};

private static PatientRiskInfo MapPatientViewModelToPatientRiskInfo(PatientViewModel patientViewModel) => new()
public static PatientRiskRequest MapPatientViewModelToPatientRiskInfo(PatientViewModel patientViewModel) => new()
{
Id = patientViewModel.PatientId,
DateOfBirth = patientViewModel.DateOfBirth,
Gender = patientViewModel.Gender
};

public static DiabetesRiskRequestModel MapPatientViewModelAndNoteToDiabetesRiskRequestModel(PatientViewModel patientViewModel)
{
PatientRiskInfo patientRiskInfo = MapPatientViewModelToPatientRiskInfo(patientViewModel);
if (patientViewModel.Notes == null)
{
return new DiabetesRiskRequestModel
{
PatientRiskInfo = patientRiskInfo
};
}
List<NoteRiskInfo> noteRiskInfos = patientViewModel.Notes
.Select(MapPatientViewModelToNoteRiskInfo)
.ToList();
return new DiabetesRiskRequestModel
{
NotesRiskInfo = noteRiskInfos,
PatientRiskInfo = patientRiskInfo
};
}
}

0 comments on commit 3c5ff20

Please sign in to comment.