1class TrendingDomainsByPetitionJob < 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_domain?, to: :rate_limit
 
6  delegate :trending_domains_by_petition, to: :Signature
 
7
  • NestedIterators - contains iterators nested 2 deep » reek
  • TooManyStatements - has approx 10 statements » reek
  • UncommunicativeVariableName - has the variable name 'e' » reek
  • Complexity 8 » saikuro
8  def perform(now = Time.current)
 
 9    return unless enable_logging_of_trending_items?
 
  • Block cyclomatic complexity is 5. It should be 4 or less. » roodi
11    trending_domains(now).each do |id, domains|
 
12      petition = Petition.find(id)
 
 
14      domains.each do |domain, count|
 
15        next unless domain.present?
 
16        next if ignore_domain?(domain)
 
 
18        begin
 
19          trending_domain = petition.trending_domains.log!(starts_at(now), domain, count)
 
 
21          if count >= threshold_for_notifying_trending_items
 
22            NotifyTrendingDomainJob.perform_later(trending_domain)
 
23          end
 
24        rescue StandardError => e
 
25          Appsignal.send_exception e
 
26        end
 
27      end
 
28    end
 
29  end
 
 
31  private
 
  • Complexity 1 » saikuro
33  def rate_limit
 
34    @rate_limit ||= RateLimit.first_or_create!
 
35  end
 
  • Complexity 1 » saikuro
37  def trending_domains(now)
 
38    trending_domains_by_petition(window(now), threshold_for_logging_trending_items)
 
39  end
 
  • Complexity 1 » saikuro
41  def petitions
 
42    Petition.where(id: petition_ids)
 
43  end
 
  • Complexity 1 » saikuro
45  def window(now)
 
46    starts_at(now)..ends_at(now)
 
47  end
 
  • Complexity 1 » saikuro
49  def starts_at(now)
 
50    @starts_at ||= ends_at(now).advance(hours: -1)
 
51  end
 
  • Complexity 1 » saikuro
53  def ends_at(now)
 
54    @ends_at ||= now.at_beginning_of_hour
 
55  end
 
56end