-
Notifications
You must be signed in to change notification settings - Fork 518
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert Refactor for Missing External MI Case (#3263)
* default template provider refactor * added missing bracket * make sp readonly --------- Co-authored-by: Shaun Donnelly <[email protected]>
- Loading branch information
1 parent
bac9ce7
commit 3e83e50
Showing
9 changed files
with
231 additions
and
50 deletions.
There are no files selected for viewing
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
100 changes: 100 additions & 0 deletions
100
src/Microsoft.Health.Fhir.Core/Features/Operations/ConvertData/DefaultTemplateProvider.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,100 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using DotLiquid; | ||
using EnsureThat; | ||
using Microsoft.Extensions.Caching.Memory; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
using Microsoft.Health.Fhir.Core.Configs; | ||
using Microsoft.Health.Fhir.Core.Features.Operations.ConvertData.Models; | ||
using Microsoft.Health.Fhir.Core.Messages.ConvertData; | ||
using Microsoft.Health.Fhir.TemplateManagement; | ||
using Microsoft.Health.Fhir.TemplateManagement.Exceptions; | ||
|
||
namespace Microsoft.Health.Fhir.Core.Features.Operations.ConvertData | ||
{ | ||
public class DefaultTemplateProvider : IConvertDataTemplateProvider, IDisposable | ||
{ | ||
private bool _disposed = false; | ||
private readonly ILogger _logger; | ||
private readonly MemoryCache _cache; | ||
private readonly ITemplateCollectionProviderFactory _templateCollectionProviderFactory; | ||
private readonly ConvertDataConfiguration _convertDataConfig; | ||
|
||
public DefaultTemplateProvider( | ||
IOptions<ConvertDataConfiguration> convertDataConfig, | ||
ILogger<IConvertDataTemplateProvider> logger) | ||
{ | ||
EnsureArg.IsNotNull(convertDataConfig, nameof(convertDataConfig)); | ||
EnsureArg.IsNotNull(logger, nameof(logger)); | ||
|
||
_convertDataConfig = convertDataConfig.Value; | ||
|
||
_logger = logger; | ||
|
||
// Initialize cache and template collection provider factory | ||
_cache = new MemoryCache(new MemoryCacheOptions | ||
{ | ||
SizeLimit = _convertDataConfig.CacheSizeLimit, | ||
}); | ||
_templateCollectionProviderFactory = new TemplateCollectionProviderFactory(_cache, Options.Create(_convertDataConfig.TemplateCollectionOptions)); | ||
} | ||
|
||
/// <summary> | ||
/// Fetch template collection from built-in archive following a default template convert request. | ||
/// </summary> | ||
/// <param name="request">The convert data request which contains template reference.</param> | ||
/// <param name="cancellationToken">Cancellation token to cancel the fetch operation.</param> | ||
/// <returns>Template collection.</returns> | ||
public async Task<List<Dictionary<string, Template>>> GetTemplateCollectionAsync(ConvertDataRequest request, CancellationToken cancellationToken) | ||
{ | ||
var accessToken = string.Empty; | ||
|
||
_logger.LogInformation("Using the default template collection for data conversion."); | ||
|
||
try | ||
{ | ||
var provider = _templateCollectionProviderFactory.CreateTemplateCollectionProvider(request.TemplateCollectionReference, accessToken); | ||
return await provider.GetTemplateCollectionAsync(cancellationToken); | ||
} | ||
catch (TemplateManagementException templateEx) | ||
{ | ||
_logger.LogWarning(templateEx, "Template collection is invalid."); | ||
throw new TemplateCollectionErrorException(string.Format(Core.Resources.FetchTemplateCollectionFailed, templateEx.Message), templateEx); | ||
} | ||
catch (Exception unhandledEx) | ||
{ | ||
_logger.LogError(unhandledEx, "Unhandled exception: failed to get template collection."); | ||
throw new FetchTemplateCollectionFailedException(string.Format(Core.Resources.FetchTemplateCollectionFailed, unhandledEx.Message), unhandledEx); | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Dispose(true); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (_disposed) | ||
{ | ||
return; | ||
} | ||
|
||
if (disposing) | ||
{ | ||
_cache?.Dispose(); | ||
} | ||
|
||
_disposed = true; | ||
} | ||
} | ||
} |
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
14 changes: 14 additions & 0 deletions
14
src/Microsoft.Health.Fhir.Core/Features/Operations/ConvertData/ITemplateProviderFactory.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,14 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
namespace Microsoft.Health.Fhir.Core.Features.Operations.ConvertData | ||
{ | ||
public interface ITemplateProviderFactory | ||
{ | ||
public IConvertDataTemplateProvider GetContainerRegistryTemplateProvider(); | ||
|
||
public IConvertDataTemplateProvider GetDefaultTemplateProvider(); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/Microsoft.Health.Fhir.Core/Features/Operations/ConvertData/TemplateProviderFactory.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,31 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
using System; | ||
using EnsureThat; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Microsoft.Health.Fhir.Core.Features.Operations.ConvertData | ||
{ | ||
public class TemplateProviderFactory : ITemplateProviderFactory | ||
{ | ||
private readonly IServiceProvider _sp; | ||
|
||
public TemplateProviderFactory(IServiceProvider sp) | ||
{ | ||
_sp = EnsureArg.IsNotNull(sp, nameof(sp)); | ||
} | ||
|
||
public IConvertDataTemplateProvider GetContainerRegistryTemplateProvider() | ||
{ | ||
return _sp.GetRequiredService<ContainerRegistryTemplateProvider>(); | ||
} | ||
|
||
public IConvertDataTemplateProvider GetDefaultTemplateProvider() | ||
{ | ||
return _sp.GetRequiredService<DefaultTemplateProvider>(); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.