T-87: M1.1l — Security-type classifier: eodSymbols.Type → securityTypeByIsin for the security-master loader (no new dependencies)
This commit is contained in:
parent
2f5ac2e7e7
commit
a368e8a47a
6 changed files with 285 additions and 4 deletions
|
|
@ -3,6 +3,7 @@ import { readFileSync } from 'node:fs';
|
|||
import { resolve } from 'node:path';
|
||||
import {
|
||||
buildSecurityTypeByIsin,
|
||||
buildSecurityTypeByIsinForStageA,
|
||||
classifyEodSecurityType,
|
||||
compareAscii,
|
||||
DEFAULT_MAX_SECURITY_TYPE_CONFLICTS,
|
||||
|
|
@ -11,6 +12,7 @@ import {
|
|||
type SecurityType,
|
||||
type SecurityTypeClassificationStats,
|
||||
type SecurityTypeInputRecord,
|
||||
type StageAInstrumentCandidate,
|
||||
} from '../../apps/stock/data-pipeline/src/security-master';
|
||||
|
||||
function mulberry32(seed: number): () => number {
|
||||
|
|
@ -290,6 +292,72 @@ describe('security-master security-type classifier', () => {
|
|||
}
|
||||
});
|
||||
|
||||
test('buildSecurityTypeByIsinForStageA includes only Stage A-accepted EOD members and prevents quarantined rows from supplying or vetoing types', () => {
|
||||
const records: SecurityTypeInputRecord[] = [
|
||||
// Accepted EOD member for US0378331005
|
||||
{ isin: 'US0378331005', rawType: 'Common Stock', sourceId: 'AAPL.US' },
|
||||
// Quarantined EOD row (not in instruments) sharing US0378331005 with conflicting Type
|
||||
{ isin: 'US0378331005', rawType: 'ETF', sourceId: 'AAPL_STALE.US' },
|
||||
// Quarantined EOD row that is the only row for US30303M1027
|
||||
{ isin: 'US30303M1027', rawType: 'ETF', sourceId: 'META_BAD.US' },
|
||||
// Accepted EOD member whose raw record had null isin (Stage A primaryIsin is authoritative)
|
||||
{ isin: null, rawType: 'Unit', sourceId: 'SCU-UN.TO' },
|
||||
];
|
||||
|
||||
const instruments: StageAInstrumentCandidate[] = [
|
||||
{
|
||||
primaryIsin: 'US0378331005',
|
||||
primaryCusip: '037833100',
|
||||
issuerCountry: 'US',
|
||||
canonicalName: 'Apple Inc.',
|
||||
members: [
|
||||
{ source: 'eod', sourceId: 'AAPL.US', currency: 'USD', method: 'tier1_isin' },
|
||||
{ source: 'ib', sourceId: '265598', currency: 'USD', method: 'tier1_isin' },
|
||||
],
|
||||
},
|
||||
{
|
||||
primaryIsin: 'CA8131371066',
|
||||
primaryCusip: '813137106',
|
||||
issuerCountry: 'CA',
|
||||
canonicalName: 'Second Cup Income Fund',
|
||||
members: [{ source: 'eod', sourceId: 'SCU-UN.TO', currency: 'CAD', method: 'tier2_cusip' }],
|
||||
},
|
||||
// IB-only instrument (no EOD members)
|
||||
{
|
||||
primaryIsin: 'CA7800871021',
|
||||
primaryCusip: '780087102',
|
||||
issuerCountry: 'CA',
|
||||
canonicalName: 'Royal Bank of Canada',
|
||||
members: [{ source: 'ib', sourceId: '100001', currency: 'CAD', method: 'tier1_isin' }],
|
||||
},
|
||||
];
|
||||
|
||||
const result = buildSecurityTypeByIsinForStageA(records, instruments);
|
||||
expect([...result.securityTypeByIsin.entries()]).toEqual([
|
||||
['CA8131371066', 'unit'],
|
||||
['US0378331005', 'common'],
|
||||
]);
|
||||
expect(result.conflicts).toEqual([]);
|
||||
expect(result.unknownRawTypes).toEqual([]);
|
||||
assertStatsReconcile(
|
||||
result.stats,
|
||||
result.securityTypeByIsin.size,
|
||||
result.conflicts.length,
|
||||
0
|
||||
);
|
||||
expect(result.stats).toEqual({
|
||||
recordsIn: 2,
|
||||
classified: 2,
|
||||
missingIsin: 0,
|
||||
missingType: 0,
|
||||
unknownType: 0,
|
||||
distinctClassifiedIsins: 2,
|
||||
isinsMapped: 2,
|
||||
isinsConflicting: 0,
|
||||
conflictsTruncated: 0,
|
||||
});
|
||||
});
|
||||
|
||||
test('local benchmark: >= 300,000 synthetic EOD records classify at >= 100,000 records/s with zero DB connections', () => {
|
||||
const RECORD_COUNT = 300_000;
|
||||
const rand = mulberry32(20261128);
|
||||
|
|
|
|||
|
|
@ -6,6 +6,8 @@ import { BarSpanIndex } from '../../apps/stock/data-pipeline/src/normalization/b
|
|||
import { cusipToIsin } from '../../apps/stock/data-pipeline/src/normalization/identifiers';
|
||||
import { parseTickerDetailed } from '../../apps/stock/data-pipeline/src/normalization/ticker';
|
||||
import {
|
||||
buildSecurityTypeByIsin,
|
||||
buildSecurityTypeByIsinForStageA,
|
||||
CEO_SYMBOLS_PROJECTION,
|
||||
classifySourceExchanges,
|
||||
compareAscii,
|
||||
|
|
@ -1600,6 +1602,130 @@ describe('security-master source-symbol-readers', () => {
|
|||
expect(sources.securityTypeByIsin.has('CA82509L1076')).toBe(false);
|
||||
});
|
||||
|
||||
test('regression: Stage A-quarantined EOD rows cannot veto a valid member type or supply a type, while raw emitted rows remain available for vocabulary diagnostics', async () => {
|
||||
const exchangeLookup = createPass1ExchangeLookup();
|
||||
|
||||
const reader = new FakeSourceSymbolReader(
|
||||
{
|
||||
eodSymbols: [
|
||||
// Valid EOD Common Stock row for AAPL (matching ISIN + CUSIP)
|
||||
{
|
||||
eodSearchCode: 'AAPL.US',
|
||||
Code: 'AAPL',
|
||||
Exchange: 'NASDAQ',
|
||||
Country: 'USA',
|
||||
Currency: 'USD',
|
||||
Isin: 'US0378331005',
|
||||
Cusip: '037833100',
|
||||
Name: 'Apple Inc.',
|
||||
Type: 'Common Stock',
|
||||
},
|
||||
// Stale EOD ETF row sharing AAPL's valid ISIN but carrying SPY's valid CUSIP ->
|
||||
// Stage A quarantines as `isin_cusip_mismatch`. Must NOT veto AAPL's 'common' type.
|
||||
{
|
||||
eodSearchCode: 'AAPL_STALE.US',
|
||||
Code: 'AAPL',
|
||||
Exchange: 'NYSE',
|
||||
Country: 'USA',
|
||||
Currency: 'USD',
|
||||
Isin: 'US0378331005',
|
||||
Cusip: '78462F103',
|
||||
Name: 'Stale Apple ETF Row',
|
||||
Type: 'ETF',
|
||||
},
|
||||
// Quarantined-only EOD row with an unmapped raw Type -> must NOT supply a type,
|
||||
// but its raw Type must stay available in `securityTypeRecords` for vocabulary diagnostics.
|
||||
{
|
||||
eodSearchCode: 'BAD_ONLY.US',
|
||||
Code: 'BADO',
|
||||
Exchange: 'NASDAQ',
|
||||
Country: 'USA',
|
||||
Currency: 'USD',
|
||||
Isin: 'US30303M1027',
|
||||
Cusip: '037833100',
|
||||
Name: 'Mismatched Meta Row',
|
||||
Type: 'Structured Note',
|
||||
},
|
||||
// Duplicate EOD sourceId -> Stage A quarantines both as `duplicate_source_id`.
|
||||
// Must NOT supply a type for CA7800871021.
|
||||
{
|
||||
eodSearchCode: 'DUP.TO',
|
||||
Code: 'RY',
|
||||
Exchange: 'TO',
|
||||
Country: 'Canada',
|
||||
Currency: 'CAD',
|
||||
Isin: 'CA7800871021',
|
||||
Name: 'Royal Bank Dup 1',
|
||||
Type: 'Preferred Stock',
|
||||
},
|
||||
{
|
||||
eodSearchCode: 'DUP.TO',
|
||||
Code: 'RY',
|
||||
Exchange: 'TO',
|
||||
Country: 'Canada',
|
||||
Currency: 'CAD',
|
||||
Isin: 'CA7800871021',
|
||||
Name: 'Royal Bank Dup 2',
|
||||
Type: 'Preferred Stock',
|
||||
},
|
||||
],
|
||||
ibSymbols: [],
|
||||
qmSymbols: [],
|
||||
ceoSymbols: [],
|
||||
eodSymbolChange: [],
|
||||
},
|
||||
{ readOnly: true, applyFilterAndProjection: true }
|
||||
);
|
||||
|
||||
const sources = await readSecurityMasterSources(reader, { exchangeLookup });
|
||||
assertStatsConservation(sources.stats.eodSymbols);
|
||||
expect(sources.stats.eodSymbols.docsRead).toBe(5);
|
||||
expect(sources.stats.eodSymbols.emitted).toBe(5);
|
||||
|
||||
const stageA = matchInstrumentsStageA(sources.stageARecords);
|
||||
expect(stageA.instruments.map(i => i.primaryIsin)).toEqual(['US0378331005']);
|
||||
expect(stageA.instruments[0]?.members.map(m => m.sourceId)).toEqual(['AAPL.US']);
|
||||
expect(
|
||||
stageA.conflicts.map(c => `${c.sourceId}:${c.conflictKind}`).sort(compareAscii)
|
||||
).toEqual([
|
||||
'AAPL_STALE.US:isin_cusip_mismatch',
|
||||
'BAD_ONLY.US:isin_cusip_mismatch',
|
||||
'DUP.TO:duplicate_source_id',
|
||||
'DUP.TO:duplicate_source_id',
|
||||
]);
|
||||
|
||||
// Loader-facing map contains ONLY the Stage A-accepted member's type ('common'), with zero conflicts.
|
||||
expect([...sources.securityTypeByIsin.entries()]).toEqual([['US0378331005', 'common']]);
|
||||
expect(sources.securityTypeConflicts).toEqual([]);
|
||||
expect(sources.securityTypeByIsin.has('US30303M1027')).toBe(false);
|
||||
expect(sources.securityTypeByIsin.has('CA7800871021')).toBe(false);
|
||||
expect(sources.securityTypeStats).toEqual({
|
||||
recordsIn: 1,
|
||||
classified: 1,
|
||||
missingIsin: 0,
|
||||
missingType: 0,
|
||||
unknownType: 0,
|
||||
distinctClassifiedIsins: 1,
|
||||
isinsMapped: 1,
|
||||
isinsConflicting: 0,
|
||||
conflictsTruncated: 0,
|
||||
});
|
||||
|
||||
// Helper produces the identical loader-facing map when called explicitly after Stage A.
|
||||
const explicitStageAMap = buildSecurityTypeByIsinForStageA(
|
||||
sources.securityTypeRecords,
|
||||
stageA.instruments
|
||||
);
|
||||
expect([...explicitStageAMap.securityTypeByIsin.entries()]).toEqual([
|
||||
...sources.securityTypeByIsin.entries(),
|
||||
]);
|
||||
|
||||
// All 5 emitted EOD rows remain available in `securityTypeRecords` for raw vocabulary diagnostics.
|
||||
expect(sources.securityTypeRecords.length).toBe(5);
|
||||
const rawDiagnostics = buildSecurityTypeByIsin(sources.securityTypeRecords);
|
||||
expect(rawDiagnostics.unknownRawTypes).toEqual([{ rawType: 'Structured Note', count: 1 }]);
|
||||
});
|
||||
|
||||
test('local benchmark: >= 368k synthetic docs (300k eod + 50k qm + 18k ib) sustain >= 25,000 docs/s', async () => {
|
||||
const EOD_COUNT = 300_000;
|
||||
const QM_COUNT = 50_000;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue