Skip to content

Commit c5f4d23

Browse files
committed
Properly populate parallel corpus analyses; add migration (untested)
1 parent 641da91 commit c5f4d23

9 files changed

Lines changed: 56 additions & 48 deletions

File tree

src/Serval/src/Serval.Client/Client.g.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10791,9 +10791,8 @@ public partial class TranslationBuild
1079110791
[System.Obsolete]
1079210792
public System.Collections.Generic.IList<ParallelCorpusAnalysis>? Analysis { get; set; } = default!;
1079310793

10794-
[Newtonsoft.Json.JsonProperty("targetQuoteConvention", Required = Newtonsoft.Json.Required.Always)]
10795-
[System.ComponentModel.DataAnnotations.Required(AllowEmptyStrings = true)]
10796-
public string TargetQuoteConvention { get; set; } = default!;
10794+
[Newtonsoft.Json.JsonProperty("targetQuoteConvention", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
10795+
public string? TargetQuoteConvention { get; set; } = default!;
1079710796

1079810797
[Newtonsoft.Json.JsonProperty("canDenormalizeQuotes", Required = Newtonsoft.Json.Required.Always)]
1079910798
public bool CanDenormalizeQuotes { get; set; } = default!;

src/Serval/src/Serval.Translation/Configuration/IMongoDataAccessConfiguratorExtensions.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,21 @@ await c.Aggregate()
6161
.AppendStage<BsonDocument>(new BsonDocument("$unset", "engine"))
6262
.Merge(c, new MergeStageOptions<Build> { WhenMatched = MergeStageWhenMatched.Replace })
6363
.ToListAsync();
64+
65+
//migrate by adding TargetQuoteConvention field populated from deprecated analysis field
66+
// await c.Aggregate()
67+
// .Match(Builders<Build>.Filter.Exists(b => b.TargetQuoteConvention, false))
68+
// .Match(Builders<Build>.Filter.Exists("analysis"))
69+
// .Unwind("analysis")
70+
// .Match(Builders<BsonDocument>.Filter.Ne("$analysis.targetQuoteConvention", ""))
71+
// .AppendStage<BsonDocument>(
72+
// new BsonDocument(
73+
// "$set",
74+
// new BsonDocument("targetQuoteConvention", "$analysis.targetQuoteConvention")
75+
// )
76+
// )
77+
// .Merge(c, new MergeStageOptions<Build> { WhenMatched = MergeStageWhenMatched.Replace })
78+
// .ToListAsync();
6479
}
6580
);
6681
configurator.AddRepository<Pretranslation>(

src/Serval/src/Serval.Translation/Contracts/TranslationBuildDto.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ public record TranslationBuildDto
3939

4040
[Obsolete]
4141
public IReadOnlyList<ParallelCorpusAnalysisDto>? Analysis { get; init; }
42-
public required string TargetQuoteConvention { get; init; }
43-
public required bool CanDenormalizeQuotes { get; init; }
42+
public string? TargetQuoteConvention { get; init; }
43+
public bool CanDenormalizeQuotes { get; init; }
4444
}

src/Serval/src/Serval.Translation/Controllers/TranslationBuildsController.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ namespace Serval.Translation.Controllers;
66
public class TranslationBuildsController(
77
IAuthorizationService authService,
88
IBuildService buildService,
9-
IUrlService urlService
9+
IUrlService urlService,
10+
IEngineService engineService
1011
) : TranslationControllerBase(authService, urlService)
1112
{
1213
private readonly IBuildService _buildService = buildService;
14+
private readonly IEngineService _engineService = engineService;
1315

1416
/// <summary>
1517
/// Get all builds for your translation engines that are created after the specified date.
@@ -31,13 +33,17 @@ public async Task<IEnumerable<TranslationBuildDto>> GetAllBuildsCreatedAfterAsyn
3133
CancellationToken cancellationToken
3234
)
3335
{
36+
IEnumerable<Build> builds;
3437
if (createdAfter is null)
3538
{
36-
return (await _buildService.GetAllAsync(Owner, cancellationToken)).Select(Map);
39+
builds = await _buildService.GetAllAsync(Owner, cancellationToken);
3740
}
3841
else
3942
{
40-
return (await _buildService.GetAllCreatedAfterAsync(Owner, createdAfter, cancellationToken)).Select(Map);
43+
builds = await _buildService.GetAllCreatedAfterAsync(Owner, createdAfter, cancellationToken);
4144
}
45+
return await Task.WhenAll(
46+
builds.Select(async b => Map(b, await _engineService.GetAsync(b.EngineRef, cancellationToken)))
47+
);
4248
}
4349
}

src/Serval/src/Serval.Translation/Controllers/TranslationControllerBase.cs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,10 @@ public abstract class TranslationControllerBase(IAuthorizationService authServic
77
{
88
private readonly IUrlService _urlService = urlService;
99

10-
protected TranslationBuildDto Map(Build source)
10+
protected TranslationBuildDto Map(Build source, Engine engine)
1111
{
12-
string targetQuoteConvention = "";
13-
if (source.TargetQuoteConvention != null)
14-
{
15-
targetQuoteConvention = source.TargetQuoteConvention;
16-
}
17-
else if (source.Analysis?.Any(a => a.TargetQuoteConvention != "") ?? false)
18-
{
19-
targetQuoteConvention = source.Analysis.First(a => a.TargetQuoteConvention != "").TargetQuoteConvention;
20-
}
12+
string targetQuoteConvention = source.TargetQuoteConvention ?? "";
13+
2114
return new TranslationBuildDto
2215
{
2316
Id = source.Id,
@@ -45,7 +38,7 @@ protected TranslationBuildDto Map(Build source)
4538
DeploymentVersion = source.DeploymentVersion,
4639
ExecutionData = Map(source.ExecutionData),
4740
Phases = source.Phases?.Select(Map).ToList(),
48-
Analysis = source.Analysis?.Select(a => Map(a, targetQuoteConvention)).ToList(),
41+
Analysis = Map(engine, targetQuoteConvention).ToList(),
4942
TargetQuoteConvention = targetQuoteConvention,
5043
CanDenormalizeQuotes = targetQuoteConvention != ""
5144
};
@@ -133,14 +126,14 @@ private static PhaseDto Map(BuildPhase source) =>
133126
Started = source.Started,
134127
};
135128

136-
private static ParallelCorpusAnalysisDto Map(ParallelCorpusAnalysis source, string targetQuoteConvention) =>
137-
new ParallelCorpusAnalysisDto
129+
private static IEnumerable<ParallelCorpusAnalysisDto> Map(Engine engine, string targetQuoteConvention) =>
130+
engine.ParallelCorpora.Select(pc => new ParallelCorpusAnalysisDto
138131
{
139-
ParallelCorpusRef = source.ParallelCorpusRef,
132+
ParallelCorpusRef = pc.Id,
140133
TargetQuoteConvention = targetQuoteConvention,
141134
SourceQuoteConvention = "ignore",
142135
CanDenormalizeQuotes = targetQuoteConvention != ""
143-
};
136+
});
144137

145138
private static ExecutionDataDto Map(ExecutionData source) =>
146139
new ExecutionDataDto

src/Serval/src/Serval.Translation/Controllers/TranslationEnginesController.cs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,7 +1259,13 @@ CancellationToken cancellationToken
12591259
)
12601260
{
12611261
await AuthorizeAsync(id, cancellationToken);
1262-
return Ok((await _buildService.GetAllAsync(Owner, id, cancellationToken)).Select(Map));
1262+
return Ok(
1263+
await Task.WhenAll(
1264+
(await _buildService.GetAllAsync(Owner, id, cancellationToken)).Select(async b =>
1265+
Map(b, await _engineService.GetAsync(b.EngineRef, cancellationToken))
1266+
)
1267+
)
1268+
);
12631269
}
12641270

12651271
/// <summary>
@@ -1314,13 +1320,16 @@ CancellationToken cancellationToken
13141320
{
13151321
EntityChangeType.None => StatusCode(StatusCodes.Status408RequestTimeout),
13161322
EntityChangeType.Delete => NotFound(),
1317-
_ => Ok(Map(change.Entity!)),
1323+
_
1324+
=> Ok(
1325+
Map(change.Entity!, await _engineService.GetAsync(change.Entity!.EngineRef, cancellationToken))
1326+
),
13181327
};
13191328
}
13201329
else
13211330
{
13221331
Build build = await _buildService.GetAsync(buildId, cancellationToken);
1323-
return Ok(Map(build));
1332+
return Ok(Map(build, await _engineService.GetAsync(build.EngineRef, cancellationToken)));
13241333
}
13251334
}
13261335

@@ -1394,7 +1403,7 @@ CancellationToken cancellationToken
13941403
if (!await _engineService.StartBuildAsync(build, cancellationToken))
13951404
return Conflict();
13961405

1397-
TranslationBuildDto dto = Map(build);
1406+
TranslationBuildDto dto = Map(build, await _engineService.GetAsync(build.EngineRef, cancellationToken));
13981407
return Created(dto.Url, dto);
13991408
}
14001409

@@ -1445,7 +1454,10 @@ CancellationToken cancellationToken
14451454
{
14461455
EntityChangeType.None => StatusCode(StatusCodes.Status408RequestTimeout),
14471456
EntityChangeType.Delete => NoContent(),
1448-
_ => Ok(Map(change.Entity!)),
1457+
_
1458+
=> Ok(
1459+
Map(change.Entity!, await _engineService.GetAsync(change.Entity!.EngineRef, cancellationToken))
1460+
),
14491461
};
14501462
}
14511463
else
@@ -1454,7 +1466,7 @@ CancellationToken cancellationToken
14541466
if (build == null)
14551467
return NoContent();
14561468

1457-
return Ok(Map(build));
1469+
return Ok(Map(build, await _engineService.GetAsync(build.EngineRef, cancellationToken)));
14581470
}
14591471
}
14601472

@@ -1490,7 +1502,7 @@ CancellationToken cancellationToken
14901502
Build? build = await _engineService.CancelBuildAsync(id, cancellationToken);
14911503
if (build is null)
14921504
return NoContent();
1493-
return Ok(Map(build));
1505+
return Ok(Map(build, await _engineService.GetAsync(build.EngineRef, cancellationToken)));
14941506
}
14951507

14961508
/// <summary>

src/Serval/src/Serval.Translation/Models/Build.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,5 @@ public record Build : IOwnedEntity
2222
public DateTime? DateStarted { get; set; }
2323
public DateTime? DateCompleted { get; set; }
2424
public IReadOnlyList<BuildPhase>? Phases { get; init; }
25-
public IReadOnlyList<ParallelCorpusAnalysis>? Analysis { get; init; }
2625
public string? TargetQuoteConvention { get; init; }
2726
}

src/Serval/test/Serval.Translation.Tests/Services/PlatformServiceTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ public async Task UpdateTargetQuoteConventionAsync_NoEngine()
232232

233233
build = await env.Builds.GetAsync(c => c.Id == build.Id);
234234

235-
Assert.That(build?.Analysis, Is.Null);
235+
Assert.That(build?.TargetQuoteConvention, Is.EqualTo(""));
236236
}
237237

238238
[Test]

src/Serval/test/Serval.Translation.Tests/Services/PretranslationServiceTests.cs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -524,14 +524,6 @@ public TestEnvironment()
524524
EngineRef = "engine1",
525525
Owner = "owner1",
526526
DateFinished = DateTime.UnixEpoch,
527-
Analysis =
528-
[
529-
new ParallelCorpusAnalysis()
530-
{
531-
ParallelCorpusRef = "corpus1",
532-
TargetQuoteConvention = "standard_english"
533-
}
534-
],
535527
TargetQuoteConvention = "standard_english"
536528
},
537529
new()
@@ -540,14 +532,6 @@ public TestEnvironment()
540532
EngineRef = "parallel_engine1",
541533
Owner = "owner1",
542534
DateFinished = DateTime.UnixEpoch,
543-
Analysis =
544-
[
545-
new ParallelCorpusAnalysis()
546-
{
547-
ParallelCorpusRef = "parallel_corpus1",
548-
TargetQuoteConvention = "standard_english"
549-
}
550-
],
551535
TargetQuoteConvention = "standard_english"
552536
}
553537
]

0 commit comments

Comments
 (0)