/posts: fix blank tag sidebars.

Fix the sidebar on the /posts index page sometimes being blank. This
could happen when either the related tag calculation was too slow and
timed out, or when Reporbooru was unavailable and we couldn't fetch the
list of popular tags.

In the tag list would otherwise be blank, we fall back to frequent tags
(the most common tags on the current page of results).

Also change it so that if Reportbooru is unconfigured, we fail
gracefully by returning blank results instead of failing with an
exception. This is so we can still view the popular searches and missed
searches pages during testing (even though they'll be blank).
This commit is contained in:
evazion
2020-06-16 12:44:51 -05:00
parent ed152a780b
commit 101e8a13b8
4 changed files with 10 additions and 23 deletions

View File

@@ -160,20 +160,16 @@ module PostSets
elsif query.is_metatag?(:search)
saved_search_tags
elsif query.is_empty_search? || query.is_metatag?(:order, :rank)
popular_tags
popular_tags.presence || frequent_tags
elsif query.is_single_term?
similar_tags
similar_tags.presence || frequent_tags
else
frequent_tags
end
end
def popular_tags
if reportbooru_service.enabled?
reportbooru_service.popular_searches(Date.today, limit: MAX_SIDEBAR_TAGS).map(&:first)
else
frequent_tags
end
ReportbooruService.new.popular_searches(Date.today, limit: MAX_SIDEBAR_TAGS)
end
def similar_tags
@@ -199,10 +195,6 @@ module PostSets
def tag_list_html(**options)
tag_set_presenter.tag_list_html(name_only: query.is_metatag?(:search), **options)
end
def reportbooru_service
@reportbooru_service ||= ReportbooruService.new
end
end
end
end

View File

@@ -11,7 +11,7 @@ class ReportbooruService
end
def missed_search_rankings(expires_in: 1.minutes)
raise NotImplementedError, "Reportbooru not configured, missed searches not available." unless enabled?
return [] unless enabled?
response = http.cache(expires_in).get("#{reportbooru_server}/missed_searches")
return [] if response.status != 200
@@ -21,7 +21,7 @@ class ReportbooruService
end
def post_search_rankings(date = Date.today, expires_in: 1.minutes)
raise NotImplementedError, "Reportbooru not configured, popular searches not available." unless enabled?
return [] unless enabled?
response = http.cache(expires_in).get("#{reportbooru_server}/post_searches/rank?date=#{date}")
return [] if response.status != 200
@@ -29,7 +29,7 @@ class ReportbooruService
end
def post_view_rankings(date = Date.today, expires_in: 1.minutes)
raise NotImplementedError, "Reportbooru not configured, post views not available." unless enabled?
return [] unless enabled?
response = http.get("#{reportbooru_server}/post_views/rank?date=#{date}")
return [] if response.status != 200
@@ -38,7 +38,6 @@ class ReportbooruService
def popular_searches(date = Date.today, limit: 100)
ranking = post_search_rankings(date)
ranking = post_search_rankings(date.yesterday) if ranking.blank?
ranking.take(limit).map(&:first)
end

View File

@@ -5,10 +5,8 @@
<li id="secondary-links-posts-hot"><%= link_to "Hot", posts_path(:tags => "order:rank") %></li>
<li id="secondary-links-posts-popular"><%= link_to "Popular", popular_explore_posts_path %></li>
<li id="secondary-links-posts-curated"><%= link_to "Curated", curated_explore_posts_path %></li>
<% if ReportbooruService.enabled? %>
<li><%= link_to "Searches", searches_explore_posts_path %></li>
<li><%= link_to "Viewed", viewed_explore_posts_path %></li>
<% end %>
<li><%= link_to "Searches", searches_explore_posts_path %></li>
<li><%= link_to "Viewed", viewed_explore_posts_path %></li>
<% end %>
<li><%= link_to "Deleted", posts_path(tags: "#{params[:tags]} status:deleted") %></li>

View File

@@ -72,10 +72,8 @@
<ul>
<li><h1>Reports</h1></li>
<li><%= link_to("Performance Reports", "https://isshiki.donmai.us/user-reports") %></li>
<% if Danbooru.config.reportbooru_server %>
<li><%= link_to("Top Searches", searches_explore_posts_path) %></li>
<li><%= link_to("Missed Searches", missed_searches_explore_posts_path) %></li>
<% end %>
<li><%= link_to("Top Searches", searches_explore_posts_path) %></li>
<li><%= link_to("Missed Searches", missed_searches_explore_posts_path) %></li>
</ul>
</section>
<section>