From 7aac4f7e72be1f149b4aa1d9d67517f6e082625c Mon Sep 17 00:00:00 2001 From: Donovan Daniels Date: Thu, 24 Oct 2024 01:36:13 -0500 Subject: [PATCH] Track times individual images have been seen --- .../yiff_rest/api_v2/images_controller.rb | 9 +++++++- .../yiff_rest/api_v2_controller.rb | 7 ++++++ app/models/api_image.rb | 22 +++++++++++++++++++ .../api_v2/images/_external_image.html.erb | 9 ++++++-- .../yiff_rest/api_v2/images/_image.html.erb | 3 ++- .../yiff_rest/api_v2/images/index.html.erb | 2 +- .../api_v2/images/new_external.html.erb | 2 +- .../yiff_rest/api_v2/manage/_nav.html.erb | 2 +- config/routes/yiff_rest_routes.rb | 1 + 9 files changed, 50 insertions(+), 7 deletions(-) diff --git a/app/controllers/yiff_rest/api_v2/images_controller.rb b/app/controllers/yiff_rest/api_v2/images_controller.rb index 438d3e4..e20a7b2 100644 --- a/app/controllers/yiff_rest/api_v2/images_controller.rb +++ b/app/controllers/yiff_rest/api_v2/images_controller.rb @@ -89,7 +89,6 @@ module YiffRest end def iqdb - @sp = {} end def query_iqdb @@ -108,6 +107,14 @@ module YiffRest render(:index) end + def leaderboard + @list = APIImage.all_seen.sort_by(&:second).reverse.to_h + @keys = @list.map(&:first) + @results = APIImage.where(id: @keys).sort_by { |img| @keys.index(img.md5) } + @pagy, @images = pagy_array(@results, size: [1, 2, 2, 1]) + render(:index) + end + private def load_category diff --git a/app/controllers/yiff_rest/api_v2_controller.rb b/app/controllers/yiff_rest/api_v2_controller.rb index 3677be1..75a12f9 100644 --- a/app/controllers/yiff_rest/api_v2_controller.rb +++ b/app/controllers/yiff_rest/api_v2_controller.rb @@ -32,6 +32,9 @@ module YiffRest r.incr("yiffy2:stats:images:ip:#{request.remote_ip}:#{category}") r.incr("yiffy2:stats:images:total") r.incr("yiffy2:stats:images:total:#{category}") + images.each do |img| + r.incr("yiffy2:stats:image:#{img.md5}") + end if @apikey.present? && !@apikey&.is_anon? r.incr("yiffy2:stats:images:key:#{@apikey.id}") r.incr("yiffy2:stats:images:key:#{@apikey.id}:#{category}") @@ -123,6 +126,9 @@ module YiffRest r.incr("yiffy2:stats:images:total:bulk") r.incrby("yiffy2:stats:images:key:#{@apikey.id}", total) r.incr("yiffy2:stats:images:key:#{@apikey.id}:bulk") + images.each do |img| + r.incr("yiffy2:stats:image:#{img.md5}") + end req.each do |category, amount| r.incrby("yiffy2:stats:images:#{category}", amount.to_i) r.incr("yiffy2:stats:images:#{category}:bulk") @@ -168,6 +174,7 @@ module YiffRest end def execute_webhook(category, bulk_categories = nil) + return unless Rails.env.production? bulk = "" color = 0x008000 if bulk_categories.present? diff --git a/app/models/api_image.rb b/app/models/api_image.rb index c91a1a0..dbd1a25 100644 --- a/app/models/api_image.rb +++ b/app/models/api_image.rb @@ -177,6 +177,7 @@ class APIImage < ApplicationRecord shortURL: short_url, external: true, viewable: is_viewable?, + seen: seen, } end @@ -198,6 +199,7 @@ class APIImage < ApplicationRecord shortURL: short_url, external: false, viewable: is_viewable?, + seen: seen, } end @@ -457,6 +459,26 @@ class APIImage < ApplicationRecord IqdbRemoveJob.perform_now(iqdb_id) end + def seen + Cache.fetch("img:#{md5}", expires_in: 1.minute) do + (Cache.redis.get("yiffy2:stats:image:#{md5}").presence || 0).to_i + end + end + + def self.all_seen + Cache.fetch("img:all_seen", expires_in: 10.minutes) do + keys = all.map { |img| "yiffy2:stats:image:#{img.md5}" } + values = Cache.redis.mget(*keys) + results = {} + keys.each_with_index do |key, index| + val = values.at(index) + next if val.nil? + results[key[-32..]] = val.to_i + end + results + end + end + def self.create_external!(type, id, category) site = ExternalAPIImage::SITES.find { |s| s.value == type } raise(Error, "Invalid site: #{type}") if site.blank? diff --git a/app/views/yiff_rest/api_v2/images/_external_image.html.erb b/app/views/yiff_rest/api_v2/images/_external_image.html.erb index ee8ee47..0b6896e 100644 --- a/app/views/yiff_rest/api_v2/images/_external_image.html.erb +++ b/app/views/yiff_rest/api_v2/images/_external_image.html.erb @@ -14,7 +14,7 @@ diff --git a/app/views/yiff_rest/api_v2/images/_image.html.erb b/app/views/yiff_rest/api_v2/images/_image.html.erb index 95e931e..04b77d1 100644 --- a/app/views/yiff_rest/api_v2/images/_image.html.erb +++ b/app/views/yiff_rest/api_v2/images/_image.html.erb @@ -15,7 +15,7 @@
  • Creator: <%= image.creator.name %>
  • Created At: <%= compact_time(image.created_at) %>
  • Updated At: <%= compact_time(image.updated_at) %>
  • - <% unless @sp[:category].present? %> + <% unless @sp.try(:[], :category).present? %>
  • Category: <%= image.category %>
  • <% end %>
  • @@ -27,6 +27,7 @@ <% end %>
  • Viewable: "><%= image.is_viewable? ? "Yes" : "No" %>
  • +
  • Times Seen: <%= image.seen %>
  • diff --git a/app/views/yiff_rest/api_v2/images/index.html.erb b/app/views/yiff_rest/api_v2/images/index.html.erb index 2e4f138..176f54d 100644 --- a/app/views/yiff_rest/api_v2/images/index.html.erb +++ b/app/views/yiff_rest/api_v2/images/index.html.erb @@ -26,7 +26,7 @@
    -
    +
    <%== pagy_bootstrap_nav(@pagy) if @pagy.pages > 1 %>
    diff --git a/app/views/yiff_rest/api_v2/images/new_external.html.erb b/app/views/yiff_rest/api_v2/images/new_external.html.erb index a3c6464..5fb4448 100644 --- a/app/views/yiff_rest/api_v2/images/new_external.html.erb +++ b/app/views/yiff_rest/api_v2/images/new_external.html.erb @@ -9,7 +9,7 @@
    <%= simple_form_for(:api_image, url: create_external_yiff_rest_api_v2_manage_images_path, method: :post) do |f| %> <%= f.input(:site, label: "Site", as: :select, selected: params.dig(:api_image, :site), collection: ExternalAPIImage::SITES.map { |s| [s.name, s.value] }, include_blank: true) %> - <%= f.input(:external_id, input_html: { value: params.dig(:api_image, :external_id) }) %> + <%= f.input(:external_id, label: "External ID", input_html: { value: params.dig(:api_image, :external_id) }) %> <%= f.input(:category, as: :select, selected: params.dig(:api_image, :category), collection: @categories.map { |cat| [cat.name, cat.db] }, include_blank: true) %> <%= f.button(:submit, "Upload Image", name: nil) %> <% end %> diff --git a/app/views/yiff_rest/api_v2/manage/_nav.html.erb b/app/views/yiff_rest/api_v2/manage/_nav.html.erb index ac62d15..bb3489b 100644 --- a/app/views/yiff_rest/api_v2/manage/_nav.html.erb +++ b/app/views/yiff_rest/api_v2/manage/_nav.html.erb @@ -18,7 +18,7 @@ <% elsif params[:controller] == "yiff_rest/api_v2/images" %> <% if %w[index iqdb query_iqdb].include?(params[:action]) %> <% else %> <% category = params.dig(:api_image, :category) if params[:action] == "new" %> diff --git a/config/routes/yiff_rest_routes.rb b/config/routes/yiff_rest_routes.rb index 477c858..2d10fed 100644 --- a/config/routes/yiff_rest_routes.rb +++ b/config/routes/yiff_rest_routes.rb @@ -60,6 +60,7 @@ module YiffRestRoutes put(:update_cache) end collection do + get(:leaderboard) get(:iqdb) match("/iqdb/query", via: %i[get post], action: :query_iqdb) post(:external, action: :create_external, as: "create_external")