| | | 1 | | using System.Globalization; |
| | | 2 | | using CsvHelper; |
| | | 3 | | |
| | | 4 | | namespace EHonda.KicktippAi.Core; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Utility class for handling Data_Collected_At column in history CSV documents. |
| | | 8 | | /// </summary> |
| | | 9 | | public static class HistoryCsvUtility |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Adds or updates the Data_Collected_At column in a history CSV document. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <param name="csvContent">The original CSV content.</param> |
| | | 15 | | /// <param name="previousCsvContent">The previous version of the CSV content (null if this is the first version).</p |
| | | 16 | | /// <param name="collectedDate">The date when the data was collected (e.g., "2025-08-30").</param> |
| | | 17 | | /// <returns>The updated CSV content with Data_Collected_At column.</returns> |
| | | 18 | | public static string AddDataCollectedAtColumn(string csvContent, string? previousCsvContent, string collectedDate) |
| | | 19 | | { |
| | | 20 | | // Check if the CSV already has Data_Collected_At column |
| | 1 | 21 | | if (HasDataCollectedAtColumn(csvContent)) |
| | | 22 | | { |
| | 1 | 23 | | return csvContent; // Already has the column |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | // Extract matches from previous version to get their collection dates |
| | 1 | 27 | | var previousMatches = previousCsvContent != null |
| | 1 | 28 | | ? ExtractMatchesWithCollectionDates(previousCsvContent) |
| | 1 | 29 | | : new Dictionary<string, string>(); |
| | | 30 | | |
| | | 31 | | // Extract current matches |
| | 1 | 32 | | var currentMatches = ExtractMatches(csvContent); |
| | | 33 | | |
| | | 34 | | // Build the new CSV with Data_Collected_At column |
| | 1 | 35 | | return BuildCsvWithDataCollectedAt(csvContent, currentMatches, previousMatches, collectedDate); |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Checks if the CSV content already has a Data_Collected_At column. |
| | | 40 | | /// </summary> |
| | | 41 | | private static bool HasDataCollectedAtColumn(string csvContent) |
| | | 42 | | { |
| | 1 | 43 | | var lines = csvContent.Split('\n', StringSplitOptions.RemoveEmptyEntries); |
| | 1 | 44 | | if (lines.Length == 0) |
| | | 45 | | { |
| | 1 | 46 | | return false; |
| | | 47 | | } |
| | | 48 | | |
| | 1 | 49 | | var header = lines[0]; |
| | 1 | 50 | | return header.Contains("Data_Collected_At", StringComparison.OrdinalIgnoreCase); |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Extracts matches from CSV content without Data_Collected_At. |
| | | 55 | | /// </summary> |
| | | 56 | | private static HashSet<string> ExtractMatches(string csvContent) |
| | | 57 | | { |
| | 1 | 58 | | var matches = new HashSet<string>(); |
| | | 59 | | |
| | 1 | 60 | | using var reader = new StringReader(csvContent); |
| | 1 | 61 | | using var csv = new CsvReader(reader, CultureInfo.InvariantCulture); |
| | | 62 | | |
| | | 63 | | try |
| | | 64 | | { |
| | 1 | 65 | | csv.Read(); |
| | 1 | 66 | | csv.ReadHeader(); |
| | | 67 | | |
| | 1 | 68 | | while (csv.Read()) |
| | | 69 | | { |
| | 1 | 70 | | var competition = csv.GetField("Competition") ?? ""; |
| | 1 | 71 | | var homeTeam = csv.GetField("Home_Team") ?? ""; |
| | 1 | 72 | | var awayTeam = csv.GetField("Away_Team") ?? ""; |
| | 1 | 73 | | var score = csv.GetField("Score") ?? ""; |
| | 1 | 74 | | var annotation = (csv.TryGetField<string>("Annotation", out var ann) ? ann : null) ?? ""; |
| | | 75 | | |
| | 1 | 76 | | var matchKey = CreateMatchKey(competition, homeTeam, awayTeam, score, annotation); |
| | 1 | 77 | | matches.Add(matchKey); |
| | | 78 | | } |
| | 1 | 79 | | } |
| | 1 | 80 | | catch (Exception) |
| | | 81 | | { |
| | | 82 | | // If CSV parsing fails, return empty set |
| | 1 | 83 | | } |
| | | 84 | | |
| | 1 | 85 | | return matches; |
| | 1 | 86 | | } |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Extracts matches with their collection dates from CSV content that has Data_Collected_At. |
| | | 90 | | /// </summary> |
| | | 91 | | private static Dictionary<string, string> ExtractMatchesWithCollectionDates(string csvContent) |
| | | 92 | | { |
| | 1 | 93 | | var matches = new Dictionary<string, string>(); |
| | | 94 | | |
| | 1 | 95 | | if (!HasDataCollectedAtColumn(csvContent)) |
| | | 96 | | { |
| | 1 | 97 | | return matches; |
| | | 98 | | } |
| | | 99 | | |
| | 1 | 100 | | using var reader = new StringReader(csvContent); |
| | 1 | 101 | | using var csv = new CsvReader(reader, CultureInfo.InvariantCulture); |
| | | 102 | | |
| | | 103 | | try |
| | | 104 | | { |
| | 1 | 105 | | csv.Read(); |
| | 1 | 106 | | csv.ReadHeader(); |
| | | 107 | | |
| | 1 | 108 | | while (csv.Read()) |
| | | 109 | | { |
| | 1 | 110 | | var competition = csv.GetField("Competition") ?? ""; |
| | 1 | 111 | | var dataCollectedAt = csv.GetField("Data_Collected_At") ?? ""; |
| | 1 | 112 | | var homeTeam = csv.GetField("Home_Team") ?? ""; |
| | 1 | 113 | | var awayTeam = csv.GetField("Away_Team") ?? ""; |
| | 1 | 114 | | var score = csv.GetField("Score") ?? ""; |
| | 1 | 115 | | var annotation = (csv.TryGetField<string>("Annotation", out var ann) ? ann : null) ?? ""; |
| | | 116 | | |
| | 1 | 117 | | var matchKey = CreateMatchKey(competition, homeTeam, awayTeam, score, annotation); |
| | 1 | 118 | | matches[matchKey] = dataCollectedAt; |
| | | 119 | | } |
| | 1 | 120 | | } |
| | 0 | 121 | | catch (Exception) |
| | | 122 | | { |
| | | 123 | | // If CSV parsing fails, return empty dictionary |
| | 0 | 124 | | } |
| | | 125 | | |
| | 1 | 126 | | return matches; |
| | 1 | 127 | | } |
| | | 128 | | |
| | | 129 | | /// <summary> |
| | | 130 | | /// Builds a new CSV with the Data_Collected_At column. |
| | | 131 | | /// </summary> |
| | | 132 | | private static string BuildCsvWithDataCollectedAt( |
| | | 133 | | string originalCsvContent, |
| | | 134 | | HashSet<string> currentMatches, |
| | | 135 | | Dictionary<string, string> previousMatches, |
| | | 136 | | string collectedDate) |
| | | 137 | | { |
| | 1 | 138 | | using var reader = new StringReader(originalCsvContent); |
| | 1 | 139 | | using var csv = new CsvReader(reader, CultureInfo.InvariantCulture); |
| | | 140 | | |
| | 1 | 141 | | using var writer = new StringWriter(); |
| | 1 | 142 | | using var csvWriter = new CsvWriter(writer, CultureInfo.InvariantCulture); |
| | | 143 | | |
| | | 144 | | try |
| | | 145 | | { |
| | 1 | 146 | | csv.Read(); |
| | 1 | 147 | | csv.ReadHeader(); |
| | | 148 | | |
| | | 149 | | // Write new header with Data_Collected_At after Competition |
| | 1 | 150 | | csvWriter.WriteField("Competition"); |
| | 1 | 151 | | csvWriter.WriteField("Data_Collected_At"); |
| | 1 | 152 | | csvWriter.WriteField("Home_Team"); |
| | 1 | 153 | | csvWriter.WriteField("Away_Team"); |
| | 1 | 154 | | csvWriter.WriteField("Score"); |
| | 1 | 155 | | csvWriter.WriteField("Annotation"); |
| | 1 | 156 | | csvWriter.NextRecord(); |
| | | 157 | | |
| | 1 | 158 | | while (csv.Read()) |
| | | 159 | | { |
| | 1 | 160 | | var competition = csv.GetField("Competition") ?? ""; |
| | 1 | 161 | | var homeTeam = csv.GetField("Home_Team") ?? ""; |
| | 1 | 162 | | var awayTeam = csv.GetField("Away_Team") ?? ""; |
| | 1 | 163 | | var score = csv.GetField("Score") ?? ""; |
| | 1 | 164 | | var annotation = (csv.TryGetField<string>("Annotation", out var ann) ? ann : null) ?? ""; |
| | | 165 | | |
| | 1 | 166 | | var matchKey = CreateMatchKey(competition, homeTeam, awayTeam, score, annotation); |
| | | 167 | | |
| | | 168 | | // Determine the collection date for this match |
| | | 169 | | string dataCollectedAt; |
| | 1 | 170 | | if (previousMatches.TryGetValue(matchKey, out var existingDate)) |
| | | 171 | | { |
| | | 172 | | // Match existed in previous version, use its existing date |
| | 1 | 173 | | dataCollectedAt = existingDate; |
| | | 174 | | } |
| | | 175 | | else |
| | | 176 | | { |
| | | 177 | | // New match, use current collection date |
| | 1 | 178 | | dataCollectedAt = collectedDate; |
| | | 179 | | } |
| | | 180 | | |
| | 1 | 181 | | csvWriter.WriteField(competition); |
| | 1 | 182 | | csvWriter.WriteField(dataCollectedAt); |
| | 1 | 183 | | csvWriter.WriteField(homeTeam); |
| | 1 | 184 | | csvWriter.WriteField(awayTeam); |
| | 1 | 185 | | csvWriter.WriteField(score); |
| | 1 | 186 | | csvWriter.WriteField(annotation); |
| | 1 | 187 | | csvWriter.NextRecord(); |
| | | 188 | | } |
| | 1 | 189 | | } |
| | 1 | 190 | | catch (Exception) |
| | | 191 | | { |
| | | 192 | | // If parsing fails, return original content |
| | 1 | 193 | | return originalCsvContent; |
| | | 194 | | } |
| | | 195 | | |
| | 1 | 196 | | return writer.ToString(); |
| | 1 | 197 | | } |
| | | 198 | | |
| | | 199 | | /// <summary> |
| | | 200 | | /// Creates a unique key for a match. |
| | | 201 | | /// </summary> |
| | | 202 | | private static string CreateMatchKey(string competition, string homeTeam, string awayTeam, string score, string anno |
| | | 203 | | { |
| | 1 | 204 | | return $"{competition}|{homeTeam}|{awayTeam}|{score}|{annotation}"; |
| | | 205 | | } |
| | | 206 | | } |