49 lines
1.5 KiB
TypeScript
49 lines
1.5 KiB
TypeScript
import type { CreateIndexesOptions, IndexSpecification } from 'mongodb';
|
|
import { BaseHandler, Handler, ScheduledOperation } from '@stock-bot/handlers';
|
|
import type { IServiceContainer } from '@stock-bot/services';
|
|
import { resolveIngestionServices } from '../../types';
|
|
import type { DataIngestionServices } from '../../types';
|
|
import { importData } from './actions';
|
|
|
|
@Handler('tt')
|
|
export class TtHandler extends BaseHandler<DataIngestionServices> {
|
|
constructor(services: IServiceContainer) {
|
|
super(resolveIngestionServices(services, 'tt'));
|
|
}
|
|
|
|
override async onInit(): Promise<void> {
|
|
if (!this.mongodb) {
|
|
return;
|
|
}
|
|
|
|
const indexes: { indexSpec: IndexSpecification; options: CreateIndexesOptions }[] = [
|
|
{
|
|
indexSpec: { contract: 1, date: 1 },
|
|
options: { name: 'contract_date_unique_idx', unique: true, background: true },
|
|
},
|
|
{
|
|
indexSpec: { symbol: 1, date: 1 },
|
|
options: { name: 'symbol_date_idx', background: true },
|
|
},
|
|
{
|
|
indexSpec: { date: 1 },
|
|
options: { name: 'date_idx', background: true },
|
|
},
|
|
];
|
|
|
|
for (const index of indexes) {
|
|
try {
|
|
await this.mongodb.createIndex('ttFutures', index.indexSpec, index.options);
|
|
} catch (error) {
|
|
this.logger.debug(`ttFutures index ${index.options.name} may already exist:`, { error });
|
|
}
|
|
}
|
|
}
|
|
|
|
@ScheduledOperation('tt-import', '0 0 * * 0', {
|
|
priority: 5,
|
|
description: 'Import TurtleTrader futures data',
|
|
immediately: true,
|
|
})
|
|
importData = importData;
|
|
}
|