<commit_before>class Email < ActiveRecord::Base
  belongs_to :from_address, :class_name => "Address"
  has_and_belongs_to_many :to_addresses, :class_name => "Address", :join_table => "deliveries"
  has_many :postfix_log_lines, -> { order :time }

  after_save :save_data_to_filesystem, :cleanup_filesystem_data_store

  # TODO Add validations

  attr_writer :data

  def self.no_emails_sent_today
    where('created_at > ?', Date.today.beginning_of_day).count
  end

  def from
    # TODO: Remove the "if" once we've added validations
    from_address.address if from_address
  end

  def from=(a)
    self.from_address = Address.find_or_create_by(address: a)
  end

  def to
    to_addresses.map{|t| t.address}
  end

  def to=(a)
    a = [a] unless a.respond_to?(:map)
    self.to_addresses = a.map{|t| Address.find_or_create_by(address: t)}
  end

  def to_as_string
    to.join(", ")
  end

  def data
    @data ||= File.read(data_filesystem_path) if is_data_on_filesystem?
  end

  # Check the delivery status for a particular email
  def delivery_status(email)
    unless postfix_log_lines_for_email(email).empty?
      postfix_log_lines_for_email(email).any? {|l| l.delivered? }
    end
  end

  def postfix_log_lines_for_email(email)
    postfix_log_lines.select{|l| l.to == email}
  end

  def overall_delivery_status
    if to.all? {|email| !delivery_status(email).nil? }
      to.all? {|email| delivery_status(email) }
    end
  end

  def update_delivery_status!
    update_attribute(:delivered, overall_delivery_status)
  end

  def self.max_no_emails_to_store_data
    # By default keep the full content of the last 100 emails
    100
  end

  def cleanup_filesystem_data_store
    # If there are more than a certain number of stored emails on the filesystem
    # remove the oldest ones
    entries = Dir.glob(File.join(Email.data_filesystem_directory, "*"))
    no_to_remove = entries.count - Email.max_no_emails_to_store_data
    if no_to_remove > 0
      # Oldest first
      entries.sort_by {|f| File.mtime f}[0...no_to_remove].each {|f| File.delete f}
    end
  end
<commit_msg>Small refactor<commit_after>class Email < ActiveRecord::Base
  belongs_to :from_address, :class_name => "Address"
  has_and_belongs_to_many :to_addresses, :class_name => "Address", :join_table => "deliveries"
  has_many :postfix_log_lines, -> { order :time }

  after_save :save_data_to_filesystem, :cleanup_filesystem_data_store

  # TODO Add validations

  attr_writer :data

  def self.no_emails_sent_today
    where('created_at > ?', Date.today.beginning_of_day).count
  end

  def from
    # TODO: Remove the "if" once we've added validations
    from_address.address if from_address
  end

  def from=(a)
    self.from_address = Address.find_or_create_by(address: a)
  end

  def to
    to_addresses.map{|t| t.address}
  end

  def to=(a)
    a = [a] unless a.respond_to?(:map)
    self.to_addresses = a.map{|t| Address.find_or_create_by(address: t)}
  end

  def to_as_string
    to.join(", ")
  end

  def data
    @data ||= File.read(data_filesystem_path) if is_data_on_filesystem?
  end

  # Check the delivery status for a particular destination
  def delivery_status(text_address)
    lines = postfix_log_lines_for_email(text_address)
    unless lines.empty?
      lines.any? {|l| l.delivered? }
    end
  end

  def postfix_log_lines_for_email(text_address)
    postfix_log_lines.select{|l| l.to == text_address}
  end

  def overall_delivery_status
    if to.all? {|email| !delivery_status(email).nil? }
      to.all? {|email| delivery_status(email) }
    end
  end

  def update_delivery_status!
    update_attribute(:delivered, overall_delivery_status)
  end

  def self.max_no_emails_to_store_data
    # By default keep the full content of the last 100 emails
    100
  end

  def cleanup_filesystem_data_store
    # If there are more than a certain number of stored emails on the filesystem
    # remove the oldest ones
    entries = Dir.glob(File.join(Email.data_filesystem_directory, "*"))
    no_to_remove = entries.count - Email.max_no_emails_to_store_data
    if no_to_remove > 0
      # Oldest first
      entries.sort_by {|f| File.mtime f}[0...no_to_remove].each {|f| File.delete f}
    end
  end

{'fraction_non_alphanumeric': 0.09265402843601896, 'fraction_numerical': 0.0037914691943127963, 'mean_word_length': 3.7911464245175934, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, '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': '68917', 'n_tokens_mistral': 1530, 'n_tokens_neox': 1471, 'n_words': 343}