Skip to content

Commit

Permalink
Merge pull request ManageIQ#17051 from kbrock/metrics_indexes
Browse files Browse the repository at this point in the history
Allow individual tables to be specified for metrics
(cherry picked from commit ac0e949)
  • Loading branch information
jrafanie authored and gtanzillo committed Mar 8, 2018
1 parent 79a4fba commit 3701509
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
8 changes: 8 additions & 0 deletions app/models/metric.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
28 changes: 28 additions & 0 deletions spec/models/metric_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 3701509

Please sign in to comment.