127 lines
3.2 KiB
JavaScript
127 lines
3.2 KiB
JavaScript
import { writeFile } from 'node:fs/promises';
|
|
|
|
const BASE_URL = process.env.API_BASE_URL || 'http://localhost:3000';
|
|
const USER_COUNT = Number(process.env.SEED_USERS || 20);
|
|
const SESSION_COUNT = Number(process.env.SEED_SESSIONS || 50);
|
|
|
|
const runId = `${Date.now()}`;
|
|
const generated = {
|
|
generatedAt: new Date().toISOString(),
|
|
runId,
|
|
teacher: null,
|
|
users: [],
|
|
sessions: [],
|
|
};
|
|
|
|
async function request(path, method = 'GET', body) {
|
|
const response = await fetch(`${BASE_URL}${path}`, {
|
|
method,
|
|
headers: { 'Content-Type': 'application/json' },
|
|
...(body ? { body: JSON.stringify(body) } : {}),
|
|
});
|
|
|
|
const text = await response.text();
|
|
let data;
|
|
|
|
try {
|
|
data = JSON.parse(text);
|
|
} catch {
|
|
throw new Error(`${method} ${path}: Invalid JSON response: ${text}`);
|
|
}
|
|
|
|
if (!response.ok) {
|
|
throw new Error(
|
|
`${method} ${path} failed (${response.status}): ${JSON.stringify(data)}`
|
|
);
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
function entityId(data) {
|
|
const entity = data.result ?? data.data ?? data;
|
|
const id = entity._id ?? entity.id;
|
|
|
|
if (!id) {
|
|
throw new Error(`Could not find created record ID: ${JSON.stringify(data)}`);
|
|
}
|
|
|
|
return id;
|
|
}
|
|
|
|
async function main() {
|
|
console.log(`Seeding ${BASE_URL}`);
|
|
|
|
const teacherResponse = await request('/teachers/', 'POST', {
|
|
fullName: `Load Test Teacher ${runId}`,
|
|
email: `teacher-${runId}@example.invalid`,
|
|
specialization: 'Mathematics',
|
|
experience: '5 years',
|
|
});
|
|
|
|
generated.teacher = {
|
|
id: entityId(teacherResponse),
|
|
email: `teacher-${runId}@example.invalid`,
|
|
};
|
|
|
|
for (let i = 1; i <= USER_COUNT; i++) {
|
|
const email = `user-${runId}-${i}@example.invalid`;
|
|
|
|
const response = await request('/users/', 'POST', {
|
|
fullName: `Load Test User ${i}`,
|
|
email,
|
|
phone: String(9000000000 + i),
|
|
});
|
|
|
|
generated.users.push({
|
|
id: entityId(response),
|
|
email,
|
|
});
|
|
|
|
if (i % 10 === 0 || i === USER_COUNT) {
|
|
console.log(`Created ${i}/${USER_COUNT} users`);
|
|
}
|
|
}
|
|
|
|
// Start tomorrow and spread sessions one hour apart.
|
|
const firstStart = Date.now() + 24 * 60 * 60 * 1000;
|
|
|
|
for (let i = 0; i < SESSION_COUNT; i++) {
|
|
const start = new Date(firstStart + i * 60 * 60 * 1000);
|
|
const end = new Date(start.getTime() + 30 * 60 * 1000);
|
|
|
|
const response = await request('/sessions/', 'POST', {
|
|
teacherId: generated.teacher.id,
|
|
startTime: start.toISOString(),
|
|
endTime: end.toISOString(),
|
|
});
|
|
|
|
generated.sessions.push({
|
|
id: entityId(response),
|
|
startTime: start.toISOString(),
|
|
endTime: end.toISOString(),
|
|
dateTimestamp: start.toISOString(),
|
|
});
|
|
|
|
if ((i + 1) % 10 === 0 || i + 1 === SESSION_COUNT) {
|
|
console.log(`Created ${i + 1}/${SESSION_COUNT} sessions`);
|
|
}
|
|
}
|
|
|
|
await writeFile(
|
|
new URL('../k6/seed-data.json', import.meta.url),
|
|
JSON.stringify(generated, null, 2) + '\n'
|
|
);
|
|
|
|
console.log('\nSeeding completed successfully.');
|
|
console.log(`Teacher: 1`);
|
|
console.log(`Users: ${generated.users.length}`);
|
|
console.log(`Sessions: ${generated.sessions.length}`);
|
|
console.log('Saved IDs to k6/seed-data.json');
|
|
}
|
|
|
|
main().catch((error) => {
|
|
console.error('\nSeeding failed:', error.message);
|
|
process.exitCode = 1;
|
|
});
|