Add debug logging to company matching for diagnosis
This commit is contained in:
@@ -570,7 +570,7 @@ public sealed class CompanyVerifierService : ICompanyVerifierService
|
||||
return matches?.Company;
|
||||
}
|
||||
|
||||
private static (CompaniesHouseSearchItem Item, int Score)? FindBestMatch(
|
||||
private (CompaniesHouseSearchItem Item, int Score)? FindBestMatch(
|
||||
string companyName,
|
||||
List<CompaniesHouseSearchItem> items,
|
||||
DateOnly? claimedStartDate)
|
||||
@@ -583,30 +583,46 @@ public sealed class CompanyVerifierService : ICompanyVerifierService
|
||||
.Where(m => m.Score >= FuzzyMatchThreshold)
|
||||
.ToList();
|
||||
|
||||
_logger.LogDebug("Found {Count} matches above threshold for '{CompanyName}'", matches.Count, companyName);
|
||||
foreach (var m in matches.Take(5))
|
||||
{
|
||||
_logger.LogDebug(" Match: {Title} ({Number}), Score: {Score}, DateOfCreation: {Date}",
|
||||
m.Item.Title, m.Item.CompanyNumber, m.Score, m.Item.DateOfCreation ?? "null");
|
||||
}
|
||||
|
||||
if (matches.Count == 0) return null;
|
||||
|
||||
// If we have a claimed start date, prefer companies that existed at that time
|
||||
if (claimedStartDate.HasValue)
|
||||
{
|
||||
_logger.LogDebug("Filtering for companies that existed at claimed start date: {StartDate}", claimedStartDate.Value);
|
||||
|
||||
var existedAtStartDate = matches
|
||||
.Where(m =>
|
||||
{
|
||||
var incDate = DateHelpers.ParseDate(m.Item.DateOfCreation);
|
||||
// Company existed if it was incorporated before the claimed start date
|
||||
return incDate == null || incDate <= claimedStartDate.Value;
|
||||
var existed = incDate == null || incDate <= claimedStartDate.Value;
|
||||
_logger.LogDebug(" {Title}: IncDate={IncDate}, Existed={Existed}",
|
||||
m.Item.Title, incDate?.ToString() ?? "null", existed);
|
||||
return existed;
|
||||
})
|
||||
.OrderByDescending(m => m.Score)
|
||||
.ToList();
|
||||
|
||||
_logger.LogDebug("Companies that existed at start date: {Count}", existedAtStartDate.Count);
|
||||
|
||||
// If any matches existed at the start date, prefer those
|
||||
if (existedAtStartDate.Count > 0)
|
||||
{
|
||||
_logger.LogDebug("Selected: {Title} ({Number})", existedAtStartDate[0].Item.Title, existedAtStartDate[0].Item.CompanyNumber);
|
||||
return existedAtStartDate[0];
|
||||
}
|
||||
}
|
||||
|
||||
// Fall back to highest score if no temporal match
|
||||
return matches.OrderByDescending(m => m.Score).First();
|
||||
var fallback = matches.OrderByDescending(m => m.Score).First();
|
||||
_logger.LogDebug("Falling back to highest score: {Title} ({Number})", fallback.Item.Title, fallback.Item.CompanyNumber);
|
||||
return fallback;
|
||||
}
|
||||
|
||||
private async Task CacheCompanyAsync(CompaniesHouseSearchItem item, CompaniesHouseCompany? details)
|
||||
|
||||
Reference in New Issue
Block a user