Skip to content

Commit

Permalink
Send requests to importer in batches
Browse files Browse the repository at this point in the history
  • Loading branch information
valtterikantanen committed Apr 30, 2024
1 parent 8153a00 commit baad702
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { FACULTIES } from './organisation/faculties'
import { connectToDatabase } from './db/connection'
import User from './db/models/user'
import testRouter from './util/testRouter'
import { processInBatches } from './util/processInBatches'

initializeSentry()

Expand Down Expand Up @@ -100,21 +101,18 @@ app.post('/access-and-special-groups', async (req, res) => {
},
})

const usersWithAccess = await Promise.all(
users.map(async ({ dataValues: user }) => {
const usersWithAccess = await processInBatches(
users.map(({ dataValues: user }) => user),
50,
async (user) => {
const iamGroups = user.iamGroups.filter((iam) =>
relevantIAMs.includes(iam),
)
const { access, specialGroup } = getIAMRights(iamGroups)
specialGroup.fullSisuAccess = await hasFullSisuAccess(user.id)

return {
...user,
iamGroups,
access,
specialGroup,
}
}),
return { ...user, iamGroups, access, specialGroup }
},
)

return res.send(usersWithAccess)
Expand Down
16 changes: 16 additions & 0 deletions src/util/processInBatches.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Utility function to process requests in chunks
export const processInBatches = async <T>(
items: T[],
batchSize: number,
processFunc: (item: T) => Promise<any>,
): Promise<any[]> => {
const results: any[] = []
for (let index = 0; index < items.length; index += batchSize) {
const batch = items.slice(index, index + batchSize)
const batchResults = await Promise.all(
batch.map((item) => processFunc(item)),
)
results.push(...batchResults)
}
return results
}

0 comments on commit baad702

Please sign in to comment.