← back to Watches
scripts/prepare-ml-data.js
31 lines
import fs from 'fs';
import pool from '../database/connection.js';
async function prepareMLData() {
const result = await pool.query(`
SELECT w.reference, w.series, w.year_introduced, ph.year, ph.price, ph.condition
FROM watches w JOIN price_history ph ON w.id = ph.watch_id
ORDER BY w.reference, ph.year
`);
const csv = ['reference,series,year_introduced,price_year,price,condition'];
for (const row of result.rows) {
csv.push(`${row.reference},${row.series},${row.year_introduced},${row.year},${row.price},${row.condition || 'unknown'}`);
}
fs.writeFileSync('./data/ml-training-data.csv', csv.join('\n'));
console.log(`Exported ${result.rows.length} records to ml-training-data.csv`);
// Split train/test
const shuffled = result.rows.sort(() => Math.random() - 0.5);
const splitIdx = Math.floor(shuffled.length * 0.8);
const train = shuffled.slice(0, splitIdx);
const test = shuffled.slice(splitIdx);
fs.writeFileSync('./data/ml-train.json', JSON.stringify(train, null, 2));
fs.writeFileSync('./data/ml-test.json', JSON.stringify(test, null, 2));
console.log(`Train: ${train.length}, Test: ${test.length}`);
}
prepareMLData().then(() => process.exit(0)).catch(e => { console.error(e); process.exit(1); });