Skip to content

Commit

Permalink
Merge pull request #134 from Carifio24/all-data-minimal
Browse files Browse the repository at this point in the history
Allow returning minimal version of Hubble "all data"
  • Loading branch information
Carifio24 authored Sep 13, 2024
2 parents e63c04d + 6e08bd3 commit 5f49a3a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
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

0 comments on commit 5f49a3a

Please sign in to comment.