Skip to content

Commit

Permalink
Merge pull request #101 from Carifio24/reduced-galaxy-data
Browse files Browse the repository at this point in the history
Add query option for galaxy flags
  • Loading branch information
Carifio24 authored Jan 18, 2024
2 parents 7d1012a + e46cc7a commit 7820e55
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
28 changes: 20 additions & 8 deletions src/stories/hubbles_law/database.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Op, QueryTypes, Sequelize, WhereOptions, col, fn, literal } from "sequelize";
import { Attributes, FindOptions, Op, QueryTypes, Sequelize, WhereOptions, col, fn, literal } from "sequelize";
import { AsyncMergedHubbleStudentClasses, Galaxy, HubbleMeasurement, SampleHubbleMeasurement, initializeModels, SyncMergedHubbleClasses } from "./models";
import { classSize, cosmicdsDB, findClassById, findStudentById } from "../../database";
import { RemoveHubbleMeasurementResult, SubmitHubbleMeasurementResult } from "./request_results";
Expand All @@ -12,7 +12,7 @@ import { logger } from "../../logger";
initializeModels(cosmicdsDB);
setUpHubbleAssociations();

const galaxyAttributes = ["ra", "decl", "z", "type", "name", "element"];
const galaxyAttributes = ["id", "ra", "decl", "z", "type", "name", "element"];

export async function submitHubbleMeasurement(data: {
student_id: number,
Expand Down Expand Up @@ -506,25 +506,37 @@ export async function removeSampleHubbleMeasurement(studentID: number, measureme
return count > 0 ? RemoveHubbleMeasurementResult.MeasurementDeleted : RemoveHubbleMeasurementResult.NoSuchMeasurement;
}

export async function getGalaxiesForTypes(types: string[]): Promise<Galaxy[]> {
return Galaxy.findAll({
export async function getGalaxiesForTypes(types: string[], flags=false): Promise<Galaxy[]> {
const query: FindOptions<Attributes<Galaxy>> = {
where: {
is_bad: 0,
spec_is_bad: 0,
is_sample: 0,
type: { [Op.in]: types }
}
});
};

if (!flags) {
query.attributes = galaxyAttributes;
}

return Galaxy.findAll(query);
}

export async function getAllGalaxies(): Promise<Galaxy[]> {
return Galaxy.findAll({
export async function getAllGalaxies(flags=false): Promise<Galaxy[]> {
const query: FindOptions<Attributes<Galaxy>> = {
where: {
is_bad: 0,
spec_is_bad: 0,
is_sample: 0
}
});
};

if (!flags) {
query.attributes = galaxyAttributes;
}

return Galaxy.findAll(query);
}

export async function getGalaxyByName(name: string): Promise<Galaxy | null> {
Expand Down
5 changes: 3 additions & 2 deletions src/stories/hubbles_law/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,17 +352,18 @@ router.put("/sync-merged-class/:classID", async(req, res) => {

router.get("/galaxies", async (req, res) => {
const types = req.query?.types ?? undefined;
const flags = (/true/i).test((req.query?.flags as string) ?? undefined);
let galaxies: Galaxy[];
if (types === undefined) {
galaxies = await getAllGalaxies();
galaxies = await getAllGalaxies(flags);
} else {
let galaxyTypes: string[];
if (Array.isArray(types)) {
galaxyTypes = types as string[];
} else {
galaxyTypes = (types as string).split(",");
}
galaxies = await getGalaxiesForTypes(galaxyTypes);
galaxies = await getGalaxiesForTypes(galaxyTypes, flags);
}
res.json(galaxies);
});
Expand Down

0 comments on commit 7820e55

Please sign in to comment.