< Summary

Information
Class: Services.ApiServices.InterpartsPriceApi.InterpartsPriceDiagnosticsEntry
Assembly: Services
File(s): /home/runner/work/Studio2Prod/Studio2Prod/AppRepository/ApiServices/InterpartsPriceApi/InterpartsPriceDiagnosticsStore.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 8
Coverable lines: 8
Total lines: 54
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
get_Id()100%210%
get_UserName()100%210%
get_CreatedAt()100%210%
get_RequestUrl()100%210%
get_StatusCode()100%210%
get_ErrorMessage()100%210%
get_ResponseBody()100%210%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/AppRepository/ApiServices/InterpartsPriceApi/InterpartsPriceDiagnosticsStore.cs

#LineLine coverage
 1using System.Collections.Concurrent;
 2using Microsoft.AspNetCore.Http;
 3
 4namespace Services.ApiServices.InterpartsPriceApi;
 5
 06public sealed record InterpartsPriceDiagnosticsEntry(
 07    Guid Id,
 08    string? UserName,
 09    DateTimeOffset CreatedAt,
 010    string RequestUrl,
 011    int? StatusCode,
 012    string ErrorMessage,
 013    string ResponseBody);
 14
 15/// <summary>Короткоживущие безопасные детали ответов Interparts Price для открытия из UI.</summary>
 16public sealed class InterpartsPriceDiagnosticsStore
 17{
 18    private static readonly TimeSpan Lifetime = TimeSpan.FromMinutes(15);
 19    private readonly ConcurrentDictionary<Guid, InterpartsPriceDiagnosticsEntry> _entries = new();
 20    private readonly IHttpContextAccessor _httpContextAccessor;
 21
 22    public InterpartsPriceDiagnosticsStore(IHttpContextAccessor httpContextAccessor) => _httpContextAccessor = httpConte
 23
 24    public Guid Add(string requestUrl, int? statusCode, string errorMessage, string? responseBody)
 25    {
 26        RemoveExpired();
 27        var id = Guid.NewGuid();
 28        var userName = _httpContextAccessor.HttpContext?.User.Identity?.Name;
 29        _entries[id] = new InterpartsPriceDiagnosticsEntry(id, userName, DateTimeOffset.Now, requestUrl,
 30            statusCode, errorMessage, Trim(responseBody));
 31        return id;
 32    }
 33
 34    public InterpartsPriceDiagnosticsEntry? Get(Guid id)
 35    {
 36        RemoveExpired();
 37        if (!_entries.TryGetValue(id, out var entry)) return null;
 38        var userName = _httpContextAccessor.HttpContext?.User.Identity?.Name;
 39        return string.Equals(entry.UserName, userName, StringComparison.OrdinalIgnoreCase) ? entry : null;
 40    }
 41
 42    private void RemoveExpired()
 43    {
 44        var threshold = DateTimeOffset.Now - Lifetime;
 45        foreach (var entry in _entries.Where(x => x.Value.CreatedAt < threshold))
 46            _entries.TryRemove(entry.Key, out _);
 47    }
 48
 49    private static string Trim(string? value)
 50    {
 51        if (string.IsNullOrWhiteSpace(value)) return "—";
 52        return value.Length <= 12000 ? value : value[..12000] + "\n… ответ сокращён";
 53    }
 54}