From 4d28da5035f838c65f650216f93afb660fa67d0d Mon Sep 17 00:00:00 2001 From: Pavel Zhur Date: Thu, 23 May 2024 04:58:54 +0300 Subject: [PATCH] GetProgressionsIndex operation implemented --- .../Functions/V1/GetProgressionsIndex.cs | 40 +++++++++++++++++++ .../Functions/V1/GetSourcesAndExternalIds.cs | 2 - .../Functions/V1/Search.cs | 2 +- .../DownstreamApiClient.cs | 8 +++- .../SourceApiClient.cs | 5 +++ .../HarmonyDB.Source.Api.Model.csproj | 1 + .../SourceApiUrls.cs | 2 + .../VInternal/GetProgressionsIndexRequest.cs | 5 +++ .../VInternal/GetProgressionsIndexResponse.cs | 8 ++++ 9 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 HarmonyDB.Index/HarmonyDB.Index.Api/Functions/V1/GetProgressionsIndex.cs create mode 100644 HarmonyDB.Source/HarmonyDB.Source.Api.Model/VInternal/GetProgressionsIndexRequest.cs create mode 100644 HarmonyDB.Source/HarmonyDB.Source.Api.Model/VInternal/GetProgressionsIndexResponse.cs diff --git a/HarmonyDB.Index/HarmonyDB.Index.Api/Functions/V1/GetProgressionsIndex.cs b/HarmonyDB.Index/HarmonyDB.Index.Api/Functions/V1/GetProgressionsIndex.cs new file mode 100644 index 00000000..047c4ccc --- /dev/null +++ b/HarmonyDB.Index/HarmonyDB.Index.Api/Functions/V1/GetProgressionsIndex.cs @@ -0,0 +1,40 @@ +using HarmonyDB.Index.Api.Services; +using HarmonyDB.Index.DownstreamApi.Client; +using HarmonyDB.Source.Api.Model; +using HarmonyDB.Source.Api.Model.VInternal; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Azure.Functions.Worker; +using Microsoft.Extensions.Logging; +using OneShelf.Common.Api; + +namespace HarmonyDB.Index.Api.Functions.V1; + +public class GetProgressionsIndex : FunctionBase +{ + private readonly DownstreamApiClient _downstreamApiClient; + + public GetProgressionsIndex(ILoggerFactory loggerFactory, DownstreamApiClient downstreamApiClient) + : base(loggerFactory) + { + _downstreamApiClient = downstreamApiClient; + } + + [Function(SourceApiUrls.VInternalGetProgressionsIndex)] + public Task Run( + [HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequest req, [FromBody] GetProgressionsIndexRequest request) + => RunHandler(request); + + protected override async Task Execute(GetProgressionsIndexRequest request) + { + var results = (await Task.WhenAll(_downstreamApiClient.GetDownstreamSourceIndices(x => x.AreProgressionsProvidedForIndexing) + .Select(_downstreamApiClient.VInternalGetProgressionsIndex))) + .SelectMany(x => x.Progressions) + .ToDictionary(x => x.Key, x => x.Value); + + return new() + { + Progressions = results, + }; + } +} \ No newline at end of file diff --git a/HarmonyDB.Index/HarmonyDB.Index.Api/Functions/V1/GetSourcesAndExternalIds.cs b/HarmonyDB.Index/HarmonyDB.Index.Api/Functions/V1/GetSourcesAndExternalIds.cs index 77f95f44..b9f11051 100644 --- a/HarmonyDB.Index/HarmonyDB.Index.Api/Functions/V1/GetSourcesAndExternalIds.cs +++ b/HarmonyDB.Index/HarmonyDB.Index.Api/Functions/V1/GetSourcesAndExternalIds.cs @@ -4,10 +4,8 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Azure.Functions.Worker; -using Microsoft.Azure.Functions.Worker.Http; using Microsoft.Extensions.Logging; using OneShelf.Authorization.Api.Client; -using OneShelf.Common; using OneShelf.Common.Api.WithAuthorization; namespace HarmonyDB.Index.Api.Functions.V1; diff --git a/HarmonyDB.Index/HarmonyDB.Index.Api/Functions/V1/Search.cs b/HarmonyDB.Index/HarmonyDB.Index.Api/Functions/V1/Search.cs index 062a00a3..01226ce6 100644 --- a/HarmonyDB.Index/HarmonyDB.Index.Api/Functions/V1/Search.cs +++ b/HarmonyDB.Index/HarmonyDB.Index.Api/Functions/V1/Search.cs @@ -35,7 +35,7 @@ protected override async Task Execute(HttpRequest httpRequest, S throw new ArgumentOutOfRangeException(nameof(query)); } - var all = await Task.WhenAll(_downstreamApiClient.SearchableDownstreamSourceIndices.Select(i => _downstreamApiClient.V1Search(i, + var all = await Task.WhenAll(_downstreamApiClient.GetDownstreamSourceIndices(x => x.IsSearchSupported).Select(i => _downstreamApiClient.V1Search(i, new() { Identity = request.Identity, diff --git a/HarmonyDB.Index/HarmonyDB.Index.DownstreamApi.Client/DownstreamApiClient.cs b/HarmonyDB.Index/HarmonyDB.Index.DownstreamApi.Client/DownstreamApiClient.cs index 64c91789..5770a86e 100644 --- a/HarmonyDB.Index/HarmonyDB.Index.DownstreamApi.Client/DownstreamApiClient.cs +++ b/HarmonyDB.Index/HarmonyDB.Index.DownstreamApi.Client/DownstreamApiClient.cs @@ -1,6 +1,7 @@ using HarmonyDB.Source.Api.Client; using HarmonyDB.Source.Api.Model.V1; using HarmonyDB.Source.Api.Model.V1.Api; +using HarmonyDB.Source.Api.Model.VInternal; using Microsoft.Extensions.Options; using OneShelf.Authorization.Api.Model; using OneShelf.Common; @@ -26,14 +27,17 @@ public DownstreamApiClient(IHttpClientFactory httpClientFactory, IOptions DownstreamSourceIndicesBySourceKey { get; } - public IEnumerable SearchableDownstreamSourceIndices => _options.DownstreamSources.WithIndices().Where(x => x.x.IsSearchSupported).Select(x => x.i); - + public IEnumerable GetDownstreamSourceIndices(Func selector) => _options.DownstreamSources.WithIndices().Where(x => selector(x.x)).Select(x => x.i); + public int DownstreamSourcesCount => _options.DownstreamSources.Count; public string GetSourceTitle(string sourceKey) => _options.DownstreamSources.SelectMany(x => x.Sources).Single(s => s.Key == sourceKey).Title; public string GetSourceKey(string externalId) => _options.DownstreamSources.SelectMany(x => x.Sources).Single(s => externalId.StartsWith(s.ExternalIdPrefix)).Key; + public async Task VInternalGetProgressionsIndex(int sourceIndex) + => await _clients[sourceIndex].VInternalGetProgressionsIndex(); + public async Task V1GetSong(Identity identity, int sourceIndex, string externalId) => await _clients[sourceIndex].V1GetSong(identity, externalId); diff --git a/HarmonyDB.Source/HarmonyDB.Source.Api.Client/SourceApiClient.cs b/HarmonyDB.Source/HarmonyDB.Source.Api.Client/SourceApiClient.cs index d899cbd9..73b62527 100644 --- a/HarmonyDB.Source/HarmonyDB.Source.Api.Client/SourceApiClient.cs +++ b/HarmonyDB.Source/HarmonyDB.Source.Api.Client/SourceApiClient.cs @@ -1,6 +1,7 @@ using HarmonyDB.Source.Api.Model; using HarmonyDB.Source.Api.Model.V1; using HarmonyDB.Source.Api.Model.V1.Api; +using HarmonyDB.Source.Api.Model.VInternal; using OneShelf.Authorization.Api.Model; using OneShelf.Common.Api.Client; @@ -22,6 +23,10 @@ public async Task V1GetSong(Identity identity, string externalI Identity = identity, ExternalId = externalId, }); + + public async Task VInternalGetProgressionsIndex() + => await PostWithCode(SourceApiUrls.VInternalGetProgressionsIndex, new()); + public async Task V1GetSongs(Identity identity, IReadOnlyList externalIds) => await Post(SourceApiUrls.V1GetSongs, new() { diff --git a/HarmonyDB.Source/HarmonyDB.Source.Api.Model/HarmonyDB.Source.Api.Model.csproj b/HarmonyDB.Source/HarmonyDB.Source.Api.Model/HarmonyDB.Source.Api.Model.csproj index 4553320d..89adf828 100644 --- a/HarmonyDB.Source/HarmonyDB.Source.Api.Model/HarmonyDB.Source.Api.Model.csproj +++ b/HarmonyDB.Source/HarmonyDB.Source.Api.Model/HarmonyDB.Source.Api.Model.csproj @@ -7,6 +7,7 @@ + diff --git a/HarmonyDB.Source/HarmonyDB.Source.Api.Model/SourceApiUrls.cs b/HarmonyDB.Source/HarmonyDB.Source.Api.Model/SourceApiUrls.cs index 3c535d9e..ab4c2ffe 100644 --- a/HarmonyDB.Source/HarmonyDB.Source.Api.Model/SourceApiUrls.cs +++ b/HarmonyDB.Source/HarmonyDB.Source.Api.Model/SourceApiUrls.cs @@ -8,4 +8,6 @@ public static class SourceApiUrls public const string V1GetSearchHeader = nameof(V1GetSearchHeader); public const string V1GetSourcesAndExternalIds = nameof(V1GetSourcesAndExternalIds); public const string V1Ping = nameof(V1Ping); + + public const string VInternalGetProgressionsIndex = nameof(VInternalGetProgressionsIndex); } \ No newline at end of file diff --git a/HarmonyDB.Source/HarmonyDB.Source.Api.Model/VInternal/GetProgressionsIndexRequest.cs b/HarmonyDB.Source/HarmonyDB.Source.Api.Model/VInternal/GetProgressionsIndexRequest.cs new file mode 100644 index 00000000..4a499417 --- /dev/null +++ b/HarmonyDB.Source/HarmonyDB.Source.Api.Model/VInternal/GetProgressionsIndexRequest.cs @@ -0,0 +1,5 @@ +namespace HarmonyDB.Source.Api.Model.VInternal; + +public class GetProgressionsIndexRequest +{ +} \ No newline at end of file diff --git a/HarmonyDB.Source/HarmonyDB.Source.Api.Model/VInternal/GetProgressionsIndexResponse.cs b/HarmonyDB.Source/HarmonyDB.Source.Api.Model/VInternal/GetProgressionsIndexResponse.cs new file mode 100644 index 00000000..592a50fe --- /dev/null +++ b/HarmonyDB.Source/HarmonyDB.Source.Api.Model/VInternal/GetProgressionsIndexResponse.cs @@ -0,0 +1,8 @@ +using HarmonyDB.Index.Analysis.Models.CompactV1; + +namespace HarmonyDB.Source.Api.Model.VInternal; + +public class GetProgressionsIndexResponse +{ + public required IReadOnlyDictionary Progressions { get; init; } +} \ No newline at end of file