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 omitting class when fetching all data #160

Merged
merged 2 commits into from
Nov 19, 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
46 changes: 32 additions & 14 deletions src/stories/hubbles_law/database.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Attributes, FindOptions, Op, QueryTypes, Sequelize, WhereAttributeHash, WhereOptions, col, fn, literal } from "sequelize";
import { AsyncMergedHubbleStudentClasses, Galaxy, HubbleMeasurement, SampleHubbleMeasurement, SyncMergedHubbleClasses } from "./models";
import { classSize, findClassById, findStudentById } from "../../database";

Check warning on line 3 in src/stories/hubbles_law/database.ts

View workflow job for this annotation

GitHub Actions / build

'classSize' is defined but never used. Allowed unused vars must match /^_/u
import { RemoveHubbleMeasurementResult, SubmitHubbleMeasurementResult } from "./request_results";
import { Class, StoryState, Student, StudentsClasses } from "../../models";
import { HubbleStudentData } from "./models/hubble_student_data";
Expand Down Expand Up @@ -431,12 +431,17 @@

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 @@
exclude,
},
where: {
[Op.and]: whereConditions
[Op.and]: whereOptions
},
include: [{
model: Galaxy,
Expand All @@ -467,7 +472,8 @@
include: [{
model: Class,
attributes: [],
through: { attributes: [] }
through: { attributes: [] },
where: classesWhere,
},
{
model: IgnoreStudent,
Expand All @@ -482,12 +488,17 @@
}

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 @@
exclude,
},
where: {
[Op.and]: whereConditions
[Op.and]: whereOptions
},
include: [{
model: Student,
Expand All @@ -517,7 +528,8 @@
{
model: Class,
attributes: [],
through: { attributes: [] }
through: { attributes: [] },
where: classesWhere,
}],
where: {
[Op.or]: [
Expand All @@ -530,16 +542,22 @@
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
Loading