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: {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue