Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add model ReportType #18201

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion app/models/chargeable_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class ChargeableField < ApplicationRecord
}.freeze

belongs_to :detail_measure, :class_name => 'ChargebackRateDetailMeasure', :foreign_key => :chargeback_rate_detail_measure_id
has_many :report_types, :as => :resource, :dependent => :destroy, :inverse_of => :report_types

validates :metric, :uniqueness => true, :presence => true
validates :group, :source, :presence => true
Expand Down Expand Up @@ -120,13 +121,23 @@ def self.seed
if measure
f[:chargeback_rate_detail_measure_id] = measures[measure].id
end

report_types = f.delete(:report_types)
rec = existing[f[:metric]]

if rec.nil?
create(f)
rec = create(f)
else
rec.attributes = f
rec.save! if rec.changed?
end

report_types&.each do |report_type|
ReportType.find_or_create_by(:resource => rec, :name => report_type[:name])
end

report_type_names = report_types ? report_types.map { |report_type| report_type[:name] } : []
ReportType.where(:resource => rec).where.not(:name => report_type_names).destroy_all if report_type_names
end
end

Expand Down
3 changes: 3 additions & 0 deletions app/models/report_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class ReportType < ApplicationRecord
belongs_to :resource, :polymorphic => true
end
51 changes: 51 additions & 0 deletions db/fixtures/chargeable_fields.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,77 +4,128 @@
:group: cpu
:source: used
:measure: Hz Units
:report_types:
- :name: ChargebackVm
- :metric: v_derived_cpu_total_cores_used
:description: Used CPU Cores
:group: cpu_cores
:source: used
:report_types:
- :name: ChargebackContainerImage
- :name: ChargebackContainerProject
- :metric: derived_vm_numvcpus
:description: Allocated CPU Count
:group: cpu
:source: allocated
:report_types:
- :name: ChargebackVm
- :metric: derived_vm_numvcpu_cores
:description: Allocated CPU Cores
:group: cpu_cores
:source: allocated
:report_types:
- :name: ChargebackContainerImage
- :metric: derived_memory_used
:description: Used Memory
:group: memory
:source: used
:measure: Bytes Units
:report_types:
- :name: ChargebackVm
- :name: ChargebackContainerProject
- :name: ChargebackContainerImage
- :metric: derived_memory_available
:description: Allocated Memory
:group: memory
:source: allocated
:measure: Bytes Units
:report_types:
- :name: ChargebackVm
- :name: ChargebackContainerImage
- :metric: net_usage_rate_average
:description: Used Network I/O
:group: net_io
:source: used
:measure: Bytes per Second Units
:report_types:
- :name: ChargebackVm
- :name: ChargebackContainerProject
- :name: ChargebackContainerImage
- :metric: disk_usage_rate_average
:description: Used Disk I/O
:group: disk_io
:source: used
:measure: Bytes per Second Units
:report_types:
- :name: ChargebackVm
- :metric: fixed_compute_1
:description: Fixed Compute Cost 1
:group: fixed
:source: compute_1
:report_types:
- :name: ChargebackVm
- :name: ChargebackContainerProject
- :name: ChargebackContainerImage
- :metric: fixed_compute_2
:description: Fixed Compute Cost 2
:group: fixed
:source: compute_2
:report_types:
- :name: ChargebackVm
- :name: ChargebackContainerProject
- :name: ChargebackContainerImage
- :metric: derived_vm_allocated_disk_storage
:description: Allocated Disk Storage
:group: storage
:source: allocated
:measure: Bytes Units
:report_types:
- :name: ChargebackVm
- :metric: derived_vm_used_disk_storage
:description: Used Disk Storage
:group: storage
:source: used
:measure: Bytes Units
:report_types:
- :name: ChargebackVm
- :metric: fixed_storage_1
:group: fixed
:source: storage_1
:description: Fixed Storage Cost 1
:report_types:
- :name: ChargebackVm
- :metric: fixed_storage_2
:group: fixed
:source: storage_2
:description: Fixed Storage Cost 2
:report_types:
- :name: ChargebackVm
- :metric: metering_used_hours
:group: metering
:source: used
:description: Metering - Hours Used
:report_types:
- :name: MeteringVm
- :name: MeteringContainerProject
- :name: MeteringContainerImage
- :metric: metering_allocated_cpu
:group: metering
:source: allocated_cpu
:description: Metering - Hours Allocated CPU
:report_types:
- :name: MeteringVm
- :metric: metering_allocated_memory
:group: metering
:source: allocated_memory
:description: Metering - Hours Allocated Memory
:report_types:
- :name: MeteringVm
- :name: MeteringContainerProject
- :name: MeteringContainerImage
- :metric: metering_allocated_cpu_cores
:group: metering
:source: allocated_cpu_cores
:description: Metering - Hours Allocated Memory
:report_types:
- :name: MeteringContainerProject
- :name: MeteringContainerImage
77 changes: 77 additions & 0 deletions spec/models/chargeable_field_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,81 @@
expect(described_class.cols_on_metric_rollup).to eq(expected_columns)
end
end

describe '#seed' do
let(:seed_hash) do
[{:metric => 'cpu_usagemhz_rate_average', :description => 'Used CPU', :group => 'cpu', :source => 'used', :measure => 'Hz Units', :report_types => [:name => 'ChargebackVm']}]
end

before do
ChargebackRateDetailMeasure.seed
allow(described_class).to receive(:seed_data).and_return(seed_hash.deep_dup)
described_class.seed
end

it 'seeds report types' do
allow(described_class).to receive(:seed_data).and_return(seed_hash.deep_dup)
expect(described_class.count).to eq(1)

expect(described_class.find_by(:metric => "cpu_usagemhz_rate_average").report_types.count).to eq(1)
expect(described_class.find_by(:metric => "cpu_usagemhz_rate_average").report_types.map(&:name)).to match_array(%w(ChargebackVm))
end

context 'new entry in yaml file after first seeding' do
let(:seed_data_new_entry) do
[{:metric => 'cpu_usagemhz_rate_average', :description => 'Used CPU', :group => 'cpu', :source => 'used', :measure => 'Hz Units', :report_types => [{:name => 'ChargebackVm'}, {:name => 'ChargebackContainerProject'}]},
{:metric => 'v_derived_cpu_total_cores_used', :description => 'Used CPU Cores', :group => 'cpu_cores', :source => 'used', :report_types => [{:name => 'ChargebackContainerImage'}, {:name => 'ChargebackContainerProject'}]}]
end

it 'seeds report types' do
allow(described_class).to receive(:seed_data_new_entry).and_return(seed_hash.deep_dup)

described_class.seed
expect(described_class.count).to eq(2)
expect(described_class.find_by(:metric => "cpu_usagemhz_rate_average").report_types.count).to eq(2)
expect(described_class.find_by(:metric => "cpu_usagemhz_rate_average").report_types.map(&:name)).to match_array(%w(ChargebackVm ChargebackContainerProject))

expect(described_class.find_by(:metric => "v_derived_cpu_total_cores_used").report_types.count).to eq(2)
expect(described_class.find_by(:metric => "v_derived_cpu_total_cores_used").report_types.map(&:name)).to match_array(%w(ChargebackContainerImage ChargebackContainerProject))
end

context 'remove entry from' do
let(:seed_hash_remove) do
[{:metric => 'cpu_usagemhz_rate_average', :description => 'Used CPU', :group => 'cpu', :source => 'used', :measure => 'Hz Units', :report_types => [:name => 'ChargebackContainerProject']},
{:metric => 'v_derived_cpu_total_cores_used', :description => 'Used CPU Cores', :group => 'cpu_cores', :source => 'used', :report_types => [{:name => 'ChargebackContainerImage'}]}]
end

it 'seeds report types' do
allow(described_class).to receive(:seed_data).and_return(seed_hash_remove.deep_dup)
described_class.seed

expect(described_class.count).to eq(2)
expect(described_class.find_by(:metric => "cpu_usagemhz_rate_average").report_types.count).to eq(1)
expect(described_class.find_by(:metric => "cpu_usagemhz_rate_average").report_types.map(&:name)).to match_array(%w(ChargebackContainerProject))

expect(described_class.find_by(:metric => "v_derived_cpu_total_cores_used").report_types.count).to eq(1)
expect(described_class.find_by(:metric => "v_derived_cpu_total_cores_used").report_types.map(&:name)).to match_array(%w(ChargebackContainerImage))
end
end
end

context 'changes report type' do
let(:seed_hash_change) do
[{:metric => 'cpu_usagemhz_rate_average', :description => 'Used CPU', :group => 'cpu', :source => 'used', :measure => 'Hz Units', :report_types => [:name => 'ChargebackContainerImage']}]
end

it 'seeds report types' do
expect(described_class.count).to eq(1)
expect(described_class.find_by(:metric => "cpu_usagemhz_rate_average").report_types.count).to eq(1)
expect(described_class.find_by(:metric => "cpu_usagemhz_rate_average").report_types.map(&:name)).to match_array(%w(ChargebackVm))

allow(described_class).to receive(:seed_data).and_return(seed_hash_change.deep_dup)
described_class.seed

expect(described_class.count).to eq(1)
expect(described_class.find_by(:metric => "cpu_usagemhz_rate_average").report_types.count).to eq(1)
expect(described_class.find_by(:metric => "cpu_usagemhz_rate_average").report_types.map(&:name)).to match_array(%w(ChargebackContainerImage))
end
end
end
end