-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #165 from ufabc-next/feature/graduationStats
Draft: Feature/graduation stats
- Loading branch information
Showing
5 changed files
with
230 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
const app = require('@/app') | ||
|
||
module.exports = async function func() { | ||
const Graduation = await app.models.graduation | ||
const graduations = await Graduation.find({}).lean() | ||
|
||
const Subject = await app.models.subjects | ||
const SubjectGraduations = await app.models.subjectGraduations | ||
|
||
const populatedGraduations = await Promise.all( | ||
graduations.map(async (graduation) => { | ||
const subjectsGraduations = await SubjectGraduations.find({ | ||
graduation: graduation._id, | ||
}).lean() | ||
|
||
const subjects = ( | ||
await Promise.all( | ||
subjectsGraduations.map(async (subjectGraduation) => { | ||
const subject = await Subject.findOne({ | ||
_id: subjectGraduation.subject, | ||
}).lean() | ||
return { | ||
...subjectGraduation, | ||
subject, | ||
} | ||
}) | ||
) | ||
).reduce((acc, subject) => { | ||
const category = subject.category | ||
if (!category) { | ||
return acc | ||
} | ||
if (!acc[category]) { | ||
acc[category] = [] | ||
} | ||
acc[category].push(subject) | ||
return acc | ||
}, {}) | ||
|
||
return { | ||
...graduation, | ||
subjects, | ||
} | ||
}) | ||
) | ||
|
||
return populatedGraduations | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const app = require('@/app') | ||
|
||
module.exports = async (router) => { | ||
router.get('/graduations', app.helpers.routes.func(require('./func.js'))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
const app = require('@/app') | ||
|
||
module.exports = async function func(context) { | ||
const { ra } = context.user | ||
if (!ra) { | ||
return [] | ||
} | ||
const graduationId = context.params.id | ||
const Graduation = await app.models.graduation | ||
const graduation = await Graduation.findOne({ | ||
_id: graduationId, | ||
}).lean() | ||
|
||
const Subject = await app.models.subjects | ||
const SubjectGraduations = await app.models.subjectGraduations | ||
const subjectsGraduations = await SubjectGraduations.find({ | ||
graduation: graduation._id, | ||
}).lean() | ||
|
||
const subjects = ( | ||
await Promise.all( | ||
subjectsGraduations.map(async (subjectGraduation) => { | ||
const subject = await Subject.findOne({ | ||
_id: subjectGraduation.subject, | ||
}).lean() | ||
return { | ||
...subjectGraduation, | ||
subject, | ||
} | ||
}) | ||
) | ||
).reduce((acc, subject) => { | ||
const category = subject.category | ||
if (!category) { | ||
return acc | ||
} | ||
if (!acc[category]) { | ||
acc[category] = [] | ||
} | ||
acc[category].push(subject) | ||
return acc | ||
}, {}) | ||
|
||
const populatedGraduation = { | ||
...graduation, | ||
subjects, | ||
} | ||
|
||
const enrollments = await app.models.enrollments | ||
.find( | ||
{ | ||
ra, | ||
conceito: { $in: ['A', 'B', 'C', 'D', 'O', 'F'] }, | ||
}, | ||
{ | ||
conceito: 1, | ||
subject: 1, | ||
disciplina: 1, | ||
pratica: 1, | ||
teoria: 1, | ||
year: 1, | ||
quad: 1, | ||
creditos: 1, | ||
updatedAt: 1, | ||
comments: 1, | ||
} | ||
) | ||
.populate(['pratica', 'teoria', 'subject']) | ||
.lean(true) | ||
|
||
const mandatories = populatedGraduation.subjects.mandatory.filter( | ||
(subject) => { | ||
return enrollments.some((enrollment) => { | ||
if (!enrollment.subject || !subject.subject) { | ||
return false | ||
} | ||
return ( | ||
enrollment.subject._id.toString() === subject.subject._id.toString() | ||
) | ||
}) | ||
} | ||
) | ||
|
||
const limited = populatedGraduation.subjects.limited.filter((subject) => { | ||
return enrollments.some((enrollment) => { | ||
if (!enrollment.subject || !subject.subject) { | ||
return false | ||
} | ||
return ( | ||
enrollment.subject._id.toString() === subject.subject._id.toString() | ||
) | ||
}) | ||
}) | ||
|
||
const invalidGrades = ['O', 'F'] | ||
|
||
return { | ||
percentage: { | ||
mandatories: | ||
mandatories.length / populatedGraduation.subjects.mandatory.length, | ||
limited: limited.length / populatedGraduation.subjects.limited.length, | ||
}, | ||
credits: { | ||
mandatories: { | ||
done: mandatories.reduce((acc, subject) => { | ||
return acc + subject.subject.creditos | ||
}, 0), | ||
total: populatedGraduation.subjects.mandatory.reduce((acc, subject) => { | ||
return acc + subject.subject.creditos | ||
}, 0), | ||
}, | ||
limited: { | ||
done: limited.reduce((acc, subject) => { | ||
return acc + subject.subject.creditos | ||
}, 0), | ||
total: populatedGraduation.limited_credits_number, | ||
}, | ||
}, | ||
missing: { | ||
mandatories: populatedGraduation.subjects.mandatory.filter((subject) => { | ||
return !mandatories.some((enrollment) => { | ||
const completed = !invalidGrades.includes(enrollment.conceito) | ||
if (!completed) return false | ||
|
||
return ( | ||
enrollment.subject._id.toString() === subject.subject._id.toString() | ||
) | ||
}) | ||
}), | ||
limited: populatedGraduation.subjects.limited.filter((subject) => { | ||
return !limited.some((enrollment) => { | ||
const completed = !invalidGrades.includes(enrollment.conceito) | ||
if (!completed) return false | ||
|
||
return ( | ||
enrollment.subject._id.toString() === subject.subject._id.toString() | ||
) | ||
}) | ||
}), | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const app = require('@/app') | ||
|
||
module.exports = async (router) => { | ||
router.get( | ||
'/graduations/stats/:id', | ||
app.helpers.routes.func(require('./func.js')) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters