<commit_before>
class MonitorDelayedJobs < Scout::Plugin
  needs 'activerecord', 'yaml'
  
  require 'activerecord'

  class DelayedJob < ActiveRecord::Base; end
  
  def build_report
    db_config = YAML::load(File.open(@options['path_to_app'] + '/config/database.yml'))
    ActiveRecord::Base.establish_connection(db_config[@options['rails_env']])
    
    report :total     => DelayedJob.count
    report :running   => DelayedJob.count(:conditions => 'locked_at IS NOT NULL')
    report :waiting   => DelayedJob.count(:conditions => [ 'run_at <= ? AND locked_at IS NULL', Time.now.utc ])
    report :scheduled => DelayedJob.count(:conditions => [ 'run_at > ? AND locked_at IS NULL', Time.now.utc ])
    report :failing   => DelayedJob.count(:conditions => 'attempts > 0')
    report :failed    => DelayedJob.count(:conditions => 'failed_at IS NOT NULL')
    
    if oldest = DelayedJob.find(:first, :conditions => [ 'run_at <= ? AND locked_at IS NULL', Time.now.utc ], :order => :id)
      report :oldest => (Time.now.utc - oldest.created_at) / 60
    else
      report :oldest => 0
    end
  end<commit_msg>Improving conditions for metrics in delayed_job.<commit_after>
class MonitorDelayedJobs < Scout::Plugin
  needs 'activerecord', 'yaml'
  
  require 'activerecord'

  class DelayedJob < ActiveRecord::Base; end
  
  def build_report
    db_config = YAML::load(File.open(@options['path_to_app'] + '/config/database.yml'))
    ActiveRecord::Base.establish_connection(db_config[@options['rails_env']])
    
    # ALl jobs
    report :total     => DelayedJob.count
    # Jobs that are currently being run by workers
    report :running   => DelayedJob.count(:conditions => 'locked_at IS NOT NULL')
    # Jobs that are ready to run but haven't ever been run
    report :waiting   => DelayedJob.count(:conditions => [ 'run_at <= ? AND locked_at IS NULL AND attempts = 0', Time.now.utc ])
    # Jobs that haven't ever been run but are not set to run until later
    report :scheduled => DelayedJob.count(:conditions => [ 'run_at > ? AND locked_at IS NULL AND attempts = 0', Time.now.utc ])
    # Jobs that aren't running that have failed at least once
    report :failing   => DelayedJob.count(:conditions => 'attempts > 0 AND failed_at IS NULL AND locked_at IS NULL')
    # Jobs that have permanently failed
    report :failed    => DelayedJob.count(:conditions => 'failed_at IS NOT NULL')
    
    # The oldest job that hasn't yet been run, in minutes
    if oldest = DelayedJob.find(:first, :conditions => [ 'run_at <= ? AND locked_at IS NULL AND attempts = 0', Time.now.utc ], :order => :id)
      report :oldest => (Time.now.utc - oldest.created_at) / 60
    else
      report :oldest => 0
    end
  end
{'fraction_non_alphanumeric': 0.12398523985239852, 'fraction_numerical': 0.004059040590405904, 'mean_word_length': 3.579391891891892, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 37, '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': '3812764', 'n_tokens_mistral': 887, 'n_tokens_neox': 827, 'n_words': 245}