Skip to content

Commit

Permalink
Merge pull request #1057 from UniversityOfHelsinkiCS/trunk
Browse files Browse the repository at this point in the history
custom populations are here
  • Loading branch information
esakemp authored Jun 28, 2019
2 parents 34a3449 + 4b9a95f commit fa7a370
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 117 deletions.
5 changes: 2 additions & 3 deletions services/backend/oodikone2-backend/src/routes/population.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ router.get('/v3/populationstatistics', async (req, res) => {
return
}
}
} catch(e) {
} catch (e) {
console.error(e)
res.status(400).json({ error: 'The query had invalid studyRights' })
return
Expand All @@ -57,8 +57,7 @@ router.get('/v3/populationstatistics', async (req, res) => {
if (req.query.months == null) {
req.query.months = 12
}

const result = await Population.optimizedStatisticsOf({ ...req.query, studyRights})
const result = await Population.optimizedStatisticsOf({ ...req.query, studyRights })

if (result.error) {
console.log(result.error)
Expand Down
23 changes: 15 additions & 8 deletions services/backend/oodikone2-backend/src/services/populations.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,13 @@ const formatStudentForPopulationStatistics = ({

const dateMonthsFromNow = (date, months) => moment(date).add(months, 'months').format('YYYY-MM-DD')

const getStudentsIncludeCoursesBetween = async (studentnumbers, startDate, endDate, studyright) => {
const getStudentsIncludeCoursesBetween = async (studentnumbers, startDate, endDate, studyright, tag) => {
const tagQuery = tag ? {
tag_id: {
[Op.eq]: tag
}
} : null

const creditsOfStudentOther = {
student_studentnumber: {
[Op.in]: studentnumbers
Expand Down Expand Up @@ -138,7 +144,7 @@ const getStudentsIncludeCoursesBetween = async (studentnumbers, startDate, endDa

const creditsOfStudent = ['320001', 'MH30_001'].includes(studyright[0]) ?
creditsOfStudentLaakis : creditsOfStudentOther

const students = await Student.findAll({
attributes: ['firstnames', 'lastname', 'studentnumber',
'dateofuniversityenrollment', 'creditcount', 'matriculationexamination',
Expand Down Expand Up @@ -216,10 +222,11 @@ const getStudentsIncludeCoursesBetween = async (studentnumbers, startDate, endDa
{
model: TagStudent,
attributes: ['id'],
where: tagQuery,
include: [
{
model: Tag,
attributes: ['tag_id','tagname']
attributes: ['tag_id', 'tagname'],
}
],
}
Expand Down Expand Up @@ -301,10 +308,10 @@ const studentnumbersWithAllStudyrightElements = async (studyRights, startDate, e
}

const parseQueryParams = query => {
const { semesters, studentStatuses, year, studyRights, months } = query
const { semesters, studentStatuses, studyRights, months, year, tagYear } = query
const startDate = semesters.includes('FALL') ?
`${year}-${semesterStart[semesters.find(s => s === 'FALL')]}` :
`${moment(year, 'YYYY').add(1, 'years').format('YYYY')}-${semesterStart[semesters.find(s => s === 'SPRING')]}`
`${tagYear}-${semesterStart[semesters.find(s => s === 'FALL')]}` :
`${moment(tagYear, 'YYYY').add(1, 'years').format('YYYY')}-${semesterStart[semesters.find(s => s === 'SPRING')]}`
const endDate = semesters.includes('SPRING') ?
`${moment(year, 'YYYY').add(1, 'years').format('YYYY')}-${semesterEnd[semesters.find(s => s === 'SPRING')]}` :
`${year}-${semesterEnd[semesters.find(s => s === 'FALL')]}`
Expand Down Expand Up @@ -417,15 +424,15 @@ const optimizedStatisticsOf = async (query) => {
}

const {
studyRights, startDate, endDate, months, exchangeStudents, cancelledStudents, nondegreeStudents
studyRights, startDate, months, endDate, exchangeStudents, cancelledStudents, nondegreeStudents
} = parseQueryParams(query)

const studentnumbers =
await studentnumbersWithAllStudyrightElements(
studyRights, startDate, endDate, exchangeStudents, cancelledStudents, nondegreeStudents
)
const students =
await getStudentsIncludeCoursesBetween(studentnumbers, startDate, dateMonthsFromNow(startDate, months), studyRights)
await getStudentsIncludeCoursesBetween(studentnumbers, startDate, dateMonthsFromNow(startDate, months), studyRights, query.tag)

const formattedStudents = await formatStudentsForApi(students, startDate, endDate, query)
return formattedStudents
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const semesters = {
const createQueryObject = (year, semester, codes, months) => ({
studyRights: codes,
year,
tagYear: year,
semesters: [semester],
months
})
Expand Down Expand Up @@ -137,90 +138,90 @@ describe('optimizedStatisticsOf tests', () => {
- Two credits in 2011-09-31 and 2012-02-31.
`, () => {

beforeAll(async () => {
await Semester.bulkCreate([semesters.fall, semesters.spring])
await Student.create(student)
await Course.create(courses.elements_of_ai)
await Credit.bulkCreate([creditFall, creditSpring])
await ElementDetails.bulkCreate([elementdetails.bsc, elementdetails.maths, elementdetails.cs])
await StudyrightExtent.create(studyrightextents.bachelors)
await Studyright.create(studyright)
await StudyrightElement.bulkCreate([studyrightelements.bsc, studyrightelements.maths])
})

test('Query result for BSc, Fall 2011 for 12 months should contain the student.', async () => {
const query = createQueryObject('2011', SEMESTER.FALL, [elementdetails.bsc.code], 12)
const { students } = await optimizedStatisticsOf(query)
expect(students.some(s => s.studentNumber === student.studentnumber)).toBe(true)
})

test('Query result for BSc, Fall 2012 for 12 months should contain the student.', async () => {
const query = createQueryObject('2012', SEMESTER.FALL, [elementdetails.bsc.code], 12)
const { students } = await optimizedStatisticsOf(query)
expect(students.some(s => s.studentNumber === student.studentnumber)).toBe(true)
})

test('Query result for Mathematics, Fall 2010 for 12 months should not contain the student.', async () => {
const query = createQueryObject('2010', SEMESTER.FALL, [elementdetails.maths.code], 12)
const { students } = await optimizedStatisticsOf(query)
expect(students.some(s => s.studentNumber === student.studentnumber)).toBe(false)
})

test('Query result for Mathematics, Fall 2011 for 12 months should contain the student.', async () => {
const query = createQueryObject('2011', SEMESTER.FALL, [elementdetails.maths.code], 12)
const { students } = await optimizedStatisticsOf(query)
expect(students.some(s => s.studentNumber === student.studentnumber)).toBe(true)
})

test('Query result for Mathematics, Fall 2012 for 12 months should not contain the student.', async () => {
const query = createQueryObject('2012', SEMESTER.FALL, [elementdetails.maths.code], 12)
const { students } = await optimizedStatisticsOf(query)
expect(students.some(s => s.studentNumber === student.studentnumber)).toBe(false)
})
beforeAll(async () => {
await Semester.bulkCreate([semesters.fall, semesters.spring])
await Student.create(student)
await Course.create(courses.elements_of_ai)
await Credit.bulkCreate([creditFall, creditSpring])
await ElementDetails.bulkCreate([elementdetails.bsc, elementdetails.maths, elementdetails.cs])
await StudyrightExtent.create(studyrightextents.bachelors)
await Studyright.create(studyright)
await StudyrightElement.bulkCreate([studyrightelements.bsc, studyrightelements.maths])
})

test('Query result for BSc, Fall 2011 for 12 months should contain the student.', async () => {
const query = createQueryObject('2011', SEMESTER.FALL, [elementdetails.bsc.code], 12)
const { students } = await optimizedStatisticsOf(query)
expect(students.some(s => s.studentNumber === student.studentnumber)).toBe(true)
})

test('Query result for BSc, Fall 2012 for 12 months should contain the student.', async () => {
const query = createQueryObject('2012', SEMESTER.FALL, [elementdetails.bsc.code], 12)
const { students } = await optimizedStatisticsOf(query)
expect(students.some(s => s.studentNumber === student.studentnumber)).toBe(true)
})

test('Query result for Mathematics, Fall 2010 for 12 months should not contain the student.', async () => {
const query = createQueryObject('2010', SEMESTER.FALL, [elementdetails.maths.code], 12)
const { students } = await optimizedStatisticsOf(query)
expect(students.some(s => s.studentNumber === student.studentnumber)).toBe(false)
})

test('Query result for Mathematics, Fall 2011 for 12 months should contain the student.', async () => {
const query = createQueryObject('2011', SEMESTER.FALL, [elementdetails.maths.code], 12)
const { students } = await optimizedStatisticsOf(query)
expect(students.some(s => s.studentNumber === student.studentnumber)).toBe(true)
})

test('Query result for Mathematics, Fall 2012 for 12 months should not contain the student.', async () => {
const query = createQueryObject('2012', SEMESTER.FALL, [elementdetails.maths.code], 12)
const { students } = await optimizedStatisticsOf(query)
expect(students.some(s => s.studentNumber === student.studentnumber)).toBe(false)
})

test('Query result for BSc and Computer Science, Fall 2011 for 12 months should not contain the student.', async () => {
const query = createQueryObject('2011', SEMESTER.FALL, [elementdetails.bsc.code, elementdetails.cs.code], 12)
const { students } = await optimizedStatisticsOf(query)
expect(students.some(s => s.studentNumber === student.student_studentnumber)).toBe(false)
})

test('Query result for BSc and Mathematics, Fall 2011 for 12 months should contain the student.', async () => {
const query = createQueryObject('2011', SEMESTER.FALL, [elementdetails.bsc.code, elementdetails.maths.code], 12)
const { students } = await optimizedStatisticsOf(query)
expect(students.some(s => s.studentNumber === student.studentnumber)).toBe(true)
})

test('Query result for BSc, Spring 2012 for 12 months should contain the student', async () => {
const query = createQueryObject('2012', SEMESTER.SPRING, [elementdetails.bsc.code], 12)
const { students } = await optimizedStatisticsOf(query)
expect(students.some(s => s.studentNumber === student.studentnumber)).toBe(true)
})

test('Query result for Mathematics, Spring 2012 for 12 months should not contain the student', async () => {
const query = createQueryObject('2012', SEMESTER.SPRING, [elementdetails.maths.code], 12)
const { students } = await optimizedStatisticsOf(query)
expect(students.some(s => s.studentNumber === student.studentnumber)).toBe(false)
})

test('Query result for BSc, Fall 2011 for 4 months should only return the FALL course instance for student. ', async () => {
const query = createQueryObject('2011', SEMESTER.FALL, [elementdetails.bsc.code], 4)
const { students } = await optimizedStatisticsOf(query)
const result = students.find(s => s.studentNumber === student.studentnumber)
const courseinstances = result.courses
expect(courseinstances.length).toBe(1)
expect(
courseinstances.some(instance =>
(instance.date.getTime() === courseinstanceFall.coursedate.getTime()) &&
(instance.course.code === courseinstanceFall.course_code))
).toBe(true)
})

test('Query result for BSc, Fall 2011 for 1 month should return student even though do not have any credits yet. ', async () => {
const query = createQueryObject('2011', SEMESTER.FALL, [elementdetails.bsc.code], 1)
const { students } = await optimizedStatisticsOf(query)
expect(students.some(s => s.studentNumber === student.studentnumber)).toBe(true)
})

test('Query result for BSc and Computer Science, Fall 2011 for 12 months should not contain the student.', async () => {
const query = createQueryObject('2011', SEMESTER.FALL, [elementdetails.bsc.code, elementdetails.cs.code], 12)
const { students } = await optimizedStatisticsOf(query)
expect(students.some(s => s.studentNumber === student.student_studentnumber)).toBe(false)
})

test('Query result for BSc and Mathematics, Fall 2011 for 12 months should contain the student.', async () => {
const query = createQueryObject('2011', SEMESTER.FALL, [elementdetails.bsc.code, elementdetails.maths.code], 12)
const { students } = await optimizedStatisticsOf(query)
expect(students.some(s => s.studentNumber === student.studentnumber)).toBe(true)
})

test('Query result for BSc, Spring 2012 for 12 months should contain the student', async () => {
const query = createQueryObject('2012', SEMESTER.SPRING, [elementdetails.bsc.code], 12)
const { students } = await optimizedStatisticsOf(query)
expect(students.some(s => s.studentNumber === student.studentnumber)).toBe(true)
})

test('Query result for Mathematics, Spring 2012 for 12 months should not contain the student', async () => {
const query = createQueryObject('2012', SEMESTER.SPRING, [elementdetails.maths.code], 12)
const { students } = await optimizedStatisticsOf(query)
expect(students.some(s => s.studentNumber === student.studentnumber)).toBe(false)
})

test('Query result for BSc, Fall 2011 for 4 months should only return the FALL course instance for student. ', async () => {
const query = createQueryObject('2011', SEMESTER.FALL, [elementdetails.bsc.code], 4)
const { students } = await optimizedStatisticsOf(query)
const result = students.find(s => s.studentNumber === student.studentnumber)
const courseinstances = result.courses
expect(courseinstances.length).toBe(1)
expect(
courseinstances.some(instance =>
(instance.date.getTime() === courseinstanceFall.coursedate.getTime()) &&
(instance.course.code === courseinstanceFall.course_code))
).toBe(true)
})

test('Query result for BSc, Fall 2011 for 1 month should return student even though do not have any credits yet. ', async () => {
const query = createQueryObject('2011', SEMESTER.FALL, [elementdetails.bsc.code], 1)
const { students } = await optimizedStatisticsOf(query)
expect(students.some(s => s.studentNumber === student.studentnumber)).toBe(true)
})

})

})
Loading

0 comments on commit fa7a370

Please sign in to comment.