Skip to content

Commit

Permalink
Reduce number of queries in the courses api
Browse files Browse the repository at this point in the history
  • Loading branch information
inulty-dfe committed Oct 25, 2024
1 parent 292ac6f commit 0d446ef
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
2 changes: 2 additions & 0 deletions app/models/course.rb
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,8 @@ def gcse_grade_required
end

def last_published_at
return enrichments.max_by(&:last_published_timestamp_utc).last_published_timestamp_utc if enrichments.loaded?

enrichments.maximum(:last_published_timestamp_utc)
end

Expand Down
7 changes: 6 additions & 1 deletion app/models/required_qualifications_summary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ def initialize(course)
end

def extract
legacy_qualifications_attribute = course.latest_published_enrichment&.required_qualifications
# This performance improvement saves ~100 database queries in the courses api endpoint
legacy_qualifications_attribute = if course.enrichments.loaded?
course.enrichments.max_by(&:created_at)&.required_qualifications
else
course.latest_published_enrichment&.required_qualifications
end
return legacy_qualifications_attribute if legacy_qualifications_attribute.present?

generate_summary_text
Expand Down
2 changes: 1 addition & 1 deletion app/serializers/api/public/v1/serializable_course.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class SerializableCourse < JSONAPI::Serializable::Resource
class << self
def enrichment_attribute(name, enrichment_name = name)
attribute name do
@object.latest_published_enrichment&.public_send(enrichment_name)
@object.enrichments.max_by(&:created_at)&.public_send(enrichment_name)
end
end
end
Expand Down

0 comments on commit 0d446ef

Please sign in to comment.