From 0ef8de95ec84c162cdaf63c5fcb3b89520e6333e Mon Sep 17 00:00:00 2001 From: Carifio24 Date: Mon, 18 Nov 2024 17:30:30 -0500 Subject: [PATCH 1/2] Allow specifying class ID in all data endpoint to omit that class's data. --- src/stories/hubbles_law/database.ts | 31 +++++++++++++++++++---------- src/stories/hubbles_law/router.ts | 10 +++++++--- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/src/stories/hubbles_law/database.ts b/src/stories/hubbles_law/database.ts index 65c7d92..0f4035a 100644 --- a/src/stories/hubbles_law/database.ts +++ b/src/stories/hubbles_law/database.ts @@ -431,12 +431,15 @@ 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 { - const whereConditions: WhereOptions = [ +export async function getAllHubbleMeasurements(before: Date | null = null, minimal=false, classID: number | null = null): Promise { + 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 } }); + } + if (classID !== null) { + whereOptions.push({ class_id: { [Op.ne]: classID } }); } const exclude = minimal ? Object.keys(HubbleMeasurement.getAttributes()).filter(key => !MINIMAL_MEASUREMENT_FIELDS.includes(key)) : []; @@ -449,7 +452,7 @@ export async function getAllHubbleMeasurements(before: Date | null = null, minim exclude, }, where: { - [Op.and]: whereConditions + [Op.and]: whereOptions }, include: [{ model: Galaxy, @@ -482,12 +485,15 @@ 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 { - const whereConditions: WhereOptions = [ +export async function getAllHubbleStudentData(before: Date | null = null, minimal=false, classID: number | null = null): Promise { + 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 } }); + } + if (classID !== null) { + whereOptions.push({ class_id: { [Op.ne]: classID } }); } const exclude = minimal ? Object.keys(HubbleStudentData.getAttributes()).filter(key => !MINIMAL_STUDENT_DATA_FIELDS.includes(key)) : []; @@ -500,7 +506,7 @@ export async function getAllHubbleStudentData(before: Date | null = null, minima exclude, }, where: { - [Op.and]: whereConditions + [Op.and]: whereOptions }, include: [{ model: Student, @@ -530,8 +536,11 @@ export async function getAllHubbleStudentData(before: Date | null = null, minima return data; } -export async function getAllHubbleClassData(before: Date | null = null, minimal=false): Promise { - 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 { + const whereOptions: WhereOptions = before !== null ? [{ last_data_update: { [Op.lt]: before } }] : []; + if (classID !== null) { + whereOptions.push({ class_id: { [Op.ne]: classID } }); + } const query: FindOptions = { include: [{ model: StudentsClasses, @@ -539,7 +548,7 @@ export async function getAllHubbleClassData(before: Date | null = null, minimal= attributes: [] }], 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 }) diff --git a/src/stories/hubbles_law/router.ts b/src/stories/hubbles_law/router.ts index b9f134d..0f4acc9 100644 --- a/src/stories/hubbles_law/router.ts +++ b/src/stories/hubbles_law/router.ts @@ -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, From d6c8ebb38a77892dece81da4cfaff2ddd6419d84 Mon Sep 17 00:00:00 2001 From: Carifio24 Date: Tue, 19 Nov 2024 00:50:46 -0500 Subject: [PATCH 2/2] Take merge groups into account when filtering all data. --- src/stories/hubbles_law/database.ts | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/stories/hubbles_law/database.ts b/src/stories/hubbles_law/database.ts index 0f4035a..8e8c1a7 100644 --- a/src/stories/hubbles_law/database.ts +++ b/src/stories/hubbles_law/database.ts @@ -438,8 +438,10 @@ export async function getAllHubbleMeasurements(before: Date | null = null, minim if (before !== null) { whereOptions.push({ last_modified: { [Op.lt]: before } }); } + const classesWhere: WhereOptions = []; if (classID !== null) { - whereOptions.push({ class_id: { [Op.ne]: classID } }); + 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)) : []; @@ -470,7 +472,8 @@ export async function getAllHubbleMeasurements(before: Date | null = null, minim include: [{ model: Class, attributes: [], - through: { attributes: [] } + through: { attributes: [] }, + where: classesWhere, }, { model: IgnoreStudent, @@ -492,8 +495,10 @@ export async function getAllHubbleStudentData(before: Date | null = null, minima if (before !== null) { whereOptions.push({ last_data_update: { [Op.lt]: before } }); } + const classesWhere: WhereOptions = []; if (classID !== null) { - whereOptions.push({ class_id: { [Op.ne]: classID } }); + 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)) : []; @@ -523,7 +528,8 @@ export async function getAllHubbleStudentData(before: Date | null = null, minima { model: Class, attributes: [], - through: { attributes: [] } + through: { attributes: [] }, + where: classesWhere, }], where: { [Op.or]: [ @@ -538,14 +544,17 @@ export async function getAllHubbleStudentData(before: Date | null = null, minima export async function getAllHubbleClassData(before: Date | null = null, minimal=false, classID: number | null = null): Promise { const whereOptions: WhereOptions = before !== null ? [{ last_data_update: { [Op.lt]: before } }] : []; + const studentsClassesWhere: WhereOptions = []; if (classID !== null) { - whereOptions.push({ class_id: { [Op.ne]: classID } }); + const classIDs = await getMergedIDsForClass(classID, true); + studentsClassesWhere.push({ class_id : { [Op.notIn]: classIDs } }); } const query: FindOptions = { include: [{ model: StudentsClasses, as: "class_data", - attributes: [] + attributes: [], + where: studentsClassesWhere, }], where: { [Op.and]: whereOptions