T-68: Bug sweep (5 bugs: T-58, T-59, T-61, T-62, T-63)
This commit is contained in:
parent
dea894695a
commit
99ed9bc5a9
2 changed files with 365 additions and 27 deletions
|
|
@ -410,6 +410,51 @@ function previousCalendarDate(ymd: string): string | null {
|
|||
return `${y}-${m}-${d}`;
|
||||
}
|
||||
|
||||
function compareSpanDateRange(a: SpanDateRange, b: SpanDateRange): number {
|
||||
return compareAscii(a.firstBarDate, b.firstBarDate) || compareAscii(a.lastBarDate, b.lastBarDate);
|
||||
}
|
||||
|
||||
/**
|
||||
* Merges matched date spans for a single listing when all spans are overlapping or
|
||||
* calendar-adjacent. Returns `null` if any gap of at least one calendar day separates
|
||||
* matched spans, so Stage B never bridges dates where the listing used another ticker.
|
||||
*/
|
||||
function mergeContiguousSpans(spans: readonly SpanDateRange[]): SpanDateRange | null {
|
||||
if (spans.length === 0) {
|
||||
return null;
|
||||
}
|
||||
const first = spans[0];
|
||||
if (first === undefined) {
|
||||
return null;
|
||||
}
|
||||
if (spans.length === 1) {
|
||||
return first;
|
||||
}
|
||||
const sorted = spans.slice().sort(compareSpanDateRange);
|
||||
const head = sorted[0];
|
||||
if (head === undefined) {
|
||||
return null;
|
||||
}
|
||||
const mergedFrom = head.firstBarDate;
|
||||
let mergedTo = head.lastBarDate;
|
||||
for (let i = 1; i < sorted.length; i++) {
|
||||
const next = sorted[i];
|
||||
if (next === undefined) {
|
||||
continue;
|
||||
}
|
||||
if (next.firstBarDate > mergedTo) {
|
||||
const prevDay = previousCalendarDate(next.firstBarDate);
|
||||
if (prevDay === null || prevDay > mergedTo) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
if (next.lastBarDate > mergedTo) {
|
||||
mergedTo = next.lastBarDate;
|
||||
}
|
||||
}
|
||||
return { firstBarDate: mergedFrom, lastBarDate: mergedTo };
|
||||
}
|
||||
|
||||
function makeListingNaturalKeyId(key: Readonly<ListingNaturalKey>): string {
|
||||
return `${key.primaryIsin}\u0000${key.mic}\u0000${key.tickerRoot}\u0000${key.classSuffix}\u0000${key.currency}`;
|
||||
}
|
||||
|
|
@ -1300,9 +1345,18 @@ export function matchListingsStageB(
|
|||
continue;
|
||||
}
|
||||
|
||||
const matchedSpanByAcc = new Map<ListingAccumulator, SpanDateRange>();
|
||||
const matchedSpansByAcc = new Map<ListingAccumulator, SpanDateRange[]>();
|
||||
const missingSpanSet = new Set<ListingAccumulator>();
|
||||
|
||||
const recordMatchedSpan = (acc: ListingAccumulator, matchedSpan: SpanDateRange): void => {
|
||||
const existing = matchedSpansByAcc.get(acc);
|
||||
if (existing === undefined) {
|
||||
matchedSpansByAcc.set(acc, [matchedSpan]);
|
||||
} else {
|
||||
existing.push(matchedSpan);
|
||||
}
|
||||
};
|
||||
|
||||
for (let k = 0; k < candidates.length; k++) {
|
||||
const cand = candidates[k];
|
||||
if (cand === undefined) {
|
||||
|
|
@ -1318,7 +1372,7 @@ export function matchListingsStageB(
|
|||
lastBarDate: cand.acc.listedTo,
|
||||
};
|
||||
if (spansOverlap(spanCheck.span, listingSpan, overlapOptions)) {
|
||||
matchedSpanByAcc.set(cand.acc, spanCheck.span);
|
||||
recordMatchedSpan(cand.acc, spanCheck.span);
|
||||
}
|
||||
}
|
||||
continue;
|
||||
|
|
@ -1358,20 +1412,12 @@ export function matchListingsStageB(
|
|||
const clampedProviderSpan: SpanDateRange = { firstBarDate: intFrom, lastBarDate: intTo };
|
||||
const aliasListingSpan: SpanDateRange = { firstBarDate: effFrom, lastBarDate: effTo };
|
||||
if (spansOverlap(clampedProviderSpan, aliasListingSpan, overlapOptions)) {
|
||||
const existing = matchedSpanByAcc.get(cand.acc);
|
||||
if (existing === undefined) {
|
||||
matchedSpanByAcc.set(cand.acc, clampedProviderSpan);
|
||||
} else {
|
||||
matchedSpanByAcc.set(cand.acc, {
|
||||
firstBarDate: intFrom < existing.firstBarDate ? intFrom : existing.firstBarDate,
|
||||
lastBarDate: intTo > existing.lastBarDate ? intTo : existing.lastBarDate,
|
||||
});
|
||||
}
|
||||
recordMatchedSpan(cand.acc, clampedProviderSpan);
|
||||
}
|
||||
}
|
||||
|
||||
if (matchedSpanByAcc.size > 1) {
|
||||
const candidateListingKeys = Array.from(matchedSpanByAcc.keys(), c => c.key).sort(
|
||||
if (matchedSpansByAcc.size > 1) {
|
||||
const candidateListingKeys = Array.from(matchedSpansByAcc.keys(), c => c.key).sort(
|
||||
compareListingNaturalKey
|
||||
);
|
||||
conflicts.push({
|
||||
|
|
@ -1387,6 +1433,47 @@ export function matchListingsStageB(
|
|||
},
|
||||
});
|
||||
sourceStats.conflict += 1;
|
||||
} else if (matchedSpansByAcc.size === 1) {
|
||||
const matchedEntry = matchedSpansByAcc.entries().next().value;
|
||||
const mergedSpan =
|
||||
matchedEntry !== undefined ? mergeContiguousSpans(matchedEntry[1]) : null;
|
||||
if (matchedEntry !== undefined && mergedSpan === null) {
|
||||
// Matched disjoint alias windows for one listing; a single mapping cannot represent both
|
||||
// without falsely claiming intervening dates under another ticker.
|
||||
conflicts.push({
|
||||
source,
|
||||
sourceId: record.sourceId,
|
||||
conflictKind: 'ambiguous_ticker_span',
|
||||
detail: {
|
||||
primaryIsin: null,
|
||||
currency,
|
||||
ibConid: null,
|
||||
competingConids: [],
|
||||
candidateListingKeys: [matchedEntry[0].key],
|
||||
},
|
||||
});
|
||||
sourceStats.conflict += 1;
|
||||
} else if (missingSpanSet.size > 0) {
|
||||
// Never match when a same-key listing has an unknown span (could hide a reused-ticker collision).
|
||||
unresolved.push({
|
||||
source,
|
||||
sourceId: record.sourceId,
|
||||
reason: 'missing_span',
|
||||
primaryIsin: null,
|
||||
});
|
||||
sourceStats.unresolved += 1;
|
||||
} else if (matchedEntry !== undefined && mergedSpan !== null) {
|
||||
const [matchedListing] = matchedEntry;
|
||||
providerMappings.push({
|
||||
source,
|
||||
sourceId: record.sourceId,
|
||||
listingKey: matchedListing.key,
|
||||
method: 'tier3_ticker_span',
|
||||
validFrom: mergedSpan.firstBarDate,
|
||||
validTo: mergedSpan.lastBarDate,
|
||||
});
|
||||
sourceStats.matched += 1;
|
||||
}
|
||||
} else if (missingSpanSet.size > 0) {
|
||||
// Never match when a same-key listing has an unknown span (could hide a reused-ticker collision).
|
||||
unresolved.push({
|
||||
|
|
@ -1396,20 +1483,6 @@ export function matchListingsStageB(
|
|||
primaryIsin: null,
|
||||
});
|
||||
sourceStats.unresolved += 1;
|
||||
} else if (matchedSpanByAcc.size === 1) {
|
||||
const matchedEntry = matchedSpanByAcc.entries().next().value;
|
||||
if (matchedEntry !== undefined) {
|
||||
const [matchedListing, matchedSpan] = matchedEntry;
|
||||
providerMappings.push({
|
||||
source,
|
||||
sourceId: record.sourceId,
|
||||
listingKey: matchedListing.key,
|
||||
method: 'tier3_ticker_span',
|
||||
validFrom: matchedSpan.firstBarDate,
|
||||
validTo: matchedSpan.lastBarDate,
|
||||
});
|
||||
sourceStats.matched += 1;
|
||||
}
|
||||
} else {
|
||||
unresolved.push({
|
||||
source,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue