qm scaffolding done
This commit is contained in:
parent
736b86e66a
commit
c799962f05
11 changed files with 1693 additions and 336 deletions
|
|
@ -0,0 +1,230 @@
|
|||
/**
|
||||
* QM Symbol Info Actions - Fetch and update symbol metadata
|
||||
*/
|
||||
|
||||
import type { BaseHandler, ExecutionContext } from '@stock-bot/handlers';
|
||||
import { QM_CONFIG, QM_SESSION_IDS } from '../shared/config';
|
||||
import { QMSessionManager } from '../shared/session-manager';
|
||||
import { QMOperationTracker } from '../shared/operation-tracker';
|
||||
|
||||
// Cache tracker instance
|
||||
let operationTracker: QMOperationTracker | null = null;
|
||||
|
||||
/**
|
||||
* Get or initialize the operation tracker
|
||||
*/
|
||||
async function getOperationTracker(handler: BaseHandler): Promise<QMOperationTracker> {
|
||||
if (!operationTracker) {
|
||||
const { initializeQMOperations } = await import('../shared/operation-registry');
|
||||
operationTracker = await initializeQMOperations(handler.mongodb, handler.logger);
|
||||
}
|
||||
return operationTracker;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update symbol info for a single symbol
|
||||
* This is a simple API fetch operation - no tracking logic here
|
||||
*/
|
||||
export async function updateSymbolInfo(
|
||||
this: BaseHandler,
|
||||
input: {
|
||||
symbol: string;
|
||||
qmSearchCode: string;
|
||||
},
|
||||
_context?: ExecutionContext
|
||||
): Promise<{
|
||||
success: boolean;
|
||||
symbol: string;
|
||||
message: string;
|
||||
data?: any;
|
||||
}> {
|
||||
const { symbol, qmSearchCode } = input;
|
||||
|
||||
this.logger.info('Fetching symbol info', { symbol, qmSearchCode });
|
||||
|
||||
const sessionManager = QMSessionManager.getInstance();
|
||||
sessionManager.initialize(this.cache, this.logger);
|
||||
|
||||
// Get a session
|
||||
const sessionId = QM_SESSION_IDS.LOOKUP;
|
||||
const session = await sessionManager.getSession(sessionId);
|
||||
|
||||
if (!session || !session.uuid) {
|
||||
throw new Error(`No active session found for QM LOOKUP`);
|
||||
}
|
||||
|
||||
try {
|
||||
// Build API request for symbol info
|
||||
const searchParams = new URLSearchParams({
|
||||
q: qmSearchCode || symbol,
|
||||
qmodTool: 'SymbolInfo',
|
||||
webmasterId: '500',
|
||||
includeExtended: 'true'
|
||||
});
|
||||
|
||||
const apiUrl = `${QM_CONFIG.LOOKUP_URL}?${searchParams.toString()}`;
|
||||
|
||||
const response = await fetch(apiUrl, {
|
||||
method: 'GET',
|
||||
headers: session.headers,
|
||||
proxy: session.proxy,
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`QM API request failed: ${response.status} ${response.statusText}`);
|
||||
}
|
||||
|
||||
const symbolData = await response.json();
|
||||
|
||||
// Update session success stats
|
||||
await sessionManager.incrementSuccessfulCalls(sessionId, session.uuid);
|
||||
|
||||
// Process and store symbol info
|
||||
if (symbolData && (Array.isArray(symbolData) ? symbolData.length > 0 : true)) {
|
||||
const symbolInfo = Array.isArray(symbolData) ? symbolData[0] : symbolData;
|
||||
|
||||
// Update symbol in database with new metadata
|
||||
const updateData = {
|
||||
...symbolInfo,
|
||||
symbol: symbol,
|
||||
qmSearchCode: qmSearchCode || symbolInfo.symbol,
|
||||
lastInfoUpdate: new Date(),
|
||||
updated_at: new Date()
|
||||
};
|
||||
|
||||
await this.mongodb.updateOne(
|
||||
'qmSymbols',
|
||||
{ symbol },
|
||||
{ $set: updateData },
|
||||
{ upsert: true }
|
||||
);
|
||||
|
||||
this.logger.info('Symbol info updated successfully', {
|
||||
symbol,
|
||||
name: symbolInfo.name,
|
||||
exchange: symbolInfo.exchange
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
symbol,
|
||||
message: `Symbol info updated for ${symbol}`,
|
||||
data: symbolInfo
|
||||
};
|
||||
} else {
|
||||
this.logger.warn('No symbol data returned from API', { symbol });
|
||||
return {
|
||||
success: false,
|
||||
symbol,
|
||||
message: `No data found for symbol ${symbol}`
|
||||
};
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
// Update session failure stats
|
||||
if (session.uuid) {
|
||||
await sessionManager.incrementFailedCalls(sessionId, session.uuid);
|
||||
}
|
||||
|
||||
this.logger.error('Error fetching symbol info', {
|
||||
symbol,
|
||||
error: error instanceof Error ? error.message : 'Unknown error'
|
||||
});
|
||||
|
||||
return {
|
||||
success: false,
|
||||
symbol,
|
||||
message: `Failed to fetch symbol info: ${error instanceof Error ? error.message : 'Unknown error'}`
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedule symbol info updates for symbols that need refreshing
|
||||
* This is the scheduled job that finds stale symbols and queues individual updates
|
||||
*/
|
||||
export async function scheduleSymbolInfoUpdates(
|
||||
this: BaseHandler,
|
||||
input: {
|
||||
limit?: number;
|
||||
forceUpdate?: boolean;
|
||||
} = {},
|
||||
_context?: ExecutionContext
|
||||
): Promise<{
|
||||
message: string;
|
||||
symbolsQueued: number;
|
||||
errors: number;
|
||||
}> {
|
||||
const { limit = 100, forceUpdate = false } = input;
|
||||
const tracker = await getOperationTracker(this);
|
||||
|
||||
this.logger.info('Scheduling symbol info updates', { limit, forceUpdate });
|
||||
|
||||
try {
|
||||
// Get symbols that need updating
|
||||
const staleSymbols = await tracker.getStaleSymbols('symbol_info', {
|
||||
minHoursSinceRun: forceUpdate ? 0 : 24 * 7, // Weekly by default
|
||||
limit
|
||||
});
|
||||
|
||||
if (staleSymbols.length === 0) {
|
||||
this.logger.info('No symbols need info updates');
|
||||
return {
|
||||
message: 'No symbols need info updates',
|
||||
symbolsQueued: 0,
|
||||
errors: 0
|
||||
};
|
||||
}
|
||||
|
||||
this.logger.info(`Found ${staleSymbols.length} symbols needing info updates`);
|
||||
|
||||
// Get full symbol data to include qmSearchCode
|
||||
const symbolDocs = await this.mongodb.find('qmSymbols', {
|
||||
symbol: { $in: staleSymbols }
|
||||
}, {
|
||||
projection: { symbol: 1, qmSearchCode: 1 }
|
||||
});
|
||||
|
||||
let queued = 0;
|
||||
let errors = 0;
|
||||
|
||||
// Schedule individual update jobs for each symbol
|
||||
for (const doc of symbolDocs) {
|
||||
try {
|
||||
await this.scheduleOperation('update-symbol-info', {
|
||||
symbol: doc.symbol,
|
||||
qmSearchCode: doc.qmSearchCode || doc.symbol
|
||||
}, {
|
||||
priority: 3,
|
||||
// Add some delay to avoid overwhelming the API
|
||||
delay: queued * 1000 // 1 second between jobs
|
||||
});
|
||||
|
||||
// Track that we've scheduled this symbol
|
||||
await tracker.updateSymbolOperation(doc.symbol, 'symbol_info', {
|
||||
status: 'success'
|
||||
});
|
||||
|
||||
queued++;
|
||||
} catch (error) {
|
||||
this.logger.error(`Failed to schedule update for ${doc.symbol}`, { error });
|
||||
errors++;
|
||||
}
|
||||
}
|
||||
|
||||
this.logger.info('Symbol info update scheduling completed', {
|
||||
symbolsQueued: queued,
|
||||
errors,
|
||||
total: staleSymbols.length
|
||||
});
|
||||
|
||||
return {
|
||||
message: `Scheduled info updates for ${queued} symbols`,
|
||||
symbolsQueued: queued,
|
||||
errors
|
||||
};
|
||||
} catch (error) {
|
||||
this.logger.error('Symbol info scheduling failed', { error });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue