From a5baeb8184409d67b232bc3e66add9eb83d9c655 Mon Sep 17 00:00:00 2001 From: Jillian Tullo Date: Tue, 11 Jul 2017 15:15:39 -0400 Subject: [PATCH] add rollups_in_range method --- app/models/metric_rollup.rb | 12 ++++++++++++ db/fixtures/miq_product_features.yml | 11 +++++++++++ spec/models/metric_rollup_spec.rb | 24 ++++++++++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/app/models/metric_rollup.rb b/app/models/metric_rollup.rb index 9e00f21572a..4a5d5d0ceb1 100644 --- a/app/models/metric_rollup.rb +++ b/app/models/metric_rollup.rb @@ -8,6 +8,8 @@ class MetricRollup < ApplicationRecord net_usage_rate_average derived_vm_used_disk_storage derived_vm_allocated_disk_storage).freeze + CAPTURE_INTERVAL_NAMES = %w(hourly daily).freeze + # # min_max column getters # @@ -53,6 +55,16 @@ def self.latest_rollups(resource_type, resource_ids = nil, capture_interval_name metrics.select('DISTINCT ON (metric_rollups.resource_id) metric_rollups.*') end + def self.rollups_in_range(resource_type, resource_ids, capture_interval_name, start_date, end_date = nil) + capture_interval_name ||= 'hourly' + end_date = end_date.nil? ? Time.zone.today : end_date + metrics = where(:resource_type => resource_type, + :capture_interval_name => capture_interval_name, + :timestamp => start_date.beginning_of_day...end_date.end_of_day) + metrics = metrics.where(:resource_id => resource_ids) if resource_ids + metrics + end + def chargeback_fields_present? return @chargeback_fields_present if defined?(@chargeback_fields_present) diff --git a/db/fixtures/miq_product_features.yml b/db/fixtures/miq_product_features.yml index 38c327562b5..0fa81685dd9 100644 --- a/db/fixtures/miq_product_features.yml +++ b/db/fixtures/miq_product_features.yml @@ -6331,3 +6331,14 @@ :description: Show Most Recent Alerts :feature_type: view :identifier: monitor_alerts_most_recent + +# Metrics +- :name: Metrics + :description: Everything under Metrics + :feature_type: node + :identifier: metrics + :children: + - :name: View + :description: View Metrics + :feature_type: view + :identifier: metrics_view diff --git a/spec/models/metric_rollup_spec.rb b/spec/models/metric_rollup_spec.rb index ca297c70549..bc564fd4a07 100644 --- a/spec/models/metric_rollup_spec.rb +++ b/spec/models/metric_rollup_spec.rb @@ -17,4 +17,28 @@ end.to raise_error ActiveRecord::EagerLoadPolymorphicError end end + + context ".rollups_in_range" do + before do + @current = FactoryGirl.create_list(:metric_rollup_vm_hr, 2) + @past = FactoryGirl.create_list(:metric_rollup_vm_hr, 2, :timestamp => Time.now.utc - 5.days) + end + + it "returns rollups from the correct range" do + rollups = described_class.rollups_in_range('VmOrTemplate', nil, 'hourly', Time.zone.today) + + expect(rollups.size).to eq(2) + expect(rollups.pluck(:id)).to match_array(@current.pluck(:id)) + + rollups = described_class.rollups_in_range('VmOrTemplate', nil, 'hourly', Time.zone.today - 5.days, Time.zone.today - 4.days) + + expect(rollups.size).to eq(2) + expect(rollups.pluck(:id)).to match_array(@past.pluck(:id)) + + rollups = described_class.rollups_in_range('VmOrTemplate', nil, 'hourly', Time.zone.today - 5.days) + + expect(rollups.size).to eq(4) + expect(rollups.pluck(:id)).to match_array(@current.pluck(:id) + @past.pluck(:id)) + end + end end