Skip to content

Commit

Permalink
Merge pull request ManageIQ#15549 from jntullo/enhancement/metric_rol…
Browse files Browse the repository at this point in the history
…lups_api

rollups_in_range method
  • Loading branch information
gtanzillo authored Aug 10, 2017
2 parents 2597fbb + a5baeb8 commit 81d444b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
12 changes: 12 additions & 0 deletions app/models/metric_rollup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down Expand Up @@ -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)

Expand Down
11 changes: 11 additions & 0 deletions db/fixtures/miq_product_features.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 24 additions & 0 deletions spec/models/metric_rollup_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 81d444b

Please sign in to comment.