<commit_before># frozen_string_literal: true

class SweepSubscriptionsJob < ApplicationJob
  queue_as :default

  def perform
    start_time = Time.current
    report = ''
    report += "Started job at #{start_time}\n"

    problem_subs = []
    obsolete_subs = Subscription.joins('join efforts on efforts.id = subscriptions.subscribable_id join events on events.id = efforts.event_id')
                      .where("subscriptions.subscribable_type = 'Effort' and events.start_time < ?", 1.year.ago)

    count = obsolete_subs.count
    if count == 0
      report += "No obsolete subscriptions found\n"
    else
      report += "Found #{count} obsolete subscriptions\n"

      obsolete_subs.find_in_batches do |subs|
        subs.each do |sub|
          problem_subs << sub.id unless sub.destroy
        end
      end

      if problem_subs.present?
        report += "Could not destroy the following subscriptions: #{problem_subs.join(', ')}\n"
      else
        report += "All obsolete subscriptions were destroyed\n"
      end
    end

    duration = Time.current - start_time
    report += "Finished job in #{duration} seconds at #{Time.current}\n"

    AdminMailer.job_report(self.class, report).deliver_now
  end
end<commit_msg>Improve reporting in job<commit_after># frozen_string_literal: true

class SweepSubscriptionsJob < ApplicationJob
  queue_as :default

  def perform
    start_time = Time.current
    report = ''
    report += "Started job at #{start_time}\n"

    problem_subs = []
    obsolete_subs = Subscription.joins('join efforts on efforts.id = subscriptions.subscribable_id join events on events.id = efforts.event_id')
                      .where("subscriptions.subscribable_type = 'Effort' and events.start_time < ?", 1.year.ago)

    count = obsolete_subs.count
    if count == 0
      report += "No obsolete subscriptions found\n"
    else
      report += "Found #{count} obsolete subscriptions\n"

      obsolete_subs.find_in_batches do |subs|
        subs.each do |sub|
          problem_subs << sub.id unless sub.destroy
        end
      end

      if problem_subs.present?
        report += "Could not destroy the following #{problem_subs.size} subscriptions: #{problem_subs.join(', ')}\n"
      else
        report += "All #{count} obsolete subscriptions were destroyed\n"
      end
    end

    duration = Time.current - start_time
    report += "Finished job in #{duration} seconds at #{Time.current}\n"

    AdminMailer.job_report(self.class, report).deliver_now
  end
end
{'fraction_non_alphanumeric': 0.09836718438868977, 'fraction_numerical': 0.0015929908403026682, 'mean_word_length': 2.906687402799378, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5537050', 'n_tokens_mistral': 776, 'n_tokens_neox': 743, 'n_words': 200}