Skip to content

Commit

Permalink
Added gem prosopite (#1260)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikalai-yankouski committed Mar 4, 2024
1 parent 23c0c2d commit 4bcf0b4
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -37,6 +37,7 @@ gem "flamegraph"
gem "memory_profiler"
gem "rack-mini-profiler"
gem "stackprof"
gem "prosopite"

gem "oauth" # for linking accounts
gem "mail" # for parsing incoming mail
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Expand Up @@ -203,6 +203,7 @@ GEM
hashery (~> 2.0)
ruby-rc4
ttfunk
prosopite (1.4.2)
psych (5.1.2)
stringio
public_suffix (5.0.4)
Expand Down Expand Up @@ -416,6 +417,7 @@ DEPENDENCIES
oauth
parslet
pdf-reader
prosopite
puma
rack-attack
rack-mini-profiler
Expand Down
8 changes: 8 additions & 0 deletions app/controllers/application_controller.rb
Expand Up @@ -9,6 +9,7 @@ class ApplicationController < ActionController::Base
before_action :mini_profiler
before_action :prepare_exception_notifier
before_action :set_traffic_style
around_action :n_plus_one_detection

# 2023-10-07 one user in one of their browser envs is getting a CSRF failure, I'm reverting
# because I'll be AFK a while.
Expand Down Expand Up @@ -188,4 +189,11 @@ def tags_filtered_by_cookie
tag: cookies[TAG_FILTER_COOKIE].to_s.split(",")
)
end

def n_plus_one_detection
Prosopite.scan
yield
ensure
Prosopite.finish
end
end
4 changes: 4 additions & 0 deletions app/models/story.rb
Expand Up @@ -415,6 +415,8 @@ def check_tags
return
end

# ignored to manage tags_a for nicer UI and because the n is typically 2-5 tags
Prosopite.pause
taggings.each do |t|
if !t.tag.can_be_applied_by?(u) && t.tag.privileged?
raise "#{u.username} does not have permission to use privileged tag #{t.tag.tag}"
Expand All @@ -429,6 +431,8 @@ def check_tags
end
end

Prosopite.resume

if taggings.reject { |t| t.marked_for_destruction? || t.tag.is_media? }.empty?
errors.add(:base, "Must have at least one non-media (PDF, video) " \
"tag. If no tags apply to your content, it probably doesn't " \
Expand Down
7 changes: 7 additions & 0 deletions config/initializers/prosopite.rb
@@ -0,0 +1,7 @@
# typed: false

if Rails.env.production?
Prosopite.custom_logger = Logger.new("/srv/lobste.rs/log/n_plus_one_detection.log")
else
Prosopite.raise = true
end

0 comments on commit 4bcf0b4

Please sign in to comment.