Skip to content

Commit

Permalink
Merge 8d0d53a into 0d23948
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrock committed Feb 20, 2018
2 parents 0d23948 + 8d0d53a commit be58192
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
17 changes: 15 additions & 2 deletions app/models/metric.rb
Expand Up @@ -3,7 +3,20 @@ class Metric < ApplicationRecord

include Metric::Common

def self.reindex_table_name
"metrics_#{Time.now.utc.hour + 1}"
def self.table_name
Thread.current[:targeted_metric_subtable_name] || super
end

def self.with_table_for_time(hour = Time.now.utc.hour)
Thread.current[:targeted_metric_subtable_name] = reindex_table_name(hour)
yield
ensure
Thread.current[:targeted_metric_subtable_name] = nil
end

# @param hour the hour to run (default: now)
# @return the table for the given hour
def self.reindex_table_name(hour = Time.now.utc.hour)
"metrics_%02d" % hour
end
end
27 changes: 26 additions & 1 deletion app/models/metric/purging.rb
Expand Up @@ -61,6 +61,8 @@ def self.purge_count(older_than, interval)
purge_scope(older_than, interval).count
end

# TODO: delete these using the same created_at queries by date
# rather than by joining to metrics - then hourly could be truncated vs deleted
def self.purge_associated_records(metric_type, ids)
# Since VimPerformanceTagValues are 6 * number of tags per performance
# record, we need to batch in smaller trips.
Expand All @@ -85,9 +87,32 @@ def self.purge_hourly(older_than, window = nil, total_limit = nil, &block)
end

def self.purge_realtime(older_than, window = nil, total_limit = nil, &block)
purge_by_date(older_than, "realtime", window, total_limit, &block)
truncate_child_tables(older_than)
end

def self.truncate_child_tables(older_than)
target_hours = determine_target_hours(older_than, Time.now.utc)
return if target_hours.blank?

target_hours.each do |hour|
Metric.with_table_for_time(hour) do
Metric.truncate
end
end
end

def self.determine_target_hours(older_than, end_date)
return [] if (end_date - older_than) > 24.hours

start_hour = older_than.utc.hour
end_hour = end_date.utc.hour
end_hour += 24 if start_hour > end_hour

good_hours = (start_hour..end_hour).map { |h| h % 24 }
(0..23).to_a - good_hours.to_a
end
private_class_method :determine_target_hours

def self.purge(older_than, interval, window = nil, total_limit = nil, &block)
purge_by_date(older_than, interval, window, total_limit, &block)
end
Expand Down
20 changes: 20 additions & 0 deletions spec/models/metric/purging_spec.rb
Expand Up @@ -82,4 +82,24 @@
end
end
end

context "#purge_realtime" do
let(:vm1) { FactoryGirl.create(:vm_vmware) }

it "deletes old, keeps new" do
EvmSpecHelper.create_guid_miq_server_zone

Timecop.freeze('2018-02-01T02:12:00Z') do
# this is overkill. since we prune every 21 minutes, there will only be ~1 table with data
(0..16).each do |hours|
FactoryGirl.create(:metric_vm_rt, :resource_id => vm1.id, :timestamp => hours.hours.ago)
end
expect(Metric.count).to eq(17)
# keep metric for 02:12 - 22:12 (down to 22:00)
described_class.purge_realtime(4.hours.ago)
expect(Metric.all.map { |metric| metric.timestamp.hour }.sort).to eq [0, 1, 2, 22, 23]
expect(Metric.count).to eq(5)
end
end
end
end

0 comments on commit be58192

Please sign in to comment.