| | | 1 | | using Microsoft.Extensions.Caching.Memory; |
| | | 2 | | using OzonDomains; |
| | | 3 | | using OzonDomains.Models; |
| | | 4 | | using Services.DataServices; |
| | | 5 | | |
| | | 6 | | namespace OzonOrdersWeb.Areas.Studio2.AnalysisServices; |
| | | 7 | | |
| | | 8 | | public sealed record OzonErrorLink(string ClientName, string ClientId, string Url, bool IsAvailable, string Reason); |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Проверка доступности dashboard ошибок Ozon по каждому клиенту (внешний сервис OzonErrorDashboard:BaseUrl): |
| | | 12 | | /// параллельные запросы через IHttpClientFactory, результат кэшируется на 2 минуты (кнопка «Проверить заново» обходит к |
| | | 13 | | /// </summary> |
| | 0 | 14 | | public sealed class OzonErrorLinksService(OzonClientServices clients, IHttpClientFactory httpClientFactory, IMemoryCache |
| | | 15 | | { |
| | 0 | 16 | | private static readonly TimeSpan Ttl = TimeSpan.FromMinutes(2); |
| | 0 | 17 | | private readonly string _baseUrl = (configuration["OzonErrorDashboard:BaseUrl"] ?? string.Empty).TrimEnd('/'); |
| | | 18 | | |
| | | 19 | | public async Task<IReadOnlyList<OzonErrorLink>> GetAsync(bool refresh, CancellationToken ct) |
| | | 20 | | { |
| | | 21 | | const string key = "pa.ozon-error-links"; |
| | 0 | 22 | | if (!refresh && cache.TryGetValue(key, out IReadOnlyList<OzonErrorLink>? cached) && cached != null) return cache |
| | | 23 | | |
| | 0 | 24 | | var ozonClients = (await clients.GetOzonClients()) |
| | 0 | 25 | | .Where(c => c.ClientType == ClientType.OZON).OrderBy(c => c.Name).ToList(); |
| | 0 | 26 | | var result = await Task.WhenAll(ozonClients.Select(c => CheckAsync(c, ct))); |
| | 0 | 27 | | cache.Set(key, (IReadOnlyList<OzonErrorLink>)result, Ttl); |
| | 0 | 28 | | return result; |
| | 0 | 29 | | } |
| | | 30 | | |
| | | 31 | | private async Task<OzonErrorLink> CheckAsync(OzonClient client, CancellationToken ct) |
| | | 32 | | { |
| | 0 | 33 | | var raw = string.IsNullOrWhiteSpace(client.DecryptClientId) ? client.ClientId : client.DecryptClientId; |
| | 0 | 34 | | var clientId = (raw ?? string.Empty).Trim(); |
| | 0 | 35 | | var name = client.Name ?? string.Empty; |
| | 0 | 36 | | if (string.IsNullOrWhiteSpace(clientId) || string.IsNullOrWhiteSpace(_baseUrl)) |
| | 0 | 37 | | return new OzonErrorLink(name, clientId, string.Empty, false, "Не указан ClientId"); |
| | | 38 | | |
| | 0 | 39 | | var link = $"{_baseUrl}/{Uri.EscapeDataString(clientId)}/dashboard"; |
| | | 40 | | try |
| | | 41 | | { |
| | 0 | 42 | | var http = httpClientFactory.CreateClient(); |
| | 0 | 43 | | http.Timeout = TimeSpan.FromSeconds(5); |
| | 0 | 44 | | using var response = await http.GetAsync(link, ct); |
| | 0 | 45 | | var body = await response.Content.ReadAsStringAsync(ct); |
| | 0 | 46 | | var notFound = body.Contains("\"detail\":\"Клиент не найден в базе\"", StringComparison.OrdinalIgnoreCase); |
| | 0 | 47 | | var available = response.IsSuccessStatusCode && !notFound; |
| | 0 | 48 | | return new OzonErrorLink(name, clientId, link, available, |
| | 0 | 49 | | available ? string.Empty : notFound ? "Клиент не найден в базе" : $"HTTP {(int)response.StatusCode}"); |
| | | 50 | | } |
| | 0 | 51 | | catch (Exception ex) when (ex is not OperationCanceledException || !ct.IsCancellationRequested) |
| | | 52 | | { |
| | 0 | 53 | | return new OzonErrorLink(name, clientId, link, false, ex.Message); |
| | | 54 | | } |
| | 0 | 55 | | } |
| | | 56 | | } |