Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
29 changes: 21 additions & 8 deletions src/SIL.XForge.Scripture/Services/MachineApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1063,24 +1063,37 @@

try
{
// Get the last completed build
TranslationBuild? translationBuild = (
await translationEnginesClient.GetAllBuildsAsync(translationEngineId, cancellationToken)
)
// Get the last build and the last completed build
IList<TranslationBuild> translationBuilds = await translationEnginesClient.GetAllBuildsAsync(
translationEngineId,
cancellationToken
);
TranslationBuild? lastTranslationBuild = translationBuilds.LastOrDefault();
TranslationBuild? lastCompletedTranslationBuild = translationBuilds
.Where(b => b.State == JobState.Completed)
.MaxBy(b => b.DateFinished);
if (translationBuild is not null)

// See if the very last build has completed
if (lastTranslationBuild?.State == JobState.Completed)
{
// See if the current scripture range has changed
string currentScriptureRange = GetDraftedScriptureRange(translationBuild);
if (project.TranslateConfig.DraftConfig.CurrentScriptureRange != currentScriptureRange)
string currentScriptureRange = GetDraftedScriptureRange(lastTranslationBuild);
if (
!string.IsNullOrWhiteSpace(currentScriptureRange)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📌 That's interesting. What are you thinking with regard to currentScriptureRange possibly being null or whitespace?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These will be older builds that did not use the scripture range format.

&& project.TranslateConfig.DraftConfig.CurrentScriptureRange != currentScriptureRange
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📌 Actually, how would this be possible? If ExecuteWebhookAsync happens, and we mark a build as completed and run RetrievePreTranslationStatusAsync (from ExecuteWebhookAsync), wouldn't we have updated project.TranslateConfig.DraftConfig.CurrentScriptureRange from that completed build? Are we doing this because maybe we might not have heard back from a webhook, and so here we tidy up in case we had never heard back?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we doing this because maybe we might not have heard back from a webhook, and so here we tidy up in case we had never heard back?

Yes.

)
{
backgroundJobClient.Enqueue<IMachineApiService>(r =>
r.RetrievePreTranslationStatusAsync(sfProjectId, CancellationToken.None)
);
}

buildDto = CreateDto(translationBuild);
buildDto = CreateDto(lastTranslationBuild);
}
else if (lastCompletedTranslationBuild is not null)
{
// The very last build is active, so return the last completed build
buildDto = CreateDto(lastCompletedTranslationBuild);
}
}
catch (ServalApiException e)
Expand Down Expand Up @@ -2289,7 +2302,7 @@
parallelCorpusId is not null
&& translationBuild.Analysis?.FirstOrDefault(a =>
a.ParallelCorpusRef == parallelCorpusId
&& !string.IsNullOrEmpty(a.SourceQuoteConvention)

Check warning on line 2305 in src/SIL.XForge.Scripture/Services/MachineApiService.cs

View workflow job for this annotation

GitHub Actions / Production build and test (ubuntu-22.04, 8.0.x, 22.13.0, 10.9.2)

'ParallelCorpusAnalysis.SourceQuoteConvention' is obsolete

Check warning on line 2305 in src/SIL.XForge.Scripture/Services/MachineApiService.cs

View workflow job for this annotation

GitHub Actions / Production build and test (ubuntu-22.04, 8.0.x, 22.13.0, 10.9.2)

'ParallelCorpusAnalysis.SourceQuoteConvention' is obsolete

Check warning on line 2305 in src/SIL.XForge.Scripture/Services/MachineApiService.cs

View workflow job for this annotation

GitHub Actions / Build and test (ubuntu-22.04, 8.0.x, 22.13.0, 10.9.2)

'ParallelCorpusAnalysis.SourceQuoteConvention' is obsolete

Check warning on line 2305 in src/SIL.XForge.Scripture/Services/MachineApiService.cs

View workflow job for this annotation

GitHub Actions / Build and test (ubuntu-22.04, 8.0.x, 22.13.0, 10.9.2)

'ParallelCorpusAnalysis.SourceQuoteConvention' is obsolete

Check warning on line 2305 in src/SIL.XForge.Scripture/Services/MachineApiService.cs

View workflow job for this annotation

GitHub Actions / Analyze C# (8.0.x, 22.13.0, 10.9.2)

'ParallelCorpusAnalysis.SourceQuoteConvention' is obsolete

Check warning on line 2305 in src/SIL.XForge.Scripture/Services/MachineApiService.cs

View workflow job for this annotation

GitHub Actions / Analyze C# (8.0.x, 22.13.0, 10.9.2)

'ParallelCorpusAnalysis.SourceQuoteConvention' is obsolete

Check warning on line 2305 in src/SIL.XForge.Scripture/Services/MachineApiService.cs

View workflow job for this annotation

GitHub Actions / Prepare dotnet backend (ubuntu-22.04, 8.0.x, 22.13.0, 10.9.2)

'ParallelCorpusAnalysis.SourceQuoteConvention' is obsolete

Check warning on line 2305 in src/SIL.XForge.Scripture/Services/MachineApiService.cs

View workflow job for this annotation

GitHub Actions / Prepare dotnet backend (ubuntu-22.04, 8.0.x, 22.13.0, 10.9.2)

'ParallelCorpusAnalysis.SourceQuoteConvention' is obsolete
&& !string.IsNullOrEmpty(a.TargetQuoteConvention)
)
is not null
Expand Down
75 changes: 72 additions & 3 deletions test/SIL.XForge.Scripture.Tests/Services/MachineApiServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,7 @@
new ParallelCorpusAnalysis
{
ParallelCorpusRef = parallelCorpusId1,
SourceQuoteConvention = "standard_english",

Check warning on line 990 in test/SIL.XForge.Scripture.Tests/Services/MachineApiServiceTests.cs

View workflow job for this annotation

GitHub Actions / Production build and test (ubuntu-22.04, 8.0.x, 22.13.0, 10.9.2)

'ParallelCorpusAnalysis.SourceQuoteConvention' is obsolete

Check warning on line 990 in test/SIL.XForge.Scripture.Tests/Services/MachineApiServiceTests.cs

View workflow job for this annotation

GitHub Actions / Build and test (ubuntu-22.04, 8.0.x, 22.13.0, 10.9.2)

'ParallelCorpusAnalysis.SourceQuoteConvention' is obsolete

Check warning on line 990 in test/SIL.XForge.Scripture.Tests/Services/MachineApiServiceTests.cs

View workflow job for this annotation

GitHub Actions / Build and test (ubuntu-22.04, 8.0.x, 22.13.0, 10.9.2)

'ParallelCorpusAnalysis.SourceQuoteConvention' is obsolete

Check warning on line 990 in test/SIL.XForge.Scripture.Tests/Services/MachineApiServiceTests.cs

View workflow job for this annotation

GitHub Actions / Analyze C# (8.0.x, 22.13.0, 10.9.2)

'ParallelCorpusAnalysis.SourceQuoteConvention' is obsolete

Check warning on line 990 in test/SIL.XForge.Scripture.Tests/Services/MachineApiServiceTests.cs

View workflow job for this annotation

GitHub Actions / Analyze C# (8.0.x, 22.13.0, 10.9.2)

'ParallelCorpusAnalysis.SourceQuoteConvention' is obsolete

Check warning on line 990 in test/SIL.XForge.Scripture.Tests/Services/MachineApiServiceTests.cs

View workflow job for this annotation

GitHub Actions / Prepare dotnet backend (ubuntu-22.04, 8.0.x, 22.13.0, 10.9.2)

'ParallelCorpusAnalysis.SourceQuoteConvention' is obsolete

Check warning on line 990 in test/SIL.XForge.Scripture.Tests/Services/MachineApiServiceTests.cs

View workflow job for this annotation

GitHub Actions / Prepare dotnet backend (ubuntu-22.04, 8.0.x, 22.13.0, 10.9.2)

'ParallelCorpusAnalysis.SourceQuoteConvention' is obsolete
TargetQuoteConvention = "standard_english",
},
],
Expand Down Expand Up @@ -1272,7 +1272,7 @@
new ParallelCorpusAnalysis
{
ParallelCorpusRef = ParallelCorpusId01,
SourceQuoteConvention = "standard_english",

Check warning on line 1275 in test/SIL.XForge.Scripture.Tests/Services/MachineApiServiceTests.cs

View workflow job for this annotation

GitHub Actions / Production build and test (ubuntu-22.04, 8.0.x, 22.13.0, 10.9.2)

'ParallelCorpusAnalysis.SourceQuoteConvention' is obsolete

Check warning on line 1275 in test/SIL.XForge.Scripture.Tests/Services/MachineApiServiceTests.cs

View workflow job for this annotation

GitHub Actions / Build and test (ubuntu-22.04, 8.0.x, 22.13.0, 10.9.2)

'ParallelCorpusAnalysis.SourceQuoteConvention' is obsolete

Check warning on line 1275 in test/SIL.XForge.Scripture.Tests/Services/MachineApiServiceTests.cs

View workflow job for this annotation

GitHub Actions / Build and test (ubuntu-22.04, 8.0.x, 22.13.0, 10.9.2)

'ParallelCorpusAnalysis.SourceQuoteConvention' is obsolete

Check warning on line 1275 in test/SIL.XForge.Scripture.Tests/Services/MachineApiServiceTests.cs

View workflow job for this annotation

GitHub Actions / Analyze C# (8.0.x, 22.13.0, 10.9.2)

'ParallelCorpusAnalysis.SourceQuoteConvention' is obsolete

Check warning on line 1275 in test/SIL.XForge.Scripture.Tests/Services/MachineApiServiceTests.cs

View workflow job for this annotation

GitHub Actions / Analyze C# (8.0.x, 22.13.0, 10.9.2)

'ParallelCorpusAnalysis.SourceQuoteConvention' is obsolete

Check warning on line 1275 in test/SIL.XForge.Scripture.Tests/Services/MachineApiServiceTests.cs

View workflow job for this annotation

GitHub Actions / Prepare dotnet backend (ubuntu-22.04, 8.0.x, 22.13.0, 10.9.2)

'ParallelCorpusAnalysis.SourceQuoteConvention' is obsolete

Check warning on line 1275 in test/SIL.XForge.Scripture.Tests/Services/MachineApiServiceTests.cs

View workflow job for this annotation

GitHub Actions / Prepare dotnet backend (ubuntu-22.04, 8.0.x, 22.13.0, 10.9.2)

'ParallelCorpusAnalysis.SourceQuoteConvention' is obsolete
TargetQuoteConvention = "standard_english",
},
];
Expand Down Expand Up @@ -1938,7 +1938,7 @@
}

[Test]
public async Task GetLastCompletedPreTranslationBuildAsync_NoRetrievePreTranslationStatusAsyncCall_Success()
public async Task GetLastCompletedPreTranslationBuildAsync_NoRetrievePreTranslationStatusAsyncCallWhenActiveBuild_Success()
{
// Set up test environment
var env = new TestEnvironment();
Expand Down Expand Up @@ -1974,6 +1974,73 @@
},
],
},
new TranslationBuild
{
Url = "https://example.com",
Id = Build02,
Engine = new ResourceLink { Id = "engineId", Url = "https://example.com" },
State = JobState.Active,
},
]
)
);

// SUT
ServalBuildDto? actual = await env.Service.GetLastCompletedPreTranslationBuildAsync(
User01,
Project01,
false,
CancellationToken.None
);

// RetrievePreTranslationStatusAsync is run via a background job, so we verify that no new job was scheduled
env.BackgroundJobClient.DidNotReceive().Create(Arg.Any<Job>(), Arg.Any<IState>());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📌 Oh, yes. Good fixing this check.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


TestEnvironment.AssertCoreBuildProperties(CompletedTranslationBuild, actual);
}

[Test]
public async Task GetLastCompletedPreTranslationBuildAsync_NoRetrievePreTranslationStatusAsyncCallWhenAlreadyRun_Success()
{
// Set up test environment
var env = new TestEnvironment();
const string scriptureRange = "GEN";
await env.Projects.UpdateAsync(
Project01,
u => u.Set(p => p.TranslateConfig.DraftConfig.CurrentScriptureRange, scriptureRange)
);
const double percentCompleted = 0;
const int revision = 43;
const JobState state = JobState.Completed;
env.TranslationEnginesClient.GetAllBuildsAsync(TranslationEngine01, CancellationToken.None)
.Returns(
Task.FromResult<IList<TranslationBuild>>(
[
new TranslationBuild
{
Url = "https://example.com",
Id = Build01,
Engine = new ResourceLink { Id = "engineId", Url = "https://example.com" },
Message = MachineApiService.BuildStateCompleted,
Progress = percentCompleted,
Revision = revision,
State = state,
DateFinished = DateTimeOffset.UtcNow,
Pretranslate =
[
new PretranslateCorpus
{
SourceFilters =
[
new ParallelCorpusFilter
{
Corpus = new ResourceLink { Id = "corpusId", Url = "https://example.com" },
ScriptureRange = scriptureRange,
},
],
},
],
},
]
)
);
Expand All @@ -1986,7 +2053,8 @@
CancellationToken.None
);

await env.Service.DidNotReceive().RetrievePreTranslationStatusAsync(Project01, CancellationToken.None);
// RetrievePreTranslationStatusAsync is run via a background job, so we verify that no new job was scheduled
env.BackgroundJobClient.DidNotReceive().Create(Arg.Any<Job>(), Arg.Any<IState>());

TestEnvironment.AssertCoreBuildProperties(CompletedTranslationBuild, actual);
}
Expand Down Expand Up @@ -2040,7 +2108,8 @@
CancellationToken.None
);

await env.Service.DidNotReceive().RetrievePreTranslationStatusAsync(Project01, CancellationToken.None);
// RetrievePreTranslationStatusAsync is run via a background job, so we verify that no new job was scheduled
env.BackgroundJobClient.DidNotReceive().Create(Arg.Any<Job>(), Arg.Any<IState>());

TestEnvironment.AssertCoreBuildProperties(CompletedTranslationBuild, actual);
}
Expand Down
Loading