From d0f307efd28ed5de2f372309d29849fcd6058bba Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Mon, 26 Feb 2018 12:38:52 -0500 Subject: [PATCH] Allow individual tables to be specified for metrics --- app/models/metric.rb | 8 ++++++-- spec/models/metric_spec.rb | 28 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/app/models/metric.rb b/app/models/metric.rb index 347e1c0009ec..8c3a2bea5916 100644 --- a/app/models/metric.rb +++ b/app/models/metric.rb @@ -3,7 +3,11 @@ class Metric < ApplicationRecord include Metric::Common - def self.reindex_table_name - "metrics_#{Time.now.utc.hour + 1}" + # @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 996fb048e4b9..56d0abf1174b 100644 --- a/spec/models/metric_spec.rb +++ b/spec/models/metric_spec.rb @@ -1181,6 +1181,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:20UTC"))).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")