Add possibility to schedule individual updates of all students in new programmes

This commit is contained in:
ShootingStar91
2024-03-05 20:47:24 +02:00
parent eeeeb1c671
commit db9543edd8
5 changed files with 49 additions and 4 deletions
+6
View File
@@ -8,6 +8,7 @@ const {
updateStudentsByStudentNumber,
abort,
updateCoursesByCourseCode,
updateStudentsIndividually,
} = require('../services/sisUpdaterService')
const logger = require('../util/logger')
@@ -37,6 +38,11 @@ router.get('/update/v2/students', async (req, res) => {
}
})
router.get('/update/v2/students_individually', async (req, res) => {
await updateStudentsIndividually()
res.status(200).send()
})
router.post('/update/v2/customlist/:type', async (req, res) => {
const type = req.params.type
const list = req.body
@@ -1,7 +1,8 @@
const axios = require('axios')
const { SIS_UPDATER_URL, SECRET_TOKEN } = require('../conf-backend')
const { Studyplan } = require('../models')
const { Studyplan, Student, StudyrightElement } = require('../models')
const { Op } = require('sequelize')
const logger = require('../util/logger')
const client = axios.create({ baseURL: SIS_UPDATER_URL })
@@ -38,6 +39,37 @@ const updateSISRedisCache = async () => {
const response = await client.get('/v1/rediscache', params)
return response.data
}
const delay = time => {
return new Promise(res => {
setTimeout(res, time)
})
}
const updateStudentsIndividually = async () => {
try {
const studentNumbers = await Student.findAll({
attributes: ['studentnumber'],
include: {
model: StudyrightElement,
where: {
code: { [Op.regexp]: '(KH*|MH*)' },
},
},
})
const uniqueStudents = [...new Set(studentNumbers.map(s => s.studentnumber))]
const chunkSize = 1000
for (let from = 0; from < uniqueStudents.length - chunkSize; from += chunkSize) {
await client.post(
'v1/students',
{ studentnumbers: uniqueStudents.slice(from, from + chunkSize), individualMode: true },
params
)
await delay(200)
}
} catch (e) {
logger.error(e)
}
}
const studyplansUpdate = async days => {
const limitDate = new Date()
@@ -67,4 +99,5 @@ module.exports = {
abort,
updateCoursesByCourseCode,
studyplansUpdate,
updateStudentsIndividually,
}
@@ -24,6 +24,7 @@ export const Updater = () => {
const updateSISMeta = () => apiCall('meta', '/updater/update/v2/meta', 'get')
const updateSISStudents = () => apiCall('students', '/updater/update/v2/students', 'get')
const updateSISStudentsIndividually = () => apiCall('students', '/updater/update/v2/students_individually', 'get')
const updateSISProgrammes = () => apiCall('curriculums', '/updater/update/v2/programmes')
const updateSISCustomList = () =>
apiCall('custom list', `/updater/update/v2/customlist/${type}`, 'post', customList.trim().split('\n'))
@@ -111,6 +112,10 @@ export const Updater = () => {
<Form.Group>
<Form.Button content="Update meta" onClick={() => updateSISMeta()} />
<Form.Button content="Update students" onClick={() => updateSISStudents()} />
<Form.Button
content="Update students individually (don't click)"
onClick={() => updateSISStudentsIndividually()}
/>
<Form.Button
content="Update curriculums"
onClick={() => {
@@ -201,13 +201,13 @@ const getHourlyPersonsToUpdate = async () => {
)
}
const scheduleByStudentNumbers = async studentNumbers => {
const scheduleByStudentNumbers = async (studentNumbers, individualMode = false) => {
logger.info('Scheduling by student numbers')
const { knex } = knexConnection
const personsToUpdate = await knex('persons').column('id', 'student_number').whereIn('student_number', studentNumbers)
await eachLimit(
chunk(personsToUpdate, CHUNK_SIZE),
chunk(personsToUpdate, individualMode ? 1 : CHUNK_SIZE),
10,
async s => await createJobs(s, 'students', SIS_MISC_SCHEDULE_CHANNEL)
)
+2 -1
View File
@@ -83,12 +83,13 @@ app.get('/v1/programmes', async (_, res) => {
})
app.post('/v1/students', async (req, res) => {
const individualMode = req.body.individualMode
const studentnumbers = req.body.studentnumbers.map(n => (n[0] === '0' ? n : `0${n}`))
logger.info(JSON.stringify(studentnumbers))
logger.info('Scheduling studentnumbers')
await scheduleByStudentNumbers(studentnumbers)
await scheduleByStudentNumbers(studentnumbers, individualMode)
res.locals.msg('Scheduled studentnumbers')
})