Merge pull request #7912 from opf/fix/cache_for_wp_query

avoid n+1 queries on query requests by caching
pull/7913/head
ulferts 5 years ago committed by GitHub
commit e18e5bfb5e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 12
      app/models/queries/filters/shared/custom_field_filter.rb
  2. 6
      app/models/queries/work_packages/filter/filter_for_wp_mixin.rb
  3. 8
      app/models/queries/work_packages/filter/status_filter.rb
  4. 8
      spec/models/queries/projects/filters/custom_field_filter_spec.rb
  5. 8
      spec/models/queries/work_packages/filter/custom_fields/custom_field_filter_spec.rb
  6. 4
      spec/models/queries/work_packages/filter/status_filter_spec.rb
  7. 5
      spec/requests/api/v3/queries/update_form_api_spec.rb
  8. 1
      spec/requests/api/v3/queries/update_query_spec.rb

@ -66,7 +66,7 @@ module Queries::Filters::Shared::CustomFieldFilter
match = name.match /cf_(\d+)/
if match.present? && match[1].to_i > 0
custom_field_context.custom_field_class.find_by(id: match[1].to_i)
all_custom_fields.detect { |cf| cf.id == match[1].to_i }
end
end
@ -104,5 +104,15 @@ module Queries::Filters::Shared::CustomFieldFilter
:Base
end
end
def all_custom_fields
key = ['Queries::Filters::Shared::CustomFieldFilter',
custom_field_context.custom_field_class,
'all_custom_fields']
RequestStore.fetch(key.join('/')) do
custom_field_context.custom_field_class.all.to_a
end
end
end
end

@ -52,7 +52,11 @@ module Queries::WorkPackages::Filter::FilterForWpMixin
end
def available?
visible_scope.exists?
key = 'Queries::WorkPackages::Filter::FilterForWpMixin/available'
RequestStore.fetch(key) do
visible_scope.exists?
end
end
def ar_object_filter?

@ -42,7 +42,7 @@ class Queries::WorkPackages::Filter::StatusFilter < Queries::WorkPackages::Filte
end
def available?
Status.exists?
all_statuses.any?
end
def type
@ -70,7 +70,11 @@ class Queries::WorkPackages::Filter::StatusFilter < Queries::WorkPackages::Filte
private
def all_statuses
@all_statuses ||= Status.all
key = 'Queries::WorkPackages::Filter::StatusFilter/all_statuses'
RequestStore.fetch(key) do
Status.all.to_a
end
end
def operator_strategy

@ -37,7 +37,7 @@ describe Queries::Projects::Filters::CustomFieldFilter, type: :model do
let(:instance_key) { nil }
let(:name) { field.name }
let(:list_project_custom_field) { FactoryBot.create(:list_project_custom_field) }
shared_let(:list_project_custom_field) { FactoryBot.create(:list_project_custom_field) }
let(:bool_project_custom_field) { FactoryBot.build_stubbed(:bool_project_custom_field) }
let(:int_project_custom_field) { FactoryBot.build_stubbed(:int_project_custom_field) }
let(:float_project_custom_field) { FactoryBot.build_stubbed(:float_project_custom_field) }
@ -61,9 +61,9 @@ describe Queries::Projects::Filters::CustomFieldFilter, type: :model do
end
before do
allow(ProjectCustomField).to receive(:find_by) do |args|
all_custom_fields.detect { |stubbed| stubbed.id == args[:id] }
end
allow(ProjectCustomField)
.to receive(:all)
.and_return(all_custom_fields)
end
describe 'invalid custom field' do

@ -38,7 +38,7 @@ describe Queries::WorkPackages::Filter::CustomFieldFilter, type: :model do
let(:instance_key) { nil }
let(:name) { field.name }
let(:list_wp_custom_field) { FactoryBot.create(:list_wp_custom_field) }
shared_let(:list_wp_custom_field) { FactoryBot.create(:list_wp_custom_field) }
let(:bool_wp_custom_field) { FactoryBot.build_stubbed(:bool_wp_custom_field) }
let(:int_wp_custom_field) { FactoryBot.build_stubbed(:int_wp_custom_field) }
let(:float_wp_custom_field) { FactoryBot.build_stubbed(:float_wp_custom_field) }
@ -62,9 +62,9 @@ describe Queries::WorkPackages::Filter::CustomFieldFilter, type: :model do
end
before do
allow(WorkPackageCustomField).to receive(:find_by) do |args|
all_custom_fields.detect { |stubbed| stubbed.id == args[:id] }
end
allow(WorkPackageCustomField)
.to receive(:all)
.and_return(all_custom_fields)
end
describe 'invalid custom field' do

@ -39,8 +39,8 @@ describe Queries::WorkPackages::Filter::StatusFilter, type: :model do
describe '#available?' do
it 'is true if any status exists' do
allow(Status)
.to receive(:exists?)
.and_return true
.to receive(:all)
.and_return [status]
expect(instance).to be_available
end

@ -309,6 +309,11 @@ describe "POST /api/v3/queries/form", type: :request do
describe 'with all parameters given' do
let(:status) { FactoryBot.create :status }
let(:additional_setup) do
status
RequestStore.clear!
end
let(:parameters) do
{
name: "Some Query",

@ -108,6 +108,7 @@ describe "PATCH /api/v3/queries/:id", type: :request do
end
before do
RequestStore.clear!
login_as user
end

Loading…
Cancel
Save