refactor(data): normalize provider symbols and remove dead scaffolding (CL34)

This commit is contained in:
Boki 2026-10-28 13:56:31 -04:00
parent 5c9e32d0ef
commit e2292e4883
11 changed files with 287 additions and 581 deletions

View file

@ -1,34 +0,0 @@
/**
* Service Container Setup for Data Pipeline
* Configures dependency injection for the data pipeline service
*/
import type { UnifiedAppConfig } from '@stock-bot/config';
import type { IServiceContainer } from '@stock-bot/handlers';
import { getLogger } from '@stock-bot/logger';
const logger = getLogger('data-pipeline-container');
/**
* Configure the service container for data pipeline workloads
*/
export function setupServiceContainer(
config: UnifiedAppConfig,
container: IServiceContainer
): IServiceContainer {
logger.info('Configuring data pipeline service container...');
// Data pipeline specific configuration
// This service does more complex queries and transformations
const poolSizes = {
mongodb: config.environment === 'production' ? 40 : 20,
postgres: config.environment === 'production' ? 50 : 25,
cache: config.environment === 'production' ? 30 : 15,
};
logger.info('Data pipeline pool sizes configured', poolSizes);
// The container is already configured with connections
// Just return it with our logging
return container;
}

View file

@ -1,4 +1,8 @@
import { requireServices, type IServiceContainer } from '@stock-bot/services';
import {
normalizeProviderSymbol,
type ProviderSymbolDocument,
} from '../../../normalization/provider-symbol';
import {
PipelineSyncRepository,
rejectDestructiveSync,
@ -21,7 +25,11 @@ export async function syncSymbolsFromProvider(
throw new Error('A supported provider (qm, eod, ib) is required');
}
const { mongodb, postgres } = requireServices(container, 'mongodb', 'postgres');
const rows = await mongodb.getDatabase().collection(collection).find({}).toArray();
const rows = await mongodb
.getDatabase()
.collection<ProviderSymbolDocument>(collection)
.find({})
.toArray();
return PipelineSyncRepository.transaction(postgres, async repository => {
const result: SyncResult = {
processed: rows.length,
@ -31,7 +39,8 @@ export async function syncSymbolsFromProvider(
errors: 0,
};
for (const row of rows) {
result[await repository.syncSymbol(provider, row)]++;
const symbol = normalizeProviderSymbol(row);
result[symbol ? await repository.syncSymbol(provider, symbol) : 'skipped']++;
}
await repository.updateStatus(provider, 'symbols', result.processed);
return result;

View file

@ -7,7 +7,6 @@ import { createHttpSecurity, httpSecurityOptions, ServiceApplication } from '@st
import { getLogger } from '@stock-bot/logger';
import { initializeStockConfig } from '@stock-bot/stock-config';
import { createRoutes } from './routes/create-routes';
import { setupServiceContainer } from './container-setup';
// Local imports
import { initializeAllHandlers } from './handlers';
@ -49,10 +48,6 @@ const app = new ServiceApplication(
{
// Custom lifecycle hooks
onAppReady: app => security.mount(app),
onContainerReady: container => {
// Setup service-specific configuration
setupServiceContainer(config, container);
},
onStarted: _port => {
const logger = getLogger('data-pipeline');
logger.info('Data pipeline service startup initiated with ServiceApplication framework');

View file

@ -0,0 +1,50 @@
/** Fields retained by the QM, EOD and IB symbol collections, including older spellings. */
export interface ProviderSymbolDocument {
symbol?: string | null;
code?: string | null;
Code?: string | null;
exchangeCode?: string | null;
exchange?: string | null;
exchange_id?: string | null;
Exchange?: string | null;
companyName?: string | null;
name?: string | null;
company_name?: string | null;
Name?: string | null;
countryCode?: string | null;
country_code?: string | null;
Country?: string | null;
currency?: string | null;
Currency?: string | null;
qmSearchCode?: string | null;
eodSearchCode?: string | null;
}
/** Provider codes stay exact: exchange resolution and curated mappings belong to SQL. */
export interface ProviderSymbol {
symbol: string;
exchangeCode: string;
providerSymbol: string;
companyName: string | null;
country: string | null;
currency: string | null;
}
/** Preserve the existing field precedence without guessing suffixes or exchange aliases. */
export function normalizeProviderSymbol(document: ProviderSymbolDocument): ProviderSymbol | null {
const symbol = document.symbol || document.code || document.Code;
const exchangeCode =
document.exchangeCode || document.exchange || document.exchange_id || document.Exchange;
if (!symbol || !exchangeCode) {
return null;
}
return {
symbol,
exchangeCode,
providerSymbol: document.qmSearchCode || document.eodSearchCode || symbol,
companyName:
document.companyName || document.name || document.company_name || document.Name || null,
country: document.countryCode || document.country_code || document.Country || null,
currency: document.currency || document.Currency || null,
};
}

View file

@ -1,7 +1,7 @@
import type { Document } from 'mongodb';
import type { TransactionCallback } from '@stock-bot/postgres';
import type { PostgresClient } from '@stock-bot/services';
import { parseReferenceDataResetRequest, type ReferenceDataResetScope } from '@stock-bot/types';
import type { ProviderSymbol } from '../normalization/provider-symbol';
type SqlClient = Pick<Parameters<TransactionCallback<unknown>>[0], 'query'>;
export type ResetScope = ReferenceDataResetScope;
@ -73,17 +73,14 @@ export class PipelineSyncRepository {
return inserted.rows.length ? 'created' : 'skipped';
}
async syncSymbol(provider: string, symbol: Document): Promise<'created' | 'updated' | 'skipped'> {
const code = symbol.symbol || symbol.code || symbol.Code;
const exchangeCode =
symbol.exchangeCode || symbol.exchange || symbol.exchange_id || symbol.Exchange;
if (!code || !exchangeCode) {
return 'skipped';
}
async syncSymbol(
provider: string,
symbol: ProviderSymbol
): Promise<'created' | 'updated' | 'skipped'> {
const mapping = await this.sql.query<{ master_exchange_id: string }>(
`SELECT master_exchange_id FROM provider_exchange_mappings
WHERE provider = $1 AND provider_exchange_code = $2 AND active = true`,
[provider, exchangeCode]
[provider, symbol.exchangeCode]
);
const exchangeId = mapping.rows[0]?.master_exchange_id;
if (!exchangeId) {
@ -91,7 +88,7 @@ export class PipelineSyncRepository {
}
const existing = await this.sql.query<{ id: string }>(
'SELECT id FROM symbols WHERE symbol = $1 AND exchange_id = $2',
[code, exchangeId]
[symbol.symbol, exchangeId]
);
const saved = await this.sql.query<{ id: string }>(
`INSERT INTO symbols (symbol, exchange_id, company_name, country, currency)
@ -99,13 +96,7 @@ export class PipelineSyncRepository {
company_name = COALESCE(EXCLUDED.company_name, symbols.company_name),
country = COALESCE(EXCLUDED.country, symbols.country),
currency = COALESCE(EXCLUDED.currency, symbols.currency), updated_at = NOW() RETURNING id`,
[
code,
exchangeId,
symbol.companyName || symbol.name || symbol.company_name || symbol.Name || null,
symbol.countryCode || symbol.country_code || symbol.Country || null,
symbol.currency || symbol.Currency || null,
]
[symbol.symbol, exchangeId, symbol.companyName, symbol.country, symbol.currency]
);
const symbolId = saved.rows[0]?.id;
if (!symbolId) {
@ -117,12 +108,7 @@ export class PipelineSyncRepository {
symbol_id = CASE WHEN provider_mappings.verified THEN provider_mappings.symbol_id ELSE EXCLUDED.symbol_id END,
provider_exchange = CASE WHEN provider_mappings.verified THEN provider_mappings.provider_exchange ELSE EXCLUDED.provider_exchange END,
last_seen = NOW()`,
[
symbolId,
provider,
symbol.qmSearchCode || symbol.eodSearchCode || symbol.symbol || symbol.code || symbol.Code,
exchangeCode,
]
[symbolId, provider, symbol.providerSymbol, symbol.exchangeCode]
);
return existing.rows.length ? 'updated' : 'created';
}