more work
This commit is contained in:
parent
7a99d08d04
commit
b87a931a2b
11 changed files with 595 additions and 183 deletions
|
|
@ -1,5 +1,6 @@
|
|||
import type { BaseHandler } from '@stock-bot/handlers';
|
||||
import type { DataIngestionServices } from '../../../types';
|
||||
import type { EodHandler } from '../eod.handler';
|
||||
import { EOD_CONFIG } from '../shared';
|
||||
import { getEodExchangeSuffix } from '../shared/utils';
|
||||
|
||||
|
|
@ -10,48 +11,40 @@ interface FetchPricesInput {
|
|||
}
|
||||
|
||||
export async function scheduleFetchPrices(
|
||||
this: BaseHandler<DataIngestionServices>
|
||||
this: EodHandler
|
||||
): Promise<{ success: boolean; jobsScheduled: number }> {
|
||||
const logger = this.logger;
|
||||
|
||||
try {
|
||||
logger.info('Scheduling price fetch jobs for all symbols');
|
||||
|
||||
// Calculate date one week ago
|
||||
const oneWeekAgo = new Date();
|
||||
oneWeekAgo.setDate(oneWeekAgo.getDate() - 7);
|
||||
// Use OperationTracker to find stale symbols
|
||||
const staleSymbols = await this.operationRegistry.getStaleSymbols('eod', 'price_update', {
|
||||
limit: 1000 // Process in batches to avoid overwhelming the system
|
||||
});
|
||||
|
||||
// Find ALL symbols that haven't been updated in the last week
|
||||
const symbols = await this.mongodb.collection('eodSymbols').find({
|
||||
delisted: false,
|
||||
$or: [
|
||||
{ lastPriceUpdate: { $lt: oneWeekAgo } },
|
||||
{ lastPriceUpdate: { $exists: false } }
|
||||
]
|
||||
}).toArray();
|
||||
|
||||
if (!symbols || symbols.length === 0) {
|
||||
if (!staleSymbols || staleSymbols.length === 0) {
|
||||
logger.info('No symbols need price updates');
|
||||
return { success: true, jobsScheduled: 0 };
|
||||
}
|
||||
|
||||
logger.info(`Found ${symbols.length} symbols needing price updates`, {
|
||||
symbols: symbols.map(s => ({
|
||||
symbol: s.Code,
|
||||
exchange: s.Exchange,
|
||||
name: s.Name,
|
||||
lastUpdate: s.lastPriceUpdate
|
||||
logger.info(`Found ${staleSymbols.length} symbols needing price updates`, {
|
||||
symbols: staleSymbols.slice(0, 10).map(s => ({
|
||||
symbol: s.symbol.Code,
|
||||
exchange: s.symbol.Exchange,
|
||||
name: s.symbol.Name,
|
||||
lastUpdate: s.lastRun
|
||||
}))
|
||||
});
|
||||
|
||||
let jobsScheduled = 0;
|
||||
|
||||
// Schedule jobs with staggered delays
|
||||
for (let i = 0; i < symbols.length; i++) {
|
||||
const symbol = symbols[i];
|
||||
for (let i = 0; i < staleSymbols.length; i++) {
|
||||
const { symbol } = staleSymbols[i];
|
||||
logger.debug(`Scheduling price fetch for ${symbol.Code}.${symbol.Exchange}`, {
|
||||
name: symbol.Name,
|
||||
lastUpdate: symbol.lastPriceUpdate,
|
||||
lastUpdate: staleSymbols[i].lastRun,
|
||||
delay: i * 100
|
||||
});
|
||||
|
||||
|
|
@ -83,7 +76,7 @@ export async function scheduleFetchPrices(
|
|||
}
|
||||
|
||||
export async function fetchPrices(
|
||||
this: BaseHandler<DataIngestionServices>,
|
||||
this: EodHandler,
|
||||
input: FetchPricesInput
|
||||
): Promise<{ success: boolean; priceCount: number }> {
|
||||
const logger = this.logger;
|
||||
|
|
@ -165,16 +158,16 @@ export async function fetchPrices(
|
|||
['date', 'symbolExchange']
|
||||
);
|
||||
|
||||
// Update the symbol's last price update timestamp
|
||||
await this.mongodb.collection('eodSymbols').updateOne(
|
||||
{ Code: symbol, Exchange: exchange },
|
||||
{
|
||||
$set: {
|
||||
lastPriceUpdate: new Date(),
|
||||
lastPriceDate: priceData.length > 0 ? priceData[priceData.length - 1].date : null
|
||||
}
|
||||
// Update operation tracker instead of directly updating the symbol
|
||||
await this.operationRegistry.updateOperation('eod', symbol, 'price_update', {
|
||||
status: 'success',
|
||||
lastRecordDate: priceData.length > 0 ? priceData[priceData.length - 1].date : null,
|
||||
recordCount: priceData.length,
|
||||
metadata: {
|
||||
insertedCount: result.insertedCount,
|
||||
updatedCount: priceData.length - result.insertedCount
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
logger.info(`Successfully saved ${result.insertedCount} price records for ${symbol}.${exchange}`);
|
||||
|
||||
|
|
@ -184,6 +177,13 @@ export async function fetchPrices(
|
|||
};
|
||||
} catch (error) {
|
||||
logger.error('Failed to fetch or save prices', { error, symbol, exchange });
|
||||
|
||||
// Update operation tracker with failure
|
||||
await this.operationRegistry.updateOperation('eod', symbol, 'price_update', {
|
||||
status: 'failure',
|
||||
error: error.message
|
||||
});
|
||||
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue