Skip to content
Open
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
113 changes: 39 additions & 74 deletions rest/controllers/grades.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ func GradesByProfessorID(c *gin.Context) {
// gradesAggregation is base function, returns the grade distribution depending on type of flag
func gradesAggregation(flag string, c *gin.Context) {
var grades []schema.GradeData
var results []map[string]any

var sectionTypeGrades []schema.TypedGradeData // used to parse the response to section-type endpoints

Expand All @@ -153,9 +152,7 @@ func gradesAggregation(flag string, c *gin.Context) {

var sectionMatch bson.D
var courseMatch bson.D
var courseFind bson.D
var professorMatch bson.D
var professorFind bson.D

var sampleCourse schema.Course // the sample course with the given prefix and course number parameter
var sampleCourseFind bson.D // the filter using prefix and course number to get sample course
Expand All @@ -167,7 +164,6 @@ func gradesAggregation(flag string, c *gin.Context) {
ctx, cancel := context.WithTimeout(c.Request.Context(), 10*time.Second)
defer cancel()

// @TODO: Recommend forcing using first_name and last_name to ensure single professors per query.
// All professors sharing the name will be aggregated together in the current implementation
prefix := c.Query("prefix")
number := c.Query("number")
Expand All @@ -183,7 +179,25 @@ func gradesAggregation(flag string, c *gin.Context) {
}
}

professor := (first_name != "" || last_name != "")
professor := (first_name != "" && last_name != "")

// Reject partial course+professor queries.
// A combined query must specify prefix, number, first_name, and last_name.
if flag != "course_endpoint" && flag != "section_endpoint" && flag != "professor_endpoint" {
courseQuery := prefix != "" || number != ""
professorQuery := first_name != "" || last_name != ""

if courseQuery && professorQuery &&
(prefix == "" || number == "" || first_name == "" || last_name == "") {
respond(
c,
http.StatusBadRequest,
"error",
"Combined course and professor queries requires prefix, number, first_name, and last_name.",
)
return
}
}

// Find internal_course_number associated with subject_prefix and course_number, which will be used later on
sampleCourseFind = bson.D{
Expand Down Expand Up @@ -263,83 +277,34 @@ func gradesAggregation(flag string, c *gin.Context) {

pipeline = mongo.Pipeline{professorMatch, lookupSectionsStage(), unwindSectionsStage(), projectGradeDistributionStage(flag, false), unwindGradeDistributionStage(), groupGradesStage(flag), sortGradesStage(flag), sumGradesStage(), groupGradeDistributionStage(flag)}

case prefix != "" && professor:
// Filter on Section by Matching Course and Professor IDs

// Here we get the valid course ids and professor ids
// and then we perform the grades aggregation against the sections collection,
// matching on the course_reference and professor

collection = sectionCollection

// Find valid professor ids
if last_name == "" {
professorFind = bson.D{{Key: "first_name", Value: first_name}}
} else if first_name == "" {
professorFind = bson.D{{Key: "last_name", Value: last_name}}
} else {
professorFind = bson.D{{Key: "first_name", Value: first_name}, {Key: "last_name", Value: last_name}}
}

cursor, err = professorCollection.Find(ctx, professorFind)
if err != nil {
respondWithInternalError(c, err)
return
}
if err = cursor.All(ctx, &results); err != nil {
respondWithInternalError(c, err)
return
}

profIDs := make([]bson.ObjectID, 0, len(results))
for _, prof := range results {
profID := prof["_id"].(bson.ObjectID)
profIDs = append(profIDs, profID)
}
case prefix != "" && number != "" && first_name != "" && last_name != "":
// Filter on Section by matching Course and Professor fields directly
// against the indexed trends view.
collection = courseCollection.Database().Collection("trends_course_and_prof_sections")

// Get valid course ids
if number == "" {
// if only the prefix is provided, filter only on the prefix
courseFind = bson.D{{Key: "subject_prefix", Value: prefix}}
} else {
// if both prefix and course_number are provided, filter on internal_course_number
courseFind = bson.D{{Key: "internal_course_number", Value: internalCourseNumber}}
match := bson.M{
"subject_prefix": prefix,
"course_number": number,
"first_name": first_name,
"last_name": last_name,
}

cursor, err = courseCollection.Find(ctx, courseFind)
if err != nil {
respondWithInternalError(c, err)
return
}
if err = cursor.All(ctx, &results); err != nil {
respondWithInternalError(c, err)
return
if section_number != "" {
match["section_number"] = section_number
}

courseIDs := make([]bson.ObjectID, 0, len(results))
for _, course := range results {
courseID := course["_id"].(bson.ObjectID)
courseIDs = append(courseIDs, courseID)
}
sectionMatch = bson.D{{Key: "$match", Value: match}}

// Build sectionMatch
if section_number == "" {
sectionMatch =
bson.D{{Key: "$match", Value: bson.D{
{Key: "course_reference", Value: bson.D{{Key: "$in", Value: courseIDs}}},
{Key: "professors", Value: bson.D{{Key: "$in", Value: profIDs}}},
}}}
} else {
sectionMatch =
bson.D{{Key: "$match", Value: bson.D{
{Key: "course_reference", Value: bson.D{{Key: "$in", Value: courseIDs}}},
{Key: "professors", Value: bson.D{{Key: "$in", Value: profIDs}}},
{Key: "section_number", Value: section_number},
}}}
pipeline = mongo.Pipeline{
sectionMatch,
projectGradeDistributionStage(flag, true),
unwindGradeDistributionStage(),
groupGradesStage(flag),
sortGradesStage(flag),
sumGradesStage(),
groupGradeDistributionStage(flag),
}

pipeline = mongo.Pipeline{sectionMatch, projectGradeDistributionStage(flag, true), unwindGradeDistributionStage(), groupGradesStage(flag), sortGradesStage(flag), sumGradesStage(), groupGradeDistributionStage(flag)}

default:
respond(c, http.StatusBadRequest, "error", "Invalid query parameters.")
return
Expand Down
10 changes: 5 additions & 5 deletions rest/schema/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,11 +350,11 @@ type AcademicCalendarDropDeadlines struct {
}

type AcademicProgram struct {
Id bson.ObjectID `bson:"_id" json:"_id"`
Title string `bson:"name" json:"name" queryable:""`
School string `bson:"school" json:"school" queryable:""`
DegreeOptions []Degree `bson:"degree_options" json:"degree_options" queryable:""`
AreasOfInterest []string `bson:"areas_of_interest" json:"areas_of_interest" queryable:""`
Id bson.ObjectID `bson:"_id" json:"_id"`
Title string `bson:"name" json:"name" queryable:""`
School string `bson:"school" json:"school" queryable:""`
DegreeOptions []Degree `bson:"degree_options" json:"degree_options" queryable:""`
AreasOfInterest []string `bson:"areas_of_interest" json:"areas_of_interest" queryable:""`
}

type Degree struct {
Expand Down
Loading