class Flapjack::CLI::Purge

Public Class Methods

new(global_options, options) click to toggle source
# File lib/flapjack/cli/purge.rb, line 10
def initialize(global_options, options)
  @global_options = global_options
  @options = options

  if @global_options[:'force-utf8']
    Encoding.default_external = 'UTF-8'
    Encoding.default_internal = 'UTF-8'
  end

  config = Flapjack::Configuration.new
  config.load(global_options[:config])
  @config_env = config.all

  if @config_env.nil? || @config_env.empty?
    exit_now! "No config data found in '#{global_options[:config]}'"
  end

  Flapjack::RedisProxy.config = config.for_redis
  Zermelo.redis = Flapjack.redis
end

Public Instance Methods

check_history() click to toggle source
# File lib/flapjack/cli/purge.rb, line 31
def check_history
  # find all checks, or the check given
  # purge old data for check
  options = {}
  if @options[:days]
    options[:older_than] = @options[:days].to_i * 24 * 60 * 60
    raise "days must be resolvable to an integer" unless @options[:days].to_i.to_s == @options[:days]
  end
  checks = if @options[:check]
    [Flapjack::Data::Check.find_by_id(options[:check])].compact
  else
    Flapjack::Data::Check.all
  end

  purge_before = Time.now - options[:older_than]
  purge_range = Zermelo::Filters::IndexRange.new(nil, purge_before, :by_score => true)

  purged = checks.inject(0) do |memo, check|
    purgees = check.states.intersect(:created_at => purge_range)
    num = purgees.count
    if num > 0
      purgees.destroy_all
      memo += num
    end
    memo
  end

  if purged == 0
    puts "Nothing to do"
  else
    puts "Purged #{purged.reduce(:+) || 0} historical check states over #{purged.length} checks."
  end
end