From f87c71cf23cdd57699f13f4bbebcc2895f8d64de Mon Sep 17 00:00:00 2001 From: r888888888 Date: Mon, 27 Jul 2015 17:21:17 -0700 Subject: [PATCH] remove post view counts, add search counts --- app/assets/javascripts/posts.js | 20 ----- app/controllers/explore/posts_controller.rb | 7 +- app/helpers/posts_helper.rb | 22 +++--- app/logical/popular_search_service.rb | 31 ++++++++ app/logical/post_sets/popular_view.rb | 73 ------------------- .../post_set_presenters/popular_view.rb | 38 ---------- app/views/explore/posts/popular_view.html.erb | 17 ----- app/views/explore/posts/searches.html.erb | 43 +++++++++++ app/views/posts/index.html.erb | 1 + .../_search_count.html.erb} | 0 .../posts/partials/show/_information.html.erb | 3 - app/views/posts/show.html.erb | 4 +- config/danbooru_default_config.rb | 2 +- config/routes.rb | 2 +- 14 files changed, 93 insertions(+), 170 deletions(-) create mode 100644 app/logical/popular_search_service.rb delete mode 100644 app/logical/post_sets/popular_view.rb delete mode 100644 app/presenters/post_set_presenters/popular_view.rb delete mode 100644 app/views/explore/posts/popular_view.html.erb create mode 100644 app/views/explore/posts/searches.html.erb rename app/views/posts/partials/{show/_view_count.html.erb => index/_search_count.html.erb} (100%) diff --git a/app/assets/javascripts/posts.js b/app/assets/javascripts/posts.js index eca330c26..0102ee093 100644 --- a/app/assets/javascripts/posts.js +++ b/app/assets/javascripts/posts.js @@ -25,7 +25,6 @@ this.initialize_post_image_resize_links(); this.initialize_post_image_resize_to_window_link(); this.initialize_similar(); - this.initialize_view_count(); if (Danbooru.meta("always-resize-images") === "true") { $("#image-resize-to-window-link").click(); @@ -536,25 +535,6 @@ } }); } - - Danbooru.Post.initialize_view_count = function() { - if ($("#views-for-post").length) { - $("#views-for-post-li").hide(); - var current_post_id = $("meta[name=post-id]").attr("content"); - $.ajax({ - url: Danbooru.meta("report-server") + "/hits/pv-" + current_post_id, - success: function(data) { - $("#views-for-post").html(data); - $("#views-for-post-li").show(); - }, - dataType: "text" - }); - } - } - - Danbooru.Post.set_view_count = function(count) { - $("#views-for-post").html(count); - } })(); $(document).ready(function() { diff --git a/app/controllers/explore/posts_controller.rb b/app/controllers/explore/posts_controller.rb index 064831647..c0f2bfe5a 100644 --- a/app/controllers/explore/posts_controller.rb +++ b/app/controllers/explore/posts_controller.rb @@ -8,10 +8,9 @@ module Explore respond_with(@posts) end - def popular_view - @post_set = PostSets::PopularView.new(params[:date], params[:scale]) - @posts = @post_set.posts - respond_with(@posts) + def searches + @date = params[:date] ? Date.parse(params[:date]) : Date.today + @search_service = PopularSearchService.new(@date, params[:scale] || "day") end def intro diff --git a/app/helpers/posts_helper.rb b/app/helpers/posts_helper.rb index d6f4eaeb6..eb47ef711 100644 --- a/app/helpers/posts_helper.rb +++ b/app/helpers/posts_helper.rb @@ -1,15 +1,17 @@ module PostsHelper - def post_view_count_js - return nil unless Danbooru.config.enable_view_counts + def post_search_count_js + return nil unless Danbooru.config.enable_post_search_counts - if action_name == "index" - return nil - elsif action_name == "show" - key = "show-#{params[:id]}" - value = session.id - digest = OpenSSL::Digest.new("sha256") - sig = OpenSSL::HMAC.hexdigest(digest, Danbooru.config.shared_remote_key, "#{key},#{value}") - return render("posts/partials/show/view_count", key: key, value: value, sig: sig) + if action_name == "index" && params[:page].nil? + tags = Tag.scan_query(params[:tags]).sort.join(" ") + + if tags.present? + key = "ps-#{tags}" + value = session.id + digest = OpenSSL::Digest.new("sha256") + sig = OpenSSL::HMAC.hexdigest(digest, Danbooru.config.shared_remote_key, "#{key},#{value}") + return render("posts/partials/index/search_count", key: key, value: value, sig: sig) + end end return nil diff --git a/app/logical/popular_search_service.rb b/app/logical/popular_search_service.rb new file mode 100644 index 000000000..9650ad517 --- /dev/null +++ b/app/logical/popular_search_service.rb @@ -0,0 +1,31 @@ +# queries reportbooru to find popular post searches +class PopularSearchService + attr_reader :date, :scale + + def initialize(date, scale) + @date = date + @scale = scale + end + + def each_search(&block) + fetch_data.scan(/\S+/).in_groups_of(2).each(&block) + end + + def fetch_data + dates = date.strftime("%Y-%m-%d") + + Cache.get("ps-#{scale}-#{dates}", 1.minute) do + url = URI.parse("#{Danbooru.config.report_server}/hits/#{scale}?date=#{dates}") + response = [] + Net::HTTP.start(url.host, url.port, :use_ssl => url.is_a?(URI::HTTPS)) do |http| + http.read_timeout = 1 + http.request_get(url.request_uri) do |res| + if res.is_a?(Net::HTTPSuccess) + response = res.body + end + end + end + response + end + end +end \ No newline at end of file diff --git a/app/logical/post_sets/popular_view.rb b/app/logical/post_sets/popular_view.rb deleted file mode 100644 index bf68b03ba..000000000 --- a/app/logical/post_sets/popular_view.rb +++ /dev/null @@ -1,73 +0,0 @@ -module PostSets - class PopularView < PostSets::Base - attr_reader :date, :scale - - def initialize(date, scale) - @date = date.blank? ? Time.zone.now : Time.zone.parse(date) - @scale = scale || "Day" - end - - def posts - @posts ||= begin - array = fetch_data.scan(/\S+/).in_groups_of(2) - post_ids = array.map(&:first) - posts = ::Post.where(id: post_ids).order(arbitrary_sql_order_clause(post_ids, "posts")) - posts.each.with_index do |post, i| - post.view_count = array[i][1].to_i - end - posts - end - end - - def min_date - case scale - when "week" - date.beginning_of_week - - when "month" - date.beginning_of_month - - else - date - end - end - - def max_date - case scale - when "week" - date.end_of_week - - when "month" - date.end_of_month - - else - date - end - end - - def source - "popular_by_#{scale.downcase}" - end - - def fetch_data - dates = date.strftime('%Y-%m-%d') - Cache.get("pv-#{scale}-#{dates}", 1.minute) do - url = URI.parse("#{Danbooru.config.report_server}/hits/#{source}?date=#{dates}") - response = [] - Net::HTTP.start(url.host, url.port, :use_ssl => url.is_a?(URI::HTTPS)) do |http| - http.read_timeout = 1 - http.request_get(url.request_uri) do |res| - if res.is_a?(Net::HTTPSuccess) - response = res.body - end - end - end - response - end - end - - def presenter - ::PostSetPresenters::Popular.new(self) - end - end -end diff --git a/app/presenters/post_set_presenters/popular_view.rb b/app/presenters/post_set_presenters/popular_view.rb deleted file mode 100644 index 736ad4301..000000000 --- a/app/presenters/post_set_presenters/popular_view.rb +++ /dev/null @@ -1,38 +0,0 @@ -module PostSetPresenters - class PopularView < Popular - def initialize(post_set) - @post_set = post_set - end - - def nav_links_for_scale(template, scale) - html = [] - html << '' - html << template.link_to( - "«prev".html_safe, - template.popular_explore_post_views_path( - :date => prev_date_for_scale(scale), - :scale => scale.downcase - ), - :rel => (link_rel_for_scale?(template, scale.downcase) ? "prev" : nil) - ) - html << template.link_to( - scale, - template.popular_explore_post_views_path( - :date => date, - :scale => scale.downcase - ), - :class => "desc" - ) - html << template.link_to( - "next»".html_safe, - template.popular_explore_post_views_path( - :date => next_date_for_scale(scale), - :scale => scale.downcase - ), - :rel => (link_rel_for_scale?(template, scale.downcase) ? "next" : nil) - ) - html << '' - html.join("\n").html_safe - end - end -end diff --git a/app/views/explore/posts/popular_view.html.erb b/app/views/explore/posts/popular_view.html.erb deleted file mode 100644 index 630d55452..000000000 --- a/app/views/explore/posts/popular_view.html.erb +++ /dev/null @@ -1,17 +0,0 @@ -
-
-

Popular (by views): <%= @post_set.presenter.range_text %>

- - <%= @post_set.presenter.nav_links(self) %> - - <%= render "posts/partials/common/inline_blacklist" %> - - <%= @post_set.presenter.post_previews_html(self) %> -
-
- -<%= render "posts/partials/common/secondary_links" %> - -<% content_for(:page_title) do %> - Popular (by views) - <%= Danbooru.config.app_name %> -<% end %> diff --git a/app/views/explore/posts/searches.html.erb b/app/views/explore/posts/searches.html.erb new file mode 100644 index 000000000..abb2d6a0b --- /dev/null +++ b/app/views/explore/posts/searches.html.erb @@ -0,0 +1,43 @@ +
+
+

Popular Searches: <%= @search_service.date %> (by <%= @search_service.scale %>)

+ + + + + + + + + + <% @search_service.each_search do |tags, count| %> + + + + + <% end %> + +
TagsCount
<%= link_to tags, posts_path(:tags => tags) %><%= count.to_i %>
+ +

+ View by + <%= link_to "day", searches_explore_posts_path(:date => params[:date], :scale => "day") %> | + <%= link_to "week", searches_explore_posts_path(:date => params[:date], :scale => "week") %> | + <%= link_to "year", searches_explore_posts_path(:date => params[:date], :scale => "year") %>. + + Back one + <%= link_to "day", searches_explore_posts_path(:date => 1.day.ago(@date), :scale => params[:scale]) %> | + <%= link_to "week", searches_explore_posts_path(:date => 1.week.ago(@date), :scale => params[:scale]) %>. + + Forward one + <%= link_to "day", searches_explore_posts_path(:date => 1.day.since(@date), :scale => params[:scale]) %> | + <%= link_to "week", searches_explore_posts_path(:date => 1.week.since(@date), :scale => params[:scale]) %>. +

+
+
+ +<%= render "posts/partials/common/secondary_links" %> + +<% content_for(:page_title) do %> + Popular Searches - <%= Danbooru.config.app_name %> +<% end %> diff --git a/app/views/posts/index.html.erb b/app/views/posts/index.html.erb index df35d72d6..511e26e64 100644 --- a/app/views/posts/index.html.erb +++ b/app/views/posts/index.html.erb @@ -50,6 +50,7 @@ <%= render "posts/partials/common/secondary_links" %> + <%= post_search_count_js %> diff --git a/app/views/posts/partials/show/_view_count.html.erb b/app/views/posts/partials/index/_search_count.html.erb similarity index 100% rename from app/views/posts/partials/show/_view_count.html.erb rename to app/views/posts/partials/index/_search_count.html.erb diff --git a/app/views/posts/partials/show/_information.html.erb b/app/views/posts/partials/show/_information.html.erb index 379481def..fbd51e4f7 100644 --- a/app/views/posts/partials/show/_information.html.erb +++ b/app/views/posts/partials/show/_information.html.erb @@ -14,9 +14,6 @@
  • Source: <%= post_source_tag(post) %>
  • Rating: <%= post.pretty_rating %>
  • Score: <%= post.score %> <% if CurrentUser.is_gold? %>(vote <%= link_to "up", post_votes_path(:post_id => post.id, :score => "up"), :remote => true, :method => :post %>/<%= link_to "down", post_votes_path(:post_id => post.id, :score => "down"), :remote => true, :method => :post %><%= link_to "unvote", unvote_post_path(post), :remote => true, :method => :put, :id => "unvote-link-for-post-#{post.id}", :class => "unvote-post-link" %>)<% end %>
  • - <% if Danbooru.config.enable_view_counts %> -
  • Views: loading...
  • - <% end %>
  • Favorites: <%= post.fav_count %> <% if CurrentUser.is_gold? %> <%= link_to "Show »".html_safe, "#", :id => "show-favlist-link" %> diff --git a/app/views/posts/show.html.erb b/app/views/posts/show.html.erb index dd2f05756..223d317a9 100644 --- a/app/views/posts/show.html.erb +++ b/app/views/posts/show.html.erb @@ -139,8 +139,6 @@ -<%= post_view_count_js %> - <% content_for(:page_title) do %> <%= @post.presenter.humanized_essential_tag_string %> - <%= Danbooru.config.app_name %> <% end %> @@ -160,7 +158,7 @@ - <% if Danbooru.config.enable_view_counts %> + <% if Danbooru.config.enable_post_search_counts %> <% end %> diff --git a/config/danbooru_default_config.rb b/config/danbooru_default_config.rb index 5ce374804..524150df6 100644 --- a/config/danbooru_default_config.rb +++ b/config/danbooru_default_config.rb @@ -356,7 +356,7 @@ module Danbooru "https://isshiki.donmai.us" end - def enable_view_counts + def enable_post_search_counts false end end diff --git a/config/routes.rb b/config/routes.rb index d40a73fb3..32347eba8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -45,7 +45,7 @@ Rails.application.routes.draw do resources :posts do collection do get :popular - get :popular_view + get :searches get :hot get :intro end