| | | 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.Wm26RecentHistory; |
| | | 9 | | |
| | | 10 | | public sealed class Wm26RecentHistoryExportDateMapCommand |
| | | 11 | | : AsyncCommand<Wm26RecentHistoryExportDateMapSettings> |
| | | 12 | | { |
| | | 13 | | private readonly IAnsiConsole _console; |
| | | 14 | | private readonly IFirebaseServiceFactory _firebaseServiceFactory; |
| | | 15 | | private readonly ILogger<Wm26RecentHistoryExportDateMapCommand> _logger; |
| | | 16 | | |
| | 1 | 17 | | public Wm26RecentHistoryExportDateMapCommand( |
| | 1 | 18 | | IAnsiConsole console, |
| | 1 | 19 | | IFirebaseServiceFactory firebaseServiceFactory, |
| | 1 | 20 | | ILogger<Wm26RecentHistoryExportDateMapCommand> logger) |
| | | 21 | | { |
| | 1 | 22 | | _console = console; |
| | 1 | 23 | | _firebaseServiceFactory = firebaseServiceFactory; |
| | 1 | 24 | | _logger = logger; |
| | 1 | 25 | | } |
| | | 26 | | |
| | | 27 | | protected override async Task<int> ExecuteAsync( |
| | | 28 | | CommandContext context, |
| | | 29 | | Wm26RecentHistoryExportDateMapSettings settings, |
| | | 30 | | CancellationToken cancellationToken) |
| | | 31 | | { |
| | | 32 | | try |
| | | 33 | | { |
| | 1 | 34 | | var competition = CompetitionResolver.ResolveCompetition( |
| | 1 | 35 | | settings.Competition, |
| | 1 | 36 | | communityContext: settings.CommunityContext); |
| | 1 | 37 | | var repositoryCompetition = CompetitionResolver.ToRepositoryCompetitionArgument(competition); |
| | 1 | 38 | | var contextRepository = _firebaseServiceFactory.CreateContextRepository(repositoryCompetition); |
| | | 39 | | |
| | 1 | 40 | | _console.MarkupLine($"[green]Exporting WM26 recent-history date map for:[/] [yellow]{settings.CommunityConte |
| | 1 | 41 | | _console.MarkupLine($"[blue]Using competition:[/] [yellow]{competition}[/]"); |
| | | 42 | | |
| | 1 | 43 | | var existingEntries = await ReadExistingEntries(settings.Output, cancellationToken); |
| | 1 | 44 | | var existingByKey = existingEntries |
| | 1 | 45 | | .GroupBy(CreateDateMapKey, StringComparer.OrdinalIgnoreCase) |
| | 1 | 46 | | .ToDictionary( |
| | 1 | 47 | | group => group.Key, |
| | 1 | 48 | | group => new Queue<HistoryDateMapEntry>(group), |
| | 1 | 49 | | StringComparer.OrdinalIgnoreCase); |
| | 1 | 50 | | var exportedEntries = new List<HistoryDateMapEntry>(); |
| | 1 | 51 | | var preservedCount = 0; |
| | | 52 | | |
| | 1 | 53 | | var documentNames = await contextRepository.GetContextDocumentNamesAsync( |
| | 1 | 54 | | settings.CommunityContext, |
| | 1 | 55 | | cancellationToken); |
| | 1 | 56 | | var historyDocumentNames = documentNames |
| | 1 | 57 | | .Where(IsRecentHistoryDocument) |
| | 0 | 58 | | .OrderBy(name => name, StringComparer.Ordinal) |
| | 1 | 59 | | .ToList(); |
| | | 60 | | |
| | 1 | 61 | | if (historyDocumentNames.Count == 0) |
| | | 62 | | { |
| | 0 | 63 | | _console.MarkupLine("[yellow]No recent-history documents found[/]"); |
| | 0 | 64 | | return 1; |
| | | 65 | | } |
| | | 66 | | |
| | 1 | 67 | | foreach (var documentName in historyDocumentNames) |
| | | 68 | | { |
| | 1 | 69 | | var document = await contextRepository.GetLatestContextDocumentAsync( |
| | 1 | 70 | | documentName, |
| | 1 | 71 | | settings.CommunityContext, |
| | 1 | 72 | | cancellationToken); |
| | 1 | 73 | | if (document is null) |
| | | 74 | | { |
| | | 75 | | continue; |
| | | 76 | | } |
| | | 77 | | |
| | 1 | 78 | | var documentEntries = HistoryCsvUtility.ExtractDateMapEntries(documentName, document.Content); |
| | 1 | 79 | | foreach (var entry in documentEntries) |
| | | 80 | | { |
| | 1 | 81 | | if (existingByKey.TryGetValue(CreateDateMapKey(entry), out var existingEntriesForRow) && |
| | 1 | 82 | | existingEntriesForRow.TryDequeue(out var existing)) |
| | | 83 | | { |
| | 1 | 84 | | exportedEntries.Add(entry with |
| | 1 | 85 | | { |
| | 1 | 86 | | PlayedAt = existing.PlayedAt, |
| | 1 | 87 | | SourceName = existing.SourceName, |
| | 1 | 88 | | SourceUrl = existing.SourceUrl, |
| | 1 | 89 | | VerifiedAt = existing.VerifiedAt, |
| | 1 | 90 | | Notes = existing.Notes |
| | 1 | 91 | | }); |
| | | 92 | | |
| | 1 | 93 | | if (!string.IsNullOrWhiteSpace(existing.PlayedAt)) |
| | | 94 | | { |
| | 1 | 95 | | preservedCount++; |
| | | 96 | | } |
| | | 97 | | } |
| | | 98 | | else |
| | | 99 | | { |
| | 1 | 100 | | exportedEntries.Add(entry); |
| | | 101 | | } |
| | | 102 | | } |
| | | 103 | | |
| | 1 | 104 | | if (settings.Verbose) |
| | | 105 | | { |
| | 0 | 106 | | _console.MarkupLine($"[dim] Exported {documentEntries.Count} row(s) from {documentName}[/]"); |
| | | 107 | | } |
| | 1 | 108 | | } |
| | | 109 | | |
| | 1 | 110 | | var outputDirectory = Path.GetDirectoryName(settings.Output); |
| | 1 | 111 | | if (!string.IsNullOrWhiteSpace(outputDirectory)) |
| | | 112 | | { |
| | 1 | 113 | | Directory.CreateDirectory(outputDirectory); |
| | | 114 | | } |
| | | 115 | | |
| | 1 | 116 | | await File.WriteAllTextAsync( |
| | 1 | 117 | | settings.Output, |
| | 1 | 118 | | HistoryCsvUtility.WriteDateMapEntries(exportedEntries), |
| | 1 | 119 | | cancellationToken); |
| | | 120 | | |
| | 1 | 121 | | _console.MarkupLine($"[green]Exported {exportedEntries.Count} date-map row(s) to {settings.Output}[/]"); |
| | 1 | 122 | | _console.MarkupLine($"[dim]Preserved {preservedCount} existing played date(s)[/]"); |
| | | 123 | | |
| | 1 | 124 | | return 0; |
| | | 125 | | } |
| | 0 | 126 | | catch (Exception ex) |
| | | 127 | | { |
| | 0 | 128 | | _logger.LogError(ex, "Failed to export WM26 recent-history date map"); |
| | 0 | 129 | | _console.MarkupLine($"[red]Error:[/] {ex.Message}"); |
| | 0 | 130 | | return 1; |
| | | 131 | | } |
| | 1 | 132 | | } |
| | | 133 | | |
| | | 134 | | private static async Task<IReadOnlyList<HistoryDateMapEntry>> ReadExistingEntries( |
| | | 135 | | string path, |
| | | 136 | | CancellationToken cancellationToken) |
| | | 137 | | { |
| | 1 | 138 | | if (!File.Exists(path)) |
| | | 139 | | { |
| | 1 | 140 | | return Array.Empty<HistoryDateMapEntry>(); |
| | | 141 | | } |
| | | 142 | | |
| | 1 | 143 | | var content = await File.ReadAllTextAsync(path, cancellationToken); |
| | 1 | 144 | | return HistoryCsvUtility.ReadDateMapEntries(content); |
| | 1 | 145 | | } |
| | | 146 | | |
| | | 147 | | private static bool IsRecentHistoryDocument(string documentName) |
| | | 148 | | { |
| | 1 | 149 | | return documentName.StartsWith("recent-history-", StringComparison.OrdinalIgnoreCase) |
| | 1 | 150 | | && documentName.EndsWith(".csv", StringComparison.OrdinalIgnoreCase); |
| | | 151 | | } |
| | | 152 | | |
| | | 153 | | private static string CreateDateMapKey(HistoryDateMapEntry entry) |
| | | 154 | | { |
| | 1 | 155 | | return string.Join('|', |
| | 1 | 156 | | entry.DocumentName.Trim(), |
| | 1 | 157 | | entry.Competition.Trim(), |
| | 1 | 158 | | entry.HomeTeam.Trim(), |
| | 1 | 159 | | entry.AwayTeam.Trim(), |
| | 1 | 160 | | entry.Score.Trim(), |
| | 1 | 161 | | entry.Annotation.Trim()); |
| | | 162 | | } |
| | | 163 | | } |