< Summary

Information
Class: OzonOrdersWeb.WebServices.SystemAlerts.SystemAlertOrderIndex
Assembly: OzonOrdersWeb
File(s): /home/runner/work/Studio2Prod/Studio2Prod/OzonOrdersWeb/WebServices/SystemAlerts/SystemAlertOrderIndex.cs
Line coverage
80%
Covered lines: 8
Uncovered lines: 2
Coverable lines: 10
Total lines: 48
Line coverage: 80%
Branch coverage
75%
Covered branches: 6
Total branches: 8
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
get_IsLoaded()100%11100%
Replace(...)100%11100%
Get(...)100%44100%
Serialize(...)100%11100%
Deserialize(...)50%5460%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/OzonOrdersWeb/WebServices/SystemAlerts/SystemAlertOrderIndex.cs

#LineLine coverage
 1using System.Text.Json;
 2using System.Text.Json.Serialization;
 3
 4namespace OzonOrdersWeb.WebServices.SystemAlerts;
 5
 6/// <summary>Алерт, затрагивающий заказ: попадает в подсказку значка на сетках заказов.</summary>
 7public sealed record OrderAlertEntry(string Key, string Severity, string Domain, string Title, string Reason);
 8
 9/// <summary>Заказ и причина в сохранённом JSON инцидента (<c>SystemAlertIncident.OrdersJson</c>), его читает и вкладка 
 10public sealed record StoredOrderRef(
 11    [property: JsonPropertyName("id")] int Id,
 12    [property: JsonPropertyName("reason")] string Reason,
 13    [property: JsonPropertyName("s")] string? Severity = null);
 14
 15/// <summary>
 16/// Живой индекс «заказ → активные алерты». Пересобирается при каждой проверке алертов целиком (замена словаря),
 17/// читается сетками заказов без SQL: поиск по идентификаторам страницы.
 18/// </summary>
 19public sealed class SystemAlertOrderIndex
 20{
 121    private static readonly IReadOnlyList<OrderAlertEntry> Empty = [];
 22    private volatile Dictionary<int, OrderAlertEntry[]>? _map;
 23
 24    /// <summary>false — индекс ещё не строился (после рестарта): его достраивают из сохранённых инцидентов.</summary>
 925    public bool IsLoaded => _map != null;
 26
 27    public void Replace(IEnumerable<(OrderAlertEntry Entry, int OrderId)> items) =>
 144828        _map = items.GroupBy(x => x.OrderId).ToDictionary(g => g.Key, g => g.Select(x => x.Entry).ToArray());
 29
 30    public IReadOnlyList<OrderAlertEntry> Get(int orderId) =>
 1931        _map != null && _map.TryGetValue(orderId, out var entries) ? entries : Empty;
 32
 33    public static string Serialize(IEnumerable<SystemAlertOrderRef> orders, int limit) =>
 16134        JsonSerializer.Serialize(orders.Take(limit).Select(x => new StoredOrderRef(x.OrderId, x.Reason, x.Severity)));
 35
 36    public static IReadOnlyList<StoredOrderRef> Deserialize(string? json)
 37    {
 338        if (string.IsNullOrWhiteSpace(json)) return [];
 39        try
 40        {
 341            return JsonSerializer.Deserialize<List<StoredOrderRef>>(json) ?? [];
 42        }
 043        catch (JsonException)
 44        {
 045            return [];
 46        }
 347    }
 48}