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

Issue 516 speed up search api #530

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
5 changes: 3 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ gem 'rails3-restful-authentication', '~> 3.0.1'
gem 'dynamic_form'
gem 'rabl'
gem 'yajl-ruby'
gem 'jbuilder'
gem 'oj'

# to-do: remove prototype dependencies so we no longer need this gem
gem 'prototype_legacy_helper', '0.0.0', :git => 'git://github.com/rails/prototype_legacy_helper.git'
Expand Down Expand Up @@ -52,7 +54,7 @@ end


group :test do
# phasing out webrat:
# phasing out webrat:
# gem "webrat", "0.7.1"
gem "capybara"
gem "database_cleaner"
Expand Down Expand Up @@ -88,4 +90,3 @@ gem "json-schema" # needed by rspec_api_documentation

gem "apipie-rails"
gem "test-unit" # seems to be needed by apipie-rails and prototype-rails

8 changes: 7 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ GEM
ffi (1.9.8)
hike (1.2.3)
i18n (0.7.0)
jbuilder (2.6.4)
activesupport (>= 3.0.0)
multi_json (>= 1.2)
journey (1.0.4)
json (1.8.3)
json-schema (2.6.1)
Expand All @@ -82,6 +85,7 @@ GEM
narray (0.6.0.4)
nokogiri (1.6.7.2)
mini_portile2 (~> 2.0.0.rc2)
oj (3.3.5)
passenger (4.0.24)
daemon_controller (>= 1.1.0)
rack
Expand Down Expand Up @@ -202,12 +206,14 @@ DEPENDENCIES
comma (= 3.0.4)
database_cleaner
dynamic_form
jbuilder
json
json-schema
memoist
multi_json (= 1.3.6)
narray (= 0.6.0.4)
nokogiri
oj
passenger
pg
prototype-rails
Expand All @@ -234,4 +240,4 @@ DEPENDENCIES
yard

BUNDLED WITH
1.14.5
1.15.3
79 changes: 79 additions & 0 deletions app/views/api/v1/base/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
json.data @row_set.each do |row|

view_url = if !row.instance_of?(TraitsAndYieldsView)
polymorphic_url(row)
else
if row.result_type =~ /traits/
"#{request.base_url}/traits/#{row.id}"
elsif row.result_type =~ /yields/
"#{request.base_url}/yields/#{row.id}"
end
end

edit_url = if !row.instance_of?(TraitsAndYieldsView)
edit_polymorphic_url(row)
else
if row.result_type =~ /traits/
"#{request.base_url}/traits/#{row.id}/edit"
elsif row.result_type =~ /yields/
"#{request.base_url}/yields/#{row.id}/edit"
end
end

children = row.class.reflect_on_all_associations(:belongs_to)

multiple_associations = row.class.reflect_on_all_associations(:has_many)

# List of join tables that don't add any useful information
excluded_join_tables = [:citation_sites, # excludes citations_sites from both citation and site display
:citation_treatments, # excludes citations_treatments from citation display
:citations_treatments, # excludes citations_treatments from treatment display
:managements_treatments, # excludes managements_treatments from both management and treatment display
:posteriors_ensembles, # this association is not working
:pfts_priors, # excludes pfts_priors from both pfts and priors display
:pfts_species, # excludes pfts_species from both pfts and species display
:sitegroups_sites # excludes sitegroups_sites from sites display
]

# Don't display join table information if no useful information is to be had
multiple_associations.reject! { |assoc| excluded_join_tables.include?(assoc.name) }

json.traits_and_yields_view do
json.(row, *row.class.column_names.map(&:to_sym))
json.view_url view_url
json.edit_url edit_url

case @associations_mode
when :count
(multiple_associations).each do |assoc|
json.set! "number of associated #{assoc.name.to_s}" do
begin
row.send(assoc.name).size
rescue => e
Rails.logger.debug("Exception: #{e.message}")
Rails.logger.debug("Couldn't send #{assoc.name.inspect} to #{row.inspect}")
end
end
end
when :ids
(multiple_associations).each do |assoc|
json.set! "associated #{assoc.name.to_s.singularize} ids" do
begin
row.send(assoc.name).map(&:id)
rescue => e
Rails.logger.debug("Exception: #{e.message}")
Rails.logger.debug("Couldn't send #{assoc.name.inspect} to #{row.inspect}")
end
end
end
when :full_info
(children + multiple_associations).each do |assoc|
next if assoc.klass == User
json.set! assoc.name do
josn.(attributes *assoc.klass.column_names.map(&:to_sym))
end
end
end

end
end
8 changes: 4 additions & 4 deletions app/views/api/v1/base/show.rabl
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ if !root_object.nil?
polymorphic_url(ob)
else
if ob.result_type =~ /traits/
trait_url(Trait.find(ob.id))
trait_url(ob.id)
elsif ob.result_type =~ /yields/
yield_url(Yield.find(ob.id))
yield_url(ob.id)
end
end
end
Expand All @@ -77,9 +77,9 @@ if !root_object.nil?
edit_polymorphic_url(ob)
else
if ob.result_type =~ /traits/
edit_trait_url(Trait.find(ob.id))
edit_trait_url(ob.id)
elsif ob.result_type =~ /yields/
edit_yield_url(Yield.find(ob.id))
edit_yield_url(ob.id)
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions app/views/layouts/api/base.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
if controller.request.format == 'xml'
data_from_template = Hash.from_xml(yield)
else # JSON
require 'yajl'
require 'oj'
data_from_template = Yajl::Parser.parse(yield)
end

Expand All @@ -30,7 +30,7 @@

# Add data from index or show template if those actions were called:
if !@row_set.nil? || [email protected]?
data_hash[:data] = data_from_template
data_hash.merge!(data_from_template)
end

# Don't show extraneous keys--just delete if the value is empty:
Expand All @@ -49,6 +49,6 @@
if controller.request.format == 'xml'
raw(data_hash.to_xml)
else
raw(Yajl::Encoder.new(pretty: true).encode(data_hash))
raw(Oj.dump(data_hash))
end
%>