1class TrendingIpsByPetitionJob < ApplicationJob
 
2  delegate :enable_logging_of_trending_items?, to: :rate_limit
 
3  delegate :threshold_for_logging_trending_items, to: :rate_limit
 
4  delegate :threshold_for_notifying_trending_items, to: :rate_limit
 
5  delegate :ignore_ip?, to: :rate_limit
 
6  delegate :ignored_domains_list, to: :rate_limit
 
7  delegate :trending_ips_by_petition, to: :Signature
 
8
  • NestedIterators - contains iterators nested 2 deep » reek
  • TooManyStatements - has approx 10 statements » reek
  • UncommunicativeVariableName - has the variable name 'e' » reek
  • Complexity 8 » saikuro
 9  def perform(now = Time.current)
 
10    return unless enable_logging_of_trending_items?
 
  • Block cyclomatic complexity is 5. It should be 4 or less. » roodi
12    trending_ips(now).each do |id, ips|
 
13      petition = Petition.find(id)
 
 
15      ips.each do |ip, count|
 
16        next unless ip.present?
 
17        next if ignore_ip?(ip)
 
 
19        begin
 
20          trending_ip = petition.trending_ips.log!(starts_at(now), ip, count)
 
 
22          if count >= threshold_for_notifying_trending_items
 
23            NotifyTrendingIpJob.perform_later(trending_ip)
 
24          end
 
25        rescue StandardError => e
 
26          Appsignal.send_exception e
 
27        end
 
28      end
 
29    end
 
30  end
 
 
32  private
 
  • Complexity 1 » saikuro
34  def rate_limit
 
35    @rate_limit ||= RateLimit.first_or_create!
 
36  end
 
  • Complexity 1 » saikuro
38  def trending_ips(now)
 
39    trending_ips_by_petition(window(now), threshold_for_logging_trending_items, ignored_domains_list)
 
40  end
 
  • Complexity 1 » saikuro
42  def petitions
 
43    Petition.where(id: petition_ids)
 
44  end
 
  • Complexity 1 » saikuro
46  def window(now)
 
47    starts_at(now)..ends_at(now)
 
48  end
 
  • Complexity 1 » saikuro
50  def starts_at(now)
 
51    @starts_at ||= ends_at(now).advance(hours: -1)
 
52  end
 
  • Complexity 1 » saikuro
54  def ends_at(now)
 
55    @ends_at ||= now.at_beginning_of_hour
 
56  end
 
57end