From 6e08bd39e5bff7318938ddac09c86fbe5144f086 Mon Sep 17 00:00:00 2001 From: Carifio24 Date: Fri, 13 Sep 2024 14:36:42 -0400 Subject: [PATCH] Update Hubble /all-data endpoint to allow returning a 'minimal' version of the values. --- src/stories/hubbles_law/database.ts | 27 +++++++++++++++++++-------- src/stories/hubbles_law/router.ts | 7 ++++--- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/stories/hubbles_law/database.ts b/src/stories/hubbles_law/database.ts index 697b95f..de1b1b4 100644 --- a/src/stories/hubbles_law/database.ts +++ b/src/stories/hubbles_law/database.ts @@ -435,7 +435,10 @@ export async function _getStageThreeStudentData(studentID: number, classID: numb return data ?? []; } -export async function getAllHubbleMeasurements(before: Date | null = null): Promise { +const MINIMAL_MEASUREMENT_FIELDS = ["student_id", "galaxy_id", "velocity_value", "est_dist_value", "class_id"]; +const MINIMAL_EXCLUDE_MEASUREMENT_FIELDS = Object.keys(HubbleMeasurement.getAttributes()).filter(key => !MINIMAL_MEASUREMENT_FIELDS.includes(key)); + +export async function getAllHubbleMeasurements(before: Date | null = null, minimal=false): Promise { const whereConditions: WhereOptions = [ { "$student.IgnoreStudents.student_id$": null } ]; @@ -447,7 +450,8 @@ export async function getAllHubbleMeasurements(before: Date | null = null): Prom attributes: { // The "student" here comes from the alias below // We do this so that we get access to the included field as just "class_id" - include: [[Sequelize.col("student.Classes.id"), "class_id"]] + include: [[Sequelize.col("student.Classes.id"), "class_id"]], + exclude: minimal ? MINIMAL_EXCLUDE_MEASUREMENT_FIELDS : [], }, where: { [Op.and]: whereConditions @@ -482,7 +486,9 @@ export async function getAllHubbleMeasurements(before: Date | null = null): Prom }); } -export async function getAllHubbleStudentData(before: Date | null = null): Promise { +const MINIMAL_STUDENT_DATA_FIELDS = ["student_id", "age_value"]; +const MINIMAL_EXCLUDE_STUDENT_DATA_FIELDS = Object.keys(HubbleStudentData.getAttributes()).filter(key => !MINIMAL_STUDENT_DATA_FIELDS.includes(key)); +export async function getAllHubbleStudentData(before: Date | null = null, minimal=false): Promise { const whereConditions: WhereOptions = [ { "$student.IgnoreStudents.student_id$": null } ]; @@ -494,7 +500,8 @@ export async function getAllHubbleStudentData(before: Date | null = null): Promi attributes: { // The "student" here comes from the alias below // We do this so that we get access to the included field as just "class_id" - include: [[Sequelize.col("student.Classes.id"), "class_id"]] + include: [[Sequelize.col("student.Classes.id"), "class_id"]], + exclude: minimal ? MINIMAL_EXCLUDE_STUDENT_DATA_FIELDS : [], }, where: { [Op.and]: whereConditions @@ -502,7 +509,7 @@ export async function getAllHubbleStudentData(before: Date | null = null): Promi include: [{ model: Student, as: "student", - attributes: ["seed", "dummy"], + attributes: minimal ? [] : ["seed", "dummy"], include: [{ model: IgnoreStudent, required: false, @@ -527,9 +534,9 @@ export async function getAllHubbleStudentData(before: Date | null = null): Promi return data; } -export async function getAllHubbleClassData(before: Date | null = null): Promise { +export async function getAllHubbleClassData(before: Date | null = null, minimal=false): Promise { const whereConditions = before !== null ? [{ last_data_update: { [Op.lt]: before } }] : []; - return HubbleClassData.findAll({ + const query: FindOptions = { include: [{ model: StudentsClasses, as: "class_data", @@ -540,7 +547,11 @@ export async function getAllHubbleClassData(before: Date | null = null): Promise }, group: ["HubbleClassData.class_id"], having: Sequelize.where(Sequelize.fn("count", Sequelize.col("HubbleClassData.class_id")), { [Op.gte]: 13 }) - }); + }; + if (minimal) { + query.attributes = ["class_id", "age_value"]; + } + return HubbleClassData.findAll(query); } export async function removeHubbleMeasurement(studentID: number, galaxyID: number): Promise { diff --git a/src/stories/hubbles_law/router.ts b/src/stories/hubbles_law/router.ts index 61c6761..924b1f3 100644 --- a/src/stories/hubbles_law/router.ts +++ b/src/stories/hubbles_law/router.ts @@ -378,13 +378,14 @@ router.get(["/class-measurements/:studentID", "stage-3-measurements/:studentID"] }); router.get("/all-data", async (req, res) => { + const minimal = (req.query?.minimal as string)?.toLowerCase() === "true"; 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), - getAllHubbleStudentData(before), - getAllHubbleClassData(before) + getAllHubbleMeasurements(before, minimal), + getAllHubbleStudentData(before, minimal), + getAllHubbleClassData(before, minimal) ]); res.json({ measurements,