| | | 1 | | using System.Globalization; |
| | | 2 | | using System.Security.Cryptography; |
| | | 3 | | using System.Text; |
| | | 4 | | using System.Text.Json; |
| | | 5 | | using EHonda.KicktippAi.Core; |
| | | 6 | | using NodaTime; |
| | | 7 | | using OpenAiIntegration; |
| | | 8 | | using Orchestrator.Commands.Observability.ExportExperimentDataset; |
| | | 9 | | using Match = EHonda.KicktippAi.Core.Match; |
| | | 10 | | |
| | | 11 | | namespace Orchestrator.Commands.Observability; |
| | | 12 | | |
| | | 13 | | internal static class ExperimentArtifactSupport |
| | | 14 | | { |
| | | 15 | | public const string Season = "2025/2026"; |
| | | 16 | | |
| | 1 | 17 | | private static readonly DateTimeZone BundesligaTimeZone = DateTimeZoneProviders.Tzdb["Europe/Berlin"]; |
| | | 18 | | |
| | | 19 | | public static HostedMatchExperimentDatasetItem BuildHostedDatasetItem(PersistedMatchOutcome outcome) |
| | | 20 | | { |
| | 1 | 21 | | ArgumentNullException.ThrowIfNull(outcome); |
| | | 22 | | |
| | 1 | 23 | | var tippSpielId = outcome.TippSpielId ?? throw new InvalidOperationException( |
| | 1 | 24 | | $"Persisted outcome for {outcome.HomeTeam} vs {outcome.AwayTeam} is missing tippspielId."); |
| | | 25 | | |
| | 1 | 26 | | if (!outcome.HasOutcome || outcome.HomeGoals is null || outcome.AwayGoals is null) |
| | | 27 | | { |
| | 0 | 28 | | throw new InvalidOperationException( |
| | 0 | 29 | | $"Persisted outcome for {outcome.HomeTeam} vs {outcome.AwayTeam} does not contain a completed score."); |
| | | 30 | | } |
| | | 31 | | |
| | 1 | 32 | | var promptMatch = RehydrateForPromptOutput(outcome); |
| | 1 | 33 | | using var matchJsonDocument = JsonDocument.Parse(PredictionPromptComposer.CreateMatchJson(promptMatch)); |
| | | 34 | | |
| | 1 | 35 | | return new HostedMatchExperimentDatasetItem( |
| | 1 | 36 | | BuildHostedDatasetItemId(outcome.Competition, outcome.CommunityContext, tippSpielId), |
| | 1 | 37 | | matchJsonDocument.RootElement.Clone(), |
| | 1 | 38 | | new HostedMatchExperimentExpectedOutput( |
| | 1 | 39 | | outcome.HomeGoals.Value, |
| | 1 | 40 | | outcome.AwayGoals.Value), |
| | 1 | 41 | | new HostedMatchExperimentMetadata( |
| | 1 | 42 | | outcome.Competition, |
| | 1 | 43 | | Season, |
| | 1 | 44 | | outcome.CommunityContext, |
| | 1 | 45 | | outcome.Matchday, |
| | 1 | 46 | | $"md{outcome.Matchday:00}", |
| | 1 | 47 | | outcome.HomeTeam, |
| | 1 | 48 | | outcome.AwayTeam, |
| | 1 | 49 | | tippSpielId)); |
| | 1 | 50 | | } |
| | | 51 | | |
| | | 52 | | public static HostedMatchExperimentDatasetItem BuildHostedDatasetItem( |
| | | 53 | | CollectedMatchOutcome outcome, |
| | | 54 | | string communityContext, |
| | | 55 | | string competition) |
| | | 56 | | { |
| | 1 | 57 | | ArgumentNullException.ThrowIfNull(outcome); |
| | 1 | 58 | | ArgumentException.ThrowIfNullOrWhiteSpace(communityContext); |
| | 1 | 59 | | ArgumentException.ThrowIfNullOrWhiteSpace(competition); |
| | | 60 | | |
| | 1 | 61 | | var tippSpielId = outcome.TippSpielId ?? throw new InvalidOperationException( |
| | 1 | 62 | | $"Collected outcome for {outcome.HomeTeam} vs {outcome.AwayTeam} is missing tippspielId."); |
| | | 63 | | |
| | 1 | 64 | | if (!outcome.HasOutcome || outcome.HomeGoals is null || outcome.AwayGoals is null) |
| | | 65 | | { |
| | 0 | 66 | | throw new InvalidOperationException( |
| | 0 | 67 | | $"Collected outcome for {outcome.HomeTeam} vs {outcome.AwayTeam} does not contain a completed score."); |
| | | 68 | | } |
| | | 69 | | |
| | 1 | 70 | | var promptMatch = RehydrateForPromptOutput(new Match(outcome.HomeTeam, outcome.AwayTeam, outcome.StartsAt, outco |
| | 1 | 71 | | using var matchJsonDocument = JsonDocument.Parse(PredictionPromptComposer.CreateMatchJson(promptMatch)); |
| | | 72 | | |
| | 1 | 73 | | return new HostedMatchExperimentDatasetItem( |
| | 1 | 74 | | BuildHostedDatasetItemId(competition, communityContext, tippSpielId), |
| | 1 | 75 | | matchJsonDocument.RootElement.Clone(), |
| | 1 | 76 | | new HostedMatchExperimentExpectedOutput( |
| | 1 | 77 | | outcome.HomeGoals.Value, |
| | 1 | 78 | | outcome.AwayGoals.Value), |
| | 1 | 79 | | new HostedMatchExperimentMetadata( |
| | 1 | 80 | | competition, |
| | 1 | 81 | | Season, |
| | 1 | 82 | | communityContext, |
| | 1 | 83 | | outcome.Matchday, |
| | 1 | 84 | | $"md{outcome.Matchday:00}", |
| | 1 | 85 | | outcome.HomeTeam, |
| | 1 | 86 | | outcome.AwayTeam, |
| | 1 | 87 | | tippSpielId)); |
| | 1 | 88 | | } |
| | | 89 | | |
| | | 90 | | public static Match RehydrateForPromptOutput(PersistedMatchOutcome outcome) |
| | | 91 | | { |
| | 1 | 92 | | ArgumentNullException.ThrowIfNull(outcome); |
| | 1 | 93 | | return RehydrateForPromptOutput(new Match(outcome.HomeTeam, outcome.AwayTeam, outcome.StartsAt, outcome.Matchday |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | public static Match RehydrateForPromptOutput(Match match) |
| | | 97 | | { |
| | 1 | 98 | | ArgumentNullException.ThrowIfNull(match); |
| | | 99 | | |
| | 1 | 100 | | var instant = match.StartsAt.ToInstant(); |
| | 1 | 101 | | var offset = BundesligaTimeZone.GetUtcOffset(instant); |
| | 1 | 102 | | var localizedStartsAt = instant.InZone(DateTimeZone.ForOffset(offset)); |
| | 1 | 103 | | return new Match(match.HomeTeam, match.AwayTeam, localizedStartsAt, match.Matchday, match.IsCancelled); |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | public static string BuildSourceDatasetName(string communityContext) |
| | | 107 | | { |
| | 1 | 108 | | ArgumentException.ThrowIfNullOrWhiteSpace(communityContext); |
| | 1 | 109 | | return $"match-predictions/bundesliga-2025-26/{communityContext}"; |
| | | 110 | | } |
| | | 111 | | |
| | | 112 | | public static string BuildCanonicalDatasetName(string communityContext) |
| | | 113 | | { |
| | 0 | 114 | | return BuildSourceDatasetName(communityContext); |
| | | 115 | | } |
| | | 116 | | public static string BuildHostedDatasetItemId(string competition, string communityContext, string tippSpielId) |
| | | 117 | | { |
| | 1 | 118 | | return string.Join( |
| | 1 | 119 | | "__", |
| | 1 | 120 | | Slugify(competition), |
| | 1 | 121 | | Slugify(communityContext), |
| | 1 | 122 | | $"ts{Slugify(tippSpielId)}"); |
| | | 123 | | } |
| | | 124 | | |
| | | 125 | | public static string BuildSliceDatasetItemId(string sourceItemId, string sliceKey) |
| | | 126 | | { |
| | 1 | 127 | | return $"{sourceItemId}__slice__{sliceKey}"; |
| | | 128 | | } |
| | | 129 | | |
| | | 130 | | public static string BuildRepeatedSliceDatasetItemId( |
| | | 131 | | string sourceItemId, |
| | | 132 | | string sliceKey, |
| | | 133 | | int repetitionIndex, |
| | | 134 | | int totalRepetitions) |
| | | 135 | | { |
| | 1 | 136 | | ArgumentException.ThrowIfNullOrWhiteSpace(sourceItemId); |
| | 1 | 137 | | ArgumentException.ThrowIfNullOrWhiteSpace(sliceKey); |
| | | 138 | | |
| | 1 | 139 | | if (repetitionIndex < 1) |
| | | 140 | | { |
| | 0 | 141 | | throw new ArgumentOutOfRangeException(nameof(repetitionIndex), repetitionIndex, "Repetition index must be at |
| | | 142 | | } |
| | | 143 | | |
| | 1 | 144 | | if (totalRepetitions < 1) |
| | | 145 | | { |
| | 0 | 146 | | throw new ArgumentOutOfRangeException(nameof(totalRepetitions), totalRepetitions, "Total repetitions must be |
| | | 147 | | } |
| | | 148 | | |
| | 1 | 149 | | var paddingWidth = Math.Max(2, totalRepetitions.ToString(CultureInfo.InvariantCulture).Length); |
| | 1 | 150 | | var repetitionToken = repetitionIndex.ToString($"D{paddingWidth}", CultureInfo.InvariantCulture); |
| | 1 | 151 | | return $"{sourceItemId}__repeated-match__{sliceKey}__{repetitionToken}"; |
| | | 152 | | } |
| | | 153 | | |
| | | 154 | | public static string BuildRepeatedMatchSliceDatasetItemId( |
| | | 155 | | string sourceItemId, |
| | | 156 | | string sliceKey, |
| | | 157 | | int fixtureIndex, |
| | | 158 | | int totalFixtures, |
| | | 159 | | int repetitionIndex, |
| | | 160 | | int totalRepetitions) |
| | | 161 | | { |
| | 1 | 162 | | ArgumentException.ThrowIfNullOrWhiteSpace(sourceItemId); |
| | 1 | 163 | | ArgumentException.ThrowIfNullOrWhiteSpace(sliceKey); |
| | | 164 | | |
| | 1 | 165 | | if (fixtureIndex < 1) |
| | | 166 | | { |
| | 0 | 167 | | throw new ArgumentOutOfRangeException(nameof(fixtureIndex), fixtureIndex, "Fixture index must be at least 1. |
| | | 168 | | } |
| | | 169 | | |
| | 1 | 170 | | if (totalFixtures < 1) |
| | | 171 | | { |
| | 0 | 172 | | throw new ArgumentOutOfRangeException(nameof(totalFixtures), totalFixtures, "Total fixtures must be at least |
| | | 173 | | } |
| | | 174 | | |
| | 1 | 175 | | if (repetitionIndex < 1) |
| | | 176 | | { |
| | 0 | 177 | | throw new ArgumentOutOfRangeException(nameof(repetitionIndex), repetitionIndex, "Repetition index must be at |
| | | 178 | | } |
| | | 179 | | |
| | 1 | 180 | | if (totalRepetitions < 1) |
| | | 181 | | { |
| | 0 | 182 | | throw new ArgumentOutOfRangeException(nameof(totalRepetitions), totalRepetitions, "Total repetitions must be |
| | | 183 | | } |
| | | 184 | | |
| | 1 | 185 | | var fixturePaddingWidth = Math.Max(2, totalFixtures.ToString(CultureInfo.InvariantCulture).Length); |
| | 1 | 186 | | var repetitionPaddingWidth = Math.Max(2, totalRepetitions.ToString(CultureInfo.InvariantCulture).Length); |
| | 1 | 187 | | var fixtureToken = fixtureIndex.ToString($"D{fixturePaddingWidth}", CultureInfo.InvariantCulture); |
| | 1 | 188 | | var repetitionToken = repetitionIndex.ToString($"D{repetitionPaddingWidth}", CultureInfo.InvariantCulture); |
| | 1 | 189 | | return $"{sourceItemId}__repeated-match-slice__{sliceKey}__m{fixtureToken}__{repetitionToken}"; |
| | | 190 | | } |
| | | 191 | | |
| | | 192 | | public static string BuildRepeatedMatchSourcePoolKey(int matchday, string homeTeam, string awayTeam) |
| | | 193 | | { |
| | 1 | 194 | | return $"md{matchday:00}-{Slugify(homeTeam)}-vs-{Slugify(awayTeam)}"; |
| | | 195 | | } |
| | | 196 | | |
| | | 197 | | public static string BuildSingleMatchSourcePoolKey(int matchday, string homeTeam, string awayTeam) |
| | | 198 | | { |
| | 0 | 199 | | return BuildRepeatedMatchSourcePoolKey(matchday, homeTeam, awayTeam); |
| | | 200 | | } |
| | | 201 | | |
| | | 202 | | public static string ComputeSelectedItemIdsHash(IEnumerable<string> itemIds) |
| | | 203 | | { |
| | 1 | 204 | | ArgumentNullException.ThrowIfNull(itemIds); |
| | | 205 | | |
| | 1 | 206 | | var joined = string.Join("\n", itemIds.OrderBy(itemId => itemId, StringComparer.Ordinal)); |
| | 1 | 207 | | var hash = SHA256.HashData(Encoding.UTF8.GetBytes(joined)); |
| | 1 | 208 | | return Convert.ToHexString(hash).ToLowerInvariant(); |
| | | 209 | | } |
| | | 210 | | |
| | | 211 | | public static string BuildRelativeEvaluationPolicyKey(EvaluationTimestampPolicy policy) |
| | | 212 | | { |
| | 0 | 213 | | ArgumentNullException.ThrowIfNull(policy); |
| | | 214 | | |
| | 0 | 215 | | if (!string.Equals(policy.Kind, EvaluationTimestampPolicy.RelativeKind, StringComparison.OrdinalIgnoreCase) |
| | 0 | 216 | | || !string.Equals(policy.Reference, EvaluationTimestampPolicy.StartsAtReference, StringComparison.OrdinalIgn |
| | | 217 | | { |
| | 0 | 218 | | throw new ArgumentException("Only startsAt-relative evaluation policies can be converted to a policy key.", |
| | | 219 | | } |
| | | 220 | | |
| | 0 | 221 | | var timeSpan = policy.Offset.ToTimeSpan(); |
| | 0 | 222 | | var sign = timeSpan.Ticks < 0 ? "-" : "+"; |
| | 0 | 223 | | var absolute = timeSpan.Duration(); |
| | 0 | 224 | | var parts = new List<string>(); |
| | | 225 | | |
| | 0 | 226 | | if (absolute.Days != 0) |
| | | 227 | | { |
| | 0 | 228 | | parts.Add($"{absolute.Days}d"); |
| | | 229 | | } |
| | | 230 | | |
| | 0 | 231 | | if (absolute.Hours != 0) |
| | | 232 | | { |
| | 0 | 233 | | parts.Add($"{absolute.Hours}h"); |
| | | 234 | | } |
| | | 235 | | |
| | 0 | 236 | | if (absolute.Minutes != 0) |
| | | 237 | | { |
| | 0 | 238 | | parts.Add($"{absolute.Minutes}m"); |
| | | 239 | | } |
| | | 240 | | |
| | 0 | 241 | | if (absolute.Seconds != 0) |
| | | 242 | | { |
| | 0 | 243 | | parts.Add($"{absolute.Seconds}s"); |
| | | 244 | | } |
| | | 245 | | |
| | 0 | 246 | | if (parts.Count == 0) |
| | | 247 | | { |
| | 0 | 248 | | parts.Add("0s"); |
| | | 249 | | } |
| | | 250 | | |
| | 0 | 251 | | return $"startsAt{sign}{string.Join(string.Empty, parts)}".ToLowerInvariant(); |
| | | 252 | | } |
| | | 253 | | |
| | | 254 | | public static string FormatStartedAtUtc(DateTimeOffset timestamp) |
| | | 255 | | { |
| | 1 | 256 | | return timestamp.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture); |
| | | 257 | | } |
| | | 258 | | |
| | | 259 | | public static string Slugify(string value) |
| | | 260 | | { |
| | 1 | 261 | | ArgumentException.ThrowIfNullOrWhiteSpace(value); |
| | | 262 | | |
| | 1 | 263 | | var normalized = value.Normalize(NormalizationForm.FormD); |
| | 1 | 264 | | var builder = new StringBuilder(normalized.Length); |
| | | 265 | | |
| | 1 | 266 | | foreach (var character in normalized) |
| | | 267 | | { |
| | 1 | 268 | | if (CharUnicodeInfo.GetUnicodeCategory(character) == UnicodeCategory.NonSpacingMark) |
| | | 269 | | { |
| | | 270 | | continue; |
| | | 271 | | } |
| | | 272 | | |
| | 1 | 273 | | if (char.IsLetterOrDigit(character)) |
| | | 274 | | { |
| | 1 | 275 | | builder.Append(char.ToLowerInvariant(character)); |
| | 1 | 276 | | continue; |
| | | 277 | | } |
| | | 278 | | |
| | 1 | 279 | | if (builder.Length == 0 || builder[^1] == '-') |
| | | 280 | | { |
| | | 281 | | continue; |
| | | 282 | | } |
| | | 283 | | |
| | 1 | 284 | | builder.Append('-'); |
| | | 285 | | } |
| | | 286 | | |
| | 1 | 287 | | return builder.ToString().Trim('-'); |
| | | 288 | | } |
| | | 289 | | } |