Skip to content

Commit

Permalink
- updates to config
Browse files Browse the repository at this point in the history
  • Loading branch information
Herrera committed Jan 28, 2025
1 parent 201c03a commit e56a3fb
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 31 deletions.
3 changes: 3 additions & 0 deletions source/backend/api/Models/Configuration/MayanConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@ public class MayanConfig
public int ImageRetries { get; set; }

public int PreviewPages { get; set; }

public bool CacheDocumentTypes { get; set; }

}
}
54 changes: 28 additions & 26 deletions source/backend/api/Services/DocumentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public class DocumentService : BaseService, IDocumentService
};

private static readonly string MayanConfigSectionKey = "Mayan";
private readonly MayanConfig _config;
private readonly MayanConfig _mayanConfig;

private readonly IDocumentRepository documentRepository;
private readonly IEdmsDocumentRepository documentStorageRepository;
Expand Down Expand Up @@ -96,11 +96,17 @@ public DocumentService(
this.avService = avService;
this.mapper = mapper;
this.keycloakOptions = options;
_config = new MayanConfig();
configuration.Bind(MayanConfigSectionKey, _config);
_mayanConfig = new MayanConfig();
configuration.Bind(MayanConfigSectionKey, _mayanConfig);
_memoryCache = memoryCache;
}

public static bool IsValidDocumentExtension(string fileName)
{
var fileNameExtension = Path.GetExtension(fileName).Replace(".", string.Empty).ToLower();
return ValidExtensions.Contains(fileNameExtension);
}

public IList<PimsDocumentTyp> GetPimsDocumentTypes()
{
this.Logger.LogInformation("Retrieving PIMS document types");
Expand Down Expand Up @@ -156,12 +162,12 @@ public async Task<DocumentUploadResponse> UploadDocumentSync(DocumentUploadReque
if (externalResponse.Status == ExternalResponseStatus.Success)
{
var externalDocument = externalResponse.Payload;
if (externalDocument.FileLatest == null && _config.UploadRetries > 0)
if (externalDocument.FileLatest == null && _mayanConfig.UploadRetries > 0)
{
var retryPolicy = Policy<ExternalResponse<DocumentDetailModel>>
.HandleResult(result => result.HttpStatusCode != HttpStatusCode.OK || result.Payload.FileLatest == null)
.Or<System.Text.Json.JsonException>()
.WaitAndRetryAsync(_config.UploadRetries, (int retry) => TimeSpan.FromSeconds(Math.Pow(2, retry)));
.WaitAndRetryAsync(_mayanConfig.UploadRetries, (int retry) => TimeSpan.FromSeconds(Math.Pow(2, retry)));
var detail = await retryPolicy.ExecuteAsync(async () => await GetStorageDocumentDetail(externalDocument.Id));
if (detail?.Payload?.FileLatest == null)
{
Expand Down Expand Up @@ -428,21 +434,23 @@ public async Task<ExternalResponse<string>> DeleteMayanStorageDocumentAsync(long
Logger.LogInformation("Retrieving storage document types");
User.ThrowIfNotAuthorizedOrServiceAccount(Permissions.DocumentView, keycloakOptions);

if(!_memoryCache.TryGetValue(CacheKeys.MayanDocumentTypes, out ExternalResponse<QueryResponse<Models.Mayan.Document.DocumentTypeModel>> cachedDocumentTypesResult))
if(_mayanConfig.CacheDocumentTypes)
{
ExternalResponse<QueryResponse<Models.Mayan.Document.DocumentTypeModel>> result = await documentStorageRepository.TryGetDocumentTypesAsync(ordering, page, pageSize);
if (result.Status != ExternalResponseStatus.Success)
if (!_memoryCache.TryGetValue(CacheKeys.MayanDocumentTypes, out ExternalResponse<QueryResponse<Models.Mayan.Document.DocumentTypeModel>> cachedDocumentTypesResult) || !_mayanConfig.CacheDocumentTypes)
{
throw GetMayanResponseError(result.Message);
}
ExternalResponse<QueryResponse<Models.Mayan.Document.DocumentTypeModel>> result = await documentStorageRepository.TryGetDocumentTypesAsync(ordering, page, pageSize);
if (result.Status != ExternalResponseStatus.Success)
{
throw GetMayanResponseError(result.Message);
}

MemoryCacheEntryOptions cacheOptions = new();
cacheOptions.SetSlidingExpiration(TimeSpan.FromHours(2));
_memoryCache.Set(CacheKeys.MayanDocumentTypes, result);
}

_memoryCache.Set(CacheKeys.MayanDocumentTypes, result, cacheOptions);
return cachedDocumentTypesResult;
}

return cachedDocumentTypesResult;
return await documentStorageRepository.TryGetDocumentTypesAsync(ordering, page, pageSize);
}

public async Task<ExternalResponse<QueryResponse<DocumentDetailModel>>> GetStorageDocumentList(string ordering = "", int? page = null, int? pageSize = null)
Expand Down Expand Up @@ -597,7 +605,7 @@ public async Task<ExternalResponse<QueryResponse<FilePageModel>>> GetDocumentFil
this.Logger.LogInformation("Retrieving pages for document: {documentId} file: {documentFileId}", documentId, documentFileId);
this.User.ThrowIfNotAuthorized(Permissions.DocumentView);

var result = await documentStorageRepository.TryGetFilePageListAsync(documentId, documentFileId, _config.PreviewPages, 1);
var result = await documentStorageRepository.TryGetFilePageListAsync(documentId, documentFileId, _mayanConfig.PreviewPages, 1);
if (result.Status != ExternalResponseStatus.Success)
{
throw GetMayanResponseError(result.Message);
Expand All @@ -612,7 +620,7 @@ public async Task<HttpResponseMessage> DownloadFilePageImageAsync(long mayanDocu

var retryPolicy = Policy<HttpResponseMessage>
.HandleResult(result => result?.StatusCode != HttpStatusCode.OK || result?.Content == null)
.WaitAndRetryAsync(_config.ImageRetries, (int retry) => TimeSpan.FromSeconds(Math.Pow(2, retry)));
.WaitAndRetryAsync(_mayanConfig.ImageRetries, (int retry) => TimeSpan.FromSeconds(Math.Pow(2, retry)));

var result = await retryPolicy.ExecuteAsync(async () => await documentStorageRepository.TryGetFilePageImage(mayanDocumentId, mayanFileId, mayanFilePageId));

Expand All @@ -623,21 +631,15 @@ public async Task<HttpResponseMessage> DownloadFilePageImageAsync(long mayanDocu
return result;
}

public static bool IsValidDocumentExtension(string fileName)
{
var fileNameExtension = Path.GetExtension(fileName).Replace(".", string.Empty).ToLower();
return ValidExtensions.Contains(fileNameExtension);
}

private async Task PrecacheDocumentPreviews(long documentId, long documentFileId)
{
this.Logger.LogInformation("Precaching the first {_config.PreviewPages} pages in document {documentId}, file {documentFileId}", _config.PreviewPages, documentId, documentFileId);
this.Logger.LogInformation("Precaching the first {_config.PreviewPages} pages in document {documentId}, file {documentFileId}", _mayanConfig.PreviewPages, documentId, documentFileId);

// Note that Mayan must generate a list of pages from an uploaded file, so retry if that is not available right away.
var retryPolicy = Policy<ExternalResponse<QueryResponse<FilePageModel>>>
.HandleResult(result => result.HttpStatusCode != HttpStatusCode.OK || result?.Payload?.Results?.Any() == false)
.WaitAndRetryAsync(_config.UploadRetries, (int retry) => TimeSpan.FromSeconds(Math.Pow(2, retry)));
var pages = await retryPolicy.ExecuteAsync(async () => await documentStorageRepository.TryGetFilePageListAsync(documentId, documentFileId, _config.PreviewPages, 1));
.WaitAndRetryAsync(_mayanConfig.UploadRetries, (int retry) => TimeSpan.FromSeconds(Math.Pow(2, retry)));
var pages = await retryPolicy.ExecuteAsync(async () => await documentStorageRepository.TryGetFilePageListAsync(documentId, documentFileId, _mayanConfig.PreviewPages, 1));

if (pages?.Payload?.Results != null)
{
Expand Down Expand Up @@ -744,7 +746,7 @@ private async Task<List<ExternalResponse<DocumentMetadataModel>>> DeleteMetadata

private MayanRepositoryException GetMayanResponseError(string errorMesage)
{
if (_config.ExposeErrors)
if (_mayanConfig.ExposeErrors)
{
return new MayanRepositoryException(errorMesage);
}
Expand Down
3 changes: 2 additions & 1 deletion source/backend/api/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
"Authority": "https://dev.loginproxy.gov.bc.ca/auth/realms/standard"
},
"Mayan": {
"ExposeErrors": true
"ExposeErrors": true,
"CacheDocumentTypes": true
},
"Keycloak": {
"Authority": "https://dev.loginproxy.gov.bc.ca/auth/realms/standard",
Expand Down
3 changes: 2 additions & 1 deletion source/backend/api/appsettings.Docker.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"BaseUri": "http://mayan-app:8000/api/v4",
"ConnectionUser": "admin",
"ConnectionPassword": "",
"ExposeErrors": "true"
"ExposeErrors": "true",
"CacheDocumentTypes": false
}
}
3 changes: 2 additions & 1 deletion source/backend/api/appsettings.Local.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"BaseUri": "",
"ConnectionUser": "admin",
"ConnectionPassword": "",
"ExposeErrors": "true"
"ExposeErrors": "true",
"CacheDocumentTypes": true
}
}
3 changes: 2 additions & 1 deletion source/backend/api/appsettings.Test.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
}
},
"Mayan": {
"ExposeErrors": true
"ExposeErrors": true,
"CacheDocumentTypes": false
},
"Pims": {
"Environment": {
Expand Down
3 changes: 2 additions & 1 deletion source/backend/api/appsettings.Uat.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
}
},
"Mayan": {
"ExposeErrors": false
"ExposeErrors": false,
"CacheDocumentTypes": false
},
"Pims": {
"Environment": {
Expand Down
1 change: 1 addition & 0 deletions source/backend/api/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
"ConnectionUser": "[MAYAN_USER]",
"ConnectionPassword": "[MAYAN_USER_PASSWORD]",
"ExposeErrors": false,
"CacheDocumentTypes": false,
"UploadRetries": 4,
"ImageRetries": 2,
"PreviewPages": 10
Expand Down

0 comments on commit e56a3fb

Please sign in to comment.