DicomMigratorApp

This commit is contained in:
shadab ahmed 2024-07-22 18:08:33 +05:30
parent 600d8fa5ec
commit c0ff0f35c3
3 changed files with 179 additions and 47 deletions

22
Dockerfile Normal file
View File

@ -0,0 +1,22 @@
# Use the official .NET SDK image to build the app
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
WORKDIR /app
# Copy the csproj and restore any dependencies
COPY *.csproj ./
RUN dotnet restore
# Copy the entire project and build the app
COPY . ./
RUN dotnet publish -c Release -o out
# Use the official .NET runtime image to run the app
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
COPY --from=build-env /app/out .
# Expose port
EXPOSE 80
# Entry point to run the application
ENTRYPOINT ["dotnet", "DicomMigratorApp.dll"]

View File

@ -8,6 +8,7 @@ using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using System.Threading;
namespace DicomMigratorApp namespace DicomMigratorApp
{ {
@ -20,7 +21,6 @@ namespace DicomMigratorApp
private static string targetIP; private static string targetIP;
private static string senderAET; private static string senderAET;
private static string targetAET; private static string targetAET;
private static string folderLocation;
private static int targetPort; private static int targetPort;
static async Task Main(string[] args) static async Task Main(string[] args)
@ -38,12 +38,11 @@ namespace DicomMigratorApp
_logger.LogInformation("Application started"); _logger.LogInformation("Application started");
connectionString = "Server=127.0.0.1;Port=3306;Database=dms_db;User Id=root;Password=Rootmatrix23@;"; connectionString = Environment.GetEnvironmentVariable("ConnectionStrings__DefaultConnection") ?? "";
targetIP = "127.0.0.1"; targetIP = Environment.GetEnvironmentVariable("Dest__IP") ?? "";
senderAET = "MYAE"; senderAET = Environment.GetEnvironmentVariable("Sender__AET") ?? "";
targetAET = "ORTHANC"; targetAET = Environment.GetEnvironmentVariable("Dest__AET") ?? "";
folderLocation = "D:\\dicom_imags\\"; var targetPortStr = Environment.GetEnvironmentVariable("Dest__Port") ?? "";
var targetPortStr = "4242";
if (!int.TryParse(targetPortStr, out targetPort)) if (!int.TryParse(targetPortStr, out targetPort))
{ {
@ -51,36 +50,54 @@ namespace DicomMigratorApp
return; return;
} }
while (true)
{
try try
{ {
var migrationStudy = await GetMigrationStudy(connectionString); var migrationStudy = await GetMigrationStudy(connectionString);
if (migrationStudy == null) if (migrationStudy == null)
{ {
_logger.LogInformation("No studies found for migration."); await Task.Delay(5000);
return; continue;
} }
_logger.LogInformation($"Retrieved studyDetails for Migration"); _logger.LogInformation($"Retrieved study details for migration: {migrationStudy.StudyInstanceUID}");
try try
{ {
await MigrateStudy(migrationStudy.StudyInstanceUID, folderLocation + migrationStudy.StudyInstanceUID); var imagePaths = await GetImagePaths(connectionString, migrationStudy.StudyInstanceUID);
await UpdateSourceCFind(connectionString, migrationStudy.StudyInstanceUID, "MIGRATION_COMPLETE");
foreach (var image in imagePaths)
{
await MigrateImage(image.Path);
await UpdateImagePathStatus(connectionString, image.Id, "MIGRATION_COMPLETE");
}
await UpdateSourceCFind(connectionString, migrationStudy.Id, "MIGRATION_COMPLETE");
} }
catch (DicomAssociationRejectedException ex) catch (DicomAssociationRejectedException ex)
{ {
_logger.LogError(ex, $"Association rejected for studyInstanceUID {migrationStudy.StudyInstanceUID}"); _logger.LogError(ex, $"Association rejected for studyInstanceUID {migrationStudy.StudyInstanceUID}");
await UpdateSourceCFind(connectionString, migrationStudy.StudyInstanceUID, "MIGRATION_ERRORED"); await UpdateSourceCFind(connectionString, migrationStudy.Id, "MIGRATION_ERRORED");
break;
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, $"An error occurred while retrieving or saving studyInstanceUID {migrationStudy.StudyInstanceUID}"); _logger.LogError(ex, $"An error occurred while retrieving or saving studyInstanceUID {migrationStudy.StudyInstanceUID}");
await UpdateSourceCFind(connectionString, migrationStudy.StudyInstanceUID, "MIGRATION_ERRORED"); await UpdateSourceCFind(connectionString, migrationStudy.Id, "MIGRATION_ERRORED");
break;
} }
} }
catch (MySqlException ex)
{
_logger.LogError(ex, "An error occurred while connecting to the database");
break;
}
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "An error occurred in the main processing loop"); _logger.LogError(ex, "An error occurred in the main processing loop");
break;
}
} }
} }
@ -95,16 +112,17 @@ namespace DicomMigratorApp
_logger.LogInformation("Opening a connection to the database"); _logger.LogInformation("Opening a connection to the database");
await connection.OpenAsync(); await connection.OpenAsync();
_logger.LogInformation("Database connection established"); _logger.LogInformation("Database connection established");
var command = new MySqlCommand("SELECT study_instance_uid,patient_id,status FROM source_cfind WHERE status='QR_COMPLETE' LIMIT 1", connection); var command = new MySqlCommand("SELECT id, study_instance_uid, patient_id, status FROM priority_migration_study WHERE status='QR_COMPLETE' ORDER BY schedule_date LIMIT 1", connection);
var reader = await command.ExecuteReaderAsync(); var reader = await command.ExecuteReaderAsync();
while (await reader.ReadAsync()) while (await reader.ReadAsync())
{ {
qrStudy = new SourceStudy qrStudy = new SourceStudy
{ {
StudyInstanceUID = reader.IsDBNull(0) ? null : reader.GetString(0), Id = reader.IsDBNull(0) ? 0 : reader.GetInt32(0),
PatientID = reader.IsDBNull(1) ? null : reader.GetString(1), StudyInstanceUID = reader.IsDBNull(1) ? null : reader.GetString(1),
Status = reader.IsDBNull(2) ? null : reader.GetString(2) PatientID = reader.IsDBNull(2) ? null : reader.GetString(2),
Status = reader.IsDBNull(3) ? null : reader.GetString(3)
}; };
} }
@ -112,8 +130,8 @@ namespace DicomMigratorApp
} }
if (qrStudy.StudyInstanceUID != null) if (qrStudy.StudyInstanceUID != null)
{ {
_logger.LogInformation("Picked study to be migrated from the database"); _logger.LogInformation($"Picked study to be migrated from the database: {qrStudy.StudyInstanceUID}");
await UpdateSourceCFind(connectionString, qrStudy.StudyInstanceUID, "MIGRATION_INPROGRESS"); await UpdateSourceCFind(connectionString, qrStudy.Id, "MIGRATION_INPROGRESS");
} }
else else
{ {
@ -123,54 +141,125 @@ namespace DicomMigratorApp
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "Error retrieving study to be migrated from the database"); _logger.LogError(ex, "Error retrieving study to be migrated from the database");
throw; // Re-throw the exception to ensure it is caught by the outer try-catch block
} }
return qrStudy; return qrStudy;
} }
private static async Task UpdateSourceCFind(string connectionString, string study_instance_uid, string updateStatus) private static async Task<List<ImageRecord>> GetImagePaths(string connectionString, string studyInstanceUID)
{
var imagePaths = new List<ImageRecord>();
try
{
using (var connection = new MySqlConnection(connectionString))
{
await connection.OpenAsync();
var command = new MySqlCommand("SELECT id, image_path FROM priority_migration_image WHERE study_instance_uid = @StudyInstanceUID AND (status != 'MIGRATION_COMPLETE' or status is null)", connection);
command.Parameters.AddWithValue("@StudyInstanceUID", studyInstanceUID);
var reader = await command.ExecuteReaderAsync();
while (await reader.ReadAsync())
{
imagePaths.Add(new ImageRecord
{
Id = reader.IsDBNull(0) ? 0 : reader.GetInt32(0),
Path = reader.IsDBNull(1) ? null : reader.GetString(1)
});
}
reader.Close();
}
_logger.LogInformation($"Retrieved {imagePaths.Count} image paths for studyInstanceUID {studyInstanceUID}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Error retrieving image paths from the database");
throw; // Re-throw the exception to ensure it is caught by the outer try-catch block
}
return imagePaths;
}
private static async Task UpdateImagePathStatus(string connectionString, int imageId, string updateStatus)
{ {
try try
{ {
using (var connection = new MySqlConnection(connectionString)) using (var connection = new MySqlConnection(connectionString))
{ {
await connection.OpenAsync(); await connection.OpenAsync();
var command = new MySqlCommand("UPDATE source_cfind SET status = @Status WHERE study_instance_uid = @StudyInstanceUID", connection); var command = new MySqlCommand("UPDATE priority_migration_image SET status = @Status WHERE id = @ImageId", connection);
command.Parameters.AddWithValue("@StudyInstanceUID", study_instance_uid); command.Parameters.AddWithValue("@ImageId", imageId);
command.Parameters.AddWithValue("@Status", updateStatus); command.Parameters.AddWithValue("@Status", updateStatus);
await command.ExecuteNonQueryAsync(); await command.ExecuteNonQueryAsync();
_logger.LogInformation($"Updated source C-FIND record for study instance uid {study_instance_uid}"); _logger.LogInformation($"Updated image path record for image ID {imageId} with status {updateStatus}");
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, $"Error updating source study record for study {study_instance_uid}"); _logger.LogError(ex, $"Error updating image path record for image ID {imageId}");
throw; // Re-throw the exception to ensure it is caught by the outer try-catch block
} }
} }
private static async Task MigrateStudy(string studyInstanceUID, string folderPath) private static async Task MigrateImage(string imagePath)
{
try
{ {
var client = DicomClientFactory.Create(targetIP, targetPort, false, senderAET, targetAET); var client = DicomClientFactory.Create(targetIP, targetPort, false, senderAET, targetAET);
var files = System.IO.Directory.GetFiles(folderPath, "*"); var dicomFile = await DicomFile.OpenAsync(imagePath);
foreach (var file in files)
{
var dicomFile = await DicomFile.OpenAsync(file);
await client.AddRequestAsync(new DicomCStoreRequest(dicomFile)); await client.AddRequestAsync(new DicomCStoreRequest(dicomFile));
}
await client.SendAsync(); await client.SendAsync();
_logger.LogInformation($"Migrated studyInstanceUID {studyInstanceUID} to target AET {targetAET}"); _logger.LogInformation($"Migrated image {imagePath} to target AET {targetAET}");
}
catch (Exception ex)
{
_logger.LogError(ex, $"Error migrating image {imagePath}");
throw; // Re-throw the exception to ensure it is caught by the outer try-catch block
}
}
private static async Task UpdateSourceCFind(string connectionString, int studyId, string updateStatus)
{
try
{
using (var connection = new MySqlConnection(connectionString))
{
await connection.OpenAsync();
var command = new MySqlCommand("UPDATE priority_migration_study SET status = @Status WHERE id = @StudyId", connection);
command.Parameters.AddWithValue("@StudyId", studyId);
command.Parameters.AddWithValue("@Status", updateStatus);
await command.ExecuteNonQueryAsync();
_logger.LogInformation($"Updated source C-FIND record for study ID {studyId} with status {updateStatus}");
}
}
catch (Exception ex)
{
_logger.LogError(ex, $"Error updating source study record for study ID {studyId}");
throw; // Re-throw the exception to ensure it is caught by the outer try-catch block
}
} }
} }
class SourceStudy class SourceStudy
{ {
public int Id { get; set; }
public string PatientID { get; set; } public string PatientID { get; set; }
public string StudyInstanceUID { get; set; } public string StudyInstanceUID { get; set; }
public string Status { get; set; } public string Status { get; set; }
} }
class ImageRecord
{
public int Id { get; set; }
public string Path { get; set; }
}
} }

21
docker-compose.yml Normal file
View File

@ -0,0 +1,21 @@
version: '3.8'
services:
dicom-migrator-app:
image: seyfertsoft/dicommigratorapp:1.0.0
container_name: dicommigrator
environment:
- "ConnectionStrings__DefaultConnection=Server=mysqldb;Port=3306;Database=dms_db;User Id=root;Password=Rootmatrix23@;"
- Dest__IP=127.0.0.1
- Sender__AET=MYAE
- Dest__AET=ORTHANC
- Dest__Port=4242
volumes:
- /path/to/images1:/app/images1
- /path/to/images2:/app/images2
networks:
- dmsnetwork
networks:
dmsnetwork:
external: true