changed up operationTracker and added eodSearchCode

This commit is contained in:
Boki 2025-07-10 08:45:06 -04:00
parent 18289f0a04
commit 7486a1fa65
11 changed files with 237 additions and 143 deletions

View file

@ -82,19 +82,19 @@ export class OperationTracker {
}
];
// Add crawl state indexes for crawl operations
// Add crawl-specific indexes for crawl operations
if (operation.type === 'crawl' || operation.type === 'intraday_crawl') {
indexes.push(
{
[`operations.${operation.name}.crawlState.finished`]: 1,
[`operations.${operation.name}.finished`]: 1,
[symbolField]: 1
},
{
[`operations.${operation.name}.crawlState.newestDateReached`]: 1,
[`operations.${operation.name}.newestDateReached`]: 1,
[symbolField]: 1
},
{
[`operations.${operation.name}.crawlState.oldestDateReached`]: 1,
[`operations.${operation.name}.oldestDateReached`]: 1,
[symbolField]: 1
}
);
@ -103,10 +103,29 @@ export class OperationTracker {
const collection = this.mongodb.collection(collectionName);
for (const indexSpec of indexes) {
await collection.createIndex(indexSpec, {
background: true,
name: `op_${operation.name}_${Object.keys(indexSpec).join('_')}`
});
try {
await collection.createIndex(indexSpec, {
background: true,
name: `op_${operation.name}_${Object.keys(indexSpec).join('_')}`
});
} catch (indexError) {
// Log but don't fail if index already exists or we hit the limit
if (indexError.message?.includes('too many indexes') || indexError.code === 86) {
this.logger.warn(`Skipping index creation due to limit`, {
operation: operation.name,
index: Object.keys(indexSpec).join('_'),
error: indexError.message
});
} else if (indexError.code === 11000 || indexError.code === 85) {
// Index already exists
this.logger.debug(`Index already exists`, {
operation: operation.name,
index: Object.keys(indexSpec).join('_')
});
} else {
throw indexError;
}
}
}
this.indexesCreated.add(operation.name);
@ -170,40 +189,50 @@ export class OperationTracker {
update.$set[`operations.${operationName}.metadata`] = data.metadata;
}
if (data.crawlState) {
const existingPath = `operations.${operationName}.crawlState`;
if (data.crawlState.finished !== undefined) {
update.$set[`${existingPath}.finished`] = data.crawlState.finished;
}
if (data.crawlState.oldestDateReached) {
update.$set[`${existingPath}.oldestDateReached`] = data.crawlState.oldestDateReached;
}
if (data.crawlState.newestDateReached) {
update.$set[`${existingPath}.newestDateReached`] = data.crawlState.newestDateReached;
}
if (data.crawlState.lastProcessedDate) {
update.$set[`${existingPath}.lastProcessedDate`] = data.crawlState.lastProcessedDate;
}
if (data.crawlState.totalDaysProcessed !== undefined) {
update.$set[`${existingPath}.totalDaysProcessed`] = data.crawlState.totalDaysProcessed;
}
if (data.crawlState.lastCrawlDirection) {
update.$set[`${existingPath}.lastCrawlDirection`] = data.crawlState.lastCrawlDirection;
}
if (data.crawlState.targetOldestDate) {
update.$set[`${existingPath}.targetOldestDate`] = data.crawlState.targetOldestDate;
}
if (data.crawlState.metadata) {
update.$set[`${existingPath}.metadata`] = data.crawlState.metadata;
}
// Handle crawl-specific fields
if (data.finished !== undefined) {
update.$set[`operations.${operationName}.finished`] = data.finished;
}
if (data.oldestDateReached) {
update.$set[`operations.${operationName}.oldestDateReached`] = data.oldestDateReached;
}
if (data.newestDateReached) {
update.$set[`operations.${operationName}.newestDateReached`] = data.newestDateReached;
}
if (data.lastProcessedDate) {
update.$set[`operations.${operationName}.lastProcessedDate`] = data.lastProcessedDate;
}
if (data.totalDaysProcessed !== undefined) {
update.$set[`operations.${operationName}.totalDaysProcessed`] = data.totalDaysProcessed;
}
if (data.lastCrawlDirection) {
update.$set[`operations.${operationName}.lastCrawlDirection`] = data.lastCrawlDirection;
}
if (data.targetOldestDate) {
update.$set[`operations.${operationName}.targetOldestDate`] = data.targetOldestDate;
}
await this.mongodb.updateOne(
this.logger.debug('Updating symbol operation with filter', {
collectionName,
filter: { [symbolField]: symbol },
symbolField,
symbol,
operation: operationName
});
const updateResult = await this.mongodb.updateOne(
collectionName,
{ [symbolField]: symbol },
update
);
this.logger.debug('Update result', {
matched: updateResult.matchedCount,
modified: updateResult.modifiedCount,
symbol,
operation: operationName
});
// Call after hook
await this.provider.afterOperationUpdate(symbol, operationName, data);
@ -268,18 +297,27 @@ export class OperationTracker {
update.$set[`operations.${operation}.metadata`] = data.metadata;
}
if (data.crawlState) {
const basePath = `operations.${operation}.crawlState`;
Object.entries(data.crawlState).forEach(([key, value]) => {
if (value !== undefined) {
// Handle Date objects properly
if (value instanceof Date || (typeof value === 'string' && key.includes('Date'))) {
update.$set[`${basePath}.${key}`] = new Date(value);
} else {
update.$set[`${basePath}.${key}`] = value;
}
}
});
// Handle crawl-specific fields
if (data.finished !== undefined) {
update.$set[`operations.${operation}.finished`] = data.finished;
}
if (data.oldestDateReached) {
update.$set[`operations.${operation}.oldestDateReached`] = data.oldestDateReached;
}
if (data.newestDateReached) {
update.$set[`operations.${operation}.newestDateReached`] = data.newestDateReached;
}
if (data.lastProcessedDate) {
update.$set[`operations.${operation}.lastProcessedDate`] = data.lastProcessedDate;
}
if (data.totalDaysProcessed !== undefined) {
update.$set[`operations.${operation}.totalDaysProcessed`] = data.totalDaysProcessed;
}
if (data.lastCrawlDirection) {
update.$set[`operations.${operation}.lastCrawlDirection`] = data.lastCrawlDirection;
}
if (data.targetOldestDate) {
update.$set[`operations.${operation}.targetOldestDate`] = data.targetOldestDate;
}
return {
@ -348,7 +386,7 @@ export class OperationTracker {
// Add symbol filter if provided
if (symbolFilter?.symbol) {
filter.symbol = symbolFilter.symbol;
filter[symbolField] = symbolFilter.symbol;
}
const symbols = await this.mongodb.find(collectionName, filter, {
@ -385,7 +423,7 @@ export class OperationTracker {
};
if (!includeFinished) {
filter[`operations.${operationName}.crawlState.finished`] = { $ne: true };
filter[`operations.${operationName}.finished`] = { $ne: true };
}
const symbols = await this.mongodb.find(collectionName, filter, {
@ -417,11 +455,9 @@ export class OperationTracker {
): Promise<void> {
await this.updateSymbolOperation(symbol, operationName, {
status: 'success',
crawlState: {
finished: true,
oldestDateReached,
newestDateReached: newestDateReached || new Date()
}
finished: true,
oldestDateReached,
newestDateReached: newestDateReached || new Date()
});
this.logger.info('Marked crawl as finished', {
@ -457,7 +493,7 @@ export class OperationTracker {
// Add symbol filter if provided
if (symbolFilter?.symbol) {
filter.symbol = symbolFilter.symbol;
filter[symbolField] = symbolFilter.symbol;
}
// Get all symbols that either:
@ -466,7 +502,7 @@ export class OperationTracker {
// 3. Have gaps (new data since last crawl)
const orConditions = [
{ [`operations.${operationName}`]: { $exists: false } },
{ [`operations.${operationName}.crawlState.finished`]: { $ne: true } }
{ [`operations.${operationName}.finished`]: { $ne: true } }
];
if (includeNewDataGaps) {
@ -475,7 +511,7 @@ export class OperationTracker {
yesterday.setHours(0, 0, 0, 0);
orConditions.push({
[`operations.${operationName}.crawlState.newestDateReached`]: { $lt: yesterday }
[`operations.${operationName}.newestDateReached`]: { $lt: yesterday }
});
}
@ -498,17 +534,16 @@ export class OperationTracker {
return symbols.map(doc => {
const opStatus = doc.operations?.[operationName];
const crawlState = opStatus?.crawlState;
// Determine gaps (only backward since we removed forward crawling)
const gaps: { forward?: boolean; backward?: boolean } = {};
if (crawlState) {
if (opStatus) {
// Only check for backward gap (historical data)
if (!crawlState.finished) {
if (!opStatus.finished) {
gaps.backward = true;
if (targetOldestDate && crawlState.oldestDateReached) {
gaps.backward = new Date(crawlState.oldestDateReached) > targetOldestDate;
if (targetOldestDate && opStatus.oldestDateReached) {
gaps.backward = new Date(opStatus.oldestDateReached) > targetOldestDate;
}
}
} else {
@ -541,23 +576,23 @@ export class OperationTracker {
const doc = await this.mongodb.findOne(collectionName, {
[symbolField]: symbol
}, {
projection: { [`operations.${operationName}.crawlState`]: 1 }
projection: { [`operations.${operationName}`]: 1 }
});
if (!doc?.operations?.[operationName]?.crawlState) {
if (!doc?.operations?.[operationName]) {
return false;
}
const crawlState = doc.operations[operationName].crawlState;
const opStatus = doc.operations[operationName];
// Check if explicitly marked as finished
if (crawlState.finished) {
if (opStatus.finished) {
return true;
}
// Check if we've reached the target oldest date
if (crawlState.oldestDateReached && targetOldestDate) {
return new Date(crawlState.oldestDateReached) <= targetOldestDate;
if (opStatus.oldestDateReached && targetOldestDate) {
return new Date(opStatus.oldestDateReached) <= targetOldestDate;
}
return false;
@ -655,7 +690,7 @@ export class OperationTracker {
// Crawl stats (if applicable)
(operation.type === 'crawl' || operation.type === 'intraday_crawl')
? collection.countDocuments({
[`operations.${operationName}.crawlState.finished`]: true
[`operations.${operationName}.finished`]: true
})
: Promise.resolve(undefined),

View file

@ -42,16 +42,10 @@ export interface OperationStatus {
error?: string;
/** Provider-specific metadata */
metadata?: Record<string, any>;
/** Crawl-specific state */
crawlState?: CrawlState;
}
/**
* State for crawl-type operations
*/
export interface CrawlState {
// Crawl-specific fields (used for crawl and intraday_crawl operations)
/** Whether the crawl has completed */
finished: boolean;
finished?: boolean;
/** Oldest date reached during crawl */
oldestDateReached?: Date;
/** Newest date reached during crawl */
@ -64,10 +58,10 @@ export interface CrawlState {
lastCrawlDirection?: 'forward' | 'backward';
/** Target oldest date to reach */
targetOldestDate?: Date;
/** Custom crawl metadata */
metadata?: Record<string, any>;
}
// CrawlState interface removed - fields moved directly to OperationStatus
/**
* Configuration for a data provider
*/
@ -94,10 +88,24 @@ export interface OperationUpdate {
recordCount?: number;
/** Error message for failures */
error?: string;
/** Crawl state for crawl operations */
crawlState?: Partial<CrawlState>;
/** Additional metadata */
metadata?: Record<string, any>;
// Crawl-specific update fields
/** Whether the crawl has completed */
finished?: boolean;
/** Oldest date reached during crawl */
oldestDateReached?: Date;
/** Newest date reached during crawl */
newestDateReached?: Date;
/** Last date that was processed (for resumption) */
lastProcessedDate?: Date;
/** Total days processed so far */
totalDaysProcessed?: number;
/** Direction of last crawl */
lastCrawlDirection?: 'forward' | 'backward';
/** Target oldest date to reach */
targetOldestDate?: Date;
}
/**