Skip to content

Commit

Permalink
Merge pull request #123 from Carifio24/sizing-things-up
Browse files Browse the repository at this point in the history
Add endpoints for getting class size and Hubble measurement count
  • Loading branch information
Carifio24 authored Jul 31, 2024
2 parents 8b0f828 + 70f6071 commit 76bda8d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
getStageState,
updateStageState,
deleteStageState,
findClassById,
} from "./database";

import { getAPIKey, hasPermission } from "./authorization";
Expand Down Expand Up @@ -417,6 +418,23 @@ app.get("/educators", async (_req, res) => {
res.json(queryResponse);
});

app.get("/classes/size/:classID", async (req, res) => {
const classID = Number(req.params.classID);
const cls = await findClassById(classID);
if (cls === null) {
res.status(404).json({
message: `Class ${classID} not found`,
});
return;
}

const size = classSize(classID);
res.json({
class_id: classID,
size
});
});

app.get("/story-state/:studentID/:storyName", async (req, res) => {
const params = req.params;
const studentID = Number(params.studentID);
Expand Down
20 changes: 20 additions & 0 deletions src/stories/hubbles_law/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,26 @@ export async function getClassMeasurements(studentID: number,
return data ?? [];
}

// The advantage of this over the function above is that it saves bandwidth,
// since we aren't sending the data itself.
// This is intended to be used with cases where we need to frequently check the class size,
// e.g. the beginning of stage 4 in the Hubble story
export async function getClassMeasurementCount(studentID: number,
classID: number | null,
excludeWithNull: boolean = false,
): Promise<number> {
const cls = classID !== null ? await findClassById(classID) : null;
const asyncClass = cls?.asynchronous ?? true;
let data: HubbleMeasurement[] | null;
console.log(classID, asyncClass);
if (classID === null || asyncClass) {
data = await getHubbleMeasurementsForAsyncStudent(studentID, classID, excludeWithNull);
} else {
data = await getHubbleMeasurementsForSyncStudent(studentID, classID, excludeWithNull);
}
return data?.length ?? 0;
}

async function getHubbleStudentDataForAsyncStudent(studentID: number, classID: number | null): Promise<HubbleStudentData[] | null> {
const classIDs = await getClassIDsForAsyncStudent(studentID, classID);
return getHubbleStudentDataForClasses(classIDs);
Expand Down
30 changes: 29 additions & 1 deletion src/stories/hubbles_law/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ import {
getGalaxyById,
removeSampleHubbleMeasurement,
getAllNthSampleHubbleMeasurements,
tryToMergeClass
tryToMergeClass,
getClassMeasurementCount
} from "./database";

import {
Expand Down Expand Up @@ -257,6 +258,33 @@ router.get("/sample-galaxy", async (_req, res) => {
res.json(galaxy);
});

router.get("/class-measurements/size/:studentID/:classID", async (req, res) => {
const studentID = parseInt(req.params.studentID);
const isValidStudent = (await findStudentById(studentID)) !== null;
if (!isValidStudent) {
res.status(404).json({
message: "Invalid student ID",
});
return;
}

const classID = parseInt(req.params.classID);
const isValidClass = (await findClassById(classID)) !== null;
if (!isValidClass) {
res.status(404).json({
message: "Invalid class ID",
});
return;
}

const completeOnly = (req.query.complete_only as string)?.toLowerCase() === "true";
const count = await getClassMeasurementCount(studentID, classID, completeOnly);
res.status(200).json({
student_id: studentID,
measurement_count: count,
});
});

router.get(["/class-measurements/:studentID/:classID", "/stage-3-data/:studentID/:classID"], async (req, res) => {
const lastCheckedStr = req.query.last_checked as string;
let lastChecked: number | null = parseInt(lastCheckedStr);
Expand Down

0 comments on commit 76bda8d

Please sign in to comment.