Skip to content

Commit

Permalink
Fixes from PR review
Browse files Browse the repository at this point in the history
  • Loading branch information
Enkidu93 committed Oct 12, 2023
1 parent b7f61e7 commit 25c7074
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 24 deletions.
18 changes: 12 additions & 6 deletions src/Serval.Client/Client.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ public partial interface ITranslationEnginesClient
/// <param name="engineType">A valid engine type: SmtTransfer, Nmt, or Echo</param>
/// <returns>The queue depth for the specified engine type</returns>
/// <exception cref="ServalApiException">A server side error occurred.</exception>
System.Threading.Tasks.Task<Queue> GetQueueDepthAsync(string engineType, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken));
System.Threading.Tasks.Task<Queue> GetQueueAsync(string engineType, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken));

/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <summary>
Expand Down Expand Up @@ -1521,21 +1521,24 @@ public string BaseUrl
/// <param name="engineType">A valid engine type: SmtTransfer, Nmt, or Echo</param>
/// <returns>The queue depth for the specified engine type</returns>
/// <exception cref="ServalApiException">A server side error occurred.</exception>
public virtual async System.Threading.Tasks.Task<Queue> GetQueueDepthAsync(string engineType, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))
public virtual async System.Threading.Tasks.Task<Queue> GetQueueAsync(string engineType, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))
{
if (engineType == null)
throw new System.ArgumentNullException("engineType");

var urlBuilder_ = new System.Text.StringBuilder();
urlBuilder_.Append(BaseUrl != null ? BaseUrl.TrimEnd('/') : "").Append("/translation/engines/queues/{engineType}");
urlBuilder_.Replace("{engineType}", System.Uri.EscapeDataString(ConvertToString(engineType, System.Globalization.CultureInfo.InvariantCulture)));
urlBuilder_.Append(BaseUrl != null ? BaseUrl.TrimEnd('/') : "").Append("/translation/engines/queues");

var client_ = _httpClient;
var disposeClient_ = false;
try
{
using (var request_ = new System.Net.Http.HttpRequestMessage())
{
var json_ = Newtonsoft.Json.JsonConvert.SerializeObject(engineType, _settings.Value);
var content_ = new System.Net.Http.StringContent(json_);
content_.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/json");
request_.Content = content_;
request_.Method = new System.Net.Http.HttpMethod("GET");
request_.Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("application/json"));

Expand Down Expand Up @@ -4116,8 +4119,11 @@ public partial class TranslationEngineConfig
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "13.18.2.0 (NJsonSchema v10.8.0.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class Queue
{
[Newtonsoft.Json.JsonProperty("depth", Required = Newtonsoft.Json.Required.Always)]
public int Depth { get; set; } = default!;
[Newtonsoft.Json.JsonProperty("size", Required = Newtonsoft.Json.Required.Always)]
public int Size { get; set; } = default!;

[Newtonsoft.Json.JsonProperty("engineType", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public string? EngineType { get; set; } = default!;

}

Expand Down
3 changes: 2 additions & 1 deletion src/Serval.Translation/Contracts/QueueDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ namespace Serval.Translation.Contracts;

public class QueueDto
{
public int Depth { get; set; } = default;
public int Size { get; set; } = default;
public string? EngineType {get; set;}
}
Original file line number Diff line number Diff line change
Expand Up @@ -192,19 +192,19 @@ public async Task<ActionResult> DeleteAsync([NotNull] string id, CancellationTok
/// <response code="403">The authenticated client cannot perform the operation</response>
/// <response code="503">A necessary service is currently unavailable. Check `/health` for more details. </response>
[Authorize(Scopes.ReadTranslationEngines)]
[HttpGet("queues/{engineType}")]
[HttpGet("queues")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
[ProducesResponseType(typeof(void), StatusCodes.Status503ServiceUnavailable)]
public async Task<ActionResult<QueueDto>> GetQueueDepth(
[NotNull] string engineType,
public async Task<ActionResult<QueueDto>> GetQueueAsync(
[FromBody] string engineType,
CancellationToken cancellationToken
)
{
try
{
return Map(await _engineService.GetQueueDepthAsync(engineType, cancellationToken));
return Map(await _engineService.GetQueueAsync(engineType, cancellationToken));
}
catch (InvalidOperationException ioe)
{
Expand Down Expand Up @@ -1030,7 +1030,7 @@ private static Build Map(Engine engine, TranslationBuildConfigDto source)
return build;
}

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

private TranslationEngineDto Map(Engine source)
{
Expand Down
3 changes: 2 additions & 1 deletion src/Serval.Translation/Models/Queue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ namespace Serval.Translation.Models;

public class Queue
{
public int Depth { get; set; } = default;
public int Size { get; set; } = default;
public string? EngineType {get; set;}
}
4 changes: 2 additions & 2 deletions src/Serval.Translation/Services/EngineService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,14 +298,14 @@ public Task DeleteAllCorpusFilesAsync(string dataFileId, CancellationToken cance
);
}

public async Task<Queue> GetQueueDepthAsync(string engineType, CancellationToken cancellationToken = default)
public async Task<Queue> GetQueueAsync(string engineType, CancellationToken cancellationToken = default)
{
var client = _grpcClientFactory.CreateClient<TranslationEngineApi.TranslationEngineApiClient>(engineType);
GetQueueDepthResponse response = await client.GetQueueDepthAsync(
new GetQueueDepthRequest { EngineType = engineType },
cancellationToken: cancellationToken
);
return new Queue { Depth = response.Depth };
return new Queue { Size = response.Depth, EngineType = engineType };
}

private Models.TranslationResult Map(V1.TranslationResult source)
Expand Down
2 changes: 1 addition & 1 deletion src/Serval.Translation/Services/IEngineService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ Task<bool> TrainSegmentPairAsync(

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

Task<Queue> GetQueueDepthAsync(string engineType, CancellationToken cancellationToken = default);
Task<Queue> GetQueueAsync(string engineType, CancellationToken cancellationToken = default);
}
12 changes: 4 additions & 8 deletions tests/Serval.E2ETests/ServalApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,23 +165,19 @@ public async Task NmtQueueMultiple()
builds += $"{JsonSerializer.Serialize(build)}\n";
}

builds +=
"Depth = " + (await _helperClient.translationEnginesClient.GetQueueDepthAsync("Nmt")).Depth.ToString();
builds += "Depth = " + (await _helperClient.translationEnginesClient.GetQueueAsync("Nmt")).Size.ToString();

//Status message of last started build says that there is at least one job ahead of it in the queue
// (this variable due to how many jobs may already exist in the production queue from other Serval instances)
TranslationBuild newestEngineCurrentBuild = await _helperClient.translationEnginesClient.GetCurrentBuildAsync(
engineIds[NUM_ENGINES - 1]
);
Assert.NotNull(newestEngineCurrentBuild.QueueDepth, JsonSerializer.Serialize(newestEngineCurrentBuild));
Assert.Multiple(async () =>
{
Assert.That(newestEngineCurrentBuild.QueueDepth, Is.GreaterThan(0), message: builds);
Assert.That(
newestEngineCurrentBuild.QueueDepth,
Is.GreaterThan(0),
message: builds
);
Assert.That(
(await _helperClient.translationEnginesClient.GetQueueDepthAsync("Nmt")).Depth,
(await _helperClient.translationEnginesClient.GetQueueAsync("Nmt")).Size,
Is.GreaterThanOrEqualTo(NUM_ENGINES - NUM_WORKERS)
);
});
Expand Down

0 comments on commit 25c7074

Please sign in to comment.