From 613dc409e8bfcbbd457609309e32283695f2d6cc Mon Sep 17 00:00:00 2001 From: Enkidu93 Date: Mon, 2 Dec 2024 11:11:01 -0500 Subject: [PATCH] Address reviewer comments --- .../UnitializedEntityCleanupService.cs | 8 ++--- .../IMongoDataAccessConfiguratorExtensions.cs | 14 ++++++-- .../Services/BuildService.cs | 11 +++++++ .../Services/EngineService.cs | 33 +++++++++++++++---- 4 files changed, 53 insertions(+), 13 deletions(-) diff --git a/src/Serval/src/Serval.Shared/Services/UnitializedEntityCleanupService.cs b/src/Serval/src/Serval.Shared/Services/UnitializedEntityCleanupService.cs index a44e5664..2dc54e93 100644 --- a/src/Serval/src/Serval.Shared/Services/UnitializedEntityCleanupService.cs +++ b/src/Serval/src/Serval.Shared/Services/UnitializedEntityCleanupService.cs @@ -26,10 +26,10 @@ public async Task CheckEntitiesAsync(IRepository entities, CancellationToken var now = DateTime.UtcNow; IEnumerable uninitializedEntities = await entities.GetAllAsync( e => - e.IsInitialized != null - && !e.IsInitialized.Value - && e.DateCreated != null - && (now - e.DateCreated) > _timeout, + e.DateCreated != null + && e.DateCreated < now - _timeout + && e.IsInitialized != null + && !e.IsInitialized.Value, cancellationToken ); diff --git a/src/Serval/src/Serval.Translation/Configuration/IMongoDataAccessConfiguratorExtensions.cs b/src/Serval/src/Serval.Translation/Configuration/IMongoDataAccessConfiguratorExtensions.cs index ea016c0e..d0092c0b 100644 --- a/src/Serval/src/Serval.Translation/Configuration/IMongoDataAccessConfiguratorExtensions.cs +++ b/src/Serval/src/Serval.Translation/Configuration/IMongoDataAccessConfiguratorExtensions.cs @@ -15,14 +15,22 @@ this IMongoDataAccessConfigurator configurator await c.Indexes.CreateOrUpdateAsync( new CreateIndexModel(Builders.IndexKeys.Ascending(e => e.Owner)) ); + await c.Indexes.CreateOrUpdateAsync( + new CreateIndexModel(Builders.IndexKeys.Ascending(e => e.DateCreated)) + ); } ); configurator.AddRepository( "translation.builds", - init: c => - c.Indexes.CreateOrUpdateAsync( + init: async c => + { + await c.Indexes.CreateOrUpdateAsync( new CreateIndexModel(Builders.IndexKeys.Ascending(b => b.EngineRef)) - ) + ); + await c.Indexes.CreateOrUpdateAsync( + new CreateIndexModel(Builders.IndexKeys.Ascending(b => b.DateCreated)) + ); + } ); configurator.AddRepository( "translation.pretranslations", diff --git a/src/Serval/src/Serval.Translation/Services/BuildService.cs b/src/Serval/src/Serval.Translation/Services/BuildService.cs index c2cfbe1f..85b50909 100644 --- a/src/Serval/src/Serval.Translation/Services/BuildService.cs +++ b/src/Serval/src/Serval.Translation/Services/BuildService.cs @@ -10,6 +10,17 @@ public async Task> GetAllAsync(string parentId, CancellationT ); } + public override async Task GetAsync(string id, CancellationToken cancellationToken = default) + { + Build? build = await Entities.GetAsync( + e => e.Id == id && (e.IsInitialized == null || e.IsInitialized.Value), + cancellationToken + ); + if (build == null) + throw new EntityNotFoundException($"Could not find the {typeof(Build).Name} '{id}'."); + return build; + } + public Task GetActiveAsync(string parentId, CancellationToken cancellationToken = default) { return Entities.GetAsync( diff --git a/src/Serval/src/Serval.Translation/Services/EngineService.cs b/src/Serval/src/Serval.Translation/Services/EngineService.cs index b18257e4..5ecafbd7 100644 --- a/src/Serval/src/Serval.Translation/Services/EngineService.cs +++ b/src/Serval/src/Serval.Translation/Services/EngineService.cs @@ -32,6 +32,17 @@ public override async Task GetAsync(string id, CancellationToken cancell return engine; } + public override async Task> GetAllAsync( + string id, + CancellationToken cancellationToken = default + ) + { + return await Entities.GetAllAsync( + e => e.Id == id && (e.IsInitialized == null || e.IsInitialized.Value), + cancellationToken + ); + } + public async Task TranslateAsync( string engineId, string segment, @@ -397,7 +408,11 @@ public async Task GetModelDownloadUrlAsync( public Task AddCorpusAsync(string engineId, Models.Corpus corpus, CancellationToken cancellationToken = default) { - return Entities.UpdateAsync(engineId, u => u.Add(e => e.Corpora, corpus), cancellationToken: cancellationToken); + return Entities.UpdateAsync( + e => e.Id == engineId && (e.IsInitialized == null || e.IsInitialized.Value), + u => u.Add(e => e.Corpora, corpus), + cancellationToken: cancellationToken + ); } public async Task UpdateCorpusAsync( @@ -409,7 +424,10 @@ public Task AddCorpusAsync(string engineId, Models.Corpus corpus, CancellationTo ) { Engine? engine = await Entities.UpdateAsync( - e => e.Id == engineId && e.Corpora.Any(c => c.Id == corpusId), + e => + e.Id == engineId + && (e.IsInitialized == null || e.IsInitialized.Value) + && e.Corpora.Any(c => c.Id == corpusId), u => { if (sourceFiles is not null) @@ -436,7 +454,7 @@ await _dataAccessContext.WithTransactionAsync( async (ct) => { originalEngine = await Entities.UpdateAsync( - engineId, + e => e.Id == engineId && (e.IsInitialized == null || e.IsInitialized.Value), u => u.RemoveAll(e => e.Corpora, c => c.Id == corpusId), returnOriginal: true, cancellationToken: ct @@ -471,7 +489,7 @@ public Task AddParallelCorpusAsync( ) { return Entities.UpdateAsync( - engineId, + e => e.Id == engineId && (e.IsInitialized == null || e.IsInitialized.Value), u => u.Add(e => e.ParallelCorpora, corpus), cancellationToken: cancellationToken ); @@ -486,7 +504,10 @@ public Task AddParallelCorpusAsync( ) { Engine? engine = await Entities.UpdateAsync( - e => e.Id == engineId && e.ParallelCorpora.Any(c => c.Id == parallelCorpusId), + e => + e.Id == engineId + && (e.IsInitialized == null || e.IsInitialized.Value) + && e.ParallelCorpora.Any(c => c.Id == parallelCorpusId), u => { if (sourceCorpora is not null) @@ -517,7 +538,7 @@ await _dataAccessContext.WithTransactionAsync( async (ct) => { originalEngine = await Entities.UpdateAsync( - engineId, + e => e.Id == engineId && (e.IsInitialized == null || e.IsInitialized.Value), u => u.RemoveAll(e => e.ParallelCorpora, c => c.Id == parallelCorpusId), returnOriginal: true, cancellationToken: ct