diff --git a/app/models/metric.rb b/app/models/metric.rb index dfb104b1c9b8..8c3a2bea5916 100644 --- a/app/models/metric.rb +++ b/app/models/metric.rb @@ -2,4 +2,12 @@ class Metric < ApplicationRecord BASE_COLS = ["id", "timestamp", "capture_interval_name", "resource_type", "resource_id", "resource_name", "tag_names", "parent_host_id", "parent_ems_cluster_id", "parent_ems_id", "parent_storage_id"] include Metric::Common + + # @param time [ActiveSupport::TimeWithZone, Time, Integer, nil] the hour to run (default: 1 hour from now) + # @return the table for the given hour + # Unfortunatly, Integer responds_to :hour, so :strftime was used instead. + def self.reindex_table_name(time = Time.now.utc.hour + 1) + hour = (time.respond_to?(:strftime) ? time.hour : time) % 24 + "metrics_%02d" % hour + end end diff --git a/spec/models/metric_spec.rb b/spec/models/metric_spec.rb index e50827c1add1..8dfcc9e29f03 100644 --- a/spec/models/metric_spec.rb +++ b/spec/models/metric_spec.rb @@ -1169,6 +1169,34 @@ end end + context "#reindex_table_name" do + it "defaults to 1 hour from now" do + Timecop.freeze("2017-01-30T09:20UTC") do + expect(Metric.reindex_table_name).to eq("metrics_10") + end + end + + it "pads table to 2 digits" do + Timecop.freeze("2017-01-30T03:20UTC") do + expect(Metric.reindex_table_name).to eq("metrics_04") + end + end + + it "provides hour wrap around" do + Timecop.freeze("2017-01-30T23:20UTC") do + expect(Metric.reindex_table_name).to eq("metrics_00") + end + end + + it "allows time to be passed in" do + expect(Metric.reindex_table_name(Time.parse("2017-01-30T23:20Z").utc)).to eq("metrics_23") + end + + it "allows hour integer to be passed in" do + expect(Metric.reindex_table_name(23)).to eq("metrics_23") + end + end + private def assert_queued_rollup(q_item, instance_id, class_name, args, deliver_on, method = "perf_rollup")