Skip to content

Commit

Permalink
Add datafile and corpus update consumers
Browse files Browse the repository at this point in the history
Ensure that not all filenames change in testing
  • Loading branch information
johnml1135 committed Dec 3, 2024
1 parent 01693e3 commit f153cb8
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ this IMediatorRegistrationConfigurator configurator
)
{
configurator.AddConsumer<DataFileDeletedConsumer>();
configurator.AddConsumer<DataFileUpdatedConsumer>();
configurator.AddConsumer<CorpusUpdatedConsumer>();
return configurator;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace Serval.Translation.Consumers;

public class CorpusUpdatedConsumer(IEngineService engineService) : IConsumer<CorpusUpdated>
{
private readonly IEngineService _engineService = engineService;

public async Task Consume(ConsumeContext<CorpusUpdated> context)
{
await _engineService.UpdateCorpusFilesAsync(
context.Message.CorpusId,
context.Message.Files.Select(Map).ToList(),
context.CancellationToken
);
}

private static CorpusFile Map(CorpusFileResult corpusFile)
{
return new CorpusFile
{
Id = corpusFile.File.DataFileId,
TextId = corpusFile.TextId,
Filename = corpusFile.File.Filename,
Format = corpusFile.File.Format,
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Serval.Translation.Consumers;

public class DataFileUpdatedConsumer(IEngineService engineService) : IConsumer<DataFileUpdated>
{
private readonly IEngineService _engineService = engineService;

public async Task Consume(ConsumeContext<DataFileUpdated> context)
{
await _engineService.UpdateDataFileFilenameFilesAsync(
context.Message.DataFileId,
context.Message.Filename,
context.CancellationToken
);
}
}
50 changes: 41 additions & 9 deletions src/Serval/src/Serval.Translation/Services/EngineService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -545,30 +545,62 @@ public Task DeleteAllCorpusFilesAsync(string dataFileId, CancellationToken cance
);
}

public Task UpdateAllCorpusFilesAsync(
DataFileUpdated dataFileUpdated,
public Task UpdateDataFileFilenameFilesAsync(
string dataFileId,
string filename,
CancellationToken cancellationToken = default
)
{
return Entities.UpdateAllAsync(
e =>
e.Corpora.Any(c =>
c.SourceFiles.Any(f => f.Id == dataFileUpdated.DataFileId)
|| c.TargetFiles.Any(f => f.Id == dataFileUpdated.DataFileId)
c.SourceFiles.Any(f => f.Id == dataFileId) || c.TargetFiles.Any(f => f.Id == dataFileId)
)
|| e.ParallelCorpora.Any(c =>
c.SourceCorpora.Any(mc => mc.Files.Any(f => f.Id == dataFileId))
|| c.TargetCorpora.Any(mc => mc.Files.Any(f => f.Id == dataFileId))
),
u =>
u.Set(
e => e.Corpora[ArrayPosition.All].SourceFiles[ArrayPosition.All].Filename,
dataFileUpdated.Filename
u.Set(e => e.Corpora[ArrayPosition.All].SourceFiles[ArrayPosition.All].Filename, filename)
.Set(e => e.Corpora[ArrayPosition.All].TargetFiles[ArrayPosition.All].Filename, filename)
.Set(
e =>
e.ParallelCorpora[ArrayPosition.All]
.SourceCorpora[ArrayPosition.All]
.Files[ArrayPosition.All]
.Filename,
filename
)
.Set(
e => e.Corpora[ArrayPosition.All].TargetFiles[ArrayPosition.All].Filename,
dataFileUpdated.Filename
e =>
e.ParallelCorpora[ArrayPosition.All]
.TargetCorpora[ArrayPosition.All]
.Files[ArrayPosition.All]
.Filename,
filename
),
cancellationToken
);
}

public Task UpdateCorpusFilesAsync(
string corpusId,
IReadOnlyList<Models.CorpusFile> files,
CancellationToken cancellationToken = default
)
{
return Entities.UpdateAllAsync(
e =>
e.ParallelCorpora.Any(c =>
c.SourceCorpora.Any(mc => mc.Id == corpusId) || c.TargetCorpora.Any(mc => mc.Id == corpusId)
),
u =>
u.Set(e => e.ParallelCorpora[ArrayPosition.All].SourceCorpora[ArrayPosition.All].Files, files)
.Set(e => e.ParallelCorpora[ArrayPosition.All].TargetCorpora[ArrayPosition.All].Files, files),
cancellationToken
);
}

public async Task<Queue> GetQueueAsync(string engineType, CancellationToken cancellationToken = default)
{
TranslationEngineApi.TranslationEngineApiClient client =
Expand Down
12 changes: 11 additions & 1 deletion src/Serval/src/Serval.Translation/Services/IEngineService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,17 @@ Task DeleteParallelCorpusAsync(

Task DeleteAllCorpusFilesAsync(string dataFileId, CancellationToken cancellationToken = default);

Task UpdateAllCorpusFilesAsync(DataFileUpdated dataFileUpdated, CancellationToken cancellationToken = default);
Task UpdateDataFileFilenameFilesAsync(
string dataFileId,
string filename,
CancellationToken cancellationToken = default
);

Task UpdateCorpusFilesAsync(
string corpusId,
IReadOnlyList<CorpusFile> files,
CancellationToken cancellationToken = default
);

Task<Queue> GetQueueAsync(string engineType, CancellationToken cancellationToken = default);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2063,12 +2063,14 @@ await corporaClient.UpdateAsync(

Engine newEngine1 = (await _env.Engines.GetAsync(ECHO_ENGINE1_ID))!;
Assert.That(newEngine1.Corpora[0].SourceFiles[0].Filename, Is.EqualTo(newFileFromRepo.Filename));
Assert.That(newEngine1.Corpora[0].TargetFiles[0].Filename, Is.EqualTo(FILE2_FILENAME));

Check failure on line 2066 in src/Serval/test/Serval.ApiServer.IntegrationTests/TranslationEngineTests.cs

View workflow job for this annotation

GitHub Actions / NUnit Tests

Serval.ApiServer.TranslationEngineTests ► DataFileUpdate_Propagated

Failed test found in: src/Serval/test/Serval.ApiServer.IntegrationTests/TestResults/test-results.trx Error: Assert.That(newEngine1.Corpora[0].TargetFiles[0].Filename, Is.EqualTo(FILE2_FILENAME)) Expected string length 6 but was 12. Strings differ at index 1. Expected: "file_b" But was: "f0qltkp3.r1e" ------------^
Raw output
  Assert.That(newEngine1.Corpora[0].TargetFiles[0].Filename, Is.EqualTo(FILE2_FILENAME))
  Expected string length 6 but was 12. Strings differ at index 1.
  Expected: "file_b"
  But was:  "f0qltkp3.r1e"
  ------------^

   at Serval.ApiServer.TranslationEngineTests.DataFileUpdate_Propagated() in /home/runner/work/serval/serval/src/Serval/test/Serval.ApiServer.IntegrationTests/TranslationEngineTests.cs:line 2066
   at NUnit.Framework.Internal.AsyncToSyncAdapter.Await[TResult](Func`1 invoke)
   at NUnit.Framework.Internal.Commands.TestMethodCommand.RunTestMethod(TestExecutionContext context)
   at NUnit.Framework.Internal.Commands.TestMethodCommand.Execute(TestExecutionContext context)
   at NUnit.Framework.Internal.Commands.BeforeAndAfterTestCommand.<>c__DisplayClass1_0.<Execute>b__0()
   at NUnit.Framework.Internal.Commands.DelegatingTestCommand.RunTestMethodInThreadAbortSafeZone(TestExecutionContext context, Action action)

1)    at Serval.ApiServer.TranslationEngineTests.DataFileUpdate_Propagated() in /home/runner/work/serval/serval/src/Serval/test/Serval.ApiServer.IntegrationTests/TranslationEngineTests.cs:line 2066


Engine newEngine2 = (await _env.Engines.GetAsync(ECHO_ENGINE2_ID))!;
Assert.That(
newEngine2.ParallelCorpora[0].SourceCorpora[0].Files[0].Filename,
Is.EqualTo(newFileFromRepo.Filename)
);
Assert.That(newEngine2.ParallelCorpora[0].TargetCorpora[0].Files[0].Filename, Is.EqualTo(FILE4_FILENAME));

DataFiles.Models.Corpus newCorpusFromRepo = (await _env.Corpora.GetAsync(TARGET_CORPUS_ID))!;
Assert.That(newCorpusFromRepo.Files[0].FileId, Is.EqualTo(FILE4_TRG_ZIP_ID));
Expand Down

0 comments on commit f153cb8

Please sign in to comment.