Skip to content

Commit

Permalink
optimised welcome page load time:
Browse files Browse the repository at this point in the history
- optimized surprise author fetching
- optimized fetching of text totals by periods
- optimized fetching of authors totals by genres
  • Loading branch information
damisul committed Apr 26, 2024
1 parent e86cdf4 commit 8a29935
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 21 deletions.
19 changes: 11 additions & 8 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,14 @@ def randomize_works(how_many)
end

def cached_authors_in_genre
Rails.cache.fetch("au_by_genre", expires_in: 24.hours) do # memoize
ret = {}
get_genres.each { |g| ret[g] = Person.has_toc.joins(expressions: :work).where(works: { genre: g }).uniq.count }
ret
Rails.cache.fetch('au_by_genre', expires_in: 24.hours) do # memoize
totals = Person.has_toc
.joins(expressions: :work)
.group(:genre)
.distinct
.count
.to_h
get_genres.index_with { |genre| totals[genre] || 0 }
end
end

Expand All @@ -168,10 +172,9 @@ def cached_authors_in_period
end

def cached_works_by_period
Rails.cache.fetch("works_by_period", expires_in: 24.hours) do # memoize
ret = {}
get_periods.each{ |p| ret[p] = Manifestation.published.joins(:expression).where(expressions: { period: p}).uniq.count}
ret
Rails.cache.fetch('works_by_period', expires_in: 24.hours) do # memoize
totals = Manifestation.published.joins(:expression).group(:period).count
get_periods.index_with { |period| totals[Expression.periods[period]] || 0 }
end
end

Expand Down
20 changes: 10 additions & 10 deletions app/controllers/authors_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

class AuthorsController < ApplicationController
include FilteringAndPaginationConcern
include AuthorsHelper

before_action only: [:new, :publish, :create, :show, :edit, :list, :add_link, :delete_link, :delete_photo, :edit_toc, :update, :to_manual_toc] do |c| c.require_editor('edit_people') end
autocomplete :tag, :name, :limit => 2
Expand Down Expand Up @@ -35,17 +36,16 @@ def publish
end

def get_random_author
rel = Person.has_toc.
joins(creations: { work: { expressions: :manifestations } }).
merge(Manifestation.published).
merge(Creation.author)

genre = params[:genre]
if genre.present?
rel = rel.where(works: { genre: genre })
end
@author = rel.distinct.order(Arel.sql('rand()')).first
render partial: 'shared/surprise_author', locals: {author: @author, initial: false, id_frag: params[:id_frag], passed_genre: genre, passed_mode: params[:mode], side: params[:side]}
render partial: 'shared/surprise_author',
locals: {
author: random_author(genre),
initial: false,
id_frag: params[:id_frag],
passed_genre: genre,
passed_mode: params[:mode],
side: params[:side]
}
end

def delete_photo
Expand Down
5 changes: 3 additions & 2 deletions app/controllers/welcome_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
include BybeUtils
class WelcomeController < ApplicationController
# impressionist # log actions for pageview stats
# impressionist # log actions for pageview stats
include AuthorsHelper

def index
@tabclass = set_tab('home')
Expand All @@ -14,7 +15,7 @@ def index
# @newest_authors = cached_newest_authors # deprecated because we stopped producing portraits
@random_authors = Person.published.has_image.order(Arel.sql('RAND()')).limit(10)
@newest_works = cached_newest_works
@surprise_author = Person.where(id: Person.has_toc.pluck(:id).sample(1))[0]
@surprise_author = random_author
@surprise_work = randomize_works(1)[0]
@authors_in_genre = cached_authors_in_genre
@works_by_genre = Manifestation.cached_work_counts_by_genre
Expand Down
23 changes: 22 additions & 1 deletion app/helpers/authors_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,25 @@ def browse_upload_date(item)
def browse_null_decorator(item)
return ''
end
end

def random_author(genre = nil)
relation = Person.has_toc
.where(
<<~SQL.squish,
exists (
select 1 from
creations c
join works w on (c.work_id = w.id)
join expressions e on (e.work_id = w.id)
join manifestations m on (m.expression_id = e.id)
where
m.status = ?
and w.genre = coalesce(?, w.genre)
)
SQL
Manifestation.statuses[:published],
genre
)
relation.order(Arel.sql('rand()')).first
end
end

0 comments on commit 8a29935

Please sign in to comment.