T-87: M1.1l — Security-type classifier: eodSymbols.Type → securityTypeByIsin for the security-master loader (no new dependencies)
This commit is contained in:
parent
a368e8a47a
commit
b2a1f5ce80
3 changed files with 102 additions and 36 deletions
|
|
@ -113,8 +113,8 @@ bun test test/unit/security-master-source-symbol-readers.test.ts
|
|||
| `Currency`, `Forex`, `FX` | `fx` |
|
||||
|
||||
- **Published-vocabulary & no-guess rule:** Because QA MongoDB is unreachable from the agent sandbox, the table is based on the published EODHD `Type` vocabulary. Non-equity-like values (`FUND`, `Mutual Fund`, `Bond`, `Notes`, `INDEX`, etc.), blanks, non-strings, and unmapped strings return `null` and are never guessed from ticker suffixes or company names. A non-string `Type` on an `eodSymbols` document does not skip the document from Stage A/B — it is counted under `securityTypeStats.missingType`.
|
||||
- **Stage A membership & raw vocabulary diagnostics:** `readEodSymbols` / `readSecurityMasterSources` build the loader-facing `securityTypeByIsin` map strictly from EOD rows accepted as Stage A instrument members (`buildSecurityTypeByIsinForStageA`), so rows quarantined by Stage A (`isin_cusip_mismatch`, `duplicate_source_id`) can neither supply nor veto a valid instrument's security type. All emitted EOD rows remain available in `securityTypeRecords` for raw vocabulary diagnostics (`buildSecurityTypeByIsin(sources.securityTypeRecords)`).
|
||||
- **Reviewing unmapped values & conflicts:** After a real security-master run, inspect `unknownSecurityTypes` (sorted by count descending, then ASCII) for any newly observed provider strings that should be added to `EOD_SECURITY_TYPE_MAP`, and inspect `securityTypeConflicts` (ISINs omitted from `securityTypeByIsin` because multiple Stage A-accepted EOD documents disagreed on their classified type; capped by `maxSecurityTypeConflicts`, default `200`, with total counts in `securityTypeStats.isinsConflicting` and `conflictsTruncated`).
|
||||
- **Stage A membership & raw vocabulary diagnostics:** `readEodSymbols` / `readSecurityMasterSources` build the loader-facing `securityTypeByIsin` map, `securityTypeConflicts`, and `securityTypeLoaderStats` strictly from EOD rows accepted as Stage A instrument members (`buildSecurityTypeByIsinForStageA`), so rows quarantined by Stage A (`isin_cusip_mismatch`, `duplicate_source_id`) can neither supply nor veto a valid instrument's security type. At the same time, `securityTypeStats` and `unknownSecurityTypes` reconcile over **all** emitted EOD rows (also retained in `securityTypeRecords`), so unmapped vocabulary on quarantined rows is never silently hidden.
|
||||
- **Reviewing unmapped values & conflicts:** After a real security-master run, inspect `unknownSecurityTypes` (sorted by count descending, then ASCII, reconciling with `securityTypeStats.unknownType` across all emitted EOD rows) for any newly observed provider strings that should be added to `EOD_SECURITY_TYPE_MAP`, and inspect `securityTypeConflicts` (ISINs omitted from `securityTypeByIsin` because multiple Stage A-accepted EOD documents disagreed on their classified type; capped by `maxSecurityTypeConflicts`, default `200`, reconciling with `securityTypeLoaderStats.isinsConflicting` and `conflictsTruncated`).
|
||||
- **Local synthetic benchmark:** Run the unit suite and fixed-seed 300,000-record synthetic benchmark (zero DB connections) with:
|
||||
|
||||
```bash
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@ import {
|
|||
} from './listing-stage-b';
|
||||
import type { SecurityType } from './persist';
|
||||
import {
|
||||
buildSecurityTypeByIsin,
|
||||
buildSecurityTypeByIsinForStageA,
|
||||
type SecurityTypeClassificationStats,
|
||||
type SecurityTypeConflict,
|
||||
|
|
@ -200,7 +201,7 @@ export type AdaptEodSymbolResult =
|
|||
readonly ok: true;
|
||||
readonly stageARecord: StageASourceRecord;
|
||||
readonly eodListing: StageBEodListingInput;
|
||||
readonly rawType: unknown;
|
||||
readonly rawType: string | null;
|
||||
readonly resolvedIsin: string | null;
|
||||
readonly invalidIdentifiersNulled: number;
|
||||
}
|
||||
|
|
@ -235,6 +236,7 @@ export interface ReadEodSymbolsResult {
|
|||
securityTypeRecords: SecurityTypeInputRecord[];
|
||||
securityTypeByIsin: Map<string, SecurityType>;
|
||||
securityTypeStats: SecurityTypeClassificationStats;
|
||||
securityTypeLoaderStats: SecurityTypeClassificationStats;
|
||||
unknownSecurityTypes: UnknownSecurityTypeCount[];
|
||||
securityTypeConflicts: SecurityTypeConflict[];
|
||||
stats: SourceCollectionReadStats;
|
||||
|
|
@ -278,6 +280,7 @@ export interface SecurityMasterSourcesResult {
|
|||
securityTypeRecords: SecurityTypeInputRecord[];
|
||||
securityTypeByIsin: Map<string, SecurityType>;
|
||||
securityTypeStats: SecurityTypeClassificationStats;
|
||||
securityTypeLoaderStats: SecurityTypeClassificationStats;
|
||||
unknownSecurityTypes: UnknownSecurityTypeCount[];
|
||||
securityTypeConflicts: SecurityTypeConflict[];
|
||||
stats: SecurityMasterSourcesStats;
|
||||
|
|
@ -771,16 +774,17 @@ function isPlainRecord(value: unknown): value is Record<string, unknown> {
|
|||
|
||||
/**
|
||||
* Extracts raw EODHD `Type`/`type` without rejecting non-string values so a malformed `Type`
|
||||
* never skips the symbol document (it is counted as `missingType` by the classifier instead).
|
||||
* never skips the symbol document. Non-string and blank values normalize to `null` so
|
||||
* `securityTypeRecords` ordering is deterministic while still counting as `missingType`.
|
||||
*/
|
||||
function readRawSecurityType(doc: Record<string, unknown>): unknown {
|
||||
function readRawSecurityType(doc: Record<string, unknown>): string | null {
|
||||
if (typeof doc.Type === 'string' && doc.Type.trim().length > 0) {
|
||||
return doc.Type;
|
||||
}
|
||||
if (typeof doc.type === 'string' && doc.type.trim().length > 0) {
|
||||
return doc.type;
|
||||
}
|
||||
return doc.Type !== undefined ? doc.Type : doc.type;
|
||||
return null;
|
||||
}
|
||||
|
||||
function isNorthAmericaIso(code: string | null): code is 'US' | 'CA' {
|
||||
|
|
@ -1050,19 +1054,7 @@ function compareSymbolChanges(a: StageBSymbolChangeInput, b: StageBSymbolChangeI
|
|||
}
|
||||
|
||||
function rawTypeSortKey(rawType: unknown): string {
|
||||
if (typeof rawType === 'string') {
|
||||
return `s:${rawType}`;
|
||||
}
|
||||
if (rawType === null) {
|
||||
return 'n:null';
|
||||
}
|
||||
if (rawType === undefined) {
|
||||
return 'u:undefined';
|
||||
}
|
||||
if (typeof rawType === 'number' || typeof rawType === 'boolean' || typeof rawType === 'bigint') {
|
||||
return `${typeof rawType}:${String(rawType)}`;
|
||||
}
|
||||
return `o:${typeof rawType}`;
|
||||
return typeof rawType === 'string' ? `s:${rawType}` : 'n:null';
|
||||
}
|
||||
|
||||
function compareSecurityTypeRecords(
|
||||
|
|
@ -1733,27 +1725,31 @@ export async function readEodSymbols(
|
|||
eodListings.sort(compareEodListings);
|
||||
securityTypeRecords.sort(compareSecurityTypeRecords);
|
||||
|
||||
// Build the loader-facing map strictly from EOD rows accepted as Stage A instrument members so
|
||||
// Stage A-quarantined rows (e.g. isin_cusip_mismatch, duplicate_source_id) cannot supply or veto
|
||||
// a valid instrument's type, while keeping all emitted rows in `securityTypeRecords` for raw
|
||||
// vocabulary diagnostics.
|
||||
const eodStageA = matchInstrumentsStageA(stageARecords);
|
||||
const securityTypeResult = buildSecurityTypeByIsinForStageA(
|
||||
securityTypeRecords,
|
||||
eodStageA.instruments,
|
||||
// Compute vocabulary diagnostics across every emitted EOD row, while building the loader-facing
|
||||
// `securityTypeByIsin` map and its conflicts strictly from EOD rows accepted as Stage A
|
||||
// instrument members so quarantined rows (e.g. isin_cusip_mismatch, duplicate_source_id)
|
||||
// cannot supply or veto a valid instrument's type.
|
||||
const securityTypeOptions =
|
||||
options.maxSecurityTypeConflicts !== undefined
|
||||
? { maxConflicts: options.maxSecurityTypeConflicts }
|
||||
: undefined
|
||||
: undefined;
|
||||
const allSecurityTypeResult = buildSecurityTypeByIsin(securityTypeRecords, securityTypeOptions);
|
||||
const eodStageA = matchInstrumentsStageA(stageARecords);
|
||||
const loaderSecurityTypeResult = buildSecurityTypeByIsinForStageA(
|
||||
securityTypeRecords,
|
||||
eodStageA.instruments,
|
||||
securityTypeOptions
|
||||
);
|
||||
|
||||
return {
|
||||
stageARecords,
|
||||
eodListings,
|
||||
securityTypeRecords,
|
||||
securityTypeByIsin: securityTypeResult.securityTypeByIsin,
|
||||
securityTypeStats: securityTypeResult.stats,
|
||||
unknownSecurityTypes: securityTypeResult.unknownRawTypes,
|
||||
securityTypeConflicts: securityTypeResult.conflicts,
|
||||
securityTypeByIsin: loaderSecurityTypeResult.securityTypeByIsin,
|
||||
securityTypeStats: allSecurityTypeResult.stats,
|
||||
securityTypeLoaderStats: loaderSecurityTypeResult.stats,
|
||||
unknownSecurityTypes: allSecurityTypeResult.unknownRawTypes,
|
||||
securityTypeConflicts: loaderSecurityTypeResult.conflicts,
|
||||
stats,
|
||||
};
|
||||
}
|
||||
|
|
@ -1971,6 +1967,7 @@ export async function readSecurityMasterSources(
|
|||
securityTypeRecords: eodResult.securityTypeRecords,
|
||||
securityTypeByIsin: eodResult.securityTypeByIsin,
|
||||
securityTypeStats: eodResult.securityTypeStats,
|
||||
securityTypeLoaderStats: eodResult.securityTypeLoaderStats,
|
||||
unknownSecurityTypes: eodResult.unknownSecurityTypes,
|
||||
securityTypeConflicts: eodResult.securityTypeConflicts,
|
||||
stats: {
|
||||
|
|
|
|||
|
|
@ -1439,6 +1439,57 @@ describe('security-master source-symbol-readers', () => {
|
|||
expect(JSON.stringify(candidate)).toBe(baselineJson);
|
||||
expect([...candidate.securityTypeByIsin.entries()]).toEqual(baselineTypeEntries);
|
||||
}
|
||||
|
||||
// Reversed-input determinism fixture: duplicate rows sharing (sourceId, isin) with distinct
|
||||
// object/array malformed `Type` values normalize to a single `null` missing-type sentinel so
|
||||
// `securityTypeRecords` order is cursor-order independent.
|
||||
const malformedDocs = [
|
||||
{
|
||||
eodSearchCode: 'MALF.US',
|
||||
Code: 'MALF',
|
||||
Exchange: 'NASDAQ',
|
||||
Country: 'USA',
|
||||
Currency: 'USD',
|
||||
Isin: 'US0378331005',
|
||||
Name: 'Malformed Type Co',
|
||||
Type: { kind: 'A', nested: 1 } as unknown as string,
|
||||
},
|
||||
{
|
||||
eodSearchCode: 'MALF.US',
|
||||
Code: 'MALF',
|
||||
Exchange: 'NASDAQ',
|
||||
Country: 'USA',
|
||||
Currency: 'USD',
|
||||
Isin: 'US0378331005',
|
||||
Name: 'Malformed Type Co',
|
||||
Type: { kind: 'B', nested: 2 } as unknown as string,
|
||||
},
|
||||
{
|
||||
eodSearchCode: 'MALF.US',
|
||||
Code: 'MALF',
|
||||
Exchange: 'NASDAQ',
|
||||
Country: 'USA',
|
||||
Currency: 'USD',
|
||||
Isin: 'US0378331005',
|
||||
Name: 'Malformed Type Co',
|
||||
Type: ['ETF'] as unknown as string,
|
||||
},
|
||||
];
|
||||
const forwardMalformed = await readSecurityMasterSources(
|
||||
new FakeSourceSymbolReader({ eodSymbols: malformedDocs }),
|
||||
{ exchangeLookup }
|
||||
);
|
||||
const reversedMalformed = await readSecurityMasterSources(
|
||||
new FakeSourceSymbolReader({ eodSymbols: [...malformedDocs].reverse() }),
|
||||
{ exchangeLookup }
|
||||
);
|
||||
expect(JSON.stringify(reversedMalformed)).toBe(JSON.stringify(forwardMalformed));
|
||||
expect(forwardMalformed.securityTypeRecords).toEqual([
|
||||
{ isin: 'US0378331005', rawType: null, sourceId: 'MALF.US' },
|
||||
{ isin: 'US0378331005', rawType: null, sourceId: 'MALF.US' },
|
||||
{ isin: 'US0378331005', rawType: null, sourceId: 'MALF.US' },
|
||||
]);
|
||||
expect(forwardMalformed.securityTypeStats.missingType).toBe(3);
|
||||
});
|
||||
|
||||
test('security-type wiring & Stage A integration: maps typed EOD docs (including lowercase type and CUSIP-derived ISIN), keeps non-string Type docs, and reports conflicts/unknowns', async () => {
|
||||
|
|
@ -1569,6 +1620,7 @@ describe('security-master source-symbol-readers', () => {
|
|||
isinsConflicting: 1,
|
||||
conflictsTruncated: 0,
|
||||
});
|
||||
expect(sources.securityTypeLoaderStats).toEqual(sources.securityTypeStats);
|
||||
|
||||
// Feed reader output directly into Stage A and verify every typed non-conflicting EOD instrument
|
||||
// resolves to an ISIN present in securityTypeByIsin (zero missing_security_type skips in persistSecurityMaster).
|
||||
|
|
@ -1694,12 +1746,12 @@ describe('security-master source-symbol-readers', () => {
|
|||
'DUP.TO:duplicate_source_id',
|
||||
]);
|
||||
|
||||
// Loader-facing map contains ONLY the Stage A-accepted member's type ('common'), with zero conflicts.
|
||||
// Loader-facing map, conflicts, and loader stats reflect ONLY Stage A-accepted EOD members.
|
||||
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({
|
||||
expect(sources.securityTypeLoaderStats).toEqual({
|
||||
recordsIn: 1,
|
||||
classified: 1,
|
||||
missingIsin: 0,
|
||||
|
|
@ -1711,6 +1763,21 @@ describe('security-master source-symbol-readers', () => {
|
|||
conflictsTruncated: 0,
|
||||
});
|
||||
|
||||
// Returned vocabulary diagnostics (`securityTypeStats` & `unknownSecurityTypes`) cover ALL 5
|
||||
// emitted EOD rows directly, including quarantined rows like `Structured Note`.
|
||||
expect(sources.securityTypeStats).toEqual({
|
||||
recordsIn: 5,
|
||||
classified: 4,
|
||||
missingIsin: 0,
|
||||
missingType: 0,
|
||||
unknownType: 1,
|
||||
distinctClassifiedIsins: 2,
|
||||
isinsMapped: 1,
|
||||
isinsConflicting: 1,
|
||||
conflictsTruncated: 0,
|
||||
});
|
||||
expect(sources.unknownSecurityTypes).toEqual([{ rawType: 'Structured Note', count: 1 }]);
|
||||
|
||||
// Helper produces the identical loader-facing map when called explicitly after Stage A.
|
||||
const explicitStageAMap = buildSecurityTypeByIsinForStageA(
|
||||
sources.securityTypeRecords,
|
||||
|
|
@ -1719,11 +1786,13 @@ describe('security-master source-symbol-readers', () => {
|
|||
expect([...explicitStageAMap.securityTypeByIsin.entries()]).toEqual([
|
||||
...sources.securityTypeByIsin.entries(),
|
||||
]);
|
||||
expect(explicitStageAMap.stats).toEqual(sources.securityTypeLoaderStats);
|
||||
|
||||
// All 5 emitted EOD rows remain available in `securityTypeRecords` for raw vocabulary diagnostics.
|
||||
// All 5 emitted EOD rows also remain available in `securityTypeRecords` and reconcile with returned diagnostics.
|
||||
expect(sources.securityTypeRecords.length).toBe(5);
|
||||
const rawDiagnostics = buildSecurityTypeByIsin(sources.securityTypeRecords);
|
||||
expect(rawDiagnostics.unknownRawTypes).toEqual([{ rawType: 'Structured Note', count: 1 }]);
|
||||
expect(rawDiagnostics.stats).toEqual(sources.securityTypeStats);
|
||||
expect(rawDiagnostics.unknownRawTypes).toEqual(sources.unknownSecurityTypes);
|
||||
});
|
||||
|
||||
test('local benchmark: >= 368k synthetic docs (300k eod + 50k qm + 18k ib) sustain >= 25,000 docs/s', async () => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue