| | | 1 | | using EHonda.KicktippAi.Core; |
| | | 2 | | using Microsoft.Extensions.Logging; |
| | | 3 | | using Orchestrator.Infrastructure; |
| | | 4 | | using Orchestrator.Infrastructure.Factories; |
| | | 5 | | using Spectre.Console; |
| | | 6 | | using Spectre.Console.Cli; |
| | | 7 | | |
| | | 8 | | namespace Orchestrator.Commands.Operations.CollectContext; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Command for uploading WM26 FIFA ranking context and KPI documents. |
| | | 12 | | /// </summary> |
| | | 13 | | public sealed class CollectContextFifaCommand : AsyncCommand<CollectContextFifaSettings> |
| | | 14 | | { |
| | | 15 | | private const string FifaRankingsDocumentName = "fifa-rankings"; |
| | | 16 | | private const string FifaRankingsDescription = "WM26 FIFA rankings for all participants, used as KPI context for bon |
| | | 17 | | |
| | | 18 | | private readonly IAnsiConsole _console; |
| | | 19 | | private readonly IFirebaseServiceFactory _firebaseServiceFactory; |
| | | 20 | | private readonly IFifaRankingSource _fifaRankingSource; |
| | | 21 | | private readonly ILogger<CollectContextFifaCommand> _logger; |
| | | 22 | | |
| | 1 | 23 | | public CollectContextFifaCommand( |
| | 1 | 24 | | IAnsiConsole console, |
| | 1 | 25 | | IFirebaseServiceFactory firebaseServiceFactory, |
| | 1 | 26 | | IFifaRankingSource fifaRankingSource, |
| | 1 | 27 | | ILogger<CollectContextFifaCommand> logger) |
| | | 28 | | { |
| | 1 | 29 | | _console = console; |
| | 1 | 30 | | _firebaseServiceFactory = firebaseServiceFactory; |
| | 1 | 31 | | _fifaRankingSource = fifaRankingSource; |
| | 1 | 32 | | _logger = logger; |
| | 1 | 33 | | } |
| | | 34 | | |
| | | 35 | | protected override async Task<int> ExecuteAsync( |
| | | 36 | | CommandContext context, |
| | | 37 | | CollectContextFifaSettings settings, |
| | | 38 | | CancellationToken cancellationToken) |
| | | 39 | | { |
| | 1 | 40 | | return await ExecuteWithSettingsAsync(settings, cancellationToken); |
| | 1 | 41 | | } |
| | | 42 | | |
| | | 43 | | internal async Task<int> ExecuteWithSettingsAsync( |
| | | 44 | | CollectContextFifaSettings settings, |
| | | 45 | | CancellationToken cancellationToken = default) |
| | | 46 | | { |
| | | 47 | | try |
| | | 48 | | { |
| | 1 | 49 | | if (string.IsNullOrWhiteSpace(settings.CommunityContext)) |
| | | 50 | | { |
| | 0 | 51 | | _console.MarkupLine("[red]Error: Community context is required[/]"); |
| | 0 | 52 | | return 1; |
| | | 53 | | } |
| | | 54 | | |
| | 1 | 55 | | var communityContext = settings.CommunityContext.Trim(); |
| | 1 | 56 | | var competition = CompetitionResolver.ResolveCompetition(settings.Competition, communityContext, communityCo |
| | 1 | 57 | | var repositoryCompetition = CompetitionResolver.ToRepositoryCompetitionArgument(competition); |
| | 1 | 58 | | var collectionDate = DateOnly.FromDateTime(DateTime.UtcNow); |
| | | 59 | | |
| | 1 | 60 | | _console.MarkupLine("[green]Collect-context fifa command initialized[/]"); |
| | 1 | 61 | | _console.MarkupLine($"[blue]Using community context:[/] [yellow]{Markup.Escape(communityContext)}[/]"); |
| | 1 | 62 | | _console.MarkupLine($"[blue]Using competition:[/] [yellow]{Markup.Escape(competition)}[/]"); |
| | | 63 | | |
| | 1 | 64 | | if (settings.Verbose) |
| | | 65 | | { |
| | 1 | 66 | | _console.MarkupLine("[dim]Verbose mode enabled[/]"); |
| | | 67 | | } |
| | | 68 | | |
| | 1 | 69 | | if (settings.DryRun) |
| | | 70 | | { |
| | 1 | 71 | | _console.MarkupLine("[magenta]Dry run mode enabled - no changes will be made to database[/]"); |
| | | 72 | | } |
| | | 73 | | |
| | 1 | 74 | | var source = await _fifaRankingSource.CollectLatestAsync(collectionDate, cancellationToken); |
| | 1 | 75 | | _console.MarkupLine($"[blue]Using FIFA ranking schedule:[/] [yellow]{Markup.Escape(source.ScheduleId)}[/]"); |
| | 1 | 76 | | _console.MarkupLine($"[blue]FIFA ranking published at:[/] [yellow]{source.PublicationDateUtc:O}[/]"); |
| | 1 | 77 | | _console.MarkupLine($"[blue]Collection date:[/] [yellow]{source.CollectionDate:yyyy-MM-dd}[/]"); |
| | 1 | 78 | | _console.MarkupLine($"[blue]Source ranking rows:[/] [yellow]{source.SourceRowCount}[/]"); |
| | 1 | 79 | | _console.MarkupLine($"[blue]Mapped WM26 teams:[/] [yellow]{source.MappedTeamCount}[/]"); |
| | | 80 | | |
| | 1 | 81 | | if (settings.DryRun) |
| | | 82 | | { |
| | 1 | 83 | | foreach (var rankingFile in source.ContextDocuments) |
| | | 84 | | { |
| | 1 | 85 | | _console.MarkupLine($"[magenta] Dry run - would save context document:[/] {Markup.Escape(rankingFil |
| | | 86 | | } |
| | | 87 | | |
| | 1 | 88 | | _console.MarkupLine($"[magenta] Dry run - would save KPI document:[/] {FifaRankingsDocumentName}"); |
| | 1 | 89 | | _console.MarkupLine($"[magenta]✓ Dry run completed - would have processed {source.ContextDocuments.Count |
| | 1 | 90 | | return 0; |
| | | 91 | | } |
| | | 92 | | |
| | 1 | 93 | | var contextRepository = _firebaseServiceFactory.CreateContextRepository(repositoryCompetition); |
| | 1 | 94 | | var kpiRepository = _firebaseServiceFactory.CreateKpiRepository(repositoryCompetition); |
| | | 95 | | |
| | 1 | 96 | | var savedContextCount = 0; |
| | 1 | 97 | | var skippedContextCount = 0; |
| | | 98 | | |
| | 1 | 99 | | foreach (var rankingFile in source.ContextDocuments) |
| | | 100 | | { |
| | 1 | 101 | | var existingContextDocument = await contextRepository.GetLatestContextDocumentAsync( |
| | 1 | 102 | | rankingFile.DocumentName, |
| | 1 | 103 | | communityContext, |
| | 1 | 104 | | cancellationToken); |
| | 1 | 105 | | var contentToSave = FifaRankingCsvUtility.PreserveExistingContentWhenRankingUnchanged( |
| | 1 | 106 | | rankingFile.Content, |
| | 1 | 107 | | existingContextDocument?.Content); |
| | 1 | 108 | | var savedVersion = await contextRepository.SaveContextDocumentAsync( |
| | 1 | 109 | | rankingFile.DocumentName, |
| | 1 | 110 | | contentToSave, |
| | 1 | 111 | | communityContext, |
| | 1 | 112 | | cancellationToken); |
| | | 113 | | |
| | 1 | 114 | | if (savedVersion.HasValue) |
| | | 115 | | { |
| | 1 | 116 | | savedContextCount++; |
| | 1 | 117 | | if (settings.Verbose) |
| | | 118 | | { |
| | 1 | 119 | | _console.MarkupLine($"[green] ✓ Saved {Markup.Escape(rankingFile.DocumentName)} as version {sav |
| | | 120 | | } |
| | | 121 | | } |
| | | 122 | | else |
| | | 123 | | { |
| | 1 | 124 | | skippedContextCount++; |
| | 1 | 125 | | if (settings.Verbose) |
| | | 126 | | { |
| | 1 | 127 | | _console.MarkupLine($"[dim] - Skipped {Markup.Escape(rankingFile.DocumentName)} (content unchan |
| | | 128 | | } |
| | | 129 | | } |
| | 1 | 130 | | } |
| | | 131 | | |
| | 1 | 132 | | var existingKpiDocument = await kpiRepository.GetKpiDocumentAsync( |
| | 1 | 133 | | FifaRankingsDocumentName, |
| | 1 | 134 | | communityContext, |
| | 1 | 135 | | cancellationToken); |
| | 1 | 136 | | var kpiContentToSave = FifaRankingCsvUtility.PreserveExistingContentWhenRankingUnchanged( |
| | 1 | 137 | | source.KpiContent, |
| | 1 | 138 | | existingKpiDocument?.Content); |
| | 1 | 139 | | var savedKpiVersion = await kpiRepository.SaveKpiDocumentAsync( |
| | 1 | 140 | | FifaRankingsDocumentName, |
| | 1 | 141 | | kpiContentToSave, |
| | 1 | 142 | | FifaRankingsDescription, |
| | 1 | 143 | | communityContext, |
| | 1 | 144 | | cancellationToken); |
| | | 145 | | |
| | 1 | 146 | | var kpiChanged = existingKpiDocument is null || |
| | 1 | 147 | | !string.Equals(existingKpiDocument.Content, kpiContentToSave, StringComparison.Ordinal); |
| | | 148 | | |
| | 1 | 149 | | _console.MarkupLine("[green]✓ FIFA ranking context collection completed![/]"); |
| | 1 | 150 | | _console.MarkupLine($"[green] Saved: {savedContextCount} context documents[/]"); |
| | 1 | 151 | | _console.MarkupLine($"[dim] Skipped: {skippedContextCount} context documents (unchanged)[/]"); |
| | 1 | 152 | | _console.MarkupLine(kpiChanged |
| | 1 | 153 | | ? $"[green] KPI document {FifaRankingsDocumentName} saved as version {savedKpiVersion}[/]" |
| | 1 | 154 | | : $"[dim] KPI document {FifaRankingsDocumentName} unchanged at version {savedKpiVersion}[/]"); |
| | | 155 | | |
| | 1 | 156 | | return 0; |
| | | 157 | | } |
| | 1 | 158 | | catch (Exception ex) |
| | | 159 | | { |
| | 1 | 160 | | _logger.LogError(ex, "Error executing collect-context fifa command"); |
| | 1 | 161 | | _console.MarkupLine($"[red]Error:[/] {Markup.Escape(ex.Message)}"); |
| | 1 | 162 | | return 1; |
| | | 163 | | } |
| | 1 | 164 | | } |
| | | 165 | | } |