Create grades in batch

This commit is contained in:
Adria Navarro 2023-12-21 17:48:39 +01:00
parent c46509d6cb
commit dbf4d6dbdd
1 changed files with 19 additions and 29 deletions

View File

@ -7,8 +7,6 @@ const generator = new Chance()
const STUDENT_COUNT = 500 const STUDENT_COUNT = 500
const SUBJECT_COUNT = 10 const SUBJECT_COUNT = 10
const batchSize = 100
if (!process.argv[2]) { if (!process.argv[2]) {
console.error("Please specify an API key as script argument.") console.error("Please specify an API key as script argument.")
process.exit(-1) process.exit(-1)
@ -16,13 +14,13 @@ if (!process.argv[2]) {
const start = Date.now() const start = Date.now()
async function batchCreate(apiKey, appId, count, table, generator) { async function batchCreate(apiKey, appId, table, items, batchSize = 100) {
let i = 0 let i = 0
async function createSingleRow() { async function createSingleRow(item) {
const row = await createRow(apiKey, appId, table, generator()) const row = await createRow(apiKey, appId, table, item)
console.log( console.log(
`${table.name} - ${++i} of ${count} created (${ `${table.name} - ${++i} of ${items.length} created (${
(Date.now() - start) / 1000 (Date.now() - start) / 1000
}s)` }s)`
) )
@ -30,11 +28,8 @@ async function batchCreate(apiKey, appId, count, table, generator) {
} }
const rows = [] const rows = []
for (let j = 0; j < count; j += batchSize) { for (let j = 0; j < items.length; j += batchSize) {
const batchPromises = Array.from( const batchPromises = items.slice(j, j + batchSize).map(createSingleRow)
{ length: Math.min(batchSize, count - j) },
createSingleRow
)
const batchRows = await Promise.all(batchPromises) const batchRows = await Promise.all(batchPromises)
rows.push(...batchRows) rows.push(...batchRows)
} }
@ -56,9 +51,8 @@ async function run() {
const students = await batchCreate( const students = await batchCreate(
apiKey, apiKey,
app._id, app._id,
STUDENT_COUNT,
studentsTable, studentsTable,
() => ({ Array.from({ length: SUBJECT_COUNT }).map(() => ({
"Student Number": (++studentNumber).toString(), "Student Number": (++studentNumber).toString(),
"First Name": generator.first(), "First Name": generator.first(),
"Last Name": generator.last(), "Last Name": generator.last(),
@ -67,7 +61,7 @@ async function run() {
"Tardiness (Days)": generator.integer({ min: 1, max: 100 }), "Tardiness (Days)": generator.integer({ min: 1, max: 100 }),
"Home Number": generator.phone(), "Home Number": generator.phone(),
"Attendance_(%)": generator.integer({ min: 0, max: 100 }), "Attendance_(%)": generator.integer({ min: 0, max: 100 }),
}) }))
) )
const subjectTable = await createTable(apiKey, app._id, { const subjectTable = await createTable(apiKey, app._id, {
@ -84,11 +78,10 @@ async function run() {
const subjects = await batchCreate( const subjects = await batchCreate(
apiKey, apiKey,
app._id, app._id,
SUBJECT_COUNT,
subjectTable, subjectTable,
() => ({ Array.from({ length: SUBJECT_COUNT }).map(() => ({
Name: generator.profession(), Name: generator.profession(),
}) }))
) )
const gradesTable = await createTable(apiKey, app._id, { const gradesTable = await createTable(apiKey, app._id, {
@ -123,21 +116,18 @@ async function run() {
name: "Grades", name: "Grades",
}) })
i = 0 await batchCreate(
for (const student of students) { apiKey,
for (const subject of subjects) { app._id,
await createRow(apiKey, app._id, gradesTable, { gradesTable,
students.flatMap(student =>
subjects.map(subject => ({
Score: generator.integer({ min: 0, max: 100 }), Score: generator.integer({ min: 0, max: 100 }),
Student: [student], Student: [student],
Subject: [subject], Subject: [subject],
}) }))
console.log( )
`Grade ${++i} of ${students.length * subjects.length} created (${ )
(Date.now() - start) / 1000
}s)`
)
}
}
} }
run() run()