| | | 1 | | using System.Globalization; |
| | | 2 | | using System.Text; |
| | | 3 | | using RoleRadar.Application.Tracking.Import.Commands; |
| | | 4 | | |
| | | 5 | | namespace RoleRadar.Application.Tracking.Import.Report; |
| | | 6 | | |
| | | 7 | | public static class TrackerImportReportGenerator |
| | | 8 | | { |
| | | 9 | | public static bool IsInsideGitWorkTree(string path) |
| | | 10 | | { |
| | 4 | 11 | | if (string.IsNullOrWhiteSpace(path)) |
| | | 12 | | { |
| | 0 | 13 | | return false; |
| | | 14 | | } |
| | | 15 | | |
| | 4 | 16 | | var fullPath = Path.GetFullPath(path); |
| | 4 | 17 | | var dir = new DirectoryInfo(Path.GetDirectoryName(fullPath) ?? fullPath); |
| | | 18 | | |
| | 20 | 19 | | while (dir is not null) |
| | | 20 | | { |
| | 18 | 21 | | if (Directory.Exists(Path.Combine(dir.FullName, ".git")) || |
| | 18 | 22 | | File.Exists(Path.Combine(dir.FullName, ".git"))) |
| | | 23 | | { |
| | 2 | 24 | | return true; |
| | | 25 | | } |
| | | 26 | | |
| | 16 | 27 | | dir = dir.Parent; |
| | | 28 | | } |
| | | 29 | | |
| | 2 | 30 | | return false; |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | public static async Task GenerateReportAsync( |
| | | 34 | | string reportPath, |
| | | 35 | | ImportTrackerCommand command, |
| | | 36 | | TrackerImportSummary summary, |
| | | 37 | | CancellationToken cancellationToken = default) |
| | | 38 | | { |
| | 4 | 39 | | ArgumentNullException.ThrowIfNull(reportPath); |
| | 4 | 40 | | ArgumentNullException.ThrowIfNull(command); |
| | 4 | 41 | | ArgumentNullException.ThrowIfNull(summary); |
| | | 42 | | |
| | 4 | 43 | | var sb = new StringBuilder(); |
| | 4 | 44 | | var inv = CultureInfo.InvariantCulture; |
| | | 45 | | |
| | | 46 | | // 1. Header & Execution Mode |
| | 4 | 47 | | sb.AppendLine("================================================================================"); |
| | 4 | 48 | | sb.AppendLine(" RoleRadar Tracker Import Execution Report "); |
| | 4 | 49 | | sb.AppendLine("================================================================================"); |
| | 4 | 50 | | sb.AppendLine(inv, $"Timestamp (UTC): {DateTime.UtcNow:yyyy-MM-dd HH:mm:ss}Z"); |
| | 4 | 51 | | sb.AppendLine(inv, $"Execution Mode: {(summary.IsDryRun ? "DRY_RUN" : "APPLIED")}"); |
| | 4 | 52 | | sb.AppendLine(inv, $"Bundle Directory: {command.BundleDirectory}"); |
| | 4 | 53 | | sb.AppendLine(inv, $"Default Date Anchor: {(command.DefaultDate.HasValue ? command.DefaultDate.Value.ToString("y |
| | 4 | 54 | | sb.AppendLine(); |
| | | 55 | | |
| | | 56 | | // 2. Aggregated Counts Summary |
| | 4 | 57 | | sb.AppendLine("--------------------------------------------------------------------------------"); |
| | 4 | 58 | | sb.AppendLine(" Aggregated Counts Summary "); |
| | 4 | 59 | | sb.AppendLine("--------------------------------------------------------------------------------"); |
| | 4 | 60 | | sb.AppendLine(inv, $"Companies Created: {summary.CompaniesCreated}"); |
| | 4 | 61 | | sb.AppendLine(inv, $"Companies Updated: {summary.CompaniesUpdated}"); |
| | 4 | 62 | | sb.AppendLine(inv, $"Companies Unchanged/Skipped: {summary.CompaniesUnchanged}"); |
| | 4 | 63 | | sb.AppendLine(inv, $"Postings Created: {summary.PostingsCreated}"); |
| | 4 | 64 | | sb.AppendLine(inv, $"Postings Updated: {summary.PostingsUpdated}"); |
| | 4 | 65 | | sb.AppendLine(inv, $"Postings Unchanged/Skipped: {summary.PostingsUnchanged}"); |
| | 4 | 66 | | sb.AppendLine(inv, $"Applications Created: {summary.ApplicationsCreated}"); |
| | 4 | 67 | | sb.AppendLine(inv, $"Applications Unchanged/Skipped: {summary.ApplicationsUnchanged}"); |
| | 4 | 68 | | sb.AppendLine(inv, $"Standalone Notes Created: {summary.StandaloneNotesCreated}"); |
| | 4 | 69 | | sb.AppendLine(inv, $"Screening Notes Created: {summary.ScreeningNotesCreated}"); |
| | 4 | 70 | | sb.AppendLine(inv, $"Criteria Registered: {summary.CriteriaCreated}"); |
| | 4 | 71 | | sb.AppendLine(inv, $"Signals Created: {summary.SignalsCreated}"); |
| | 4 | 72 | | sb.AppendLine(inv, $"Mapped to Event Notes: {summary.MappedToEventNotes}"); |
| | 4 | 73 | | sb.AppendLine(inv, $"Resume Variants Defaulted: {summary.ResumeVariantsDefaulted}"); |
| | 4 | 74 | | sb.AppendLine(inv, $"Dates Defaulted: {summary.DatesDefaulted}"); |
| | 4 | 75 | | sb.AppendLine(inv, $"Dates Inferred (Rollback/SameYear): {summary.DatesInferred}"); |
| | 4 | 76 | | sb.AppendLine(inv, $"Dates Flagged (or Scores Flagged): {summary.DatesFlagged}"); |
| | 4 | 77 | | sb.AppendLine(inv, $"Culture Summaries Truncated (>500): {summary.CultureTruncations}"); |
| | 4 | 78 | | sb.AppendLine(inv, $"Culture Levels Unmapped: {summary.CultureUnmapped}"); |
| | 4 | 79 | | sb.AppendLine(inv, $"Total Field Truncations: {summary.FieldTruncations.Values.Sum()}"); |
| | 4 | 80 | | sb.AppendLine(inv, $"Unclaimed Applied Roles Remaining: {summary.UnclaimedAppliedPostings}"); |
| | 4 | 81 | | sb.AppendLine(inv, $"Total Row Errors Encountered: {summary.Errors.Count}"); |
| | 4 | 82 | | if (summary.FieldTruncations.Count > 0) |
| | | 83 | | { |
| | 0 | 84 | | sb.AppendLine(" Field Truncation Breakdown:"); |
| | 0 | 85 | | foreach (var (field, count) in summary.FieldTruncations.OrderBy(kv => kv.Key, StringComparer.OrdinalIgnoreCa |
| | | 86 | | { |
| | 0 | 87 | | sb.AppendLine(inv, $" - {field}: {count}"); |
| | | 88 | | } |
| | | 89 | | } |
| | 4 | 90 | | sb.AppendLine(); |
| | | 91 | | |
| | | 92 | | // 2b. File Reconciliation Summary |
| | 4 | 93 | | sb.AppendLine("--------------------------------------------------------------------------------"); |
| | 4 | 94 | | sb.AppendLine(" File-Level Reconciliation Summary "); |
| | 4 | 95 | | sb.AppendLine("--------------------------------------------------------------------------------"); |
| | 4 | 96 | | if (summary.FileReconciliations.Count == 0) |
| | | 97 | | { |
| | 2 | 98 | | sb.AppendLine("No files processed."); |
| | | 99 | | } |
| | | 100 | | else |
| | | 101 | | { |
| | 2 | 102 | | sb.AppendLine(string.Format(inv, "{0,-25} {1,8} {2,8} {3,8} {4,10} {5,8} {6,8}", "File", "Read", "Created", |
| | 2 | 103 | | sb.AppendLine(new string('-', 81)); |
| | 34 | 104 | | foreach (var f in summary.FileReconciliations.Values.OrderBy(x => x.FileName, StringComparer.OrdinalIgnoreCa |
| | | 105 | | { |
| | 10 | 106 | | if (f.IsAbsent) |
| | | 107 | | { |
| | 0 | 108 | | sb.AppendLine(string.Format(inv, "{0,-25} {1,8} {2,8} {3,8} {4,10} {5,8} {6,8}", f.FileName, "absent |
| | | 109 | | } |
| | | 110 | | else |
| | | 111 | | { |
| | 10 | 112 | | sb.AppendLine(string.Format(inv, "{0,-25} {1,8} {2,8} {3,8} {4,10} {5,8} {6,8}", f.FileName, f.Read, |
| | 10 | 113 | | if (f.SkippedByReason.Count > 0) |
| | | 114 | | { |
| | 0 | 115 | | sb.AppendLine(" Skipped Breakdown:"); |
| | 0 | 116 | | foreach (var (reason, count) in f.SkippedByReason.OrderBy(kv => kv.Key, StringComparer.OrdinalIg |
| | | 117 | | { |
| | 0 | 118 | | sb.AppendLine(inv, $" - {reason}: {count}"); |
| | | 119 | | } |
| | | 120 | | } |
| | 10 | 121 | | if (f.UnchangedByReason.Count > 0) |
| | | 122 | | { |
| | 0 | 123 | | sb.AppendLine(" Unchanged Breakdown:"); |
| | 0 | 124 | | foreach (var (reason, count) in f.UnchangedByReason.OrderBy(kv => kv.Key, StringComparer.Ordinal |
| | | 125 | | { |
| | 0 | 126 | | sb.AppendLine(inv, $" - {reason}: {count}"); |
| | | 127 | | } |
| | | 128 | | } |
| | | 129 | | } |
| | | 130 | | } |
| | | 131 | | } |
| | 4 | 132 | | sb.AppendLine(); |
| | | 133 | | |
| | | 134 | | // 2c. Source Field Coverage |
| | 4 | 135 | | sb.AppendLine("--------------------------------------------------------------------------------"); |
| | 4 | 136 | | sb.AppendLine(" Source Field Coverage "); |
| | 4 | 137 | | sb.AppendLine("--------------------------------------------------------------------------------"); |
| | 4 | 138 | | sb.AppendLine(string.Format(inv, "{0,-23} {1,-20} {2,10} {3}", "File", "Field", "Non-Empty", "Handling")); |
| | 4 | 139 | | sb.AppendLine(new string('-', 88)); |
| | 488 | 140 | | foreach (var def in SourceFieldCatalog.AllFields) |
| | | 141 | | { |
| | 240 | 142 | | var nonNullCount = summary.GetSourceFieldNonEmptyCount(def.File, def.Field); |
| | 240 | 143 | | sb.AppendLine(string.Format(inv, "{0,-23} {1,-20} {2,10} {3}", def.File, def.Field, nonNullCount, def.Handl |
| | | 144 | | } |
| | 4 | 145 | | sb.AppendLine(); |
| | | 146 | | |
| | 4 | 147 | | if (summary.DispositionHistogram.Count > 0) |
| | | 148 | | { |
| | 2 | 149 | | sb.AppendLine("--------------------------------------------------------------------------------"); |
| | 2 | 150 | | sb.AppendLine(" Post-Run Disposition Histogram "); |
| | 2 | 151 | | sb.AppendLine("--------------------------------------------------------------------------------"); |
| | 16 | 152 | | foreach (var (disposition, count) in summary.DispositionHistogram.OrderBy(kv => kv.Key, StringComparer.Ordin |
| | | 153 | | { |
| | 4 | 154 | | sb.AppendLine(inv, $" - {disposition}: {count}"); |
| | | 155 | | } |
| | 2 | 156 | | sb.AppendLine(); |
| | | 157 | | } |
| | | 158 | | |
| | 4 | 159 | | sb.AppendLine("--------------------------------------------------------------------------------"); |
| | 4 | 160 | | sb.AppendLine(" Mapping Decisions "); |
| | 4 | 161 | | sb.AppendLine("--------------------------------------------------------------------------------"); |
| | 4 | 162 | | sb.AppendLine(" - Remote Type: 'remote_mentioned' is preserved as RemoteType.Unknown without storing text in"); |
| | 4 | 163 | | sb.AppendLine(" culture summary (counted under Unmapped Values as 'remote_mentioned: N not stored')."); |
| | 4 | 164 | | sb.AppendLine(" - Resume Variant: Domain entity requires non-nullable ResumeVariant; blank values default"); |
| | 4 | 165 | | sb.AppendLine(" to ResumeVariant.AsIs with 'resume variant unspecified' appended to applied event notes"); |
| | 4 | 166 | | sb.AppendLine(" (counted under Resume Variants Defaulted)."); |
| | 4 | 167 | | sb.AppendLine(" - Fit & Referral: Fit values and referral=true are mapped to applied event notes"); |
| | 4 | 168 | | sb.AppendLine(" (counted under Mapped to Event Notes)."); |
| | 4 | 169 | | sb.AppendLine(); |
| | | 170 | | |
| | 4 | 171 | | if (summary.UnmappedValues.Count > 0) |
| | | 172 | | { |
| | 4 | 173 | | sb.AppendLine("--------------------------------------------------------------------------------"); |
| | 4 | 174 | | sb.AppendLine(" Unmapped Values "); |
| | 4 | 175 | | sb.AppendLine("--------------------------------------------------------------------------------"); |
| | 26 | 176 | | foreach (var (field, count) in summary.UnmappedValues.OrderBy(kv => kv.Key, StringComparer.OrdinalIgnoreCase |
| | | 177 | | { |
| | 6 | 178 | | if (string.Equals(field, "remote_mentioned", StringComparison.OrdinalIgnoreCase)) |
| | | 179 | | { |
| | 2 | 180 | | sb.AppendLine(inv, $" - remote_mentioned: {count} not stored"); |
| | 2 | 181 | | continue; |
| | | 182 | | } |
| | 4 | 183 | | var samples = summary.UnmappedValueSamples.TryGetValue(field, out var list) && list.Count > 0 |
| | 4 | 184 | | ? $" (samples: {string.Join(", ", list.Take(5))})" |
| | 4 | 185 | | : string.Empty; |
| | 4 | 186 | | sb.AppendLine(inv, $" - {field}: {count}{samples}"); |
| | | 187 | | } |
| | 4 | 188 | | sb.AppendLine(); |
| | | 189 | | } |
| | | 190 | | |
| | | 191 | | // 3. Companies Requiring Alias Review |
| | 4 | 192 | | sb.AppendLine("--------------------------------------------------------------------------------"); |
| | 4 | 193 | | sb.AppendLine(" Companies Requiring Alias Review "); |
| | 4 | 194 | | sb.AppendLine("--------------------------------------------------------------------------------"); |
| | 4 | 195 | | if (summary.AutoCreatedCompanies.Count == 0) |
| | | 196 | | { |
| | 2 | 197 | | sb.AppendLine("None. All referenced companies existed or matched known aliases."); |
| | | 198 | | } |
| | | 199 | | else |
| | | 200 | | { |
| | 2 | 201 | | sb.AppendLine("The following companies were auto-created with status 'neutral' and a blank description."); |
| | 2 | 202 | | sb.AppendLine("Inspect for near-miss names and add canonical aliases to company-aliases.csv before --apply:" |
| | 12 | 203 | | foreach (var comp in summary.AutoCreatedCompanies.Distinct()) |
| | | 204 | | { |
| | 4 | 205 | | sb.AppendLine(inv, $" - {comp}"); |
| | | 206 | | } |
| | | 207 | | } |
| | 4 | 208 | | sb.AppendLine(); |
| | | 209 | | |
| | | 210 | | // 4. Unclaimed "Applied" Postings |
| | 4 | 211 | | sb.AppendLine("--------------------------------------------------------------------------------"); |
| | 4 | 212 | | sb.AppendLine(" Unclaimed Applied Postings "); |
| | 4 | 213 | | sb.AppendLine("--------------------------------------------------------------------------------"); |
| | 4 | 214 | | if (summary.UnclaimedAppliedPostingTitles.Count == 0) |
| | | 215 | | { |
| | 2 | 216 | | sb.AppendLine("None. All applied postings in postings.json had matching applications."); |
| | | 217 | | } |
| | | 218 | | else |
| | | 219 | | { |
| | 2 | 220 | | sb.AppendLine("The following postings had status 'applied' in postings.json but no matching record"); |
| | 2 | 221 | | sb.AppendLine("in applications.csv. They have been imported in disposition 'scored':"); |
| | 8 | 222 | | foreach (var title in summary.UnclaimedAppliedPostingTitles) |
| | | 223 | | { |
| | 2 | 224 | | sb.AppendLine(inv, $" - {title}"); |
| | | 225 | | } |
| | | 226 | | } |
| | 4 | 227 | | sb.AppendLine(); |
| | | 228 | | |
| | | 229 | | // 5. Contacts Detected for Manual Entry |
| | 4 | 230 | | sb.AppendLine("--------------------------------------------------------------------------------"); |
| | 4 | 231 | | sb.AppendLine(" Contacts Detected for Manual Entry "); |
| | 4 | 232 | | sb.AppendLine("--------------------------------------------------------------------------------"); |
| | 4 | 233 | | if (summary.PeopleDetected.Count == 0) |
| | | 234 | | { |
| | 2 | 235 | | sb.AppendLine("None. No detected individuals or contains_people flags in applications.csv."); |
| | | 236 | | } |
| | | 237 | | else |
| | | 238 | | { |
| | 2 | 239 | | sb.AppendLine("Per Constitution Principle I, contact records are NEVER created automatically."); |
| | 2 | 240 | | sb.AppendLine("Use 'roleradar contact add <name>' to record interactions with the following contacts:"); |
| | 8 | 241 | | foreach (var detected in summary.PeopleDetected) |
| | | 242 | | { |
| | 2 | 243 | | sb.AppendLine(inv, $" - {detected}"); |
| | | 244 | | } |
| | | 245 | | } |
| | 4 | 246 | | sb.AppendLine(); |
| | | 247 | | |
| | | 248 | | // 6. Row Validation Errors & Warnings |
| | 4 | 249 | | sb.AppendLine("--------------------------------------------------------------------------------"); |
| | 4 | 250 | | sb.AppendLine(" Row Validation Errors & Skipped Rows "); |
| | 4 | 251 | | sb.AppendLine("--------------------------------------------------------------------------------"); |
| | 4 | 252 | | if (summary.Errors.Count == 0) |
| | | 253 | | { |
| | 4 | 254 | | sb.AppendLine("None. All rows processed cleanly without validation errors."); |
| | | 255 | | } |
| | | 256 | | else |
| | | 257 | | { |
| | 0 | 258 | | foreach (var err in summary.Errors) |
| | | 259 | | { |
| | 0 | 260 | | sb.AppendLine(inv, $" - [{err.SourceFile}:{err.LineNumber}] ({err.ErrorCode}) {err.Message}"); |
| | | 261 | | } |
| | | 262 | | } |
| | 4 | 263 | | sb.AppendLine(); |
| | | 264 | | |
| | 4 | 265 | | var dir = Path.GetDirectoryName(reportPath); |
| | 4 | 266 | | if (!string.IsNullOrWhiteSpace(dir) && !Directory.Exists(dir)) |
| | | 267 | | { |
| | 0 | 268 | | Directory.CreateDirectory(dir); |
| | | 269 | | } |
| | | 270 | | |
| | 4 | 271 | | await File.WriteAllTextAsync(reportPath, sb.ToString(), Encoding.UTF8, cancellationToken); |
| | 4 | 272 | | } |
| | | 273 | | } |