Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Network Hip initial implementation(AIIMS) #373

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
4 changes: 2 additions & 2 deletions hip-service.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{C6699092-7
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "In.ProjectEKA.HipServiceTest", "test\In.ProjectEKA.HipServiceTest\In.ProjectEKA.HipServiceTest.csproj", "{B5A9C0B9-25EC-472E-B2CD-3EC5A69584F7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "In.ProjectEKA.DefaultHip", "src\In.ProjectEKA.DefaultHip\In.ProjectEKA.DefaultHip.csproj", "{23976B61-FBF9-41B8-AEA2-B70AD47F00A8}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "In.ProjectEKA.FHIRHip", "src\In.ProjectEKA.FHIRHip\In.ProjectEKA.FHIRHip.csproj", "{23976B61-FBF9-41B8-AEA2-B70AD47F00A8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "In.ProjectEKA.DefaultHipTest", "test\In.ProjectEKA.DefaultHipTest\In.ProjectEKA.DefaultHipTest.csproj", "{4412373D-2E69-412A-8264-FA2806902173}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "In.ProjectEKA.FHIRHipTest", "test\In.ProjectEKA.FHIRHipTest\In.ProjectEKA.FHIRHipTest.csproj", "{4412373D-2E69-412A-8264-FA2806902173}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "In.ProjectEKA.HipLibrary", "src\In.ProjectEKA.HipLibrary\In.ProjectEKA.HipLibrary.csproj", "{40B6794D-AA65-4B3D-B929-0180CA40E3D6}"
EndProject
Expand Down

This file was deleted.

40 changes: 0 additions & 40 deletions src/In.ProjectEKA.DefaultHip/Link/PatientRepository.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace In.ProjectEKA.DefaultHip.DataFlow
namespace In.ProjectEKA.FHIRHip.DataFlow
{
using System.Collections.Generic;

Expand Down
125 changes: 125 additions & 0 deletions src/In.ProjectEKA.FHIRHip/DataFlow/Collect.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
namespace In.ProjectEKA.FHIRHip.DataFlow
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Mime;
using System.Text;
using System.Threading.Tasks;
using HipLibrary.Patient;
using HipLibrary.Patient.Model;
using Hl7.Fhir.Model;
using Hl7.Fhir.Serialization;
using Hl7.Fhir.Utility;
using Model;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Optional;
using Serilog;

public class Collect : ICollect
{
private readonly DataFlowConfiguration DataFlowConfiguration;
private readonly HttpClient HttpClient;

public Collect(DataFlowConfiguration dataFlowConfiguration, HttpClient httpClient)
{
DataFlowConfiguration = dataFlowConfiguration;
HttpClient = httpClient;
}

public async Task<Option<Entries>> CollectData(TraceableDataRequest dataRequest)
{
var bundles = new List<CareBundle>();
var patientData = await FindPatientsData(dataRequest).ConfigureAwait(false);
var careContextReferences = patientData.Keys.ToList();
foreach (var careContextReference in careContextReferences)
{
foreach (var result in patientData.GetOrDefault(careContextReference))
{
Log.Information($"Returning file: {result}");
var fjp = new FhirJsonParser();
bundles.Add(new CareBundle(careContextReference, fjp.Parse<Bundle>(result)));
}
}
var entries = new Entries(bundles);
return Option.Some(entries);
}

private async Task<Dictionary<string, List<string>>> FindPatientsData(TraceableDataRequest request)
{
LogDataRequest(request);

var patientReferenceNumber = request.CareContexts.First().PatientReference;
var careContexts = request.CareContexts.Select(careContext => careContext.CareContextReference).ToList();
var dataResponse = await GetPatientsData(new NetworkDataRequest(patientReferenceNumber,
careContexts,
request.DateRange,
request.HiType)).ConfigureAwait(false);
var structuredData = new Dictionary<string, List<string>>();
return dataResponse.Map(content =>
{
foreach (var result in content.Results)
{
if (structuredData.ContainsKey(result.CareContext))
{
structuredData[result.CareContext].Add(result.FHIRData);
}
else
{
structuredData.Add(result.CareContext, new List<string> {result.FHIRData});
}
}
return structuredData;
}).ValueOr(structuredData);
}

private async Task<Option<NetworkDataResponse>> GetPatientsData(NetworkDataRequest networkDataRequest)
{
try
{
var json = JsonConvert.SerializeObject(networkDataRequest, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
ContractResolver = new DefaultContractResolver
{
NamingStrategy = new CamelCaseNamingStrategy()
}
});
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, new Uri($"{DataFlowConfiguration.Url}"))
{
Content = new StringContent(json, Encoding.UTF8, MediaTypeNames.Application.Json)
};
var response = await HttpClient.SendAsync(httpRequestMessage).ConfigureAwait(false);
if (!response.IsSuccessStatusCode)
{
return Option.None<NetworkDataResponse>();
}
var responseContent = response.Content;
using var reader = new StreamReader(await responseContent.ReadAsStreamAsync());
var result = await reader.ReadToEndAsync().ConfigureAwait(false);
return Option.Some(JsonConvert.DeserializeObject<NetworkDataResponse>(result));
}
catch (Exception exception)
{
Log.Error(exception, exception.StackTrace);
return Option.None<NetworkDataResponse>();
}
}

private static void LogDataRequest(TraceableDataRequest request)
{
var ccList = JsonConvert.SerializeObject(request.CareContexts);
var requestedHiTypes = string.Join(", ", request.HiType.Select(hiType => hiType.ToString()));
Log.Information("Data request received." +
$" transactionId:{request.TransactionId} , " +
$"CareContexts:{ccList}, " +
$"HiTypes:{requestedHiTypes}," +
$" From date:{request.DateRange.From}," +
$" To date:{request.DateRange.To}, " +
$"CallbackUrl:{request.DataPushUrl}");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace In.ProjectEKA.FHIRHip.DataFlow.Model
{
public class DataFlowConfiguration
{
public int DataSizeLimitInMbs { get; set; }
public int DataLinkTtlInMinutes { get; set; }
public string Url { get; set; }
public bool IsAuthEnabled { get; set; }
public string AuthUrl { get; set; }
}
}
18 changes: 18 additions & 0 deletions src/In.ProjectEKA.FHIRHip/DataFlow/Model/NetworkData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace In.ProjectEKA.FHIRHip.DataFlow.Model
{
using HipLibrary.Patient.Model;

public class NetworkData
{
public string CareContext { get; set; }
public HiType HiType { get; set; }
public string FHIRData { get; set; }

public NetworkData(string careContext, HiType hiType, string fhirData)
{
CareContext = careContext;
HiType = hiType;
FHIRData = fhirData;
}
}
}
21 changes: 21 additions & 0 deletions src/In.ProjectEKA.FHIRHip/DataFlow/Model/NetworkDataRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace In.ProjectEKA.FHIRHip.DataFlow.Model
{
using System.Collections.Generic;
using HipLibrary.Patient.Model;

public class NetworkDataRequest
{
public string PatientReference { get; set; }
public IEnumerable<string> CareContexts { get; set; }
public DateRange DataRange { get; set; }
public IEnumerable<HiType> HiTypes { get; set; }

public NetworkDataRequest(string patientReference, IEnumerable<string> careContexts, DateRange dataRange, IEnumerable<HiType> hiTypes)
{
PatientReference = patientReference;
CareContexts = careContexts;
DataRange = dataRange;
HiTypes = hiTypes;
}
}
}
14 changes: 14 additions & 0 deletions src/In.ProjectEKA.FHIRHip/DataFlow/Model/NetworkDataResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace In.ProjectEKA.FHIRHip.DataFlow.Model
{
using System.Collections.Generic;

public class NetworkDataResponse
{
public IEnumerable<NetworkData> Results { get; set; }

public NetworkDataResponse(IEnumerable<NetworkData> results)
{
Results = results;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace In.ProjectEKA.FHIRHip.Discovery.Model
{
public class PatientConfiguration
{
public string BaseUrl { get; set; }
public string PathDiscovery { get; set; }
public string PathLink { get; set; }
}
}
17 changes: 17 additions & 0 deletions src/In.ProjectEKA.FHIRHip/Discovery/Model/PatientResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace In.ProjectEKA.FHIRHip.Discovery.Model
{
using System.Collections.Generic;
using HipLibrary.Patient.Model;

public class PatientResponse
{
public string PhoneNumber { get; set; }
public string Identifier { get; set; }
public string Email { get; set; }
public string Name { get; set; }
public string CaseReferenceNumber { get; set; }
public Gender Gender { get; set; }
public ushort YearOfBirth { get; set; }
public IEnumerable<CareContextRepresentation> CareContexts { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace In.ProjectEKA.FHIRHip.Discovery.Model
{
using System.Collections.Generic;

public class PatientResponseResult
{
public IEnumerable<PatientResponse> Results { get; set; }

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
namespace In.ProjectEKA.FHIRHip.Discovery
{
using System;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using HipLibrary.Matcher;
using HipLibrary.Patient.Model;
using Model;
using Newtonsoft.Json;
using Serilog;

public class PatientMatchingRepository : IMatchingRepository
{
private readonly HttpClient client;
private readonly PatientConfiguration patientConfiguration;


public PatientMatchingRepository(HttpClient client, PatientConfiguration patientConfiguration)
{
this.client = client;
this.patientConfiguration = patientConfiguration;
}

public async Task<IQueryable<Patient>> Where(DiscoveryRequest predicate)
{
try
{
var request = new HttpRequestMessage(HttpMethod.Post, patientConfiguration.BaseUrl + patientConfiguration.PathDiscovery)
{
Content = new StringContent(
JsonConvert.SerializeObject(new
{
mobileNumber = predicate.Patient.VerifiedIdentifiers.First().Value
}),
Encoding.UTF8,
"application/json")
};
var response = await client.SendAsync(request);
var responseContent = response.Content;
using var reader = new StreamReader(await responseContent.ReadAsStreamAsync());
var result = await reader.ReadToEndAsync().ConfigureAwait(false);
var patientResponseResult = JsonConvert.DeserializeObject<PatientResponseResult>(result);
return patientResponseResult.Results.Select(patient => new Patient
{
Name = patient.Name,
Gender = patient.Gender,
Identifier = patient.Identifier,
CareContexts = patient.CareContexts,
PhoneNumber = patient.PhoneNumber,
YearOfBirth = patient.YearOfBirth
}).AsQueryable();

}
catch (Exception e)
{
Log.Error(e.Message);
return null;
}
}
}
}
Loading