T-69: Bug sweep (3 bugs: T-64, T-67, T-71)
This commit is contained in:
parent
2d40d5146a
commit
7f1c9e70ef
4 changed files with 613 additions and 23 deletions
|
|
@ -59,7 +59,8 @@ export type DelistingChainConflictKind =
|
|||
| 'ambiguous_otc_continuation'
|
||||
| 'invalid_listing_dates'
|
||||
| 'duplicate_listing_key'
|
||||
| 'missing_otc_listed_from';
|
||||
| 'missing_otc_listed_from'
|
||||
| 'malformed_listing_entry';
|
||||
|
||||
export interface DelistingChainConflictDetail {
|
||||
primaryIsin: string;
|
||||
|
|
@ -87,6 +88,7 @@ export interface DelistingChainOutcomeStats {
|
|||
export interface DelistingChainStats {
|
||||
totalInputListings: number;
|
||||
processedListings: number;
|
||||
skippedMalformedEntries: number;
|
||||
skippedInvalidDates: number;
|
||||
skippedDuplicateKeys: number;
|
||||
continuitiesCreated: number;
|
||||
|
|
@ -393,18 +395,46 @@ export function linkDelistingChains(
|
|||
const totalInputListings = input.listings.length;
|
||||
|
||||
// Pass 1a: validate container slots and count natural-key occurrences so duplicates are quarantined.
|
||||
const normalizedKeys = new Array<ListingNaturalKey>(totalInputListings);
|
||||
const keyIds = new Array<string>(totalInputListings);
|
||||
const conflicts: DelistingChainConflictRecord[] = [];
|
||||
const normalizedKeys = new Array<ListingNaturalKey | undefined>(totalInputListings);
|
||||
const keyIds = new Array<string | undefined>(totalInputListings);
|
||||
const keyCounts = new Map<string, number>();
|
||||
|
||||
let skippedMalformedEntries = 0;
|
||||
|
||||
for (let i = 0; i < totalInputListings; i++) {
|
||||
let malformedKind: string | null = null;
|
||||
const row = i in input.listings ? input.listings[i] : undefined;
|
||||
if (!(i in input.listings)) {
|
||||
throw new TypeError(`Invalid sparse slot at input.listings[${i}]: expected a listing object`);
|
||||
malformedKind = 'sparse slot';
|
||||
} else if (row === undefined) {
|
||||
malformedKind = 'undefined';
|
||||
} else if (row === null) {
|
||||
malformedKind = 'null';
|
||||
} else if (Array.isArray(row)) {
|
||||
malformedKind = 'array';
|
||||
} else if (typeof row !== 'object') {
|
||||
malformedKind = typeof row;
|
||||
}
|
||||
const row = input.listings[i];
|
||||
if (row === undefined || row === null || typeof row !== 'object') {
|
||||
throw new TypeError(`Invalid listing entry at input.listings[${i}]: expected an object`);
|
||||
|
||||
if (malformedKind !== null || row === undefined) {
|
||||
skippedMalformedEntries += 1;
|
||||
conflicts.push({
|
||||
conflictKind: 'malformed_listing_entry',
|
||||
detail: {
|
||||
primaryIsin: '',
|
||||
currency: '',
|
||||
fromListingKeys: [],
|
||||
toListingKeys: [],
|
||||
candidateListingKeys: [],
|
||||
listedFrom: null,
|
||||
listedTo: null,
|
||||
reason: `Malformed listing entry at input.listings[${i}]: expected a listing object, received ${malformedKind ?? 'undefined'}`,
|
||||
},
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
const key = normalizeKeyFromListing(row);
|
||||
const keyId = makeListingNaturalKeyId(key);
|
||||
normalizedKeys[i] = key;
|
||||
|
|
@ -412,10 +442,29 @@ export function linkDelistingChains(
|
|||
keyCounts.set(keyId, (keyCounts.get(keyId) ?? 0) + 1);
|
||||
}
|
||||
|
||||
const conflicts: DelistingChainConflictRecord[] = [];
|
||||
const validRows: NormalizedListingRow[] = [];
|
||||
const groups = new Map<string, InstrumentCurrencyGroup>();
|
||||
const reportedDuplicateKeyIds = new Set<string>();
|
||||
const droppedOtcKeysByGroup = new Map<string, Map<string, ListingNaturalKey>>();
|
||||
|
||||
const recordDroppedOtcIfApplicable = (key: ListingNaturalKey, keyId: string): void => {
|
||||
if (
|
||||
!normalizedOtcMics.has(key.mic) ||
|
||||
key.primaryIsin.length === 0 ||
|
||||
key.currency.length === 0
|
||||
) {
|
||||
return;
|
||||
}
|
||||
const groupKey = makeGroupKey(key.primaryIsin, key.currency);
|
||||
let droppedForGroup = droppedOtcKeysByGroup.get(groupKey);
|
||||
if (droppedForGroup === undefined) {
|
||||
droppedForGroup = new Map<string, ListingNaturalKey>();
|
||||
droppedOtcKeysByGroup.set(groupKey, droppedForGroup);
|
||||
}
|
||||
if (!droppedForGroup.has(keyId)) {
|
||||
droppedForGroup.set(keyId, key);
|
||||
}
|
||||
};
|
||||
|
||||
let skippedDuplicateKeys = 0;
|
||||
let skippedInvalidDates = 0;
|
||||
|
|
@ -432,6 +481,7 @@ export function linkDelistingChains(
|
|||
const occurrences = keyCounts.get(keyId) ?? 0;
|
||||
if (occurrences > 1) {
|
||||
skippedDuplicateKeys += 1;
|
||||
recordDroppedOtcIfApplicable(key, keyId);
|
||||
if (!reportedDuplicateKeyIds.has(keyId)) {
|
||||
reportedDuplicateKeyIds.add(keyId);
|
||||
conflicts.push({
|
||||
|
|
@ -483,6 +533,7 @@ export function linkDelistingChains(
|
|||
|
||||
if (dateErrorReason !== null) {
|
||||
skippedInvalidDates += 1;
|
||||
recordDroppedOtcIfApplicable(key, keyId);
|
||||
conflicts.push({
|
||||
conflictKind: 'invalid_listing_dates',
|
||||
detail: {
|
||||
|
|
@ -549,36 +600,64 @@ export function linkDelistingChains(
|
|||
continue;
|
||||
}
|
||||
|
||||
if (group.missingFromOtcs.length > 0) {
|
||||
const droppedOtcMap = droppedOtcKeysByGroup.get(
|
||||
makeGroupKey(group.primaryIsin, group.currency)
|
||||
);
|
||||
const droppedOtcCount = droppedOtcMap?.size ?? 0;
|
||||
|
||||
if (group.missingFromOtcs.length > 0 || droppedOtcCount > 0) {
|
||||
const sortedPrimaryKeys = group.endedPrimaries
|
||||
.map(p => p.key)
|
||||
.sort(compareListingNaturalKey);
|
||||
const sortedMissingOtcKeys = group.missingFromOtcs
|
||||
.map(o => o.key)
|
||||
.sort(compareListingNaturalKey);
|
||||
const allKeys = [...sortedPrimaryKeys, ...sortedMissingOtcKeys].sort(
|
||||
const blockingOtcKeyMap = new Map<string, ListingNaturalKey>();
|
||||
for (const o of group.missingFromOtcs) {
|
||||
blockingOtcKeyMap.set(makeListingNaturalKeyId(o.key), o.key);
|
||||
}
|
||||
if (droppedOtcMap !== undefined) {
|
||||
for (const [kId, k] of droppedOtcMap) {
|
||||
if (!blockingOtcKeyMap.has(kId)) {
|
||||
blockingOtcKeyMap.set(kId, k);
|
||||
}
|
||||
}
|
||||
}
|
||||
const sortedBlockingOtcKeys = [...blockingOtcKeyMap.values()].sort(compareListingNaturalKey);
|
||||
const allKeys = [...sortedPrimaryKeys, ...sortedBlockingOtcKeys].sort(
|
||||
compareListingNaturalKey
|
||||
);
|
||||
const singleOtc = group.missingFromOtcs.length === 1 ? group.missingFromOtcs[0] : undefined;
|
||||
const singleMissingOnly =
|
||||
group.missingFromOtcs.length === 1 && droppedOtcCount === 0
|
||||
? group.missingFromOtcs[0]
|
||||
: undefined;
|
||||
let reason: string;
|
||||
if (droppedOtcCount === 0) {
|
||||
reason =
|
||||
group.missingFromOtcs.length === 1
|
||||
? 'OTC listing has null listedFrom; cannot establish transfer window or effectiveDate'
|
||||
: `${group.missingFromOtcs.length} OTC listings have null listedFrom; cannot establish transfer window or effectiveDate`;
|
||||
} else if (group.missingFromOtcs.length === 0) {
|
||||
reason =
|
||||
droppedOtcCount === 1
|
||||
? 'OTC listing was dropped due to duplicate key or invalid dates; cannot establish transfer window or effectiveDate'
|
||||
: `${droppedOtcCount} OTC listings were dropped due to duplicate key or invalid dates; cannot establish transfer window or effectiveDate`;
|
||||
} else {
|
||||
reason = `${group.missingFromOtcs.length} undated OTC listing(s) and ${droppedOtcCount} dropped OTC listing(s) in group; cannot establish transfer window or effectiveDate`;
|
||||
}
|
||||
conflicts.push({
|
||||
conflictKind: 'missing_otc_listed_from',
|
||||
detail: {
|
||||
primaryIsin: group.primaryIsin,
|
||||
currency: group.currency,
|
||||
fromListingKeys: sortedPrimaryKeys.map(cloneListingKey),
|
||||
toListingKeys: sortedMissingOtcKeys.map(cloneListingKey),
|
||||
toListingKeys: sortedBlockingOtcKeys.map(cloneListingKey),
|
||||
candidateListingKeys: allKeys.map(cloneListingKey),
|
||||
listedFrom: null,
|
||||
listedTo: singleOtc !== undefined ? singleOtc.listedTo : null,
|
||||
reason:
|
||||
group.missingFromOtcs.length === 1
|
||||
? 'OTC listing has null listedFrom; cannot establish transfer window or effectiveDate'
|
||||
: `${group.missingFromOtcs.length} OTC listings have null listedFrom; cannot establish transfer window or effectiveDate`,
|
||||
listedTo: singleMissingOnly !== undefined ? singleMissingOnly.listedTo : null,
|
||||
reason,
|
||||
},
|
||||
});
|
||||
// Rule 5 ("never guess"): an undated OTC row in the same (primaryIsin, currency) group
|
||||
// could be the true continuation, so suppress all continuity linking for this group
|
||||
// until listedFrom is resolved.
|
||||
// Rule 5 ("never guess"): an undated or dropped OTC row in the same (primaryIsin, currency)
|
||||
// group could be the true continuation, so suppress all continuity linking for this group
|
||||
// until OTC listing metadata is resolved.
|
||||
continue;
|
||||
}
|
||||
|
||||
|
|
@ -772,6 +851,7 @@ export function linkDelistingChains(
|
|||
stats: {
|
||||
totalInputListings,
|
||||
processedListings: validRows.length,
|
||||
skippedMalformedEntries,
|
||||
skippedInvalidDates,
|
||||
skippedDuplicateKeys,
|
||||
continuitiesCreated: continuities.length,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue