|
| 1 | +// app/lib/generate-seed-data.js |
| 2 | + |
| 3 | +const { faker } = require('@faker-js/faker'); |
| 4 | +const weighted = require('weighted'); |
| 5 | +const fs = require('fs'); |
| 6 | +const path = require('path'); |
| 7 | + |
| 8 | +const { generateParticipant } = require('./generators/participant-generator'); |
| 9 | +const { generateClinicsForBSU } = require('./generators/clinic-generator'); |
| 10 | +const { generateEvent } = require('./generators/event-generator'); |
| 11 | + |
| 12 | +// Load existing data |
| 13 | +const breastScreeningUnits = require('../data/breast-screening-units'); |
| 14 | +const ethnicities = require('../data/ethnicities'); |
| 15 | + |
| 16 | +const CONFIG = { |
| 17 | + numberOfParticipants: 1000, |
| 18 | + outputPath: path.join(__dirname, '../data/generated'), |
| 19 | + clinicDefaults: { |
| 20 | + slotsPerDay: 32, |
| 21 | + daysToGenerate: 5, |
| 22 | + startTime: '09:00', |
| 23 | + endTime: '17:00', |
| 24 | + slotDurationMinutes: 8 |
| 25 | + }, |
| 26 | + eventOutcomes: { |
| 27 | + 'clear': 0.95, |
| 28 | + 'needs_further_tests': 0.04, |
| 29 | + 'cancer_detected': 0.01 |
| 30 | + } |
| 31 | +}; |
| 32 | + |
| 33 | +const generateData = async () => { |
| 34 | + // Create output directory if it doesn't exist |
| 35 | + if (!fs.existsSync(CONFIG.outputPath)) { |
| 36 | + fs.mkdirSync(CONFIG.outputPath, { recursive: true }); |
| 37 | + } |
| 38 | + |
| 39 | + // Generate base data |
| 40 | + console.log('Generating participants...'); |
| 41 | + const participants = Array.from({ length: CONFIG.numberOfParticipants }, () => |
| 42 | + generateParticipant({ ethnicities, breastScreeningUnits }) |
| 43 | + ); |
| 44 | + |
| 45 | + console.log('Generating clinics and events...'); |
| 46 | + const clinics = []; |
| 47 | + const events = []; |
| 48 | + |
| 49 | + // Calculate date range |
| 50 | + const startDate = new Date(); |
| 51 | + startDate.setDate(startDate.getDate() - 3); |
| 52 | + |
| 53 | + for (let i = 0; i < CONFIG.clinicDefaults.daysToGenerate; i++) { |
| 54 | + const clinicDate = new Date(startDate); |
| 55 | + clinicDate.setDate(clinicDate.getDate() + i); |
| 56 | + |
| 57 | + // Generate clinics for each BSU (currently just Oxford) |
| 58 | + breastScreeningUnits.forEach(unit => { |
| 59 | + const newClinics = generateClinicsForBSU({ |
| 60 | + date: clinicDate, |
| 61 | + breastScreeningUnit: unit, |
| 62 | + config: CONFIG.clinicDefaults |
| 63 | + }); |
| 64 | + |
| 65 | + // Generate events for each clinic |
| 66 | + newClinics.forEach(clinic => { |
| 67 | + const clinicEvents = clinic.slots |
| 68 | + .filter(slot => Math.random() > 0.2) |
| 69 | + .map(slot => { |
| 70 | + const participant = faker.helpers.arrayElement(participants); |
| 71 | + return generateEvent({ |
| 72 | + slot, |
| 73 | + participant, |
| 74 | + clinic, |
| 75 | + outcomeWeights: CONFIG.eventOutcomes |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + events.push(...clinicEvents); |
| 80 | + }); |
| 81 | + |
| 82 | + clinics.push(...newClinics); |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + // Write generated data to files |
| 87 | + const writeData = (filename, data) => { |
| 88 | + fs.writeFileSync( |
| 89 | + path.join(CONFIG.outputPath, filename), |
| 90 | + JSON.stringify(data, null, 2) |
| 91 | + ); |
| 92 | + }; |
| 93 | + |
| 94 | + writeData('participants.json', { participants }); |
| 95 | + writeData('clinics.json', { clinics }); |
| 96 | + writeData('events.json', { events }); |
| 97 | + |
| 98 | + console.log('\nData generation complete!'); |
| 99 | + console.log(`Generated:`); |
| 100 | + console.log(`- ${participants.length} participants`); |
| 101 | + console.log(`- ${clinics.length} clinics`); |
| 102 | + console.log(`- ${events.length} events`); |
| 103 | +}; |
| 104 | + |
| 105 | +// Run the generator |
| 106 | +generateData().catch(console.error); |
0 commit comments