work on qm filings
This commit is contained in:
parent
710577eb3d
commit
960daf4cad
17 changed files with 2319 additions and 32 deletions
|
|
@ -82,12 +82,22 @@ export class OperationTracker {
|
|||
}
|
||||
];
|
||||
|
||||
// Add crawl state index for crawl operations
|
||||
// Add crawl state indexes for crawl operations
|
||||
if (operation.type === 'crawl' || operation.type === 'intraday_crawl') {
|
||||
indexes.push({
|
||||
[`operations.${operation.name}.crawlState.finished`]: 1,
|
||||
[symbolField]: 1
|
||||
});
|
||||
indexes.push(
|
||||
{
|
||||
[`operations.${operation.name}.crawlState.finished`]: 1,
|
||||
[symbolField]: 1
|
||||
},
|
||||
{
|
||||
[`operations.${operation.name}.crawlState.newestDateReached`]: 1,
|
||||
[symbolField]: 1
|
||||
},
|
||||
{
|
||||
[`operations.${operation.name}.crawlState.oldestDateReached`]: 1,
|
||||
[symbolField]: 1
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
const collection = this.mongodb.collection(collectionName);
|
||||
|
|
@ -168,9 +178,21 @@ export class OperationTracker {
|
|||
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;
|
||||
}
|
||||
|
|
@ -250,7 +272,12 @@ export class OperationTracker {
|
|||
const basePath = `operations.${operation}.crawlState`;
|
||||
Object.entries(data.crawlState).forEach(([key, value]) => {
|
||||
if (value !== undefined) {
|
||||
update.$set[`${basePath}.${key}`] = value;
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
@ -374,13 +401,15 @@ export class OperationTracker {
|
|||
async markCrawlFinished(
|
||||
symbol: string,
|
||||
operationName: string,
|
||||
oldestDateReached: Date
|
||||
oldestDateReached: Date,
|
||||
newestDateReached?: Date
|
||||
): Promise<void> {
|
||||
await this.updateSymbolOperation(symbol, operationName, {
|
||||
status: 'success',
|
||||
crawlState: {
|
||||
finished: true,
|
||||
oldestDateReached
|
||||
oldestDateReached,
|
||||
newestDateReached: newestDateReached || new Date()
|
||||
}
|
||||
});
|
||||
|
||||
|
|
@ -388,10 +417,137 @@ export class OperationTracker {
|
|||
provider: this.provider.getProviderConfig().name,
|
||||
symbol,
|
||||
operation: operationName,
|
||||
oldestDateReached
|
||||
oldestDateReached,
|
||||
newestDateReached
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get symbols for intraday crawl with gap detection
|
||||
*/
|
||||
async getSymbolsForIntradayCrawl(
|
||||
operationName: string,
|
||||
options: {
|
||||
limit?: number;
|
||||
targetOldestDate?: Date;
|
||||
includeNewDataGaps?: boolean;
|
||||
} = {}
|
||||
): Promise<Array<SymbolWithOperations & { gaps?: { forward?: boolean; backward?: boolean } }>> {
|
||||
const { collectionName, symbolField } = this.provider.getProviderConfig();
|
||||
const { limit = 100, targetOldestDate, includeNewDataGaps = true } = options;
|
||||
|
||||
this.provider.validateOperation(operationName);
|
||||
|
||||
// Build filter
|
||||
const filter: any = {
|
||||
active: { $ne: false }
|
||||
};
|
||||
|
||||
// Get all symbols that either:
|
||||
// 1. Have never been crawled
|
||||
// 2. Are not finished
|
||||
// 3. Have gaps (new data since last crawl)
|
||||
const orConditions = [
|
||||
{ [`operations.${operationName}`]: { $exists: false } },
|
||||
{ [`operations.${operationName}.crawlState.finished`]: { $ne: true } }
|
||||
];
|
||||
|
||||
if (includeNewDataGaps) {
|
||||
const yesterday = new Date();
|
||||
yesterday.setDate(yesterday.getDate() - 1);
|
||||
yesterday.setHours(0, 0, 0, 0);
|
||||
|
||||
orConditions.push({
|
||||
[`operations.${operationName}.crawlState.newestDateReached`]: { $lt: yesterday }
|
||||
});
|
||||
}
|
||||
|
||||
filter.$or = orConditions;
|
||||
|
||||
const symbols = await this.mongodb.find(collectionName, filter, {
|
||||
limit,
|
||||
projection: {
|
||||
[symbolField]: 1,
|
||||
[`operations.${operationName}`]: 1
|
||||
},
|
||||
sort: {
|
||||
[`operations.${operationName}.lastRunAt`]: 1
|
||||
}
|
||||
});
|
||||
|
||||
return symbols.map(doc => {
|
||||
const opStatus = doc.operations?.[operationName];
|
||||
const crawlState = opStatus?.crawlState;
|
||||
|
||||
// Determine gaps
|
||||
const gaps: { forward?: boolean; backward?: boolean } = {};
|
||||
|
||||
if (crawlState) {
|
||||
// Check for forward gap (new data)
|
||||
if (crawlState.newestDateReached) {
|
||||
const daysSinceNewest = Math.floor(
|
||||
(Date.now() - new Date(crawlState.newestDateReached).getTime()) / (1000 * 60 * 60 * 24)
|
||||
);
|
||||
gaps.forward = daysSinceNewest > 1;
|
||||
}
|
||||
|
||||
// Check for backward gap (historical data)
|
||||
if (!crawlState.finished) {
|
||||
gaps.backward = true;
|
||||
if (targetOldestDate && crawlState.oldestDateReached) {
|
||||
gaps.backward = new Date(crawlState.oldestDateReached) > targetOldestDate;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Never crawled, has both gaps
|
||||
gaps.forward = true;
|
||||
gaps.backward = true;
|
||||
}
|
||||
|
||||
return {
|
||||
symbol: doc[symbolField],
|
||||
lastRecordDate: opStatus?.lastRecordDate,
|
||||
operationStatus: opStatus,
|
||||
gaps
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if intraday crawl is complete
|
||||
*/
|
||||
async isIntradayCrawlComplete(
|
||||
symbol: string,
|
||||
operationName: string,
|
||||
targetOldestDate: Date
|
||||
): Promise<boolean> {
|
||||
const { collectionName, symbolField } = this.provider.getProviderConfig();
|
||||
|
||||
const doc = await this.mongodb.findOne(collectionName, {
|
||||
[symbolField]: symbol
|
||||
}, {
|
||||
projection: { [`operations.${operationName}.crawlState`]: 1 }
|
||||
});
|
||||
|
||||
if (!doc?.operations?.[operationName]?.crawlState) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const crawlState = doc.operations[operationName].crawlState;
|
||||
|
||||
// Check if explicitly marked as finished
|
||||
if (crawlState.finished) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check if we've reached the target oldest date
|
||||
if (crawlState.oldestDateReached && targetOldestDate) {
|
||||
return new Date(crawlState.oldestDateReached) <= targetOldestDate;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get symbols that need data updates based on last record date
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -54,8 +54,16 @@ export interface CrawlState {
|
|||
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;
|
||||
/** Custom crawl metadata */
|
||||
metadata?: Record<string, any>;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue