diff --git a/HarmonyDB.Index/HarmonyDB.Index.Api/Functions/VDev/IndexFunctions.cs b/HarmonyDB.Index/HarmonyDB.Index.Api/Functions/VDev/IndexFunctions.cs index db724662..5a1d2c18 100644 --- a/HarmonyDB.Index/HarmonyDB.Index.Api/Functions/VDev/IndexFunctions.cs +++ b/HarmonyDB.Index/HarmonyDB.Index.Api/Functions/VDev/IndexFunctions.cs @@ -1,3 +1,5 @@ +using HarmonyDB.Index.Analysis.Services; +using HarmonyDB.Index.Api.Services; using HarmonyDB.Index.BusinessLogic.Models; using HarmonyDB.Index.BusinessLogic.Services; using HarmonyDB.Index.DownstreamApi.Client; @@ -19,14 +21,18 @@ public class IndexFunctions private readonly ProgressionsCache _progressionsCache; private readonly LoopsStatisticsCache _loopsStatisticsCache; private readonly IndexHeadersCache _indexHeadersCache; + private readonly InputParser _inputParser; + private readonly ProgressionsSearch _progressionsSearch; - public IndexFunctions(ILogger logger, DownstreamApiClient downstreamApiClient, ProgressionsCache progressionsCache, LoopsStatisticsCache loopsStatisticsCache, SecurityContext securityContext, IndexHeadersCache indexHeadersCache) + public IndexFunctions(ILogger logger, DownstreamApiClient downstreamApiClient, ProgressionsCache progressionsCache, LoopsStatisticsCache loopsStatisticsCache, SecurityContext securityContext, IndexHeadersCache indexHeadersCache, InputParser inputParser, ProgressionsSearch progressionsSearch) { _logger = logger; _downstreamApiClient = downstreamApiClient; _progressionsCache = progressionsCache; _loopsStatisticsCache = loopsStatisticsCache; _indexHeadersCache = indexHeadersCache; + _inputParser = inputParser; + _progressionsSearch = progressionsSearch; securityContext.InitService(); } @@ -129,4 +135,10 @@ public async Task VDevRebuildLoopStatisticsCache([HttpTrigger(Aut await _loopsStatisticsCache.Rebuild(); return new OkObjectResult((await _loopsStatisticsCache.Get()).Count); } + + [Function(nameof(VDevFindAndCount))] + public async Task VDevFindAndCount([HttpTrigger(AuthorizationLevel.Function, "get")] HttpRequest req, string searchQuery) + { + return new OkObjectResult(_progressionsSearch.Search((await _progressionsCache.Get()).Values, _inputParser.Parse(searchQuery)).Count); + } } \ No newline at end of file diff --git a/HarmonyDB.Index/HarmonyDB.Index.Api/Program.cs b/HarmonyDB.Index/HarmonyDB.Index.Api/Program.cs index 6408a98b..57b81e0c 100644 --- a/HarmonyDB.Index/HarmonyDB.Index.Api/Program.cs +++ b/HarmonyDB.Index/HarmonyDB.Index.Api/Program.cs @@ -24,6 +24,7 @@ .AddAuthorizationApiClient(context.Configuration) .AddDownstreamApiClient(context.Configuration) .AddScoped() + .AddScoped() .AddIndexBusinessLogic() .AddSecurityContext(); }) diff --git a/HarmonyDB.Index/HarmonyDB.Index.Api/Services/InputParser.cs b/HarmonyDB.Index/HarmonyDB.Index.Api/Services/InputParser.cs new file mode 100644 index 00000000..fb4a5438 --- /dev/null +++ b/HarmonyDB.Index/HarmonyDB.Index.Api/Services/InputParser.cs @@ -0,0 +1,41 @@ +using HarmonyDB.Common.Representations.OneShelf; +using HarmonyDB.Index.Analysis.Models; +using HarmonyDB.Index.Analysis.Services; + +namespace HarmonyDB.Index.Api.Services; + +public class InputParser +{ + private readonly ProgressionsBuilder _progressionsBuilder; + private readonly ChordDataParser _chordDataParser; + + public InputParser(ProgressionsBuilder progressionsBuilder, ChordDataParser chordDataParser) + { + _progressionsBuilder = progressionsBuilder; + _chordDataParser = chordDataParser; + } + + public HarmonyMovementsSequence Parse(string input) + { + var html = new NodeHtml + { + Name = "pre", + Children = input + .Split(' ', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) + .Select(c => new NodeChord + { + Children = [new NodeText(c).AsChild()] + }.AsChild()) + .ToList(), + }; + var chordsProgression = + _progressionsBuilder.BuildProgression(html.AsChords(new()).Select(_chordDataParser.GetProgressionData) + .ToList()); + + var sequence = chordsProgression.ExtendedHarmonyMovementsSequences.Single(); + + Console.WriteLine(string.Join(" ", sequence.Movements.Select(m => m.Title))); + + return sequence; + } +} \ No newline at end of file diff --git a/HarmonyDB.Index/HarmonyDB.Index.BusinessLogic/Services/LoopsStatisticsCache.cs b/HarmonyDB.Index/HarmonyDB.Index.BusinessLogic/Services/LoopsStatisticsCache.cs index 6793d6aa..6212dbe7 100644 --- a/HarmonyDB.Index/HarmonyDB.Index.BusinessLogic/Services/LoopsStatisticsCache.cs +++ b/HarmonyDB.Index/HarmonyDB.Index.BusinessLogic/Services/LoopsStatisticsCache.cs @@ -16,7 +16,7 @@ public class LoopsStatisticsCache : FileCacheBase logger, ProgressionsSearch progressionsSearch, ProgressionsCache progressionsCache) + public LoopsStatisticsCache(ILogger logger, ProgressionsSearch progressionsSearch, ProgressionsCache progressionsCache) : base(logger) { _progressionsSearch = progressionsSearch;