|
| 1 | +// Licensed to Elasticsearch B.V under one or more agreements. |
| 2 | +// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. |
| 3 | +// See the LICENSE file in the project root for more information |
| 4 | + |
| 5 | +using System.Collections.Frozen; |
| 6 | +using System.Text.Json; |
| 7 | +using Elastic.Markdown.IO; |
| 8 | +using Elastic.Markdown.IO.State; |
| 9 | +using Microsoft.Extensions.Logging; |
| 10 | + |
| 11 | +namespace Elastic.Markdown.CrossLinks; |
| 12 | + |
| 13 | +public record FetchedCrossLinks |
| 14 | +{ |
| 15 | + public required FrozenDictionary<string, LinkReference> LinkReferences { get; init; } |
| 16 | + public required HashSet<string> DeclaredRepositories { get; init; } |
| 17 | + |
| 18 | + public static FetchedCrossLinks Empty { get; } = new() |
| 19 | + { |
| 20 | + DeclaredRepositories = [], |
| 21 | + LinkReferences = new Dictionary<string, LinkReference>().ToFrozenDictionary() |
| 22 | + }; |
| 23 | +} |
| 24 | + |
| 25 | +public abstract class CrossLinkFetcher(ILoggerFactory logger) : IDisposable |
| 26 | +{ |
| 27 | + private readonly ILogger _logger = logger.CreateLogger(nameof(CrossLinkFetcher)); |
| 28 | + private readonly HttpClient _client = new(); |
| 29 | + private LinkIndex? _linkIndex; |
| 30 | + |
| 31 | + public static LinkReference Deserialize(string json) => |
| 32 | + JsonSerializer.Deserialize(json, SourceGenerationContext.Default.LinkReference)!; |
| 33 | + |
| 34 | + public abstract Task<FetchedCrossLinks> Fetch(); |
| 35 | + |
| 36 | + protected async Task<LinkIndex> FetchLinkIndex() |
| 37 | + { |
| 38 | + if (_linkIndex is not null) |
| 39 | + { |
| 40 | + _logger.LogInformation("Using cached link index"); |
| 41 | + return _linkIndex; |
| 42 | + } |
| 43 | + var url = $"https://elastic-docs-link-index.s3.us-east-2.amazonaws.com/link-index.json"; |
| 44 | + _logger.LogInformation("Fetching {Url}", url); |
| 45 | + var json = await _client.GetStringAsync(url); |
| 46 | + _linkIndex = LinkIndex.Deserialize(json); |
| 47 | + return _linkIndex; |
| 48 | + } |
| 49 | + |
| 50 | + protected async Task<LinkReference> Fetch(string repository) |
| 51 | + { |
| 52 | + var linkIndex = await FetchLinkIndex(); |
| 53 | + if (!linkIndex.Repositories.TryGetValue(repository, out var repositoryLinks)) |
| 54 | + throw new Exception($"Repository {repository} not found in link index"); |
| 55 | + |
| 56 | + if (!repositoryLinks.TryGetValue("main", out var linkIndexEntry)) |
| 57 | + throw new Exception($"Repository {repository} not found in link index"); |
| 58 | + |
| 59 | + return await FetchLinkIndexEntry(repository, linkIndexEntry); |
| 60 | + } |
| 61 | + |
| 62 | + protected async Task<LinkReference> FetchLinkIndexEntry(string repository, LinkIndexEntry linkIndexEntry) |
| 63 | + { |
| 64 | + var linkReference = await TryGetCachedLinkReference(repository, linkIndexEntry); |
| 65 | + if (linkReference is not null) |
| 66 | + return linkReference; |
| 67 | + |
| 68 | + var url = $"https://elastic-docs-link-index.s3.us-east-2.amazonaws.com/elastic/{repository}/main/links.json"; |
| 69 | + _logger.LogInformation("Fetching links.json for '{Repository}': {Url}", repository, url); |
| 70 | + var json = await _client.GetStringAsync(url); |
| 71 | + linkReference = Deserialize(json); |
| 72 | + WriteLinksJsonCachedFile(repository, linkIndexEntry, json); |
| 73 | + return linkReference; |
| 74 | + } |
| 75 | + |
| 76 | + private void WriteLinksJsonCachedFile(string repository, LinkIndexEntry linkIndexEntry, string json) |
| 77 | + { |
| 78 | + var cachedFileName = $"links-elastic-{repository}-main-{linkIndexEntry.ETag}.json"; |
| 79 | + var cachedPath = Path.Combine(Paths.ApplicationData.FullName, "links", cachedFileName); |
| 80 | + if (File.Exists(cachedPath)) |
| 81 | + return; |
| 82 | + try |
| 83 | + { |
| 84 | + _ = Directory.CreateDirectory(Path.GetDirectoryName(cachedPath)!); |
| 85 | + File.WriteAllText(cachedPath, json); |
| 86 | + } |
| 87 | + catch (Exception e) |
| 88 | + { |
| 89 | + _logger.LogError(e, "Failed to write cached link reference {CachedPath}", cachedPath); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private async Task<LinkReference?> TryGetCachedLinkReference(string repository, LinkIndexEntry linkIndexEntry) |
| 94 | + { |
| 95 | + var cachedFileName = $"links-elastic-{repository}-main-{linkIndexEntry.ETag}.json"; |
| 96 | + var cachedPath = Path.Combine(Paths.ApplicationData.FullName, "links", cachedFileName); |
| 97 | + if (File.Exists(cachedPath)) |
| 98 | + { |
| 99 | + try |
| 100 | + { |
| 101 | + var json = await File.ReadAllTextAsync(cachedPath); |
| 102 | + var linkReference = Deserialize(json); |
| 103 | + return linkReference; |
| 104 | + } |
| 105 | + catch (Exception e) |
| 106 | + { |
| 107 | + _logger.LogError(e, "Failed to read cached link reference {CachedPath}", cachedPath); |
| 108 | + return null; |
| 109 | + } |
| 110 | + } |
| 111 | + return null; |
| 112 | + |
| 113 | + } |
| 114 | + |
| 115 | + public void Dispose() |
| 116 | + { |
| 117 | + _client.Dispose(); |
| 118 | + logger.Dispose(); |
| 119 | + GC.SuppressFinalize(this); |
| 120 | + } |
| 121 | +} |
0 commit comments