| | | 1 | | using Microsoft.Extensions.Logging; |
| | | 2 | | using Spectre.Console.Cli; |
| | | 3 | | using Spectre.Console; |
| | | 4 | | using Google.Cloud.Firestore; |
| | | 5 | | using System.Globalization; |
| | | 6 | | using System.Text.Json; |
| | | 7 | | using EHonda.KicktippAi.Core; |
| | | 8 | | using Orchestrator.Infrastructure.Factories; |
| | | 9 | | |
| | | 10 | | namespace Orchestrator.Commands.Observability.Cost; |
| | | 11 | | |
| | | 12 | | public class CostCommand : AsyncCommand<CostSettings> |
| | | 13 | | { |
| | | 14 | | private readonly IAnsiConsole _console; |
| | | 15 | | private readonly IFirebaseServiceFactory _firebaseServiceFactory; |
| | | 16 | | private readonly ILogger<CostCommand> _logger; |
| | | 17 | | |
| | 0 | 18 | | public CostCommand( |
| | 0 | 19 | | IAnsiConsole console, |
| | 0 | 20 | | IFirebaseServiceFactory firebaseServiceFactory, |
| | 0 | 21 | | ILogger<CostCommand> logger) |
| | | 22 | | { |
| | 0 | 23 | | _console = console; |
| | 0 | 24 | | _firebaseServiceFactory = firebaseServiceFactory; |
| | 0 | 25 | | _logger = logger; |
| | 0 | 26 | | } |
| | | 27 | | |
| | | 28 | | public override async Task<int> ExecuteAsync(CommandContext context, CostSettings settings) |
| | | 29 | | { |
| | | 30 | | |
| | | 31 | | try |
| | | 32 | | { |
| | | 33 | | // Load configuration from file if specified |
| | 0 | 34 | | if (!string.IsNullOrWhiteSpace(settings.ConfigFile)) |
| | | 35 | | { |
| | 0 | 36 | | var fileConfig = await LoadConfigurationFromFile(settings.ConfigFile); |
| | 0 | 37 | | settings = MergeConfigurations(fileConfig, settings); |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | // Create Firebase services using factory (factory handles env var loading) |
| | 0 | 41 | | var firestoreDb = _firebaseServiceFactory.FirestoreDb; |
| | 0 | 42 | | var predictionRepository = _firebaseServiceFactory.CreatePredictionRepository(); |
| | | 43 | | |
| | 0 | 44 | | _console.MarkupLine($"[green]Cost command initialized[/]"); |
| | | 45 | | |
| | 0 | 46 | | if (settings.Verbose) |
| | | 47 | | { |
| | 0 | 48 | | _console.MarkupLine("[dim]Verbose mode enabled[/]"); |
| | | 49 | | } |
| | | 50 | | |
| | 0 | 51 | | if (settings.All) |
| | | 52 | | { |
| | 0 | 53 | | _console.MarkupLine("[blue]All mode enabled - aggregating over all available data[/]"); |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | // Parse filter parameters |
| | 0 | 57 | | var matchdays = ParseMatchdays(settings); |
| | 0 | 58 | | var models = ParseModels(settings); |
| | 0 | 59 | | var communityContexts = ParseCommunityContexts(settings); |
| | | 60 | | |
| | | 61 | | // Get available models and community contexts if not specified |
| | 0 | 62 | | var availableModels = models ?? await GetAvailableModels(firestoreDb); |
| | 0 | 63 | | var availableCommunityContexts = communityContexts ?? await GetAvailableCommunityContexts(firestoreDb); |
| | 0 | 64 | | var availableMatchdays = matchdays ?? await GetAvailableMatchdays(firestoreDb); |
| | | 65 | | |
| | 0 | 66 | | if (settings.Verbose) |
| | | 67 | | { |
| | 0 | 68 | | _console.MarkupLine($"[dim]Filters:[/]"); |
| | 0 | 69 | | _console.MarkupLine($"[dim] Matchdays: {(matchdays?.Any() == true ? string.Join(", ", matchdays) : $"al |
| | 0 | 70 | | _console.MarkupLine($"[dim] Models: {(models?.Any() == true ? string.Join(", ", models) : $"all ({avail |
| | 0 | 71 | | _console.MarkupLine($"[dim] Community Contexts: {(communityContexts?.Any() == true ? string.Join(", ", |
| | 0 | 72 | | _console.MarkupLine($"[dim] Include Bonus: {settings.Bonus || settings.All}[/]"); |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | // Calculate costs |
| | 0 | 76 | | var totalCost = 0.0; |
| | 0 | 77 | | var matchPredictionCost = 0.0; |
| | 0 | 78 | | var bonusPredictionCost = 0.0; |
| | 0 | 79 | | var matchPredictionCount = 0; |
| | 0 | 80 | | var bonusPredictionCount = 0; |
| | | 81 | | |
| | | 82 | | // Structure to store detailed breakdown data with reprediction index support |
| | 0 | 83 | | var detailedData = new List<(string CommunityContext, string Model, string Category, int RepredictionIndex, |
| | | 84 | | |
| | 0 | 85 | | _console.Status() |
| | 0 | 86 | | .Spinner(Spinner.Known.Dots) |
| | 0 | 87 | | .Start("Calculating costs...", ctx => |
| | 0 | 88 | | { |
| | 0 | 89 | | foreach (var model in availableModels) |
| | 0 | 90 | | { |
| | 0 | 91 | | foreach (var communityContext in availableCommunityContexts) |
| | 0 | 92 | | { |
| | 0 | 93 | | ctx.Status($"Processing {model} - {communityContext}..."); |
| | 0 | 94 | | |
| | 0 | 95 | | if (settings.Verbose) |
| | 0 | 96 | | { |
| | 0 | 97 | | _console.MarkupLine($"[dim] Processing model: {model}, community context: {communityCon |
| | 0 | 98 | | } |
| | 0 | 99 | | |
| | 0 | 100 | | // Get match prediction costs by reprediction index |
| | 0 | 101 | | var matchCostsByIndex = predictionRepository.GetMatchPredictionCostsByRepredictionIndexAsync |
| | 0 | 102 | | |
| | 0 | 103 | | foreach (var kvp in matchCostsByIndex) |
| | 0 | 104 | | { |
| | 0 | 105 | | var repredictionIndex = kvp.Key; |
| | 0 | 106 | | var (cost, count) = kvp.Value; |
| | 0 | 107 | | |
| | 0 | 108 | | matchPredictionCost += cost; |
| | 0 | 109 | | matchPredictionCount += count; |
| | 0 | 110 | | |
| | 0 | 111 | | // Store detailed data for breakdown |
| | 0 | 112 | | if (settings.DetailedBreakdown && (cost > 0 || count > 0)) |
| | 0 | 113 | | { |
| | 0 | 114 | | detailedData.Add((communityContext, model, "Match", repredictionIndex, count, cost)) |
| | 0 | 115 | | } |
| | 0 | 116 | | |
| | 0 | 117 | | if (settings.Verbose && (cost > 0 || count > 0)) |
| | 0 | 118 | | { |
| | 0 | 119 | | _console.MarkupLine($"[dim] Match predictions (reprediction {repredictionIndex}): |
| | 0 | 120 | | } |
| | 0 | 121 | | } |
| | 0 | 122 | | |
| | 0 | 123 | | // Get bonus prediction costs if requested or if all mode is enabled |
| | 0 | 124 | | if (settings.Bonus || settings.All) |
| | 0 | 125 | | { |
| | 0 | 126 | | var bonusCostsByIndex = predictionRepository.GetBonusPredictionCostsByRepredictionIndexA |
| | 0 | 127 | | |
| | 0 | 128 | | foreach (var kvp in bonusCostsByIndex) |
| | 0 | 129 | | { |
| | 0 | 130 | | var repredictionIndex = kvp.Key; |
| | 0 | 131 | | var (cost, count) = kvp.Value; |
| | 0 | 132 | | |
| | 0 | 133 | | bonusPredictionCost += cost; |
| | 0 | 134 | | bonusPredictionCount += count; |
| | 0 | 135 | | |
| | 0 | 136 | | // Store detailed data for breakdown |
| | 0 | 137 | | if (settings.DetailedBreakdown && (cost > 0 || count > 0)) |
| | 0 | 138 | | { |
| | 0 | 139 | | detailedData.Add((communityContext, model, "Bonus", repredictionIndex, count, co |
| | 0 | 140 | | } |
| | 0 | 141 | | |
| | 0 | 142 | | if (settings.Verbose && (cost > 0 || count > 0)) |
| | 0 | 143 | | { |
| | 0 | 144 | | _console.MarkupLine($"[dim] Bonus predictions (reprediction {repredictionInde |
| | 0 | 145 | | } |
| | 0 | 146 | | } |
| | 0 | 147 | | } |
| | 0 | 148 | | } |
| | 0 | 149 | | } |
| | 0 | 150 | | }); |
| | | 151 | | |
| | 0 | 152 | | totalCost = matchPredictionCost + bonusPredictionCost; |
| | | 153 | | |
| | | 154 | | // Display results |
| | 0 | 155 | | var table = new Table(); |
| | 0 | 156 | | table.Border(TableBorder.Rounded); |
| | | 157 | | |
| | 0 | 158 | | if (settings.DetailedBreakdown) |
| | | 159 | | { |
| | | 160 | | // Add columns for detailed breakdown with reprediction support |
| | 0 | 161 | | table.AddColumn("Community Context"); |
| | 0 | 162 | | table.AddColumn("Model"); |
| | 0 | 163 | | table.AddColumn("Category"); |
| | 0 | 164 | | table.AddColumn("Index 0", col => col.RightAligned()); |
| | 0 | 165 | | table.AddColumn("Index 1", col => col.RightAligned()); |
| | 0 | 166 | | table.AddColumn("Index 2+", col => col.RightAligned()); |
| | 0 | 167 | | table.AddColumn("Total Count", col => col.RightAligned()); |
| | 0 | 168 | | table.AddColumn("Total Cost (USD)", col => col.RightAligned()); |
| | | 169 | | |
| | | 170 | | // Group data by community context, model, and category to aggregate reprediction indices |
| | 0 | 171 | | var groupedData = detailedData |
| | 0 | 172 | | .GroupBy(d => new { d.CommunityContext, d.Model, d.Category }) |
| | 0 | 173 | | .Select(g => new |
| | 0 | 174 | | { |
| | 0 | 175 | | g.Key.CommunityContext, |
| | 0 | 176 | | g.Key.Model, |
| | 0 | 177 | | g.Key.Category, |
| | 0 | 178 | | Index0Count = g.Where(x => x.RepredictionIndex == 0).Sum(x => x.Count), |
| | 0 | 179 | | Index0Cost = g.Where(x => x.RepredictionIndex == 0).Sum(x => x.Cost), |
| | 0 | 180 | | Index1Count = g.Where(x => x.RepredictionIndex == 1).Sum(x => x.Count), |
| | 0 | 181 | | Index1Cost = g.Where(x => x.RepredictionIndex == 1).Sum(x => x.Cost), |
| | 0 | 182 | | Index2PlusCount = g.Where(x => x.RepredictionIndex >= 2).Sum(x => x.Count), |
| | 0 | 183 | | Index2PlusCost = g.Where(x => x.RepredictionIndex >= 2).Sum(x => x.Cost), |
| | 0 | 184 | | TotalCount = g.Sum(x => x.Count), |
| | 0 | 185 | | TotalCost = g.Sum(x => x.Cost) |
| | 0 | 186 | | }) |
| | 0 | 187 | | .OrderBy(g => g.CommunityContext) |
| | 0 | 188 | | .ThenBy(g => g.Model) |
| | 0 | 189 | | .ThenBy(g => g.Category) |
| | 0 | 190 | | .ToList(); |
| | | 191 | | |
| | | 192 | | // Add rows for detailed breakdown with alternating styling |
| | 0 | 193 | | for (int i = 0; i < groupedData.Count; i++) |
| | | 194 | | { |
| | 0 | 195 | | var data = groupedData[i]; |
| | 0 | 196 | | var isEvenRow = i % 2 == 0; |
| | | 197 | | |
| | 0 | 198 | | var index0Text = data.Index0Count > 0 ? $"{data.Index0Count} (${data.Index0Cost.ToString("F2", Cultu |
| | 0 | 199 | | var index1Text = data.Index1Count > 0 ? $"{data.Index1Count} (${data.Index1Cost.ToString("F2", Cultu |
| | 0 | 200 | | var index2PlusText = data.Index2PlusCount > 0 ? $"{data.Index2PlusCount} (${data.Index2PlusCost.ToSt |
| | | 201 | | |
| | 0 | 202 | | if (isEvenRow) |
| | | 203 | | { |
| | | 204 | | // Even rows - normal styling |
| | 0 | 205 | | table.AddRow( |
| | 0 | 206 | | data.CommunityContext, |
| | 0 | 207 | | data.Model, |
| | 0 | 208 | | data.Category, |
| | 0 | 209 | | index0Text, |
| | 0 | 210 | | index1Text, |
| | 0 | 211 | | index2PlusText, |
| | 0 | 212 | | data.TotalCount.ToString(CultureInfo.InvariantCulture), |
| | 0 | 213 | | $"${data.TotalCost.ToString("F4", CultureInfo.InvariantCulture)}" |
| | 0 | 214 | | ); |
| | | 215 | | } |
| | | 216 | | else |
| | | 217 | | { |
| | | 218 | | // Odd rows - subtle blue tint for visual differentiation |
| | 0 | 219 | | table.AddRow( |
| | 0 | 220 | | $"[blue]{data.CommunityContext}[/]", |
| | 0 | 221 | | $"[blue]{data.Model}[/]", |
| | 0 | 222 | | $"[blue]{data.Category}[/]", |
| | 0 | 223 | | $"[blue]{index0Text}[/]", |
| | 0 | 224 | | $"[blue]{index1Text}[/]", |
| | 0 | 225 | | $"[blue]{index2PlusText}[/]", |
| | 0 | 226 | | $"[blue]{data.TotalCount.ToString(CultureInfo.InvariantCulture)}[/]", |
| | 0 | 227 | | $"[blue]${data.TotalCost.ToString("F4", CultureInfo.InvariantCulture)}[/]" |
| | 0 | 228 | | ); |
| | | 229 | | } |
| | | 230 | | } |
| | | 231 | | |
| | | 232 | | // Add total row |
| | 0 | 233 | | if (detailedData.Any()) |
| | | 234 | | { |
| | | 235 | | // Calculate totals by reprediction index |
| | 0 | 236 | | var totalIndex0Count = detailedData.Where(x => x.RepredictionIndex == 0).Sum(x => x.Count); |
| | 0 | 237 | | var totalIndex0Cost = detailedData.Where(x => x.RepredictionIndex == 0).Sum(x => x.Cost); |
| | 0 | 238 | | var totalIndex1Count = detailedData.Where(x => x.RepredictionIndex == 1).Sum(x => x.Count); |
| | 0 | 239 | | var totalIndex1Cost = detailedData.Where(x => x.RepredictionIndex == 1).Sum(x => x.Cost); |
| | 0 | 240 | | var totalIndex2PlusCount = detailedData.Where(x => x.RepredictionIndex >= 2).Sum(x => x.Count); |
| | 0 | 241 | | var totalIndex2PlusCost = detailedData.Where(x => x.RepredictionIndex >= 2).Sum(x => x.Cost); |
| | | 242 | | |
| | 0 | 243 | | var totalIndex0Text = totalIndex0Count > 0 ? $"{totalIndex0Count} (${totalIndex0Cost.ToString("F2", |
| | 0 | 244 | | var totalIndex1Text = totalIndex1Count > 0 ? $"{totalIndex1Count} (${totalIndex1Cost.ToString("F2", |
| | 0 | 245 | | var totalIndex2PlusText = totalIndex2PlusCount > 0 ? $"{totalIndex2PlusCount} (${totalIndex2PlusCost |
| | | 246 | | |
| | 0 | 247 | | table.AddEmptyRow(); |
| | 0 | 248 | | table.AddRow( |
| | 0 | 249 | | "[bold]Total[/]", |
| | 0 | 250 | | "", |
| | 0 | 251 | | "", |
| | 0 | 252 | | $"[bold]{totalIndex0Text}[/]", |
| | 0 | 253 | | $"[bold]{totalIndex1Text}[/]", |
| | 0 | 254 | | $"[bold]{totalIndex2PlusText}[/]", |
| | 0 | 255 | | $"[bold]{(matchPredictionCount + bonusPredictionCount).ToString(CultureInfo.InvariantCulture)}[/ |
| | 0 | 256 | | $"[bold]${totalCost.ToString("F4", CultureInfo.InvariantCulture)}[/]" |
| | 0 | 257 | | ); |
| | | 258 | | } |
| | | 259 | | } |
| | | 260 | | else |
| | | 261 | | { |
| | | 262 | | // Standard summary table |
| | 0 | 263 | | table.AddColumn("Category"); |
| | 0 | 264 | | table.AddColumn("Count", col => col.RightAligned()); |
| | 0 | 265 | | table.AddColumn("Cost (USD)", col => col.RightAligned()); |
| | | 266 | | |
| | 0 | 267 | | var rowIndex = 0; |
| | | 268 | | |
| | | 269 | | // Add Match row |
| | 0 | 270 | | var isEvenRow = rowIndex % 2 == 0; |
| | 0 | 271 | | if (isEvenRow) |
| | | 272 | | { |
| | 0 | 273 | | table.AddRow("Match", matchPredictionCount.ToString(CultureInfo.InvariantCulture), $"${matchPredicti |
| | | 274 | | } |
| | | 275 | | else |
| | | 276 | | { |
| | 0 | 277 | | table.AddRow( |
| | 0 | 278 | | "[blue]Match[/]", |
| | 0 | 279 | | $"[blue]{matchPredictionCount.ToString(CultureInfo.InvariantCulture)}[/]", |
| | 0 | 280 | | $"[blue]${matchPredictionCost.ToString("F4", CultureInfo.InvariantCulture)}[/]" |
| | 0 | 281 | | ); |
| | | 282 | | } |
| | 0 | 283 | | rowIndex++; |
| | | 284 | | |
| | | 285 | | // Add Bonus row if applicable |
| | 0 | 286 | | if (settings.Bonus || settings.All) |
| | | 287 | | { |
| | 0 | 288 | | isEvenRow = rowIndex % 2 == 0; |
| | 0 | 289 | | if (isEvenRow) |
| | | 290 | | { |
| | 0 | 291 | | table.AddRow("Bonus", bonusPredictionCount.ToString(CultureInfo.InvariantCulture), $"${bonusPred |
| | | 292 | | } |
| | | 293 | | else |
| | | 294 | | { |
| | 0 | 295 | | table.AddRow( |
| | 0 | 296 | | "[blue]Bonus[/]", |
| | 0 | 297 | | $"[blue]{bonusPredictionCount.ToString(CultureInfo.InvariantCulture)}[/]", |
| | 0 | 298 | | $"[blue]${bonusPredictionCost.ToString("F4", CultureInfo.InvariantCulture)}[/]" |
| | 0 | 299 | | ); |
| | | 300 | | } |
| | | 301 | | } |
| | | 302 | | |
| | 0 | 303 | | table.AddEmptyRow(); |
| | 0 | 304 | | table.AddRow("[bold]Total[/]", $"[bold]{(matchPredictionCount + bonusPredictionCount).ToString(CultureIn |
| | | 305 | | } |
| | | 306 | | |
| | 0 | 307 | | _console.Write(table); |
| | | 308 | | |
| | 0 | 309 | | _console.MarkupLine($"[green]✓ Cost calculation completed[/]"); |
| | | 310 | | |
| | 0 | 311 | | return 0; |
| | | 312 | | } |
| | 0 | 313 | | catch (Exception ex) |
| | | 314 | | { |
| | 0 | 315 | | _logger.LogError(ex, "Failed to calculate costs"); |
| | 0 | 316 | | _console.MarkupLine($"[red]✗ Failed to calculate costs: {ex.Message}[/]"); |
| | 0 | 317 | | return 1; |
| | | 318 | | } |
| | 0 | 319 | | } |
| | | 320 | | |
| | | 321 | | private List<int>? ParseMatchdays(CostSettings settings) |
| | | 322 | | { |
| | 0 | 323 | | if (settings.All || string.IsNullOrWhiteSpace(settings.Matchdays)) |
| | 0 | 324 | | return null; // null means all matchdays |
| | | 325 | | |
| | 0 | 326 | | if (settings.Matchdays.Trim().ToLowerInvariant() == "all") |
| | 0 | 327 | | return null; |
| | | 328 | | |
| | | 329 | | try |
| | | 330 | | { |
| | 0 | 331 | | return settings.Matchdays |
| | 0 | 332 | | .Split(',', StringSplitOptions.RemoveEmptyEntries) |
| | 0 | 333 | | .Select(md => int.Parse(md.Trim())) |
| | 0 | 334 | | .ToList(); |
| | | 335 | | } |
| | 0 | 336 | | catch (FormatException) |
| | | 337 | | { |
| | 0 | 338 | | throw new ArgumentException($"Invalid matchday format: {settings.Matchdays}. Use comma-separated numbers (e. |
| | | 339 | | } |
| | 0 | 340 | | } |
| | | 341 | | |
| | | 342 | | private async Task<List<int>> GetAvailableMatchdays(FirestoreDb firestoreDb) |
| | | 343 | | { |
| | 0 | 344 | | var matchdays = new HashSet<int>(); |
| | 0 | 345 | | var competition = "bundesliga-2025-26"; |
| | | 346 | | |
| | | 347 | | // Query match predictions for unique matchdays |
| | 0 | 348 | | var query = firestoreDb.Collection("match-predictions") |
| | 0 | 349 | | .WhereEqualTo("competition", competition); |
| | 0 | 350 | | var snapshot = await query.GetSnapshotAsync(); |
| | | 351 | | |
| | 0 | 352 | | foreach (var doc in snapshot.Documents) |
| | | 353 | | { |
| | 0 | 354 | | if (doc.TryGetValue<int>("matchday", out var matchday) && matchday > 0) |
| | | 355 | | { |
| | 0 | 356 | | matchdays.Add(matchday); |
| | | 357 | | } |
| | | 358 | | } |
| | | 359 | | |
| | 0 | 360 | | return matchdays.OrderBy(m => m).ToList(); |
| | 0 | 361 | | } |
| | | 362 | | |
| | | 363 | | private List<string>? ParseModels(CostSettings settings) |
| | | 364 | | { |
| | 0 | 365 | | if (settings.All || string.IsNullOrWhiteSpace(settings.Models)) |
| | 0 | 366 | | return null; // null means all models |
| | | 367 | | |
| | 0 | 368 | | if (settings.Models.Trim().ToLowerInvariant() == "all") |
| | 0 | 369 | | return null; |
| | | 370 | | |
| | 0 | 371 | | return settings.Models |
| | 0 | 372 | | .Split(',', StringSplitOptions.RemoveEmptyEntries) |
| | 0 | 373 | | .Select(m => m.Trim()) |
| | 0 | 374 | | .Where(m => !string.IsNullOrWhiteSpace(m)) |
| | 0 | 375 | | .ToList(); |
| | | 376 | | } |
| | | 377 | | |
| | | 378 | | private List<string>? ParseCommunityContexts(CostSettings settings) |
| | | 379 | | { |
| | 0 | 380 | | if (settings.All || string.IsNullOrWhiteSpace(settings.CommunityContexts)) |
| | 0 | 381 | | return null; // null means all community contexts |
| | | 382 | | |
| | 0 | 383 | | if (settings.CommunityContexts.Trim().ToLowerInvariant() == "all") |
| | 0 | 384 | | return null; |
| | | 385 | | |
| | 0 | 386 | | return settings.CommunityContexts |
| | 0 | 387 | | .Split(',', StringSplitOptions.RemoveEmptyEntries) |
| | 0 | 388 | | .Select(cc => cc.Trim()) |
| | 0 | 389 | | .Where(cc => !string.IsNullOrWhiteSpace(cc)) |
| | 0 | 390 | | .ToList(); |
| | | 391 | | } |
| | | 392 | | |
| | | 393 | | private async Task<List<string>> GetAvailableModels(FirestoreDb firestoreDb) |
| | | 394 | | { |
| | 0 | 395 | | var models = new HashSet<string>(); |
| | 0 | 396 | | var competition = "bundesliga-2025-26"; |
| | | 397 | | |
| | | 398 | | // Query match predictions for unique models |
| | 0 | 399 | | var matchQuery = firestoreDb.Collection("match-predictions") |
| | 0 | 400 | | .WhereEqualTo("competition", competition); |
| | 0 | 401 | | var matchSnapshot = await matchQuery.GetSnapshotAsync(); |
| | | 402 | | |
| | 0 | 403 | | foreach (var doc in matchSnapshot.Documents) |
| | | 404 | | { |
| | 0 | 405 | | if (doc.TryGetValue<string>("model", out var model) && !string.IsNullOrWhiteSpace(model)) |
| | | 406 | | { |
| | 0 | 407 | | models.Add(model); |
| | | 408 | | } |
| | | 409 | | } |
| | | 410 | | |
| | | 411 | | // Query bonus predictions for unique models |
| | 0 | 412 | | var bonusQuery = firestoreDb.Collection("bonus-predictions") |
| | 0 | 413 | | .WhereEqualTo("competition", competition); |
| | 0 | 414 | | var bonusSnapshot = await bonusQuery.GetSnapshotAsync(); |
| | | 415 | | |
| | 0 | 416 | | foreach (var doc in bonusSnapshot.Documents) |
| | | 417 | | { |
| | 0 | 418 | | if (doc.TryGetValue<string>("model", out var model) && !string.IsNullOrWhiteSpace(model)) |
| | | 419 | | { |
| | 0 | 420 | | models.Add(model); |
| | | 421 | | } |
| | | 422 | | } |
| | | 423 | | |
| | 0 | 424 | | return models.ToList(); |
| | 0 | 425 | | } |
| | | 426 | | |
| | | 427 | | private async Task<List<string>> GetAvailableCommunityContexts(FirestoreDb firestoreDb) |
| | | 428 | | { |
| | 0 | 429 | | var communityContexts = new HashSet<string>(); |
| | 0 | 430 | | var competition = "bundesliga-2025-26"; |
| | | 431 | | |
| | | 432 | | // Query match predictions for unique community contexts |
| | 0 | 433 | | var matchQuery = firestoreDb.Collection("match-predictions") |
| | 0 | 434 | | .WhereEqualTo("competition", competition); |
| | 0 | 435 | | var matchSnapshot = await matchQuery.GetSnapshotAsync(); |
| | | 436 | | |
| | 0 | 437 | | foreach (var doc in matchSnapshot.Documents) |
| | | 438 | | { |
| | 0 | 439 | | if (doc.TryGetValue<string>("communityContext", out var context) && !string.IsNullOrWhiteSpace(context)) |
| | | 440 | | { |
| | 0 | 441 | | communityContexts.Add(context); |
| | | 442 | | } |
| | | 443 | | } |
| | | 444 | | |
| | | 445 | | // Query bonus predictions for unique community contexts |
| | 0 | 446 | | var bonusQuery = firestoreDb.Collection("bonus-predictions") |
| | 0 | 447 | | .WhereEqualTo("competition", competition); |
| | 0 | 448 | | var bonusSnapshot = await bonusQuery.GetSnapshotAsync(); |
| | | 449 | | |
| | 0 | 450 | | foreach (var doc in bonusSnapshot.Documents) |
| | | 451 | | { |
| | 0 | 452 | | if (doc.TryGetValue<string>("communityContext", out var context) && !string.IsNullOrWhiteSpace(context)) |
| | | 453 | | { |
| | 0 | 454 | | communityContexts.Add(context); |
| | | 455 | | } |
| | | 456 | | } |
| | | 457 | | |
| | 0 | 458 | | return communityContexts.ToList(); |
| | 0 | 459 | | } |
| | | 460 | | |
| | | 461 | | private async Task<CostConfiguration> LoadConfigurationFromFile(string configFilePath) |
| | | 462 | | { |
| | | 463 | | try |
| | | 464 | | { |
| | | 465 | | // Resolve relative paths |
| | 0 | 466 | | var resolvedPath = Path.IsPathRooted(configFilePath) |
| | 0 | 467 | | ? configFilePath |
| | 0 | 468 | | : Path.Combine(Directory.GetCurrentDirectory(), configFilePath); |
| | | 469 | | |
| | 0 | 470 | | if (!File.Exists(resolvedPath)) |
| | | 471 | | { |
| | 0 | 472 | | throw new FileNotFoundException($"Configuration file not found: {resolvedPath}"); |
| | | 473 | | } |
| | | 474 | | |
| | 0 | 475 | | _logger.LogInformation("Loading configuration from: {ConfigPath}", resolvedPath); |
| | | 476 | | |
| | 0 | 477 | | var jsonContent = await File.ReadAllTextAsync(resolvedPath); |
| | 0 | 478 | | var options = new JsonSerializerOptions |
| | 0 | 479 | | { |
| | 0 | 480 | | PropertyNameCaseInsensitive = true, |
| | 0 | 481 | | AllowTrailingCommas = true, |
| | 0 | 482 | | ReadCommentHandling = JsonCommentHandling.Skip |
| | 0 | 483 | | }; |
| | | 484 | | |
| | 0 | 485 | | var config = JsonSerializer.Deserialize<CostConfiguration>(jsonContent, options); |
| | 0 | 486 | | if (config == null) |
| | | 487 | | { |
| | 0 | 488 | | throw new InvalidOperationException($"Failed to deserialize configuration from: {resolvedPath}"); |
| | | 489 | | } |
| | | 490 | | |
| | 0 | 491 | | return config; |
| | | 492 | | } |
| | 0 | 493 | | catch (JsonException ex) |
| | | 494 | | { |
| | 0 | 495 | | throw new InvalidOperationException($"Invalid JSON in configuration file: {configFilePath}. {ex.Message}", e |
| | | 496 | | } |
| | 0 | 497 | | catch (Exception ex) |
| | | 498 | | { |
| | 0 | 499 | | throw new InvalidOperationException($"Failed to load configuration from file: {configFilePath}. {ex.Message} |
| | | 500 | | } |
| | 0 | 501 | | } |
| | | 502 | | |
| | | 503 | | private CostSettings MergeConfigurations(CostConfiguration fileConfig, CostSettings cliSettings) |
| | | 504 | | { |
| | 0 | 505 | | _logger.LogInformation("Merging file configuration with command line options (CLI options take precedence)"); |
| | | 506 | | |
| | | 507 | | // Create a new settings object with file config as base, CLI overrides |
| | 0 | 508 | | var mergedSettings = new CostSettings(); |
| | | 509 | | |
| | | 510 | | // Apply file config first (if values are not null/default) |
| | 0 | 511 | | if (!string.IsNullOrWhiteSpace(fileConfig.Matchdays)) |
| | 0 | 512 | | mergedSettings.Matchdays = fileConfig.Matchdays; |
| | | 513 | | |
| | 0 | 514 | | if (fileConfig.Bonus.HasValue) |
| | 0 | 515 | | mergedSettings.Bonus = fileConfig.Bonus.Value; |
| | | 516 | | |
| | 0 | 517 | | if (!string.IsNullOrWhiteSpace(fileConfig.Models)) |
| | 0 | 518 | | mergedSettings.Models = fileConfig.Models; |
| | | 519 | | |
| | 0 | 520 | | if (!string.IsNullOrWhiteSpace(fileConfig.CommunityContexts)) |
| | 0 | 521 | | mergedSettings.CommunityContexts = fileConfig.CommunityContexts; |
| | | 522 | | |
| | 0 | 523 | | if (fileConfig.All.HasValue) |
| | 0 | 524 | | mergedSettings.All = fileConfig.All.Value; |
| | | 525 | | |
| | 0 | 526 | | if (fileConfig.Verbose.HasValue) |
| | 0 | 527 | | mergedSettings.Verbose = fileConfig.Verbose.Value; |
| | | 528 | | |
| | 0 | 529 | | if (fileConfig.DetailedBreakdown.HasValue) |
| | 0 | 530 | | mergedSettings.DetailedBreakdown = fileConfig.DetailedBreakdown.Value; |
| | | 531 | | |
| | | 532 | | // Override with CLI settings (non-default values) |
| | 0 | 533 | | if (!string.IsNullOrWhiteSpace(cliSettings.Matchdays)) |
| | | 534 | | { |
| | 0 | 535 | | mergedSettings.Matchdays = cliSettings.Matchdays; |
| | 0 | 536 | | if (mergedSettings.Verbose) |
| | 0 | 537 | | _logger.LogInformation("CLI override: Matchdays = {Value}", cliSettings.Matchdays); |
| | | 538 | | } |
| | | 539 | | |
| | 0 | 540 | | if (cliSettings.Bonus) // Only override if explicitly set to true |
| | | 541 | | { |
| | 0 | 542 | | mergedSettings.Bonus = cliSettings.Bonus; |
| | 0 | 543 | | if (mergedSettings.Verbose) |
| | 0 | 544 | | _logger.LogInformation("CLI override: Bonus = {Value}", cliSettings.Bonus); |
| | | 545 | | } |
| | | 546 | | |
| | 0 | 547 | | if (!string.IsNullOrWhiteSpace(cliSettings.Models)) |
| | | 548 | | { |
| | 0 | 549 | | mergedSettings.Models = cliSettings.Models; |
| | 0 | 550 | | if (mergedSettings.Verbose) |
| | 0 | 551 | | _logger.LogInformation("CLI override: Models = {Value}", cliSettings.Models); |
| | | 552 | | } |
| | | 553 | | |
| | 0 | 554 | | if (!string.IsNullOrWhiteSpace(cliSettings.CommunityContexts)) |
| | | 555 | | { |
| | 0 | 556 | | mergedSettings.CommunityContexts = cliSettings.CommunityContexts; |
| | 0 | 557 | | if (mergedSettings.Verbose) |
| | 0 | 558 | | _logger.LogInformation("CLI override: CommunityContexts = {Value}", cliSettings.CommunityContexts); |
| | | 559 | | } |
| | | 560 | | |
| | 0 | 561 | | if (cliSettings.All) // Only override if explicitly set to true |
| | | 562 | | { |
| | 0 | 563 | | mergedSettings.All = cliSettings.All; |
| | 0 | 564 | | if (mergedSettings.Verbose) |
| | 0 | 565 | | _logger.LogInformation("CLI override: All = {Value}", cliSettings.All); |
| | | 566 | | } |
| | | 567 | | |
| | 0 | 568 | | if (cliSettings.Verbose) // Only override if explicitly set to true |
| | | 569 | | { |
| | 0 | 570 | | mergedSettings.Verbose = cliSettings.Verbose; |
| | | 571 | | } |
| | | 572 | | |
| | 0 | 573 | | if (cliSettings.DetailedBreakdown) // Only override if explicitly set to true |
| | | 574 | | { |
| | 0 | 575 | | mergedSettings.DetailedBreakdown = cliSettings.DetailedBreakdown; |
| | 0 | 576 | | if (mergedSettings.Verbose) |
| | 0 | 577 | | _logger.LogInformation("CLI override: DetailedBreakdown = {Value}", cliSettings.DetailedBreakdown); |
| | | 578 | | } |
| | | 579 | | |
| | | 580 | | // Always preserve the ConfigFile setting |
| | 0 | 581 | | mergedSettings.ConfigFile = cliSettings.ConfigFile; |
| | | 582 | | |
| | 0 | 583 | | return mergedSettings; |
| | | 584 | | } |
| | | 585 | | } |