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