Skip to content

Commit

Permalink
Tighten up obs images query
Browse files Browse the repository at this point in the history
  • Loading branch information
nimmolo committed Jan 10, 2024
1 parent 01dab82 commit 1bf9f8d
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 10 deletions.
5 changes: 4 additions & 1 deletion app/controllers/names_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ def init_related_query_ivars
@has_subtaxa = 0
# Query for names of subtaxa, used in special query link
# Note this is only creating a schematic of a query, used in the link.

# Create query for immediate children.
@children_query = create_query(:Name, :all,
names: @name.id,
Expand Down Expand Up @@ -293,12 +294,14 @@ def init_related_query_ivars
# Don't run if there aren't any children.
@has_subtaxa = @first_child ? @subtaxa_query.select_count : 0
end

# NOTE: `_observation_menu` makes many select_count queries like this!
# That is where most of the heavy loading is. Check helpers/show_name_helper
#
# Third query (maybe combine with second)
@obss = Name::Observations.new(@name)
@best_images = @obss.with_images.take(6).map(&:thumb_image)
# This initiates a query for the images of only the most confident obs
@best_images = @obss.best_images

# This seems like it queries the NameDescription table.
# Would be better to eager load descriptions and derive @best_description
Expand Down
6 changes: 6 additions & 0 deletions app/models/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,12 @@ class Image < AbstractModel
after_update :track_copyright_changes
before_destroy :update_thumbnails

scope :interactive_includes, lambda {
strict_loading.includes(
:image_votes, :license, :projects, :user
)
}

# Array of all observations, users and glossary terms using this image.
def all_subjects
observations + profile_users + glossary_terms
Expand Down
6 changes: 3 additions & 3 deletions app/models/name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -667,14 +667,14 @@ class Name < AbstractModel
}
scope :show_includes, lambda {
strict_loading.includes(
:comments,
# :comments,
:correct_spelling,
{ description: [:authors, :reviewer] },
{ descriptions: [:authors, :editors, :reviewer, :writer_groups] },
{ interests: :user },
:misspellings,
{ namings: [:user] },
{ observations: [:location, :thumb_image, :user] },
# { namings: [:user] },
# { observations: [:location, :thumb_image, :user] },
:rss_log,
{ synonym: :names },
:user,
Expand Down
6 changes: 6 additions & 0 deletions app/models/name/observations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,11 @@ def where_name_proposed
def with_images
of_taxon_this_name.reject { |obs| obs&.thumb_image_id.nil? }
end

def best_images
image_ids = with_images.take(6).map(&:thumb_image_id)
# One new lookup for the images.
Image.interactive_includes.where(id: image_ids).order(vote_cache: :desc)
end
end
end
6 changes: 0 additions & 6 deletions app/views/controllers/names/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,13 @@ add_tab_set(name_show_tabs(name: @name))
<% end %>
<% footer = tag.div(id: "name_previous_export") do
puts("show_previous_version")
concat(show_previous_version(@name, @versions))
puts("show_previous_version")
concat(export_status_controls(@name))
end %>
<%= panel_block(id: "name_footer", footer: footer) do
concat(tag.div(id: "name_authors_editors") do
puts("show_authors_and_editors")
show_authors_and_editors(obj: @name, versions: @versions, user: @user)
puts("show_authors_and_editors")
end)
concat(:show_name_num_notifications.t(num: @name.interests))
end %>
Expand All @@ -68,7 +64,5 @@ add_tab_set(name_show_tabs(name: @name))
</div><!--.row-->

<%=
puts("show_object_footer")
show_object_footer(@name, @versions)
puts("show_object_footer")
%>

0 comments on commit 1bf9f8d

Please sign in to comment.