<commit_before>require 'sparkpost'
require 'ostruct'


class WeeklyReportJob < BaseJob
  include ActionView::Helpers::NumberHelper

  queue_as do
    :weekly
  end

  def self.planify_next
    next_time = (Date.today + 7).at_beginning_of_week.beginning_of_day
    WeeklyReportJob.set(wait_until: next_time).perform_later
  end

  def perform
    users = User.all
    users.each do |user|
      process_user(user)
    end
    WeeklyReportJob.planify_next
  end

  def process_user(user)
    file_path = Rails.root.join("app", "views", "mail.slim")
    logger.info "Processing user : " + user.email

    pages = user.pages.sort_by { |p| p["name"] }
    @context = OpenStruct.new
    @context.pages = []
    @context.period_start = (Date.today - 7).at_beginning_of_week.beginning_of_day
    @context.period_end = (Date.today - 7).at_end_of_week.end_of_day

    pages.each do |page|
      @context.pages << construct_page(page, @context.period_start, @context.period_end)
    end

    message = Slim::Template.new(file_path).render(self)
    send_mail(user, generate_title(@context.period_start, @context.period_end), message)
  rescue Exception => e
    logger.error "Error processing user " + user.email
    logger.error e.to_s
  end

  def generate_title(start_date, end_date)
    "Botnbot weekly report " + start_date.strftime("%m/%d/%Y") + " to " + end_date.strftime("%m/%d/%Y")
  end

  def send_mail(user, title, message)
    sp = SparkPost::Client.new()
    sp.transmission.send_message(user.email, '<EMAIL>', title, message)
  rescue Exception => e
    logger.error "Error sending mail to user " + user.email
    logger.error e.to_s
  end
<commit_msg>Do not send email to users with no pages<commit_after>require 'sparkpost'
require 'ostruct'


class WeeklyReportJob < BaseJob
  include ActionView::Helpers::NumberHelper

  queue_as do
    :weekly
  end

  def self.planify_next
    next_time = (Date.today + 7).at_beginning_of_week.beginning_of_day
    WeeklyReportJob.set(wait_until: next_time).perform_later
  end

  def perform
    users = User.all
    users.each do |user|
      process_user(user)
    end
    WeeklyReportJob.planify_next
  end

  def process_user(user)
    logger.info "Processing user : " + user.email

    pages = user.pages.sort_by { |p| p["name"] }
    unless pages.empty?
      @context = OpenStruct.new
      @context.pages = []
      @context.period_start = (Date.today - 7).at_beginning_of_week.beginning_of_day
      @context.period_end = (Date.today - 7).at_end_of_week.end_of_day

      pages.each do |page|
        @context.pages << construct_page(page, @context.period_start, @context.period_end)
      end

      file_path = Rails.root.join("app", "views", "mail.slim")
      message = Slim::Template.new(file_path).render(self)
      send_mail(user, generate_title(@context.period_start, @context.period_end), message)
    end
  rescue Exception => e
    logger.error "Error processing user " + user.email
    logger.error e.to_s
  end

  def generate_title(start_date, end_date)
    "Botnbot weekly report " + start_date.strftime("%m/%d/%Y") + " to " + end_date.strftime("%m/%d/%Y")
  end

  def send_mail(user, title, message)
    sp = SparkPost::Client.new()
    sp.transmission.send_message(user.email, '<EMAIL>', title, message)
  rescue Exception => e
    logger.error "Error sending mail to user " + user.email
    logger.error e.to_s
  end

{'fraction_non_alphanumeric': 0.11736950751990563, 'fraction_numerical': 0.0017693895606015924, 'mean_word_length': 3.540829986613119, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, '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': '3686033', 'n_tokens_mistral': 1185, 'n_tokens_neox': 1146, 'n_words': 237}