| | | 1 | | using System.Collections.Generic; |
| | | 2 | | using System.Linq; |
| | | 3 | | using System.Text.Json; |
| | | 4 | | using System.Text.Json.Serialization; |
| | | 5 | | using EHonda.KicktippAi.Core; |
| | | 6 | | using FirebaseAdapter.Models; |
| | | 7 | | using Google.Cloud.Firestore; |
| | | 8 | | using Microsoft.Extensions.Logging; |
| | | 9 | | using NodaTime; |
| | | 10 | | |
| | | 11 | | namespace FirebaseAdapter; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Firebase Firestore implementation of the prediction repository. |
| | | 15 | | /// </summary> |
| | | 16 | | public class FirebasePredictionRepository : IPredictionRepository |
| | | 17 | | { |
| | 1 | 18 | | private static readonly JsonSerializerOptions JustificationSerializerOptions = new() |
| | 1 | 19 | | { |
| | 1 | 20 | | PropertyNamingPolicy = JsonNamingPolicy.CamelCase, |
| | 1 | 21 | | DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull |
| | 1 | 22 | | }; |
| | | 23 | | |
| | | 24 | | private readonly FirestoreDb _firestoreDb; |
| | | 25 | | private readonly ILogger<FirebasePredictionRepository> _logger; |
| | | 26 | | private readonly string _predictionsCollection; |
| | | 27 | | private readonly string _matchesCollection; |
| | | 28 | | private readonly string _bonusPredictionsCollection; |
| | | 29 | | private readonly string _competition; |
| | | 30 | | |
| | 1 | 31 | | public FirebasePredictionRepository(FirestoreDb firestoreDb, ILogger<FirebasePredictionRepository> logger) |
| | | 32 | | { |
| | 1 | 33 | | _firestoreDb = firestoreDb ?? throw new ArgumentNullException(nameof(firestoreDb)); |
| | 1 | 34 | | _logger = logger ?? throw new ArgumentNullException(nameof(logger)); |
| | | 35 | | |
| | | 36 | | // Use unified collection names (no longer community-specific) |
| | 1 | 37 | | _predictionsCollection = "match-predictions"; |
| | 1 | 38 | | _matchesCollection = "matches"; |
| | 1 | 39 | | _bonusPredictionsCollection = "bonus-predictions"; |
| | 1 | 40 | | _competition = "bundesliga-2025-26"; // Remove community suffix |
| | | 41 | | |
| | 1 | 42 | | _logger.LogInformation("Firebase repository initialized"); |
| | 1 | 43 | | } |
| | | 44 | | |
| | | 45 | | public async Task SavePredictionAsync(Match match, Prediction prediction, string model, string tokenUsage, double co |
| | | 46 | | { |
| | | 47 | | try |
| | | 48 | | { |
| | 1 | 49 | | var now = Timestamp.GetCurrentTimestamp(); |
| | | 50 | | |
| | | 51 | | // Check if a prediction already exists for this match, model, and community context |
| | | 52 | | // Order by repredictionIndex descending to get the latest version for updating |
| | 1 | 53 | | var query = _firestoreDb.Collection(_predictionsCollection) |
| | 1 | 54 | | .WhereEqualTo("homeTeam", match.HomeTeam) |
| | 1 | 55 | | .WhereEqualTo("awayTeam", match.AwayTeam) |
| | 1 | 56 | | .WhereEqualTo("startsAt", ConvertToTimestamp(match.StartsAt)) |
| | 1 | 57 | | .WhereEqualTo("competition", _competition) |
| | 1 | 58 | | .WhereEqualTo("model", model) |
| | 1 | 59 | | .WhereEqualTo("communityContext", communityContext) |
| | 1 | 60 | | .OrderByDescending("repredictionIndex") |
| | 1 | 61 | | .Limit(1); |
| | | 62 | | |
| | 1 | 63 | | var snapshot = await query.GetSnapshotAsync(cancellationToken); |
| | | 64 | | |
| | | 65 | | DocumentReference docRef; |
| | 1 | 66 | | bool isUpdate = false; |
| | 1 | 67 | | Timestamp? existingCreatedAt = null; |
| | 1 | 68 | | int repredictionIndex = 0; |
| | | 69 | | |
| | 1 | 70 | | if (snapshot.Documents.Count > 0) |
| | | 71 | | { |
| | | 72 | | // Update existing document (latest reprediction) |
| | 1 | 73 | | var existingDoc = snapshot.Documents.First(); |
| | 1 | 74 | | docRef = existingDoc.Reference; |
| | 1 | 75 | | isUpdate = true; |
| | | 76 | | |
| | | 77 | | // Preserve the original values |
| | 1 | 78 | | var existingData = existingDoc.ConvertTo<FirestoreMatchPrediction>(); |
| | 1 | 79 | | existingCreatedAt = existingData.CreatedAt; |
| | 1 | 80 | | repredictionIndex = existingData.RepredictionIndex; // Keep same reprediction index for override |
| | | 81 | | |
| | 1 | 82 | | _logger.LogDebug("Updating existing prediction for match {HomeTeam} vs {AwayTeam} (document: {DocumentId |
| | 1 | 83 | | match.HomeTeam, match.AwayTeam, existingDoc.Id, repredictionIndex); |
| | | 84 | | } |
| | | 85 | | else |
| | | 86 | | { |
| | | 87 | | // Create new document |
| | 1 | 88 | | var documentId = Guid.NewGuid().ToString(); |
| | 1 | 89 | | docRef = _firestoreDb.Collection(_predictionsCollection).Document(documentId); |
| | 1 | 90 | | repredictionIndex = 0; // First prediction |
| | | 91 | | |
| | 1 | 92 | | _logger.LogDebug("Creating new prediction for match {HomeTeam} vs {AwayTeam} (document: {DocumentId}, re |
| | 1 | 93 | | match.HomeTeam, match.AwayTeam, documentId, repredictionIndex); |
| | | 94 | | } |
| | | 95 | | |
| | 1 | 96 | | var firestorePrediction = new FirestoreMatchPrediction |
| | 1 | 97 | | { |
| | 1 | 98 | | Id = docRef.Id, |
| | 1 | 99 | | HomeTeam = match.HomeTeam, |
| | 1 | 100 | | AwayTeam = match.AwayTeam, |
| | 1 | 101 | | StartsAt = ConvertToTimestamp(match.StartsAt), |
| | 1 | 102 | | Matchday = match.Matchday, |
| | 1 | 103 | | HomeGoals = prediction.HomeGoals, |
| | 1 | 104 | | AwayGoals = prediction.AwayGoals, |
| | 1 | 105 | | Justification = SerializeJustification(prediction.Justification), |
| | 1 | 106 | | UpdatedAt = now, |
| | 1 | 107 | | Competition = _competition, |
| | 1 | 108 | | Model = model, |
| | 1 | 109 | | TokenUsage = tokenUsage, |
| | 1 | 110 | | Cost = cost, |
| | 1 | 111 | | CommunityContext = communityContext, |
| | 1 | 112 | | ContextDocumentNames = contextDocumentNames.ToArray(), |
| | 1 | 113 | | RepredictionIndex = repredictionIndex |
| | 1 | 114 | | }; |
| | | 115 | | |
| | | 116 | | // Set CreatedAt: preserve existing value for updates unless overrideCreatedAt is explicitly requested |
| | 1 | 117 | | firestorePrediction.CreatedAt = (overrideCreatedAt || existingCreatedAt == null) ? now : existingCreatedAt.V |
| | | 118 | | |
| | 1 | 119 | | await docRef.SetAsync(firestorePrediction, cancellationToken: cancellationToken); |
| | | 120 | | |
| | 1 | 121 | | var action = isUpdate ? "Updated" : "Saved"; |
| | 1 | 122 | | _logger.LogInformation("{Action} prediction for match {HomeTeam} vs {AwayTeam} on matchday {Matchday} (repre |
| | 1 | 123 | | action, match.HomeTeam, match.AwayTeam, match.Matchday, repredictionIndex); |
| | 1 | 124 | | } |
| | 0 | 125 | | catch (Exception ex) |
| | | 126 | | { |
| | 0 | 127 | | _logger.LogError(ex, "Failed to save prediction for match {HomeTeam} vs {AwayTeam}", |
| | 0 | 128 | | match.HomeTeam, match.AwayTeam); |
| | 0 | 129 | | throw; |
| | | 130 | | } |
| | 1 | 131 | | } |
| | | 132 | | |
| | | 133 | | public async Task<Prediction?> GetPredictionAsync(Match match, string model, string communityContext, CancellationTo |
| | | 134 | | { |
| | 1 | 135 | | return await GetPredictionAsync(match.HomeTeam, match.AwayTeam, match.StartsAt, model, communityContext, cancell |
| | 1 | 136 | | } |
| | | 137 | | |
| | | 138 | | public async Task<Prediction?> GetPredictionAsync(string homeTeam, string awayTeam, ZonedDateTime startsAt, string m |
| | | 139 | | { |
| | | 140 | | try |
| | | 141 | | { |
| | | 142 | | // Query by match characteristics, model, community context, and competition |
| | | 143 | | // Order by repredictionIndex descending to get the latest version |
| | 1 | 144 | | var query = _firestoreDb.Collection(_predictionsCollection) |
| | 1 | 145 | | .WhereEqualTo("homeTeam", homeTeam) |
| | 1 | 146 | | .WhereEqualTo("awayTeam", awayTeam) |
| | 1 | 147 | | .WhereEqualTo("startsAt", ConvertToTimestamp(startsAt)) |
| | 1 | 148 | | .WhereEqualTo("competition", _competition) |
| | 1 | 149 | | .WhereEqualTo("model", model) |
| | 1 | 150 | | .WhereEqualTo("communityContext", communityContext) |
| | 1 | 151 | | .OrderByDescending("repredictionIndex") |
| | 1 | 152 | | .Limit(1); |
| | | 153 | | |
| | 1 | 154 | | var snapshot = await query.GetSnapshotAsync(cancellationToken); |
| | | 155 | | |
| | 1 | 156 | | if (snapshot.Documents.Count == 0) |
| | | 157 | | { |
| | 1 | 158 | | return null; |
| | | 159 | | } |
| | | 160 | | |
| | 1 | 161 | | var firestorePrediction = snapshot.Documents.First().ConvertTo<FirestoreMatchPrediction>(); |
| | 1 | 162 | | return new Prediction( |
| | 1 | 163 | | firestorePrediction.HomeGoals, |
| | 1 | 164 | | firestorePrediction.AwayGoals, |
| | 1 | 165 | | DeserializeJustification(firestorePrediction.Justification)); |
| | | 166 | | } |
| | 0 | 167 | | catch (Exception ex) |
| | | 168 | | { |
| | 0 | 169 | | _logger.LogError(ex, "Failed to get prediction for match {HomeTeam} vs {AwayTeam} using model {Model} and co |
| | 0 | 170 | | homeTeam, awayTeam, model, communityContext); |
| | 0 | 171 | | throw; |
| | | 172 | | } |
| | 1 | 173 | | } |
| | | 174 | | |
| | | 175 | | public async Task<PredictionMetadata?> GetPredictionMetadataAsync(Match match, string model, string communityContext |
| | | 176 | | { |
| | | 177 | | try |
| | | 178 | | { |
| | | 179 | | // Query by match characteristics, model, community context, and competition |
| | 1 | 180 | | var query = _firestoreDb.Collection(_predictionsCollection) |
| | 1 | 181 | | .WhereEqualTo("homeTeam", match.HomeTeam) |
| | 1 | 182 | | .WhereEqualTo("awayTeam", match.AwayTeam) |
| | 1 | 183 | | .WhereEqualTo("startsAt", ConvertToTimestamp(match.StartsAt)) |
| | 1 | 184 | | .WhereEqualTo("competition", _competition) |
| | 1 | 185 | | .WhereEqualTo("model", model) |
| | 1 | 186 | | .WhereEqualTo("communityContext", communityContext); |
| | | 187 | | |
| | 1 | 188 | | var snapshot = await query.GetSnapshotAsync(cancellationToken); |
| | | 189 | | |
| | 1 | 190 | | if (snapshot.Documents.Count == 0) |
| | | 191 | | { |
| | 0 | 192 | | return null; |
| | | 193 | | } |
| | | 194 | | |
| | 1 | 195 | | var firestorePrediction = snapshot.Documents.First().ConvertTo<FirestoreMatchPrediction>(); |
| | 1 | 196 | | var prediction = new Prediction( |
| | 1 | 197 | | firestorePrediction.HomeGoals, |
| | 1 | 198 | | firestorePrediction.AwayGoals, |
| | 1 | 199 | | DeserializeJustification(firestorePrediction.Justification)); |
| | 1 | 200 | | var createdAt = firestorePrediction.CreatedAt.ToDateTimeOffset(); |
| | 1 | 201 | | var contextDocumentNames = firestorePrediction.ContextDocumentNames?.ToList() ?? new List<string>(); |
| | | 202 | | |
| | 1 | 203 | | return new PredictionMetadata(prediction, createdAt, contextDocumentNames); |
| | | 204 | | } |
| | 0 | 205 | | catch (Exception ex) |
| | | 206 | | { |
| | 0 | 207 | | _logger.LogError(ex, "Failed to get prediction metadata for match {HomeTeam} vs {AwayTeam} using model {Mode |
| | 0 | 208 | | match.HomeTeam, match.AwayTeam, model, communityContext); |
| | 0 | 209 | | throw; |
| | | 210 | | } |
| | 1 | 211 | | } |
| | | 212 | | |
| | | 213 | | public async Task<IReadOnlyList<Match>> GetMatchDayAsync(int matchDay, CancellationToken cancellationToken = default |
| | | 214 | | { |
| | | 215 | | try |
| | | 216 | | { |
| | 1 | 217 | | var query = _firestoreDb.Collection(_matchesCollection) |
| | 1 | 218 | | .WhereEqualTo("competition", _competition) |
| | 1 | 219 | | .WhereEqualTo("matchday", matchDay) |
| | 1 | 220 | | .OrderBy("startsAt"); |
| | | 221 | | |
| | 1 | 222 | | var snapshot = await query.GetSnapshotAsync(cancellationToken); |
| | | 223 | | |
| | 1 | 224 | | var matches = snapshot.Documents |
| | 1 | 225 | | .Select(doc => doc.ConvertTo<FirestoreMatch>()) |
| | 1 | 226 | | .Select(fm => new Match( |
| | 1 | 227 | | fm.HomeTeam, |
| | 1 | 228 | | fm.AwayTeam, |
| | 1 | 229 | | ConvertFromTimestamp(fm.StartsAt), |
| | 1 | 230 | | fm.Matchday, |
| | 1 | 231 | | fm.IsCancelled)) |
| | 1 | 232 | | .ToList(); |
| | | 233 | | |
| | 1 | 234 | | return matches.AsReadOnly(); |
| | | 235 | | } |
| | 0 | 236 | | catch (Exception ex) |
| | | 237 | | { |
| | 0 | 238 | | _logger.LogError(ex, "Failed to get matches for matchday {Matchday}", matchDay); |
| | 0 | 239 | | throw; |
| | | 240 | | } |
| | 1 | 241 | | } |
| | | 242 | | |
| | | 243 | | public async Task<IReadOnlyList<MatchPrediction>> GetMatchDayWithPredictionsAsync(int matchDay, string model, string |
| | | 244 | | { |
| | | 245 | | try |
| | | 246 | | { |
| | | 247 | | // Get all matches for the matchday |
| | 1 | 248 | | var matches = await GetMatchDayAsync(matchDay, cancellationToken); |
| | | 249 | | |
| | | 250 | | // Get predictions for all matches using the specified model and community context |
| | 1 | 251 | | var matchPredictions = new List<MatchPrediction>(); |
| | | 252 | | |
| | 1 | 253 | | foreach (var match in matches) |
| | | 254 | | { |
| | 1 | 255 | | var prediction = await GetPredictionAsync(match, model, communityContext, cancellationToken); |
| | 1 | 256 | | matchPredictions.Add(new MatchPrediction(match, prediction)); |
| | 1 | 257 | | } |
| | | 258 | | |
| | 1 | 259 | | return matchPredictions.AsReadOnly(); |
| | | 260 | | } |
| | 0 | 261 | | catch (Exception ex) |
| | | 262 | | { |
| | 0 | 263 | | _logger.LogError(ex, "Failed to get matches with predictions for matchday {Matchday} using model {Model} and |
| | 0 | 264 | | throw; |
| | | 265 | | } |
| | 1 | 266 | | } |
| | | 267 | | |
| | | 268 | | public async Task<IReadOnlyList<MatchPrediction>> GetAllPredictionsAsync(string model, string communityContext, Canc |
| | | 269 | | { |
| | | 270 | | try |
| | | 271 | | { |
| | 1 | 272 | | var query = _firestoreDb.Collection(_predictionsCollection) |
| | 1 | 273 | | .WhereEqualTo("competition", _competition) |
| | 1 | 274 | | .WhereEqualTo("model", model) |
| | 1 | 275 | | .WhereEqualTo("communityContext", communityContext) |
| | 1 | 276 | | .OrderBy("matchday"); |
| | | 277 | | |
| | 1 | 278 | | var snapshot = await query.GetSnapshotAsync(cancellationToken); |
| | | 279 | | |
| | 1 | 280 | | var matchPredictions = snapshot.Documents |
| | 1 | 281 | | .Select(doc => doc.ConvertTo<FirestoreMatchPrediction>()) |
| | 1 | 282 | | .Select(fp => new MatchPrediction( |
| | 1 | 283 | | new Match(fp.HomeTeam, fp.AwayTeam, ConvertFromTimestamp(fp.StartsAt), fp.Matchday), |
| | 1 | 284 | | new Prediction( |
| | 1 | 285 | | fp.HomeGoals, |
| | 1 | 286 | | fp.AwayGoals, |
| | 1 | 287 | | DeserializeJustification(fp.Justification)))) |
| | 1 | 288 | | .ToList(); |
| | | 289 | | |
| | 1 | 290 | | return matchPredictions.AsReadOnly(); |
| | | 291 | | } |
| | 0 | 292 | | catch (Exception ex) |
| | | 293 | | { |
| | 0 | 294 | | _logger.LogError(ex, "Failed to get all predictions for model {Model} and community context {CommunityContex |
| | 0 | 295 | | throw; |
| | | 296 | | } |
| | 1 | 297 | | } |
| | | 298 | | |
| | | 299 | | public async Task<bool> HasPredictionAsync(Match match, string model, string communityContext, CancellationToken can |
| | | 300 | | { |
| | | 301 | | try |
| | | 302 | | { |
| | | 303 | | // Query by match characteristics, model, and community context instead of using deterministic ID |
| | 1 | 304 | | var query = _firestoreDb.Collection(_predictionsCollection) |
| | 1 | 305 | | .WhereEqualTo("homeTeam", match.HomeTeam) |
| | 1 | 306 | | .WhereEqualTo("awayTeam", match.AwayTeam) |
| | 1 | 307 | | .WhereEqualTo("startsAt", ConvertToTimestamp(match.StartsAt)) |
| | 1 | 308 | | .WhereEqualTo("competition", _competition) |
| | 1 | 309 | | .WhereEqualTo("model", model) |
| | 1 | 310 | | .WhereEqualTo("communityContext", communityContext); |
| | | 311 | | |
| | 1 | 312 | | var snapshot = await query.GetSnapshotAsync(cancellationToken); |
| | 1 | 313 | | return snapshot.Documents.Count > 0; |
| | | 314 | | } |
| | 0 | 315 | | catch (Exception ex) |
| | | 316 | | { |
| | 0 | 317 | | _logger.LogError(ex, "Failed to check if prediction exists for match {HomeTeam} vs {AwayTeam} using model {M |
| | 0 | 318 | | match.HomeTeam, match.AwayTeam, model, communityContext); |
| | 0 | 319 | | throw; |
| | | 320 | | } |
| | 1 | 321 | | } |
| | | 322 | | |
| | | 323 | | public async Task SaveBonusPredictionAsync(BonusQuestion bonusQuestion, BonusPrediction bonusPrediction, string mode |
| | | 324 | | { |
| | | 325 | | try |
| | | 326 | | { |
| | 1 | 327 | | var now = Timestamp.GetCurrentTimestamp(); |
| | | 328 | | |
| | | 329 | | // Check if a prediction already exists for this question, model, and community context |
| | | 330 | | // Order by repredictionIndex descending to get the latest version for updating |
| | 1 | 331 | | var query = _firestoreDb.Collection(_bonusPredictionsCollection) |
| | 1 | 332 | | .WhereEqualTo("questionText", bonusQuestion.Text) |
| | 1 | 333 | | .WhereEqualTo("competition", _competition) |
| | 1 | 334 | | .WhereEqualTo("model", model) |
| | 1 | 335 | | .WhereEqualTo("communityContext", communityContext) |
| | 1 | 336 | | .OrderByDescending("repredictionIndex") |
| | 1 | 337 | | .Limit(1); |
| | | 338 | | |
| | 1 | 339 | | var snapshot = await query.GetSnapshotAsync(cancellationToken); |
| | | 340 | | |
| | | 341 | | DocumentReference docRef; |
| | 1 | 342 | | bool isUpdate = false; |
| | 1 | 343 | | Timestamp? existingCreatedAt = null; |
| | 1 | 344 | | int repredictionIndex = 0; |
| | | 345 | | |
| | 1 | 346 | | if (snapshot.Documents.Count > 0) |
| | | 347 | | { |
| | | 348 | | // Update existing document (latest reprediction) |
| | 1 | 349 | | var existingDoc = snapshot.Documents.First(); |
| | 1 | 350 | | docRef = existingDoc.Reference; |
| | 1 | 351 | | isUpdate = true; |
| | | 352 | | |
| | | 353 | | // Preserve the original values |
| | 1 | 354 | | var existingData = existingDoc.ConvertTo<FirestoreBonusPrediction>(); |
| | 1 | 355 | | existingCreatedAt = existingData.CreatedAt; |
| | 1 | 356 | | repredictionIndex = existingData.RepredictionIndex; // Keep same reprediction index for override |
| | | 357 | | |
| | 1 | 358 | | _logger.LogDebug("Updating existing bonus prediction for question '{QuestionText}' (document: {DocumentI |
| | 1 | 359 | | bonusQuestion.Text, existingDoc.Id, repredictionIndex); |
| | | 360 | | } |
| | | 361 | | else |
| | | 362 | | { |
| | | 363 | | // Create new document |
| | 1 | 364 | | var documentId = Guid.NewGuid().ToString(); |
| | 1 | 365 | | docRef = _firestoreDb.Collection(_bonusPredictionsCollection).Document(documentId); |
| | 1 | 366 | | repredictionIndex = 0; // First prediction |
| | | 367 | | |
| | 1 | 368 | | _logger.LogDebug("Creating new bonus prediction for question '{QuestionText}' (document: {DocumentId}, r |
| | 1 | 369 | | bonusQuestion.Text, documentId, repredictionIndex); |
| | | 370 | | } |
| | | 371 | | |
| | | 372 | | // Extract selected option texts for observability |
| | 1 | 373 | | var optionTextsLookup = bonusQuestion.Options.ToDictionary(o => o.Id, o => o.Text); |
| | 1 | 374 | | var selectedOptionTexts = bonusPrediction.SelectedOptionIds |
| | 1 | 375 | | .Select(id => optionTextsLookup.TryGetValue(id, out var text) ? text : $"Unknown option: {id}") |
| | 1 | 376 | | .ToArray(); |
| | | 377 | | |
| | 1 | 378 | | var firestoreBonusPrediction = new FirestoreBonusPrediction |
| | 1 | 379 | | { |
| | 1 | 380 | | Id = docRef.Id, |
| | 1 | 381 | | QuestionText = bonusQuestion.Text, |
| | 1 | 382 | | SelectedOptionIds = bonusPrediction.SelectedOptionIds.ToArray(), |
| | 1 | 383 | | SelectedOptionTexts = selectedOptionTexts, |
| | 1 | 384 | | UpdatedAt = now, |
| | 1 | 385 | | Competition = _competition, |
| | 1 | 386 | | Model = model, |
| | 1 | 387 | | TokenUsage = tokenUsage, |
| | 1 | 388 | | Cost = cost, |
| | 1 | 389 | | CommunityContext = communityContext, |
| | 1 | 390 | | ContextDocumentNames = contextDocumentNames.ToArray(), |
| | 1 | 391 | | RepredictionIndex = repredictionIndex |
| | 1 | 392 | | }; |
| | | 393 | | |
| | | 394 | | // Set CreatedAt: preserve existing value for updates unless overrideCreatedAt is explicitly requested |
| | 1 | 395 | | firestoreBonusPrediction.CreatedAt = (overrideCreatedAt || existingCreatedAt == null) ? now : existingCreate |
| | | 396 | | |
| | 1 | 397 | | await docRef.SetAsync(firestoreBonusPrediction, cancellationToken: cancellationToken); |
| | | 398 | | |
| | 1 | 399 | | var action = isUpdate ? "Updated" : "Saved"; |
| | 1 | 400 | | _logger.LogDebug("{Action} bonus prediction for question '{QuestionText}' with selections: {SelectedOptions} |
| | 1 | 401 | | action, bonusQuestion.Text, string.Join(", ", selectedOptionTexts), repredictionIndex); |
| | 1 | 402 | | } |
| | 0 | 403 | | catch (Exception ex) |
| | | 404 | | { |
| | 0 | 405 | | _logger.LogError(ex, "Failed to save bonus prediction for question: {QuestionText}", |
| | 0 | 406 | | bonusQuestion.Text); |
| | 0 | 407 | | throw; |
| | | 408 | | } |
| | 1 | 409 | | } |
| | | 410 | | |
| | | 411 | | public async Task<BonusPrediction?> GetBonusPredictionAsync(string questionId, string model, string communityContext |
| | | 412 | | { |
| | | 413 | | try |
| | | 414 | | { |
| | | 415 | | // Query by questionId, model, community context, and competition instead of using direct document lookup |
| | 1 | 416 | | var query = _firestoreDb.Collection(_bonusPredictionsCollection) |
| | 1 | 417 | | .WhereEqualTo("questionId", questionId) |
| | 1 | 418 | | .WhereEqualTo("competition", _competition) |
| | 1 | 419 | | .WhereEqualTo("model", model) |
| | 1 | 420 | | .WhereEqualTo("communityContext", communityContext); |
| | | 421 | | |
| | 1 | 422 | | var snapshot = await query.GetSnapshotAsync(cancellationToken); |
| | | 423 | | |
| | 1 | 424 | | if (snapshot.Documents.Count == 0) |
| | | 425 | | { |
| | 1 | 426 | | return null; |
| | | 427 | | } |
| | | 428 | | |
| | 0 | 429 | | var firestoreBonusPrediction = snapshot.Documents.First().ConvertTo<FirestoreBonusPrediction>(); |
| | 0 | 430 | | return new BonusPrediction(firestoreBonusPrediction.SelectedOptionIds.ToList()); |
| | | 431 | | } |
| | 0 | 432 | | catch (Exception ex) |
| | | 433 | | { |
| | 0 | 434 | | _logger.LogError(ex, "Failed to get bonus prediction for question {QuestionId} using model {Model} and commu |
| | 0 | 435 | | throw; |
| | | 436 | | } |
| | 1 | 437 | | } |
| | | 438 | | |
| | | 439 | | public async Task<BonusPrediction?> GetBonusPredictionByTextAsync(string questionText, string model, string communit |
| | | 440 | | { |
| | | 441 | | try |
| | | 442 | | { |
| | | 443 | | // Query by questionText, model, and community context |
| | | 444 | | // Order by repredictionIndex descending to get the latest version |
| | 1 | 445 | | var query = _firestoreDb.Collection(_bonusPredictionsCollection) |
| | 1 | 446 | | .WhereEqualTo("questionText", questionText) |
| | 1 | 447 | | .WhereEqualTo("competition", _competition) |
| | 1 | 448 | | .WhereEqualTo("model", model) |
| | 1 | 449 | | .WhereEqualTo("communityContext", communityContext) |
| | 1 | 450 | | .OrderByDescending("repredictionIndex") |
| | 1 | 451 | | .Limit(1); |
| | | 452 | | |
| | 1 | 453 | | var snapshot = await query.GetSnapshotAsync(cancellationToken); |
| | | 454 | | |
| | 1 | 455 | | if (snapshot.Documents.Count == 0) |
| | | 456 | | { |
| | 1 | 457 | | _logger.LogDebug("No bonus prediction found for question text: {QuestionText} with model: {Model} and co |
| | 1 | 458 | | return null; |
| | | 459 | | } |
| | | 460 | | |
| | 1 | 461 | | var firestoreBonusPrediction = snapshot.Documents.First().ConvertTo<FirestoreBonusPrediction>(); |
| | 1 | 462 | | var bonusPrediction = new BonusPrediction(firestoreBonusPrediction.SelectedOptionIds.ToList()); |
| | | 463 | | |
| | 1 | 464 | | _logger.LogDebug("Found bonus prediction for question text: {QuestionText} with model: {Model} and community |
| | 1 | 465 | | questionText, model, communityContext, firestoreBonusPrediction.RepredictionIndex); |
| | | 466 | | |
| | 1 | 467 | | return bonusPrediction; |
| | | 468 | | } |
| | 0 | 469 | | catch (Exception ex) |
| | | 470 | | { |
| | 0 | 471 | | _logger.LogError(ex, "Failed to retrieve bonus prediction by text: {QuestionText} with model: {Model} and co |
| | 0 | 472 | | throw; |
| | | 473 | | } |
| | 1 | 474 | | } |
| | | 475 | | |
| | | 476 | | public async Task<BonusPredictionMetadata?> GetBonusPredictionMetadataByTextAsync(string questionText, string model, |
| | | 477 | | { |
| | | 478 | | try |
| | | 479 | | { |
| | | 480 | | // Query by questionText, model, and community context |
| | 1 | 481 | | var query = _firestoreDb.Collection(_bonusPredictionsCollection) |
| | 1 | 482 | | .WhereEqualTo("questionText", questionText) |
| | 1 | 483 | | .WhereEqualTo("competition", _competition) |
| | 1 | 484 | | .WhereEqualTo("model", model) |
| | 1 | 485 | | .WhereEqualTo("communityContext", communityContext) |
| | 1 | 486 | | .Limit(1); |
| | | 487 | | |
| | 1 | 488 | | var snapshot = await query.GetSnapshotAsync(cancellationToken); |
| | | 489 | | |
| | 1 | 490 | | if (snapshot.Documents.Count == 0) |
| | | 491 | | { |
| | 0 | 492 | | _logger.LogDebug("No bonus prediction metadata found for question text: {QuestionText} with model: {Mode |
| | 0 | 493 | | return null; |
| | | 494 | | } |
| | | 495 | | |
| | 1 | 496 | | var firestoreBonusPrediction = snapshot.Documents.First().ConvertTo<FirestoreBonusPrediction>(); |
| | 1 | 497 | | var bonusPrediction = new BonusPrediction(firestoreBonusPrediction.SelectedOptionIds.ToList()); |
| | 1 | 498 | | var createdAt = firestoreBonusPrediction.CreatedAt.ToDateTimeOffset(); |
| | 1 | 499 | | var contextDocumentNames = firestoreBonusPrediction.ContextDocumentNames?.ToList() ?? new List<string>(); |
| | | 500 | | |
| | 1 | 501 | | _logger.LogDebug("Found bonus prediction metadata for question text: {QuestionText} with model: {Model} and |
| | 1 | 502 | | questionText, model, communityContext); |
| | | 503 | | |
| | 1 | 504 | | return new BonusPredictionMetadata(bonusPrediction, createdAt, contextDocumentNames); |
| | | 505 | | } |
| | 0 | 506 | | catch (Exception ex) |
| | | 507 | | { |
| | 0 | 508 | | _logger.LogError(ex, "Failed to retrieve bonus prediction metadata by text: {QuestionText} with model: {Mode |
| | 0 | 509 | | throw; |
| | | 510 | | } |
| | 1 | 511 | | } |
| | | 512 | | |
| | | 513 | | public async Task<IReadOnlyList<BonusPrediction>> GetAllBonusPredictionsAsync(string model, string communityContext, |
| | | 514 | | { |
| | | 515 | | try |
| | | 516 | | { |
| | 1 | 517 | | var query = _firestoreDb.Collection(_bonusPredictionsCollection) |
| | 1 | 518 | | .WhereEqualTo("competition", _competition) |
| | 1 | 519 | | .WhereEqualTo("model", model) |
| | 1 | 520 | | .WhereEqualTo("communityContext", communityContext) |
| | 1 | 521 | | .OrderBy("createdAt"); |
| | | 522 | | |
| | 1 | 523 | | var snapshot = await query.GetSnapshotAsync(cancellationToken); |
| | | 524 | | |
| | 1 | 525 | | var bonusPredictions = new List<BonusPrediction>(); |
| | 1 | 526 | | foreach (var document in snapshot.Documents) |
| | | 527 | | { |
| | 1 | 528 | | var firestoreBonusPrediction = document.ConvertTo<FirestoreBonusPrediction>(); |
| | 1 | 529 | | bonusPredictions.Add(new BonusPrediction( |
| | 1 | 530 | | firestoreBonusPrediction.SelectedOptionIds.ToList())); |
| | | 531 | | } |
| | | 532 | | |
| | 1 | 533 | | return bonusPredictions.AsReadOnly(); |
| | | 534 | | } |
| | 0 | 535 | | catch (Exception ex) |
| | | 536 | | { |
| | 0 | 537 | | _logger.LogError(ex, "Failed to get all bonus predictions for model {Model} and community context {Community |
| | 0 | 538 | | throw; |
| | | 539 | | } |
| | 1 | 540 | | } |
| | | 541 | | |
| | | 542 | | public async Task<bool> HasBonusPredictionAsync(string questionId, string model, string communityContext, Cancellati |
| | | 543 | | { |
| | | 544 | | try |
| | | 545 | | { |
| | | 546 | | // Query by questionId, model, and community context instead of using direct document lookup |
| | 1 | 547 | | var query = _firestoreDb.Collection(_bonusPredictionsCollection) |
| | 1 | 548 | | .WhereEqualTo("questionId", questionId) |
| | 1 | 549 | | .WhereEqualTo("competition", _competition) |
| | 1 | 550 | | .WhereEqualTo("model", model) |
| | 1 | 551 | | .WhereEqualTo("communityContext", communityContext); |
| | | 552 | | |
| | 1 | 553 | | var snapshot = await query.GetSnapshotAsync(cancellationToken); |
| | 1 | 554 | | return snapshot.Documents.Count > 0; |
| | | 555 | | } |
| | 0 | 556 | | catch (Exception ex) |
| | | 557 | | { |
| | 0 | 558 | | _logger.LogError(ex, "Failed to check if bonus prediction exists for question {QuestionId} using model {Mode |
| | 0 | 559 | | throw; |
| | | 560 | | } |
| | 1 | 561 | | } |
| | | 562 | | |
| | | 563 | | /// <summary> |
| | | 564 | | /// Stores a match in the matches collection for matchday management. |
| | | 565 | | /// This is typically called when importing match schedules. |
| | | 566 | | /// </summary> |
| | | 567 | | public async Task StoreMatchAsync(Match match, CancellationToken cancellationToken = default) |
| | | 568 | | { |
| | | 569 | | try |
| | | 570 | | { |
| | 1 | 571 | | var documentId = Guid.NewGuid().ToString(); |
| | | 572 | | |
| | 1 | 573 | | var firestoreMatch = new FirestoreMatch |
| | 1 | 574 | | { |
| | 1 | 575 | | Id = documentId, |
| | 1 | 576 | | HomeTeam = match.HomeTeam, |
| | 1 | 577 | | AwayTeam = match.AwayTeam, |
| | 1 | 578 | | StartsAt = ConvertToTimestamp(match.StartsAt), |
| | 1 | 579 | | Matchday = match.Matchday, |
| | 1 | 580 | | Competition = _competition, |
| | 1 | 581 | | IsCancelled = match.IsCancelled |
| | 1 | 582 | | }; |
| | | 583 | | |
| | 1 | 584 | | await _firestoreDb.Collection(_matchesCollection) |
| | 1 | 585 | | .Document(documentId) |
| | 1 | 586 | | .SetAsync(firestoreMatch, cancellationToken: cancellationToken); |
| | | 587 | | |
| | 1 | 588 | | _logger.LogDebug("Stored match {HomeTeam} vs {AwayTeam} for matchday {Matchday}{Cancelled}", |
| | 1 | 589 | | match.HomeTeam, match.AwayTeam, match.Matchday, match.IsCancelled ? " (CANCELLED)" : ""); |
| | 1 | 590 | | } |
| | 0 | 591 | | catch (Exception ex) |
| | | 592 | | { |
| | 0 | 593 | | _logger.LogError(ex, "Failed to store match {HomeTeam} vs {AwayTeam}", |
| | 0 | 594 | | match.HomeTeam, match.AwayTeam); |
| | 0 | 595 | | throw; |
| | | 596 | | } |
| | 1 | 597 | | } |
| | | 598 | | |
| | | 599 | | private static Timestamp ConvertToTimestamp(ZonedDateTime zonedDateTime) |
| | | 600 | | { |
| | 1 | 601 | | var instant = zonedDateTime.ToInstant(); |
| | 1 | 602 | | return Timestamp.FromDateTimeOffset(instant.ToDateTimeOffset()); |
| | | 603 | | } |
| | | 604 | | |
| | | 605 | | private static ZonedDateTime ConvertFromTimestamp(Timestamp timestamp) |
| | | 606 | | { |
| | 1 | 607 | | var dateTimeOffset = timestamp.ToDateTimeOffset(); |
| | 1 | 608 | | var instant = Instant.FromDateTimeOffset(dateTimeOffset); |
| | 1 | 609 | | return instant.InUtc(); |
| | | 610 | | } |
| | | 611 | | |
| | | 612 | | public async Task<int> GetMatchRepredictionIndexAsync(Match match, string model, string communityContext, Cancellati |
| | | 613 | | { |
| | | 614 | | try |
| | | 615 | | { |
| | | 616 | | // Query by match characteristics, model, community context, and competition |
| | | 617 | | // Order by repredictionIndex descending to get the latest version |
| | 1 | 618 | | var query = _firestoreDb.Collection(_predictionsCollection) |
| | 1 | 619 | | .WhereEqualTo("homeTeam", match.HomeTeam) |
| | 1 | 620 | | .WhereEqualTo("awayTeam", match.AwayTeam) |
| | 1 | 621 | | .WhereEqualTo("startsAt", ConvertToTimestamp(match.StartsAt)) |
| | 1 | 622 | | .WhereEqualTo("competition", _competition) |
| | 1 | 623 | | .WhereEqualTo("model", model) |
| | 1 | 624 | | .WhereEqualTo("communityContext", communityContext) |
| | 1 | 625 | | .OrderByDescending("repredictionIndex") |
| | 1 | 626 | | .Limit(1); |
| | | 627 | | |
| | 1 | 628 | | var snapshot = await query.GetSnapshotAsync(cancellationToken); |
| | | 629 | | |
| | 1 | 630 | | if (snapshot.Documents.Count == 0) |
| | | 631 | | { |
| | 1 | 632 | | return -1; // No prediction exists |
| | | 633 | | } |
| | | 634 | | |
| | 1 | 635 | | var firestorePrediction = snapshot.Documents.First().ConvertTo<FirestoreMatchPrediction>(); |
| | 1 | 636 | | return firestorePrediction.RepredictionIndex; |
| | | 637 | | } |
| | 0 | 638 | | catch (Exception ex) |
| | | 639 | | { |
| | 0 | 640 | | _logger.LogError(ex, "Failed to get reprediction index for match {HomeTeam} vs {AwayTeam} using model {Model |
| | 0 | 641 | | match.HomeTeam, match.AwayTeam, model, communityContext); |
| | 0 | 642 | | throw; |
| | | 643 | | } |
| | 1 | 644 | | } |
| | | 645 | | |
| | | 646 | | public async Task<int> GetBonusRepredictionIndexAsync(string questionText, string model, string communityContext, Ca |
| | | 647 | | { |
| | | 648 | | try |
| | | 649 | | { |
| | | 650 | | // Query by question text, model, community context, and competition |
| | | 651 | | // Order by repredictionIndex descending to get the latest version |
| | 1 | 652 | | var query = _firestoreDb.Collection(_bonusPredictionsCollection) |
| | 1 | 653 | | .WhereEqualTo("questionText", questionText) |
| | 1 | 654 | | .WhereEqualTo("competition", _competition) |
| | 1 | 655 | | .WhereEqualTo("model", model) |
| | 1 | 656 | | .WhereEqualTo("communityContext", communityContext) |
| | 1 | 657 | | .OrderByDescending("repredictionIndex") |
| | 1 | 658 | | .Limit(1); |
| | | 659 | | |
| | 1 | 660 | | var snapshot = await query.GetSnapshotAsync(cancellationToken); |
| | | 661 | | |
| | 1 | 662 | | if (snapshot.Documents.Count == 0) |
| | | 663 | | { |
| | 1 | 664 | | return -1; // No prediction exists |
| | | 665 | | } |
| | | 666 | | |
| | 1 | 667 | | var firestorePrediction = snapshot.Documents.First().ConvertTo<FirestoreBonusPrediction>(); |
| | 1 | 668 | | return firestorePrediction.RepredictionIndex; |
| | | 669 | | } |
| | 0 | 670 | | catch (Exception ex) |
| | | 671 | | { |
| | 0 | 672 | | _logger.LogError(ex, "Failed to get reprediction index for bonus question '{QuestionText}' using model {Mode |
| | 0 | 673 | | questionText, model, communityContext); |
| | 0 | 674 | | throw; |
| | | 675 | | } |
| | 1 | 676 | | } |
| | | 677 | | |
| | | 678 | | public async Task SaveRepredictionAsync(Match match, Prediction prediction, string model, string tokenUsage, double |
| | | 679 | | { |
| | | 680 | | try |
| | | 681 | | { |
| | 1 | 682 | | var now = Timestamp.GetCurrentTimestamp(); |
| | | 683 | | |
| | | 684 | | // Create new document for this reprediction |
| | 1 | 685 | | var documentId = Guid.NewGuid().ToString(); |
| | 1 | 686 | | var docRef = _firestoreDb.Collection(_predictionsCollection).Document(documentId); |
| | | 687 | | |
| | 1 | 688 | | _logger.LogDebug("Creating reprediction for match {HomeTeam} vs {AwayTeam} (document: {DocumentId}, repredic |
| | 1 | 689 | | match.HomeTeam, match.AwayTeam, documentId, repredictionIndex); |
| | | 690 | | |
| | 1 | 691 | | var firestorePrediction = new FirestoreMatchPrediction |
| | 1 | 692 | | { |
| | 1 | 693 | | Id = docRef.Id, |
| | 1 | 694 | | HomeTeam = match.HomeTeam, |
| | 1 | 695 | | AwayTeam = match.AwayTeam, |
| | 1 | 696 | | StartsAt = ConvertToTimestamp(match.StartsAt), |
| | 1 | 697 | | Matchday = match.Matchday, |
| | 1 | 698 | | HomeGoals = prediction.HomeGoals, |
| | 1 | 699 | | AwayGoals = prediction.AwayGoals, |
| | 1 | 700 | | Justification = SerializeJustification(prediction.Justification), |
| | 1 | 701 | | CreatedAt = now, |
| | 1 | 702 | | UpdatedAt = now, |
| | 1 | 703 | | Competition = _competition, |
| | 1 | 704 | | Model = model, |
| | 1 | 705 | | TokenUsage = tokenUsage, |
| | 1 | 706 | | Cost = cost, |
| | 1 | 707 | | CommunityContext = communityContext, |
| | 1 | 708 | | ContextDocumentNames = contextDocumentNames.ToArray(), |
| | 1 | 709 | | RepredictionIndex = repredictionIndex |
| | 1 | 710 | | }; |
| | | 711 | | |
| | 1 | 712 | | await docRef.SetAsync(firestorePrediction, cancellationToken: cancellationToken); |
| | | 713 | | |
| | 1 | 714 | | _logger.LogInformation("Saved reprediction for match {HomeTeam} vs {AwayTeam} on matchday {Matchday} (repred |
| | 1 | 715 | | match.HomeTeam, match.AwayTeam, match.Matchday, repredictionIndex); |
| | 1 | 716 | | } |
| | 0 | 717 | | catch (Exception ex) |
| | | 718 | | { |
| | 0 | 719 | | _logger.LogError(ex, "Failed to save reprediction for match {HomeTeam} vs {AwayTeam}", |
| | 0 | 720 | | match.HomeTeam, match.AwayTeam); |
| | 0 | 721 | | throw; |
| | | 722 | | } |
| | 1 | 723 | | } |
| | | 724 | | |
| | | 725 | | public async Task SaveBonusRepredictionAsync(BonusQuestion bonusQuestion, BonusPrediction bonusPrediction, string mo |
| | | 726 | | { |
| | | 727 | | try |
| | | 728 | | { |
| | 1 | 729 | | var now = Timestamp.GetCurrentTimestamp(); |
| | | 730 | | |
| | | 731 | | // Create new document for this reprediction |
| | 1 | 732 | | var documentId = Guid.NewGuid().ToString(); |
| | 1 | 733 | | var docRef = _firestoreDb.Collection(_bonusPredictionsCollection).Document(documentId); |
| | | 734 | | |
| | 1 | 735 | | _logger.LogDebug("Creating bonus reprediction for question '{QuestionText}' (document: {DocumentId}, repredi |
| | 1 | 736 | | bonusQuestion.Text, documentId, repredictionIndex); |
| | | 737 | | |
| | | 738 | | // Extract selected option texts for observability |
| | 1 | 739 | | var optionTextsLookup = bonusQuestion.Options.ToDictionary(o => o.Id, o => o.Text); |
| | 1 | 740 | | var selectedOptionTexts = bonusPrediction.SelectedOptionIds |
| | 1 | 741 | | .Select(id => optionTextsLookup.TryGetValue(id, out var text) ? text : $"Unknown option: {id}") |
| | 1 | 742 | | .ToArray(); |
| | | 743 | | |
| | 1 | 744 | | var firestoreBonusPrediction = new FirestoreBonusPrediction |
| | 1 | 745 | | { |
| | 1 | 746 | | Id = docRef.Id, |
| | 1 | 747 | | QuestionText = bonusQuestion.Text, |
| | 1 | 748 | | SelectedOptionIds = bonusPrediction.SelectedOptionIds.ToArray(), |
| | 1 | 749 | | SelectedOptionTexts = selectedOptionTexts, |
| | 1 | 750 | | CreatedAt = now, |
| | 1 | 751 | | UpdatedAt = now, |
| | 1 | 752 | | Competition = _competition, |
| | 1 | 753 | | Model = model, |
| | 1 | 754 | | TokenUsage = tokenUsage, |
| | 1 | 755 | | Cost = cost, |
| | 1 | 756 | | CommunityContext = communityContext, |
| | 1 | 757 | | ContextDocumentNames = contextDocumentNames.ToArray(), |
| | 1 | 758 | | RepredictionIndex = repredictionIndex |
| | 1 | 759 | | }; |
| | | 760 | | |
| | 1 | 761 | | await docRef.SetAsync(firestoreBonusPrediction, cancellationToken: cancellationToken); |
| | | 762 | | |
| | 1 | 763 | | _logger.LogInformation("Saved bonus reprediction for question '{QuestionText}' (reprediction index: {Repredi |
| | 1 | 764 | | bonusQuestion.Text, repredictionIndex); |
| | 1 | 765 | | } |
| | 0 | 766 | | catch (Exception ex) |
| | | 767 | | { |
| | 0 | 768 | | _logger.LogError(ex, "Failed to save bonus reprediction for question: {QuestionText}", |
| | 0 | 769 | | bonusQuestion.Text); |
| | 0 | 770 | | throw; |
| | | 771 | | } |
| | 1 | 772 | | } |
| | | 773 | | |
| | | 774 | | /// <summary> |
| | | 775 | | /// Get match prediction costs and counts grouped by reprediction index for cost analysis. |
| | | 776 | | /// Used specifically by the cost command to include all repredictions. |
| | | 777 | | /// </summary> |
| | | 778 | | public async Task<Dictionary<int, (double cost, int count)>> GetMatchPredictionCostsByRepredictionIndexAsync( |
| | | 779 | | string model, |
| | | 780 | | string communityContext, |
| | | 781 | | List<int>? matchdays = null, |
| | | 782 | | CancellationToken cancellationToken = default) |
| | | 783 | | { |
| | | 784 | | try |
| | | 785 | | { |
| | 1 | 786 | | var costsByIndex = new Dictionary<int, (double cost, int count)>(); |
| | | 787 | | |
| | | 788 | | // Query for match predictions with cost data |
| | 1 | 789 | | var query = _firestoreDb.Collection(_predictionsCollection) |
| | 1 | 790 | | .WhereEqualTo("competition", _competition) |
| | 1 | 791 | | .WhereEqualTo("model", model) |
| | 1 | 792 | | .WhereEqualTo("communityContext", communityContext); |
| | | 793 | | |
| | | 794 | | // Add matchday filter if specified |
| | 1 | 795 | | if (matchdays?.Count > 0) |
| | | 796 | | { |
| | 1 | 797 | | query = query.WhereIn("matchday", matchdays.Cast<object>().ToArray()); |
| | | 798 | | } |
| | | 799 | | |
| | 1 | 800 | | var snapshot = await query.GetSnapshotAsync(cancellationToken); |
| | | 801 | | |
| | 1 | 802 | | foreach (var doc in snapshot.Documents) |
| | | 803 | | { |
| | 1 | 804 | | if (doc.Exists) |
| | | 805 | | { |
| | 1 | 806 | | var prediction = doc.ConvertTo<FirestoreMatchPrediction>(); |
| | 1 | 807 | | var repredictionIndex = prediction.RepredictionIndex; |
| | | 808 | | |
| | 1 | 809 | | if (!costsByIndex.ContainsKey(repredictionIndex)) |
| | | 810 | | { |
| | 1 | 811 | | costsByIndex[repredictionIndex] = (0.0, 0); |
| | | 812 | | } |
| | | 813 | | |
| | 1 | 814 | | var (currentCost, currentCount) = costsByIndex[repredictionIndex]; |
| | 1 | 815 | | costsByIndex[repredictionIndex] = (currentCost + prediction.Cost, currentCount + 1); |
| | | 816 | | } |
| | | 817 | | } |
| | | 818 | | |
| | 1 | 819 | | return costsByIndex; |
| | | 820 | | } |
| | 0 | 821 | | catch (Exception ex) |
| | | 822 | | { |
| | 0 | 823 | | _logger.LogError(ex, "Failed to get match prediction costs by reprediction index for model {Model} and commu |
| | 0 | 824 | | model, communityContext); |
| | 0 | 825 | | throw; |
| | | 826 | | } |
| | 1 | 827 | | } |
| | | 828 | | |
| | | 829 | | /// <summary> |
| | | 830 | | /// Get bonus prediction costs and counts grouped by reprediction index for cost analysis. |
| | | 831 | | /// Used specifically by the cost command to include all repredictions. |
| | | 832 | | /// </summary> |
| | | 833 | | public async Task<Dictionary<int, (double cost, int count)>> GetBonusPredictionCostsByRepredictionIndexAsync( |
| | | 834 | | string model, |
| | | 835 | | string communityContext, |
| | | 836 | | CancellationToken cancellationToken = default) |
| | | 837 | | { |
| | | 838 | | try |
| | | 839 | | { |
| | 1 | 840 | | var costsByIndex = new Dictionary<int, (double cost, int count)>(); |
| | | 841 | | |
| | | 842 | | // Query for bonus predictions with cost data |
| | 1 | 843 | | var query = _firestoreDb.Collection(_bonusPredictionsCollection) |
| | 1 | 844 | | .WhereEqualTo("competition", _competition) |
| | 1 | 845 | | .WhereEqualTo("model", model) |
| | 1 | 846 | | .WhereEqualTo("communityContext", communityContext); |
| | | 847 | | |
| | 1 | 848 | | var snapshot = await query.GetSnapshotAsync(cancellationToken); |
| | | 849 | | |
| | 1 | 850 | | foreach (var doc in snapshot.Documents) |
| | | 851 | | { |
| | 1 | 852 | | if (doc.Exists) |
| | | 853 | | { |
| | 1 | 854 | | var prediction = doc.ConvertTo<FirestoreBonusPrediction>(); |
| | 1 | 855 | | var repredictionIndex = prediction.RepredictionIndex; |
| | | 856 | | |
| | 1 | 857 | | if (!costsByIndex.ContainsKey(repredictionIndex)) |
| | | 858 | | { |
| | 1 | 859 | | costsByIndex[repredictionIndex] = (0.0, 0); |
| | | 860 | | } |
| | | 861 | | |
| | 1 | 862 | | var (currentCost, currentCount) = costsByIndex[repredictionIndex]; |
| | 1 | 863 | | costsByIndex[repredictionIndex] = (currentCost + prediction.Cost, currentCount + 1); |
| | | 864 | | } |
| | | 865 | | } |
| | | 866 | | |
| | 1 | 867 | | return costsByIndex; |
| | | 868 | | } |
| | 0 | 869 | | catch (Exception ex) |
| | | 870 | | { |
| | 0 | 871 | | _logger.LogError(ex, "Failed to get bonus prediction costs by reprediction index for model {Model} and commu |
| | 0 | 872 | | model, communityContext); |
| | 0 | 873 | | throw; |
| | | 874 | | } |
| | 1 | 875 | | } |
| | | 876 | | |
| | | 877 | | private string? SerializeJustification(PredictionJustification? justification) |
| | | 878 | | { |
| | 1 | 879 | | if (justification == null) |
| | | 880 | | { |
| | 1 | 881 | | return null; |
| | | 882 | | } |
| | | 883 | | |
| | 1 | 884 | | if (!HasJustificationContent(justification)) |
| | | 885 | | { |
| | 1 | 886 | | return null; |
| | | 887 | | } |
| | | 888 | | |
| | 1 | 889 | | var stored = new StoredJustification |
| | 1 | 890 | | { |
| | 1 | 891 | | KeyReasoning = justification.KeyReasoning?.Trim() ?? string.Empty, |
| | 1 | 892 | | ContextSources = new StoredContextSources |
| | 1 | 893 | | { |
| | 1 | 894 | | MostValuable = justification.ContextSources?.MostValuable? |
| | 1 | 895 | | .Where(entry => entry != null) |
| | 1 | 896 | | .Select(ToStoredContextSource) |
| | 1 | 897 | | .ToList() ?? new List<StoredContextSource>(), |
| | 1 | 898 | | LeastValuable = justification.ContextSources?.LeastValuable? |
| | 1 | 899 | | .Where(entry => entry != null) |
| | 1 | 900 | | .Select(ToStoredContextSource) |
| | 1 | 901 | | .ToList() ?? new List<StoredContextSource>() |
| | 1 | 902 | | }, |
| | 1 | 903 | | Uncertainties = justification.Uncertainties? |
| | 1 | 904 | | .Where(item => !string.IsNullOrWhiteSpace(item)) |
| | 1 | 905 | | .Select(item => item.Trim()) |
| | 1 | 906 | | .ToList() ?? new List<string>() |
| | 1 | 907 | | }; |
| | | 908 | | |
| | 1 | 909 | | return JsonSerializer.Serialize(stored, JustificationSerializerOptions); |
| | | 910 | | } |
| | | 911 | | |
| | | 912 | | private static bool HasJustificationContent(PredictionJustification justification) |
| | | 913 | | { |
| | 1 | 914 | | if (!string.IsNullOrWhiteSpace(justification.KeyReasoning)) |
| | | 915 | | { |
| | 1 | 916 | | return true; |
| | | 917 | | } |
| | | 918 | | |
| | 1 | 919 | | if (justification.ContextSources?.MostValuable != null && |
| | 1 | 920 | | justification.ContextSources.MostValuable.Any(HasSourceContent)) |
| | | 921 | | { |
| | 1 | 922 | | return true; |
| | | 923 | | } |
| | | 924 | | |
| | 1 | 925 | | if (justification.ContextSources?.LeastValuable != null && |
| | 1 | 926 | | justification.ContextSources.LeastValuable.Any(HasSourceContent)) |
| | | 927 | | { |
| | 1 | 928 | | return true; |
| | | 929 | | } |
| | | 930 | | |
| | 1 | 931 | | return justification.Uncertainties != null && |
| | 1 | 932 | | justification.Uncertainties.Any(item => !string.IsNullOrWhiteSpace(item)); |
| | | 933 | | } |
| | | 934 | | |
| | | 935 | | private static bool HasSourceContent(PredictionJustificationContextSource source) |
| | | 936 | | { |
| | 1 | 937 | | return !string.IsNullOrWhiteSpace(source?.DocumentName) || |
| | 1 | 938 | | !string.IsNullOrWhiteSpace(source?.Details); |
| | | 939 | | } |
| | | 940 | | |
| | | 941 | | private PredictionJustification? DeserializeJustification(string? serialized) |
| | | 942 | | { |
| | 1 | 943 | | if (string.IsNullOrWhiteSpace(serialized)) |
| | | 944 | | { |
| | 1 | 945 | | return null; |
| | | 946 | | } |
| | | 947 | | |
| | 1 | 948 | | var trimmed = serialized.Trim(); |
| | | 949 | | |
| | 1 | 950 | | if (!trimmed.StartsWith("{")) |
| | | 951 | | { |
| | 0 | 952 | | return new PredictionJustification( |
| | 0 | 953 | | trimmed, |
| | 0 | 954 | | new PredictionJustificationContextSources( |
| | 0 | 955 | | Array.Empty<PredictionJustificationContextSource>(), |
| | 0 | 956 | | Array.Empty<PredictionJustificationContextSource>()), |
| | 0 | 957 | | Array.Empty<string>()); |
| | | 958 | | } |
| | | 959 | | |
| | | 960 | | try |
| | | 961 | | { |
| | 1 | 962 | | var stored = JsonSerializer.Deserialize<StoredJustification>(trimmed, JustificationSerializerOptions); |
| | | 963 | | |
| | 1 | 964 | | if (stored == null) |
| | | 965 | | { |
| | 0 | 966 | | return null; |
| | | 967 | | } |
| | | 968 | | |
| | 1 | 969 | | var contextSources = stored.ContextSources ?? new StoredContextSources(); |
| | | 970 | | |
| | 1 | 971 | | var mostValuable = contextSources.MostValuable? |
| | 1 | 972 | | .Where(entry => entry != null) |
| | 1 | 973 | | .Select(ToDomainContextSource) |
| | 1 | 974 | | .ToList() ?? new List<PredictionJustificationContextSource>(); |
| | | 975 | | |
| | 1 | 976 | | var leastValuable = contextSources.LeastValuable? |
| | 1 | 977 | | .Where(entry => entry != null) |
| | 1 | 978 | | .Select(ToDomainContextSource) |
| | 1 | 979 | | .ToList() ?? new List<PredictionJustificationContextSource>(); |
| | | 980 | | |
| | 1 | 981 | | var uncertainties = stored.Uncertainties? |
| | 1 | 982 | | .Where(item => !string.IsNullOrWhiteSpace(item)) |
| | 1 | 983 | | .Select(item => item.Trim()) |
| | 1 | 984 | | .ToList() ?? new List<string>(); |
| | | 985 | | |
| | 1 | 986 | | var justification = new PredictionJustification( |
| | 1 | 987 | | stored.KeyReasoning?.Trim() ?? string.Empty, |
| | 1 | 988 | | new PredictionJustificationContextSources(mostValuable, leastValuable), |
| | 1 | 989 | | uncertainties); |
| | | 990 | | |
| | 1 | 991 | | return HasJustificationContent(justification) ? justification : null; |
| | | 992 | | } |
| | 0 | 993 | | catch (JsonException ex) |
| | | 994 | | { |
| | 0 | 995 | | _logger.LogWarning(ex, "Failed to parse structured justification JSON; falling back to legacy text format"); |
| | | 996 | | |
| | 0 | 997 | | var fallbackJustification = new PredictionJustification( |
| | 0 | 998 | | trimmed, |
| | 0 | 999 | | new PredictionJustificationContextSources( |
| | 0 | 1000 | | Array.Empty<PredictionJustificationContextSource>(), |
| | 0 | 1001 | | Array.Empty<PredictionJustificationContextSource>()), |
| | 0 | 1002 | | Array.Empty<string>()); |
| | | 1003 | | |
| | 0 | 1004 | | return HasJustificationContent(fallbackJustification) ? fallbackJustification : null; |
| | | 1005 | | } |
| | 1 | 1006 | | } |
| | | 1007 | | |
| | | 1008 | | private static StoredContextSource ToStoredContextSource(PredictionJustificationContextSource source) |
| | | 1009 | | { |
| | 1 | 1010 | | return new StoredContextSource |
| | 1 | 1011 | | { |
| | 1 | 1012 | | DocumentName = source.DocumentName?.Trim() ?? string.Empty, |
| | 1 | 1013 | | Details = source.Details?.Trim() ?? string.Empty |
| | 1 | 1014 | | }; |
| | | 1015 | | } |
| | | 1016 | | |
| | | 1017 | | private static PredictionJustificationContextSource ToDomainContextSource(StoredContextSource source) |
| | | 1018 | | { |
| | 1 | 1019 | | var documentName = source.DocumentName?.Trim() ?? string.Empty; |
| | 1 | 1020 | | var details = source.Details?.Trim() ?? string.Empty; |
| | 1 | 1021 | | return new PredictionJustificationContextSource(documentName, details); |
| | | 1022 | | } |
| | | 1023 | | |
| | | 1024 | | private sealed class StoredJustification |
| | | 1025 | | { |
| | 1 | 1026 | | public string? KeyReasoning { get; set; } |
| | 1 | 1027 | | public StoredContextSources? ContextSources { get; set; } |
| | 1 | 1028 | | public List<string>? Uncertainties { get; set; } |
| | | 1029 | | } |
| | | 1030 | | |
| | | 1031 | | private sealed class StoredContextSources |
| | | 1032 | | { |
| | 1 | 1033 | | public List<StoredContextSource>? MostValuable { get; set; } |
| | 1 | 1034 | | public List<StoredContextSource>? LeastValuable { get; set; } |
| | | 1035 | | } |
| | | 1036 | | |
| | | 1037 | | private sealed class StoredContextSource |
| | | 1038 | | { |
| | 1 | 1039 | | public string? DocumentName { get; set; } |
| | 1 | 1040 | | public string? Details { get; set; } |
| | | 1041 | | } |
| | | 1042 | | } |