forked from ManageIQ/manageiq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpurge_miq_report_results.rb
executable file
·59 lines (49 loc) · 2.07 KB
/
purge_miq_report_results.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env ruby
require File.expand_path('../config/environment', __dir__)
require 'optimist'
MODES = %w(count purge)
ARGV.shift if ARGV[0] == '--' # if invoked with rails runner
opts = Optimist.options do
banner "Purge miq_report_results records.\n\nUsage: ruby #{$0} [options]\n\nOptions:\n\t"
opt :mode, "Mode (#{MODES.join(", ")})", :default => "count"
opt :window, "Window of records to delete at once", :default => 100
opt :date, "Range of reports to keep by date (default: VMDB configuration)", :type => :string
opt :remaining, "Number of results to keep per report (default: VMDB configuration)", :type => :int
end
Optimist.die :mode, "must be one of #{MODES.join(", ")}" unless MODES.include?(opts[:mode])
Optimist.die :window, "must be a number greater than 0" if opts[:window] <= 0
if opts[:remaining_given]
Optimist.die :remaining, "must be a number greater than 0" if opts[:remaining] <= 0
purge_mode = :remaining
purge_value = opts[:remaining]
elsif opts[:date_given]
Optimist.die :date, "must be a number with method (e.g. 6.months)" unless opts[:date].number_with_method?
purge_mode = :date
purge_value = opts[:date].to_i_with_method.seconds.ago.utc
else
purge_mode, purge_value = MiqReportResult.purge_mode_and_value
end
def log(msg)
$log.info("MIQ(#{__FILE__}) #{msg}")
puts msg
end
formatter = Class.new.extend(ActionView::Helpers::NumberHelper)
msg = case purge_mode
when :remaining then "last #{purge_value} results"
when :date then "[#{purge_value.iso8601}]"
end
log("Executing in #{opts[:mode]} mode for report results older than #{msg}")
count = MiqReportResult.purge_count(purge_mode, purge_value)
log("Purge Count: #{formatter.number_with_delimiter(count)}")
puts
exit if opts[:mode] != "purge"
log("Purging...")
require 'ruby-progressbar'
pbar = ProgressBar.create(:title => "Purging", :total => count, :autofinish => false)
if count > 0
MiqReportResult.purge(purge_mode, purge_value, opts[:window]) do |increment, _|
pbar.progress += increment
end
end
pbar.finish
log("Purging...Complete")