using System.Text.RegularExpressions; using Flypaper.Core.Models; using Flypaper.Storage.Entities; namespace Flypaper.Storage.Explanations; public static partial class ExplanationText { [GeneratedRegex("[0-9a-fA-F]{8,}")] private static partial Regex LongHex(); [GeneratedRegex("[0-9]+")] private static partial Regex Digits(); public static string? Path(string? target) { if (target is null) { return null; } var space = target.IndexOf(' '); if (space < 0) { return target; } var hostAndPath = target[(space + 1)..]; if (hostAndPath.StartsWith('/')) { return hostAndPath; } var slash = hostAndPath.IndexOf('/'); return slash < 0 ? "/" : hostAndPath[slash..]; } public static string Outcome(TrapOutcome outcome, long durationMs) => outcome == TrapOutcome.Rejected ? outcome.ToString() : $"{outcome} {durationMs / 1000}s"; public static string Value(ExplanationKind kind, TrapEventEntity trapEvent) => kind switch { ExplanationKind.Target => Path(trapEvent.Target) ?? "", ExplanationKind.ClientInfo => trapEvent.ClientInfo ?? "", _ => Outcome(trapEvent.Outcome, trapEvent.DurationMs) }; public static string PathShape(string path) { var query = path.IndexOf('?'); var bare = query < 0 ? path : path[..query]; return Digits().Replace(LongHex().Replace(bare, "{hex}"), "{n}"); } }