Skip to content

Commit

Permalink
Get queue by ID - take time from 3-5 seconds per query to < 100ms.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnml1135 committed Oct 4, 2024
1 parent c8449c8 commit b69baa8
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/Machine/src/Serval.Machine.Shared/Services/ClearMLService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ ILogger<ClearMLService> logger

private readonly IClearMLAuthenticationService _clearMLAuthService = clearMLAuthService;
private readonly ILogger<ClearMLService> _logger = logger;
private IDictionary<string, string>? _queueNamesToIds = null;

public async Task<string?> GetProjectIdAsync(string name, CancellationToken cancellationToken = default)
{
Expand Down Expand Up @@ -145,13 +146,26 @@ public async Task<IReadOnlyList<ClearMLTask>> GetTasksForQueueAsync(
CancellationToken cancellationToken = default
)
{
var body = new JsonObject { ["name"] = queue };
JsonObject? result = await CallAsync("queues", "get_all_ex", body, cancellationToken);
var tasks = (JsonArray?)result?["data"]?["queues"]?[0]?["entries"];
await PopulateQueueNamesToIdsAsync(cancellationToken);

var body = new JsonObject { ["queue"] = _queueNamesToIds![queue] };
JsonObject? result = await CallAsync("queues", "get_by_id", body, cancellationToken);
var tasks = (JsonArray?)result?["data"]?["queue"]?["entries"];
IEnumerable<string> taskIds = tasks?.Select(t => (string)t?["id"]!) ?? new List<string>();
return await GetTasksByIdAsync(taskIds, cancellationToken);
}

private async Task PopulateQueueNamesToIdsAsync(CancellationToken cancellationToken = default)
{
if (_queueNamesToIds != null)
return;
JsonObject? result = await CallAsync("queues", "get_all", new JsonObject(), cancellationToken);
var queues = (JsonArray?)result?["data"]?["queues"];
if (queues is null)
throw new InvalidOperationException("Malformed response from ClearML server.");
_queueNamesToIds = queues.ToDictionary(q => (string)q!["name"]!, q => (string)q!["id"]!);
}

public async Task<ClearMLTask?> GetTaskByNameAsync(string name, CancellationToken cancellationToken = default)
{
IReadOnlyList<ClearMLTask> tasks = await GetTasksAsync(new JsonObject { ["name"] = name }, cancellationToken);
Expand All @@ -165,6 +179,8 @@ public Task<IReadOnlyList<ClearMLTask>> GetTasksByIdAsync(
CancellationToken cancellationToken = default
)
{
if (!ids.Any())
return Task.FromResult(Array.Empty<ClearMLTask>() as IReadOnlyList<ClearMLTask>);
return GetTasksAsync(new JsonObject { ["id"] = JsonValue.Create(ids.ToArray()) }, cancellationToken);
}

Expand Down

0 comments on commit b69baa8

Please sign in to comment.