diff --git a/app/models/metric/purging.rb b/app/models/metric/purging.rb index 6be37814b3a..d91001b6e6a 100644 --- a/app/models/metric/purging.rb +++ b/app/models/metric/purging.rb @@ -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. @@ -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 diff --git a/spec/models/metric/purging_spec.rb b/spec/models/metric/purging_spec.rb index 7d1fbf901ef..580754e65f2 100644 --- a/spec/models/metric/purging_spec.rb +++ b/spec/models/metric/purging_spec.rb @@ -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)