<commit_before>require 'timeout'

class PassengerStatusAggregator

  attr_accessor :stats

  def initialize(options)
    @stats = {
      :max => 0,
      :booted => 0,
      :active => 0,
      :inactive => 0,
      :queued => 0,
    }
    @ssh_command = options[:ssh_command]
    @app_hostnames = options[:app_hostnames]
    @passenger_status_command = options[:passenger_status_command]
    run
  end

  def percent_capacity
    if @stats[:max] > 0
      @stats[:active] / @stats[:max] * 100
    else
      0
    end
  end

  private

  def app_hostnames
    YAML.load_file("config/newrelic_plugin.yml")["agents"]["passenger_stats"]["app_hostnames"]
  end

  def run
    app_hostnames.each do |hostname|
      collect_stats(hostname)
    end
  end

  def collect_stats(hostname)
    begin
      Timeout::timeout(10) do
        output = `#{@ssh_command} #{hostname} '#{@passenger_status_command}'`
        parse_output(output)
      end
    rescue StandardError
      # continue on if we get an error
    end
  end

  # Example output from passenger-status:
  # ----------- General information -----------
  # max      = 8
  # count    = 6
  # active   = 4
  # inactive = 2
  # Waiting on global queue: 5
  def parse_output(output)
    @stats[:max] += output.match(/max\s+=\s+(\d+)/)[1].to_i
    @stats[:booted] += output.match(/count\s+=\s+(\d+)/)[1].to_i
    @stats[:active] += output.match(/active\s+=\s+(\d+)/)[1].to_i
    @stats[:inactive] += output.match(/inactive\s+=\s+(\d+)/)[1].to_i
    @stats[:queued] += output.match(/Waiting on global queue:\s+(\d+)/)[1].to_i
  end

end<commit_msg>convert to float<commit_after>require 'timeout'

class PassengerStatusAggregator

  attr_accessor :stats

  def initialize(options)
    @stats = {
      :max => 0,
      :booted => 0,
      :active => 0,
      :inactive => 0,
      :queued => 0,
    }
    @ssh_command = options[:ssh_command]
    @app_hostnames = options[:app_hostnames]
    @passenger_status_command = options[:passenger_status_command]
    run
  end

  def percent_capacity
    if @stats[:max] > 0
      @stats[:active].to_f / @stats[:max].to_f * 100
    else
      0
    end
  end

  private

  def app_hostnames
    YAML.load_file("config/newrelic_plugin.yml")["agents"]["passenger_stats"]["app_hostnames"]
  end

  def run
    app_hostnames.each do |hostname|
      collect_stats(hostname)
    end
  end

  def collect_stats(hostname)
    begin
      Timeout::timeout(10) do
        output = `#{@ssh_command} #{hostname} '#{@passenger_status_command}'`
        parse_output(output)
      end
    rescue StandardError
      # continue on if we get an error
    end
  end

  # Example output from passenger-status:
  # ----------- General information -----------
  # max      = 8
  # count    = 6
  # active   = 4
  # inactive = 2
  # Waiting on global queue: 5
  def parse_output(output)
    @stats[:max] += output.match(/max\s+=\s+(\d+)/)[1].to_i
    @stats[:booted] += output.match(/count\s+=\s+(\d+)/)[1].to_i
    @stats[:active] += output.match(/active\s+=\s+(\d+)/)[1].to_i
    @stats[:inactive] += output.match(/inactive\s+=\s+(\d+)/)[1].to_i
    @stats[:queued] += output.match(/Waiting on global queue:\s+(\d+)/)[1].to_i
  end

end
{'fraction_non_alphanumeric': 0.16401621453071405, 'fraction_numerical': 0.013719987527284067, 'mean_word_length': 2.717265353418308, 'pattern_counts': {'":': 0, '<': 3, '<?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': '1766282', 'n_tokens_mistral': 1171, 'n_tokens_neox': 1130, 'n_words': 221}