<commit_before>class RepoSubscription < ActiveRecord::Base
  validate :repo_id, :uniqueness => {:scope => :user_id}
  belongs_to :repo
  belongs_to :user
  has_many   :issue_assignments

  def ready_for_next?
    return true if last_sent_at.blank?
    last_sent_at < 24.hours.ago
  end

  def wait?
    !ready_for_next?
  end

  def send_triage_email!
    Resque.enqueue(SendTriageEmail, self.id)
  end

  def self.queue_triage_emails!
    find_each(:conditions => ["last_sent_at is null or last_sent_at < ?", 23.hours.ago]) do |repo_sub|
      repo_sub.send_triage_email!
    end
  end

  def assigned_issues
    issue_assignments.includes(:issue).map {|assignment| assignment.issue }
  end

  def issue_for_triage!
    assigned_issue_ids = assigned_issues.map(&:id) || [-1]
    repo.issues.where(:state => 'open').where("id not in (?)", assigned_issue_ids).all.sample
  end

  def assign_issue!
    return false if wait?
    issue = issue_for_triage!
    issue_assignments.create(:issue => issue) unless issue.blank?
    return issue
  ensure
    self.update_attributes :last_sent_at => Time.now unless wait?
  end


  class SendTriageEmail
    @queue = :send_triage_email
    def self.perform(id)
      repo_sub = RepoSubscription.includes(:user, :repo).where(:id => id)
      issue = repo_sub.assign_issue!
      UserMailer.send_triage(:repo => repo_sub.repo, :user => repo_sub.user, :issue => issue).deliver
    end
  end

end
<commit_msg>sql hack, -1 should never be an id, so we don't do sub-select and pass in a null value<commit_after>class RepoSubscription < ActiveRecord::Base
  validate :repo_id, :uniqueness => {:scope => :user_id}
  belongs_to :repo
  belongs_to :user
  has_many   :issue_assignments

  def ready_for_next?
    return true if last_sent_at.blank?
    last_sent_at < 24.hours.ago
  end

  def wait?
    !ready_for_next?
  end

  def send_triage_email!
    Resque.enqueue(SendTriageEmail, self.id)
  end

  def self.queue_triage_emails!
    find_each(:conditions => ["last_sent_at is null or last_sent_at < ?", 23.hours.ago]) do |repo_sub|
      repo_sub.send_triage_email!
    end
  end

  def assigned_issues
    issue_assignments.includes(:issue).map {|assignment| assignment.issue }
  end

  def issue_for_triage!
    assigned_issue_ids = assigned_issues.map(&:id) + [-1]
    repo.issues.where(:state => 'open').where("id not in (?)", assigned_issue_ids).all.sample
  end

  def assign_issue!
    return false if wait?
    issue = issue_for_triage!
    issue_assignments.create(:issue => issue) unless issue.blank?
    return issue
  ensure
    self.update_attributes :last_sent_at => Time.now unless wait?
  end


  class SendTriageEmail
    @queue = :send_triage_email
    def self.perform(id)
      repo_sub = RepoSubscription.includes(:user, :repo).where(:id => id)
      issue = repo_sub.assign_issue!
      UserMailer.send_triage(:repo => repo_sub.repo, :user => repo_sub.user, :issue => issue).deliver
    end
  end

end

{'fraction_non_alphanumeric': 0.10540540540540541, 'fraction_numerical': 0.0037162162162162164, 'mean_word_length': 3.707472178060413, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 23, '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': '3604389', 'n_tokens_mistral': 1042, 'n_tokens_neox': 1018, 'n_words': 228}