forked from ManageIQ/manageiq
-
Notifications
You must be signed in to change notification settings - Fork 1
/
purge_metrics.rb
executable file
·53 lines (46 loc) · 2.06 KB
/
purge_metrics.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
#!/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 metrics records.\n\nUsage: ruby #{$0} [options]\n\nOptions:\n\t"
opt :mode, "Mode (#{MODES.join(", ")})", :default => "count"
opt :realtime, "Realtime range", :default => "4.hours"
opt :hourly, "Hourly range", :default => "6.months"
opt :daily, "Daily range", :default => "6.months"
opt :window, "Window of records to delete at once", :default => 10000
opt :limit, "Total number of records to delete per method"
end
Optimist.die :mode, "must be one of #{MODES.join(", ")}" unless MODES.include?(opts[:mode])
Optimist.die :realtime, "must be a number with method (e.g. 4.hours)" unless opts[:realtime].number_with_method?
Optimist.die :hourly, "must be a number with method (e.g. 6.months)" unless opts[:hourly].number_with_method?
Optimist.die :daily, "must be a number with method (e.g. 6.months)" unless opts[:daily].number_with_method?
Optimist.die :window, "must be a number greater than 0" if opts[:window] <= 0
def log(msg)
$log.info("MIQ(#{__FILE__}) #{msg}")
puts msg
end
formatter = Class.new.extend(ActionView::Helpers::NumberHelper)
log("Purge Counts")
dates = {}
counts = {}
%w[realtime hourly daily].each do |interval|
dates[interval] = opts[interval.to_sym].to_i_with_method.seconds.ago.utc
counts[interval] = Metric::Purging.purge_count(dates[interval], interval)
log(" #{"#{interval.titleize}:".ljust(9)} #{formatter.number_with_delimiter(counts[interval])}")
end
puts
exit if opts[:mode] != "purge"
log("Purging...")
require 'ruby-progressbar'
%w[realtime hourly daily].each do |interval|
pbar = ProgressBar.create(:title => interval.titleize, :total => counts[interval], :autofinish => false)
if counts[interval] > 0
Metric::Purging.purge(dates[interval], interval, opts[:window], opts[:limit]) do |count, _|
pbar.progress += count
end
end
pbar.finish
end
log("Purging...Complete")