using System.Globalization; using Admin.NET.Plugin.AiDOP.DataPlatform.Core.Connectors; namespace Admin.NET.Plugin.AiDOP.DataPlatform.Core.Edge; public sealed record EdgeRelationalTimeSettings(string Column, int DelaySeconds, int LookbackSeconds, int RetentionSeconds) { public bool IsValid() => Column is { Length: > 0 and <= 63 } && Column.All(ch => char.IsAsciiLetterOrDigit(ch) || ch == '_') && DelaySeconds > 0 && LookbackSeconds >= DelaySeconds && RetentionSeconds > (long)LookbackSeconds + DelaySeconds; } public static class EdgeTimeWatermark { private const string BoundaryPrefix = "until:"; private const string PagePrefix = "page:"; public static string Encode(DateTimeOffset value) => FormattableString.Invariant($"{BoundaryPrefix}{value.UtcTicks}"); public static DateTimeOffset Decode(string? value) { if (value is null || !value.StartsWith(BoundaryPrefix, StringComparison.Ordinal) || !long.TryParse(value.AsSpan(BoundaryPrefix.Length), NumberStyles.None, CultureInfo.InvariantCulture, out var ticks) || ticks < 0 || ticks > DateTime.MaxValue.Ticks) throw new EdgeContractException("EDGE_TIME_BOUNDARY_INVALID"); return new DateTimeOffset(ticks, TimeSpan.Zero); } public static string Page(string checkpoint) => PagePrefix + checkpoint; public static bool AdvancesPage(string? from, string? to, bool incremental) { if (to?.StartsWith(PagePrefix, StringComparison.Ordinal) != true) return false; try { if (incremental) { var next = RelationalTimePosition.Parse(to[PagePrefix.Length..]); if (from?.StartsWith(BoundaryPrefix, StringComparison.Ordinal) == true) { _ = Decode(from); return true; } return from?.StartsWith(PagePrefix, StringComparison.Ordinal) == true && next.CompareTo(RelationalTimePosition.Parse(from[PagePrefix.Length..])) > 0; } return long.TryParse(to.AsSpan(PagePrefix.Length), NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out var nextKey) && (from is null || (from.StartsWith(PagePrefix, StringComparison.Ordinal) && long.TryParse(from.AsSpan(PagePrefix.Length), NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out var previous) && nextKey > previous)); } catch (ConnectorContractException) { return false; } } }