Skip to content

Commit

Permalink
Merge pull request ManageIQ#17017 from kbrock/purge_truncate
Browse files Browse the repository at this point in the history
truncate realtime metrics instead of purging
(cherry picked from commit 7d4db70)
  • Loading branch information
gtanzillo committed Mar 8, 2018
1 parent 17fc98c commit 79a4fba
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
34 changes: 33 additions & 1 deletion app/models/metric/purging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ def self.purge_count(older_than, interval)
purge_scope(older_than, interval).count
end

# Used for MetricRollup (not Metric)
# A list of ids (not a scope) is brought in and associated vimPerformanceTagValue records are deleted
#
# TODO: Would be more efficient to just use the full scope here (not id list)
# - we would then just use the standard purge_in_batches
# - remove the to_a from purge_in_batches
# - possibly use the standard purge_in_batches
# - change the metrics rollups to use truncate instead
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 +93,33 @@ 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

# truncate metrics child tables
# Determines hours not being preserved and truncates them
# Used for realtime metrics.
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.connection.truncate(Metric.reindex_table_name(hour), "Metric Truncate table #{hour}")
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
1 change: 1 addition & 0 deletions spec/models/metric/purging_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
end
expect(Metric.count).to eq(17)
# keep metric for 05:13 - 09:13
# note: old metrics will delete metrics at 09:14..09:59 - the new metrics will keep those
described_class.purge_realtime(4.hours.ago)
expect(Metric.all.map { |metric| metric.timestamp.hour }.sort).to eq [5, 6, 7, 8, 9]
expect(Metric.count).to eq(5)
Expand Down

0 comments on commit 79a4fba

Please sign in to comment.