Skip to content

Commit

Permalink
Allow individual tables to be specified for metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrock committed Feb 26, 2018
1 parent 4fcd3da commit d0f307e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
8 changes: 6 additions & 2 deletions app/models/metric.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
28 changes: 28 additions & 0 deletions spec/models/metric_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit d0f307e

Please sign in to comment.