Skip to content

DicomAssociation Controller #480

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions docs/api/rest/dicom-association.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<!--
~ Copyright 2021-2023 MONAI Consortium
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

# DICOM Association information

The `/dai' endpoint is for retrieving a list of information regarding dicom
associations.

## GET /dai/

#### Query Parameters

| Name | Type | Description |
|------------|----------|---------------------------------------------|
| startTime | DateTime | (Optional) Start date to query from. |
| endTime | DateTime | (Optional) End date to query from. |
| pageNumber | Number | (Optional) Page number to query.(default 0) |
| pageSize | Number | (Optional) Page size of query. |

Max & Defaults for PageSize can be set in appSettings.

```json
"endpointSettings": {
"defaultPageSize": number,
"maxPageSize": number
}
```

Endpoint returns a paged result for example

```json
{
"PageNumber": 1,
"PageSize": 10,
"FirstPage": "/payload?pageNumber=1&pageSize=10",
"LastPage": "/payload?pageNumber=1&pageSize=10",
"TotalPages": 1,
"TotalRecords": 3,
"NextPage": null,
"PreviousPage": null,
"Data": [...]
"Succeeded": true,
"Errors": null,
"Message": null
}
```
30 changes: 30 additions & 0 deletions src/Configuration/EndpointSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2021-2023 MONAI Consortium
* Copyright 2019-2021 NVIDIA Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using Microsoft.Extensions.Configuration;

namespace Monai.Deploy.InformaticsGateway.Configuration
{
public class EndpointSettings
{
[ConfigurationKeyName("defaultPageSize")]
public int DefaultPageSize { get; set; } = 10;

[ConfigurationKeyName("maxPageSize")]
public int MaxPageSize { get; set; } = 10;
}
}
7 changes: 7 additions & 0 deletions src/Configuration/InformaticsGatewayConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ public class InformaticsGatewayConfiguration
[ConfigurationKeyName("plugins")]
public PlugInConfiguration PlugInConfigurations { get; set; }

/// <summary>
/// Represents the <c>endpointSettings</c> section of the configuration file.
/// </summary>
[ConfigurationKeyName("endpointSettings")]
public EndpointSettings EndpointSettings { get; set; }

public InformaticsGatewayConfiguration()
{
Dicom = new DicomConfiguration();
Expand All @@ -93,6 +99,7 @@ public InformaticsGatewayConfiguration()
Database = new DatabaseConfiguration();
Hl7 = new Hl7Configuration();
PlugInConfigurations = new PlugInConfiguration();
EndpointSettings = new EndpointSettings();
}
}
}
15 changes: 15 additions & 0 deletions src/Database/Api/Repositories/IDicomAssociationInfoRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,20 @@ public interface IDicomAssociationInfoRepository
Task<List<DicomAssociationInfo>> ToListAsync(CancellationToken cancellationToken = default);

Task<DicomAssociationInfo> AddAsync(DicomAssociationInfo item, CancellationToken cancellationToken = default);

/// <summary>
/// Retrieves a list of DicomAssociationInfo in the database.
/// </summary>
Task<IList<DicomAssociationInfo>> GetAllAsync(int skip,
int? limit,
DateTime startTime,
DateTime endTime,
CancellationToken cancellationToken);

/// <summary>
/// Gets count of objects
/// </summary>
/// <returns>Count of objects.</returns>
Task<long> CountAsync();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,24 @@ public async Task<DicomAssociationInfo> AddAsync(DicomAssociationInfo item, Canc
}).ConfigureAwait(false);
}

public async Task<IList<DicomAssociationInfo>> GetAllAsync(int skip,
int? limit,
DateTime startTime,
DateTime endTime,
CancellationToken cancellationToken)
{
return await _dataset
.Where(t =>
t.DateTimeDisconnected >= startTime.ToUniversalTime() &&
t.DateTimeDisconnected <= endTime.ToUniversalTime())
.Skip(skip)
.Take(limit!.Value)
.ToListAsync(cancellationToken)
.ConfigureAwait(false);
}

public Task<long> CountAsync() => _dataset.LongCountAsync();

public async Task<List<DicomAssociationInfo>> ToListAsync(CancellationToken cancellationToken = default)
{
return await _retryPolicy.ExecuteAsync(async () =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

namespace Monai.Deploy.InformaticsGateway.Database.MongoDB.Repositories
{
public class DicomAssociationInfoRepository : IDicomAssociationInfoRepository, IDisposable
public class DicomAssociationInfoRepository : MongoDBRepositoryBase, IDicomAssociationInfoRepository, IDisposable
{
private readonly ILogger<DicomAssociationInfoRepository> _logger;
private readonly IServiceScope _scope;
Expand Down Expand Up @@ -78,6 +78,29 @@ public async Task<List<DicomAssociationInfo>> ToListAsync(CancellationToken canc
}).ConfigureAwait(false);
}

public Task<IList<DicomAssociationInfo>> GetAllAsync(int skip,
int? limit,
DateTime startTime,
DateTime endTime,
CancellationToken cancellationToken)
{
var builder = Builders<DicomAssociationInfo>.Filter;
var filter = builder.Empty;
filter &= builder.Where(t => t.DateTimeDisconnected >= startTime.ToUniversalTime());
filter &= builder.Where(t => t.DateTimeDisconnected <= endTime.ToUniversalTime());

return GetAllAsync(_collection,
filter,
Builders<DicomAssociationInfo>.Sort.Descending(x => x.DateTimeDisconnected),
skip,
limit);
}

public Task<long> CountAsync()
{
return _collection.CountDocumentsAsync(Builders<DicomAssociationInfo>.Filter.Empty);
}

protected virtual void Dispose(bool disposing)
{
if (!_disposedValue)
Expand Down
64 changes: 64 additions & 0 deletions src/Database/MongoDB/Repositories/MongoDBRepositoryBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2021-2023 MONAI Consortium
* Copyright 2019-2021 NVIDIA Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System.Linq.Expressions;
using MongoDB.Driver;

namespace Monai.Deploy.InformaticsGateway.Database.MongoDB.Repositories
{
public abstract class MongoDBRepositoryBase
{
/// <summary>
/// Get All T that match filters provided.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="collection">Collection to run against.</param>
/// <param name="filterFunction">Filter function you can filter on properties of T.</param>
/// <param name="sortFunction">Function used to sort data.</param>
/// <param name="skip">Items to skip.</param>
/// <param name="limit">Items to limit results by.</param>
/// <returns></returns>
protected static async Task<IList<T>> GetAllAsync<T>(IMongoCollection<T> collection,
Expression<Func<T, bool>>? filterFunction,
SortDefinition<T> sortFunction,
int? skip = null,
int? limit = null)
{
return await collection
.Find(filterFunction)
.Skip(skip)
.Limit(limit)
.Sort(sortFunction)
.ToListAsync().ConfigureAwait(false);
}

protected static async Task<IList<T>> GetAllAsync<T>(IMongoCollection<T> collection,
FilterDefinition<T> filterFunction,
SortDefinition<T> sortFunction,
int? skip = null,
int? limit = null)
{
var result = await collection
.Find(filterFunction)
.Skip(skip)
.Limit(limit)
.Sort(sortFunction)
.ToListAsync().ConfigureAwait(false);
return result;
}
}
}
3 changes: 3 additions & 0 deletions src/InformaticsGateway/Logging/Log.0.General.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,8 @@ public static partial class Log

[LoggerMessage(EventId = 13, Level = LogLevel.Critical, Message = "Failed to start {ServiceName}.")]
public static partial void ServiceFailedToStart(this ILogger logger, string serviceName, Exception ex);

[LoggerMessage(EventId = 14, Level = LogLevel.Error, Message = "Unexpected error occurred in GET /dai API..")]
public static partial void DAIControllerGetAllAsyncError(this ILogger logger, Exception ex);
}
}
107 changes: 107 additions & 0 deletions src/InformaticsGateway/Services/Common/Pagination/PagedResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright 2021-2023 MONAI Consortium
* Copyright 2019-2021 NVIDIA Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System;
using Monai.Deploy.InformaticsGateway.Services.UriService;

namespace Monai.Deploy.InformaticsGateway.Services.Common.Pagination
{
/// <summary>
/// Paged Response for use with pagination's.
/// </summary>
/// <typeparam name="T">Type of response.</typeparam>
public class PagedResponse<T> : Response<T>
{
/// <summary>
/// Initializes a new instance of the <see cref="PagedResponse{T}"/> class.
/// </summary>
/// <param name="data">Response Data.</param>
/// <param name="pageNumber">Page number.</param>
/// <param name="pageSize">Page size.</param>
public PagedResponse(T data, int pageNumber, int pageSize)
{
PageNumber = pageNumber;
PageSize = pageSize;
Data = data;
Message = null;
Succeeded = true;
Errors = null;
}

/// <summary>
/// Gets or sets PageNumber.
/// </summary>
public int PageNumber { get; set; }

/// <summary>
/// Gets or sets PageSize.
/// </summary>
public int PageSize { get; set; }

/// <summary>
/// Gets or sets FirstPage.
/// </summary>
public string? FirstPage { get; set; }

/// <summary>
/// Gets or sets LastPage.
/// </summary>
public string? LastPage { get; set; }

/// <summary>
/// Gets or sets TotalPages.
/// </summary>
public int TotalPages { get; set; }

/// <summary>
/// Gets or sets TotalRecords.
/// </summary>
public long TotalRecords { get; set; }

/// <summary>
/// Gets or sets NextPage.
/// </summary>
public string? NextPage { get; set; }

/// <summary>
/// Gets or sets previousPage.
/// </summary>
public string? PreviousPage { get; set; }

public void SetUp(PaginationFilter validFilter, long totalRecords, IUriService uriService, string route)
{
var totalPages = (double)totalRecords / PageSize;
var roundedTotalPages = Convert.ToInt32(Math.Ceiling(totalPages));

var pageNumber = validFilter.PageNumber ?? 0;
NextPage =
pageNumber >= 1 && pageNumber < roundedTotalPages
? uriService.GetPageUriString(new PaginationFilter(pageNumber + 1, PageSize), route)
: null;

PreviousPage =
pageNumber - 1 >= 1 && pageNumber <= roundedTotalPages
? uriService.GetPageUriString(new PaginationFilter(pageNumber - 1, PageSize), route)
: null;

FirstPage = uriService.GetPageUriString(new PaginationFilter(1, PageSize), route);
LastPage = uriService.GetPageUriString(new PaginationFilter(roundedTotalPages, PageSize), route);
TotalPages = roundedTotalPages;
TotalRecords = totalRecords;
}
}
}
Loading