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

Perf: Better response times from api/public/v1/courses #4616

Merged
merged 8 commits into from
Dec 2, 2024
20 changes: 16 additions & 4 deletions app/controllers/api/public/v1/courses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,36 @@ class CoursesController < API::Public::V1::ApplicationController
def index
render jsonapi: paginate(courses),
include: include_param,
meta: { count: courses.count('course.id') },
meta: { count: cached_course_count },
class: API::Public::V1::SerializerService.call
rescue ActiveRecord::StatementInvalid
render json: { status: 400, message: 'Invalid changed_since value, the format should be an ISO8601 UTC timestamp, for example: `2019-01-01T12:01:00Z`' }.to_json, status: :bad_request
end

private

def cached_course_count
year = permitted_params[:recruitment_cycle_year] || RecruitmentCycle.current.year

Rails.cache.fetch("api_course_count_#{year}", expires_in: 5.minutes) do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's have a discussion about this expiration time?

courses.count('course.id')
inulty-dfe marked this conversation as resolved.
Show resolved Hide resolved
end
end

def courses
@courses ||= APICourseSearchService.call(
filter: params[:filter],
sort: params[:sort],
filter: permitted_params[:filter],
sort: permitted_params[:sort],
course_scope: recruitment_cycle.courses
)
end

def include_param
params.fetch(:include, '')
permitted_params.fetch(:include, '')
end

def permitted_params
params.permit('page', 'sort', 'per_page', 'courses', 'recruitment_cycle_year', 'include', 'filter' => %w[updated_since funding_type])
end
end
end
Expand Down
6 changes: 5 additions & 1 deletion app/lib/required_qualifications_summary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ def initialize(course)
end

def extract
legacy_qualifications_attribute = course.latest_published_enrichment&.required_qualifications
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: 2 additions & 0 deletions app/models/course.rb
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,8 @@ def gcse_grade_required
end

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

enrichments.maximum(:last_published_timestamp_utc)
end
Comment on lines +597 to 600
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it can be generalised for all associations.

Have you considered looking into ActiveRecord/Associations/CollectionProxy? A #maximum method there might be able to address this issue across the entire app with a single change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that looks tempting. but I would be careful about doing that. It's something we should discuss though, it seems like a useful thing to do.


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 @@ -138,7 +138,7 @@ def enrichment_attribute(name, enrichment_name = name)
end

attribute :subject_codes do
@object.subjects.pluck(:subject_code).compact
@object.subjects.map(&:subject_code).compact
end

attribute :required_qualifications do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@

it 'delegates to the CourseSearchService' do
expect(CourseSearchService).to have_received(:call).with(
hash_including(filter: ActionController::Parameters.new(funding_type: 'salary'))
hash_including(filter: ActionController::Parameters.new(funding_type: 'salary').permit!)
)
end
end
Expand Down
12 changes: 12 additions & 0 deletions spec/lib/required_qualifications_summary_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@
end
end

context 'when the course enrichments are not loaded' do
let(:enrichment) { build(:course_enrichment, :published, required_qualifications: 'GCSE Computer Science') }
let(:course) { create(:course, enrichments: [enrichment]) }

it 'returns the expected value' do
course.reload
expect(course.enrichments.loaded?).to be_falsey
summary = described_class.new(course).extract
expect(summary).to eq 'GCSE Computer Science'
end
end

context 'when required_qualifications enrichment attribute is blank' do
it 'assembles a whole summary based on course attributes' do
course = create(:course, :primary)
Expand Down
32 changes: 32 additions & 0 deletions spec/models/course_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3157,6 +3157,38 @@
end
end

describe '#last_published_at' do
context 'with an unpublished course' do
let(:course) { create(:course, study_mode: :part_time, enrichments: [create(:course_enrichment)]) }

it 'returns nil' do
expect(course.last_published_at).to be_nil
end
end

context 'with a twice published course' do
let(:course) do
create(:course, study_mode: :part_time, enrichments:
[
create(:course_enrichment, :published, last_published_timestamp_utc: '1/1/2024'),
create(:course_enrichment, :published, last_published_timestamp_utc: '1/1/2023')
])
end

it 'selects the enrichment with the latest date' do
expect(course.last_published_at).to eq('1/1/2024')
end

context 'when the enrichments are not loaded' do
it 'selects the enrichment with the latest date' do
course.reload
expect(course.enrichments.loaded?).to be_falsey
expect(course.last_published_at).to eq('1/1/2024')
end
end
end
end

describe 'funding_type and program_type' do
context 'setting the funding to apprenticeship' do
it 'sets the funding to apprenticeship and program_type to pg_teaching_apprenticeship' do
Expand Down
Loading