93 lines
No EOL
2.8 KiB
TypeScript
93 lines
No EOL
2.8 KiB
TypeScript
import { MongoClient } from 'mongodb';
|
|
|
|
async function migratePriceTracking() {
|
|
const client = new MongoClient('mongodb://localhost:27017');
|
|
await client.connect();
|
|
|
|
const mongodb = client.db('stock');
|
|
|
|
try {
|
|
console.log('Starting price tracking migration...');
|
|
|
|
const collection = mongodb.collection('eodSymbols');
|
|
const batchSize = 100;
|
|
let processedCount = 0;
|
|
let hasMore = true;
|
|
|
|
while (hasMore) {
|
|
// Find documents that need migration
|
|
const documents = await collection.find({
|
|
lastPriceUpdate: { $exists: true },
|
|
'operations.price_update': { $exists: false }
|
|
}).limit(batchSize).toArray();
|
|
|
|
if (documents.length === 0) {
|
|
hasMore = false;
|
|
break;
|
|
}
|
|
|
|
// Process each document
|
|
for (const doc of documents) {
|
|
// Normalize date to 00:00:00 UTC
|
|
const lastPriceUpdate = new Date(doc.lastPriceUpdate);
|
|
const normalizedDate = new Date(Date.UTC(
|
|
lastPriceUpdate.getUTCFullYear(),
|
|
lastPriceUpdate.getUTCMonth(),
|
|
lastPriceUpdate.getUTCDate(),
|
|
0, 0, 0, 0
|
|
));
|
|
|
|
// Parse lastPriceDate if it exists
|
|
let lastRecordDate = null;
|
|
if (doc.lastPriceDate) {
|
|
try {
|
|
lastRecordDate = new Date(doc.lastPriceDate);
|
|
} catch (e) {
|
|
console.warn(`Failed to parse lastPriceDate for ${doc.Code}: ${doc.lastPriceDate}`);
|
|
}
|
|
}
|
|
|
|
// Update the document
|
|
await collection.updateOne(
|
|
{ _id: doc._id },
|
|
{
|
|
$set: {
|
|
'operations.price_update': {
|
|
lastRunAt: normalizedDate,
|
|
lastSuccessAt: normalizedDate,
|
|
status: 'success',
|
|
...(lastRecordDate && { lastRecordDate })
|
|
}
|
|
}
|
|
}
|
|
);
|
|
|
|
processedCount++;
|
|
if (processedCount % 1000 === 0) {
|
|
console.log(`Processed ${processedCount} documents...`);
|
|
}
|
|
}
|
|
}
|
|
|
|
console.log(`Migration completed. Total documents migrated: ${processedCount}`);
|
|
|
|
// Optional: Remove old fields
|
|
const removeOldFields = false; // Set to true to remove old fields
|
|
if (removeOldFields) {
|
|
console.log('Removing old fields...');
|
|
const result = await collection.updateMany(
|
|
{},
|
|
{ $unset: { lastPriceUpdate: '', lastPriceDate: '' } }
|
|
);
|
|
console.log(`Removed old fields from ${result.modifiedCount} documents`);
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error('Migration failed:', error);
|
|
} finally {
|
|
await client.close();
|
|
}
|
|
}
|
|
|
|
// Run the migration
|
|
migratePriceTracking().catch(console.error); |