Skip to content

Commit

Permalink
Allow nested options - convert to BsonDocument but keep backwards com…
Browse files Browse the repository at this point in the history
…patible.
  • Loading branch information
johnml1135 committed Jan 25, 2024
1 parent 41348df commit dce8329
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net.Sockets;
using MongoDB.Bson;

namespace Serval.Translation.Controllers;

Expand Down Expand Up @@ -1075,12 +1076,7 @@ private static Build Map(Engine engine, TranslationBuildConfigDto source)
}
try
{
var jsonSerializerOptions = new JsonSerializerOptions();
jsonSerializerOptions.Converters.Add(new ObjectToInferredTypesConverter());
build.Options = JsonSerializer.Deserialize<IDictionary<string, object>>(
source.Options?.ToString() ?? "{}",
jsonSerializerOptions
);
build.OptionsDocument = BsonDocument.Parse(source.Options?.ToString() ?? "{}");
}
catch (Exception e)
{
Expand All @@ -1089,7 +1085,7 @@ private static Build Map(Engine engine, TranslationBuildConfigDto source)
return build;
}

private QueueDto Map(Queue source) => new() { Size = source.Size, EngineType = source.EngineType };
private static QueueDto Map(Queue source) => new() { Size = source.Size, EngineType = source.EngineType };

private TranslationEngineDto Map(Engine source)
{
Expand All @@ -1110,6 +1106,11 @@ private TranslationEngineDto Map(Engine source)

private TranslationBuildDto Map(Build source)
{
string options;
if (source.OptionsDocument is not null)
options = source.OptionsDocument.ToString();
else
options = JsonSerializer.Serialize(source.Options);
return new TranslationBuildDto
{
Id = source.Id,
Expand All @@ -1129,7 +1130,7 @@ private TranslationBuildDto Map(Build source)
QueueDepth = source.QueueDepth,
State = source.State,
DateFinished = source.DateFinished,
Options = source.Options
Options = options
};
}

Expand Down
1 change: 1 addition & 0 deletions src/Serval.Translation/Models/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ public class Build : IEntity
public JobState State { get; set; } = JobState.Pending;
public DateTime? DateFinished { get; set; }
public IDictionary<string, object>? Options { get; set; }
public BsonDocument? OptionsDocument { get; set; }
}
7 changes: 6 additions & 1 deletion src/Serval.Translation/Services/EngineService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,17 @@ public async Task<bool> StartBuildAsync(Build build, CancellationToken cancellat
Dictionary<string, PretranslateCorpus>? pretranslate = build.Pretranslate?.ToDictionary(c => c.CorpusRef);
Dictionary<string, TrainingCorpus>? trainOn = build.TrainOn?.ToDictionary(c => c.CorpusRef);
var client = _grpcClientFactory.CreateClient<TranslationEngineApi.TranslationEngineApiClient>(engine.Type);
string options;
if (build.OptionsDocument is not null)
options = build.OptionsDocument.ToString();
else
options = JsonSerializer.Serialize(build.Options);
var request = new StartBuildRequest
{
EngineType = engine.Type,
EngineId = engine.Id,
BuildId = build.Id,
Options = JsonSerializer.Serialize(build.Options),
Options = options,
Corpora =
{
engine.Corpora.Select(c =>
Expand Down
1 change: 1 addition & 0 deletions src/Serval.Translation/Usings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
global using Microsoft.Extensions.Configuration;
global using Microsoft.Extensions.Logging;
global using Microsoft.Extensions.Options;
global using MongoDB.Bson;
global using NSwag.Annotations;
global using Serval.Shared.Configuration;
global using Serval.Shared.Contracts;
Expand Down
63 changes: 27 additions & 36 deletions tests/Serval.ApiServer.IntegrationTests/TranslationEngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public async Task SetUp()
TargetLanguage = "en",
Type = "Echo",
Owner = "client1",
Corpora = new List<Translation.Models.Corpus>()
Corpora = []
};
var e1 = new Engine
{
Expand All @@ -74,7 +74,7 @@ public async Task SetUp()
TargetLanguage = "en",
Type = "Echo",
Owner = "client1",
Corpora = new List<Translation.Models.Corpus>()
Corpora = []
};
var e2 = new Engine
{
Expand All @@ -84,7 +84,7 @@ public async Task SetUp()
TargetLanguage = "en",
Type = "Echo",
Owner = "client2",
Corpora = new List<Translation.Models.Corpus>()
Corpora = []
};
var be0 = new Engine
{
Expand All @@ -94,7 +94,7 @@ public async Task SetUp()
TargetLanguage = "es",
Type = "SMTTransfer",
Owner = "client1",
Corpora = new List<Translation.Models.Corpus>()
Corpora = []
};
var ce0 = new Engine
{
Expand All @@ -104,7 +104,7 @@ public async Task SetUp()
TargetLanguage = "es",
Type = "Nmt",
Owner = "client1",
Corpora = new List<Translation.Models.Corpus>()
Corpora = []
};

await _env.Engines.InsertAllAsync(new[] { e0, e1, e2, be0, ce0 });
Expand Down Expand Up @@ -740,7 +740,7 @@ public async Task GetAllPretranslationsAsync_Exists()
CorpusRef = addedCorpus.Id,
TextId = "all",
EngineRef = ECHO_ENGINE1_ID,
Refs = new List<string> { "ref1", "ref2" },
Refs = ["ref1", "ref2"],
Translation = "translation",
ModelRevision = 1
};
Expand Down Expand Up @@ -799,7 +799,7 @@ public async Task GetAllPretranslationsAsync_TextIdExists()
CorpusRef = addedCorpus.Id,
TextId = "all",
EngineRef = ECHO_ENGINE1_ID,
Refs = new List<string> { "ref1", "ref2" },
Refs = ["ref1", "ref2"],
Translation = "translation",
ModelRevision = 1
};
Expand All @@ -825,7 +825,7 @@ public async Task GetAllPretranslationsAsync_TextIdDoesNotExist()
CorpusRef = addedCorpus.Id,
TextId = "all",
EngineRef = ECHO_ENGINE1_ID,
Refs = new List<string> { "ref1", "ref2" },
Refs = ["ref1", "ref2"],
Translation = "translation",
ModelRevision = 1
};
Expand Down Expand Up @@ -964,22 +964,21 @@ public async Task StartBuildForEngineByIdAsync(IEnumerable<string> scope, int ex
{
case 201:
TranslationCorpus addedCorpus = await client.AddCorpusAsync(engineId, TestCorpusConfig);
ptcc = new PretranslateCorpusConfig
{
CorpusId = addedCorpus.Id,
TextIds = new List<string> { "all" }
};
tcc = new()
{
CorpusId = addedCorpus.Id,
TextIds = new List<string> { "all" }
};
ptcc = new PretranslateCorpusConfig { CorpusId = addedCorpus.Id, TextIds = ["all"] };
tcc = new() { CorpusId = addedCorpus.Id, TextIds = ["all"] };
tbc = new TranslationBuildConfig
{
Pretranslate = new List<PretranslateCorpusConfig> { ptcc },
TrainOn = new List<TrainingCorpusConfig> { tcc },
Options =
"{\"max_steps\":10, \"use_key_terms\":false, \"some_double\":10.5, \"some_string\":\"string\"}"
Pretranslate = [ptcc],
TrainOn = [tcc],
Options = """
{"max_steps":10,
"use_key_terms":false,
"some_double":10.5,
"some_nested":{
"other_double":10.5
},
"some_string":"string"}
"""
};
TranslationBuild resultAfterStart;
Assert.ThrowsAsync<ServalApiException>(async () =>
Expand All @@ -996,12 +995,8 @@ public async Task StartBuildForEngineByIdAsync(IEnumerable<string> scope, int ex
case 400:
case 403:
case 404:
ptcc = new PretranslateCorpusConfig
{
CorpusId = "cccccccccccccccccccccccc",
TextIds = new List<string> { "all" }
};
tbc = new TranslationBuildConfig { Pretranslate = new List<PretranslateCorpusConfig> { ptcc } };
ptcc = new PretranslateCorpusConfig { CorpusId = "cccccccccccccccccccccccc", TextIds = ["all"] };
tbc = new TranslationBuildConfig { Pretranslate = [ptcc] };
var ex = Assert.ThrowsAsync<ServalApiException>(async () =>
{
await client.StartBuildAsync(engineId, tbc);
Expand Down Expand Up @@ -1112,12 +1107,8 @@ public async Task TryToQueueMultipleBuildsPerSingleUser()
var engineId = NMT_ENGINE1_ID;
var expectedStatusCode = 409;
TranslationCorpus addedCorpus = await client.AddCorpusAsync(engineId, TestCorpusConfigNonEcho);
var ptcc = new PretranslateCorpusConfig
{
CorpusId = addedCorpus.Id,
TextIds = new List<string> { "all" }
};
var tbc = new TranslationBuildConfig { Pretranslate = new List<PretranslateCorpusConfig> { ptcc } };
var ptcc = new PretranslateCorpusConfig { CorpusId = addedCorpus.Id, TextIds = ["all"] };
var tbc = new TranslationBuildConfig { Pretranslate = [ptcc] };
TranslationBuild build = await client.StartBuildAsync(engineId, tbc);
var ex = Assert.ThrowsAsync<ServalApiException>(async () =>
{
Expand Down Expand Up @@ -1174,7 +1165,7 @@ private static AsyncUnaryCall<TResponse> CreateAsyncUnaryCall<TResponse>(StatusC
Task.FromException<TResponse>(new RpcException(status)),
Task.FromResult(new Metadata()),
() => status,
() => new Metadata(),
() => [],
() => { }
);
}
Expand All @@ -1185,7 +1176,7 @@ private static AsyncUnaryCall<TResponse> CreateAsyncUnaryCall<TResponse>(TRespon
Task.FromResult(response),
Task.FromResult(new Metadata()),
() => Status.DefaultSuccess,
() => new Metadata(),
() => [],
() => { }
);
}
Expand Down

0 comments on commit dce8329

Please sign in to comment.