Skip to content

Commit

Permalink
Merge pull request #160 from Carifio24/omit-class-data-merge-groups
Browse files Browse the repository at this point in the history
Allow omitting class when fetching all data
  • Loading branch information
Carifio24 authored Nov 19, 2024
2 parents 760825a + d6c8ebb commit 264693c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
46 changes: 32 additions & 14 deletions src/stories/hubbles_law/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,12 +431,17 @@ export async function _getStageThreeStudentData(studentID: number, classID: numb

const MINIMAL_MEASUREMENT_FIELDS = ["student_id", "galaxy_id", "velocity_value", "est_dist_value", "class_id"];

export async function getAllHubbleMeasurements(before: Date | null = null, minimal=false): Promise<HubbleMeasurement[]> {
const whereConditions: WhereOptions = [
export async function getAllHubbleMeasurements(before: Date | null = null, minimal=false, classID: number | null = null): Promise<HubbleMeasurement[]> {
const whereOptions: WhereOptions = [
{ "$student.IgnoreStudents.student_id$": null }
];
if (before !== null) {
whereConditions.push({ last_modified: { [Op.lt]: before } });
whereOptions.push({ last_modified: { [Op.lt]: before } });
}
const classesWhere: WhereOptions<Class> = [];
if (classID !== null) {
const classIDs = await getMergedIDsForClass(classID, true);
classesWhere.push({ id: { [Op.notIn]: classIDs } });
}
const exclude = minimal ? Object.keys(HubbleMeasurement.getAttributes()).filter(key => !MINIMAL_MEASUREMENT_FIELDS.includes(key)) : [];

Expand All @@ -449,7 +454,7 @@ export async function getAllHubbleMeasurements(before: Date | null = null, minim
exclude,
},
where: {
[Op.and]: whereConditions
[Op.and]: whereOptions
},
include: [{
model: Galaxy,
Expand All @@ -467,7 +472,8 @@ export async function getAllHubbleMeasurements(before: Date | null = null, minim
include: [{
model: Class,
attributes: [],
through: { attributes: [] }
through: { attributes: [] },
where: classesWhere,
},
{
model: IgnoreStudent,
Expand All @@ -482,12 +488,17 @@ export async function getAllHubbleMeasurements(before: Date | null = null, minim
}

const MINIMAL_STUDENT_DATA_FIELDS = ["student_id", "age_value"];
export async function getAllHubbleStudentData(before: Date | null = null, minimal=false): Promise<HubbleStudentData[]> {
const whereConditions: WhereOptions = [
export async function getAllHubbleStudentData(before: Date | null = null, minimal=false, classID: number | null = null): Promise<HubbleStudentData[]> {
const whereOptions: WhereOptions = [
{ "$student.IgnoreStudents.student_id$": null }
];
if (before !== null) {
whereConditions.push({ last_data_update: { [Op.lt]: before } });
whereOptions.push({ last_data_update: { [Op.lt]: before } });
}
const classesWhere: WhereOptions<Class> = [];
if (classID !== null) {
const classIDs = await getMergedIDsForClass(classID, true);
classesWhere.push({ id : { [Op.notIn]: classIDs } });
}
const exclude = minimal ? Object.keys(HubbleStudentData.getAttributes()).filter(key => !MINIMAL_STUDENT_DATA_FIELDS.includes(key)) : [];

Expand All @@ -500,7 +511,7 @@ export async function getAllHubbleStudentData(before: Date | null = null, minima
exclude,
},
where: {
[Op.and]: whereConditions
[Op.and]: whereOptions
},
include: [{
model: Student,
Expand All @@ -517,7 +528,8 @@ export async function getAllHubbleStudentData(before: Date | null = null, minima
{
model: Class,
attributes: [],
through: { attributes: [] }
through: { attributes: [] },
where: classesWhere,
}],
where: {
[Op.or]: [
Expand All @@ -530,16 +542,22 @@ export async function getAllHubbleStudentData(before: Date | null = null, minima
return data;
}

export async function getAllHubbleClassData(before: Date | null = null, minimal=false): Promise<HubbleClassData[]> {
const whereConditions = before !== null ? [{ last_data_update: { [Op.lt]: before } }] : [];
export async function getAllHubbleClassData(before: Date | null = null, minimal=false, classID: number | null = null): Promise<HubbleClassData[]> {
const whereOptions: WhereOptions<HubbleClassData> = before !== null ? [{ last_data_update: { [Op.lt]: before } }] : [];
const studentsClassesWhere: WhereOptions<StudentsClasses> = [];
if (classID !== null) {
const classIDs = await getMergedIDsForClass(classID, true);
studentsClassesWhere.push({ class_id : { [Op.notIn]: classIDs } });
}
const query: FindOptions<HubbleClassData> = {
include: [{
model: StudentsClasses,
as: "class_data",
attributes: []
attributes: [],
where: studentsClassesWhere,
}],
where: {
[Op.and]: whereConditions
[Op.and]: whereOptions
},
group: ["HubbleClassData.class_id"],
having: Sequelize.where(Sequelize.fn("count", Sequelize.col("HubbleClassData.class_id")), { [Op.gte]: 13 })
Expand Down
10 changes: 7 additions & 3 deletions src/stories/hubbles_law/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,13 +454,17 @@ router.put("/merge-class", async (req, res) => {

router.get("/all-data", async (req, res) => {
const minimal = (req.query?.minimal as string)?.toLowerCase() === "true";
let classID: number | null = parseInt(req.query.class_id as string);
if (isNaN(classID)) {
classID = null;
}
const beforeMs: number = parseInt(req.query.before as string);
const before = isNaN(beforeMs) ? null : new Date(beforeMs);
const [measurements, studentData, classData] =
await Promise.all([
getAllHubbleMeasurements(before, minimal),
getAllHubbleStudentData(before, minimal),
getAllHubbleClassData(before, minimal)
getAllHubbleMeasurements(before, minimal, classID),
getAllHubbleStudentData(before, minimal, classID),
getAllHubbleClassData(before, minimal, classID)
]);
res.json({
measurements,
Expand Down

0 comments on commit 264693c

Please sign in to comment.