diff --git a/src/Machine/src/Serval.Machine.Shared/Services/ClearMLService.cs b/src/Machine/src/Serval.Machine.Shared/Services/ClearMLService.cs index 33ddcb5b..df9a5b88 100644 --- a/src/Machine/src/Serval.Machine.Shared/Services/ClearMLService.cs +++ b/src/Machine/src/Serval.Machine.Shared/Services/ClearMLService.cs @@ -21,6 +21,7 @@ ILogger logger private readonly IClearMLAuthenticationService _clearMLAuthService = clearMLAuthService; private readonly ILogger _logger = logger; + private IDictionary? _queueNamesToIds = null; public async Task GetProjectIdAsync(string name, CancellationToken cancellationToken = default) { @@ -145,13 +146,26 @@ public async Task> 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 taskIds = tasks?.Select(t => (string)t?["id"]!) ?? new List(); 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 GetTaskByNameAsync(string name, CancellationToken cancellationToken = default) { IReadOnlyList tasks = await GetTasksAsync(new JsonObject { ["name"] = name }, cancellationToken); @@ -165,6 +179,8 @@ public Task> GetTasksByIdAsync( CancellationToken cancellationToken = default ) { + if (!ids.Any()) + return Task.FromResult(Array.Empty() as IReadOnlyList); return GetTasksAsync(new JsonObject { ["id"] = JsonValue.Create(ids.ToArray()) }, cancellationToken); }