Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow returning minimal version of Hubble "all data" #134

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions src/stories/hubbles_law/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,10 @@ export async function _getStageThreeStudentData(studentID: number, classID: numb
return data ?? [];
}

export async function getAllHubbleMeasurements(before: Date | null = null): Promise<HubbleMeasurement[]> {
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<HubbleMeasurement[]> {
const whereConditions: WhereOptions = [
{ "$student.IgnoreStudents.student_id$": null }
];
Expand All @@ -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
Expand Down Expand Up @@ -482,7 +486,9 @@ export async function getAllHubbleMeasurements(before: Date | null = null): Prom
});
}

export async function getAllHubbleStudentData(before: Date | null = null): Promise<HubbleStudentData[]> {
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<HubbleStudentData[]> {
const whereConditions: WhereOptions = [
{ "$student.IgnoreStudents.student_id$": null }
];
Expand All @@ -494,15 +500,16 @@ 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
},
include: [{
model: Student,
as: "student",
attributes: ["seed", "dummy"],
attributes: minimal ? [] : ["seed", "dummy"],
include: [{
model: IgnoreStudent,
required: false,
Expand All @@ -527,9 +534,9 @@ export async function getAllHubbleStudentData(before: Date | null = null): Promi
return data;
}

export async function getAllHubbleClassData(before: Date | null = null): Promise<HubbleClassData[]> {
export async function getAllHubbleClassData(before: Date | null = null, minimal=false): Promise<HubbleClassData[]> {
const whereConditions = before !== null ? [{ last_data_update: { [Op.lt]: before } }] : [];
return HubbleClassData.findAll({
const query: FindOptions<HubbleClassData> = {
include: [{
model: StudentsClasses,
as: "class_data",
Expand All @@ -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<RemoveHubbleMeasurementResult> {
Expand Down
7 changes: 4 additions & 3 deletions src/stories/hubbles_law/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading