Skip to content

Commit

Permalink
Merge pull request ManageIQ#11570 from yrudman/added-generation-of-ch…
Browse files Browse the repository at this point in the history
…argeback-report-for-service

Added generation of chargeback report for service
  • Loading branch information
gtanzillo authored Oct 3, 2016
2 parents 0a7e365 + 630dacb commit d96040e
Show file tree
Hide file tree
Showing 3 changed files with 304 additions and 1 deletion.
37 changes: 37 additions & 0 deletions app/models/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,41 @@ def tenant_identity
user = User.super_admin.tap { |u| u.current_group = miq_group } if user.nil? || !user.miq_group_ids.include?(miq_group_id)
user
end

def self.queue_chargeback_reports(options = {})
Service.all.each do |s|
s.queue_chargeback_report_generation(options) unless s.vms.empty?
end
end

def chargeback_report_name
"Chargeback-Vm-Monthly-#{name}"
end

def generate_chargeback_report(options = {})
_log.info "Generation of chargeback report for service #{name} started..."
MiqReportResult.where(:name => chargeback_report_name).destroy_all
report = MiqReport.new(chargeback_yaml)
report.queue_generate_table(options)
_log.info "Report #{chargeback_report_name} generated"
end

def chargeback_yaml
yaml = YAML.load_file(File.join(Rails.root, "product/chargeback/chargeback_vm_monthly.yaml"))
yaml["db_options"][:options][:service_id] = id
yaml["title"] = chargeback_report_name
yaml
end

def queue_chargeback_report_generation(options = {})
MiqQueue.put(
:role => "reporting",
:class_name => self.class.name,
:instance_id => id,
:method_name => "generate_chargeback_report",
:priority => MiqQueue::NORMAL_PRIORITY,
:args => options
)
_log.info "Added to queue: generate_chargeback_report for service #{name}"
end
end
191 changes: 191 additions & 0 deletions product/chargeback/chargeback_vm_monthly.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
---
title: Chargeback_Vm_Monthly
rpt_group: Custom
rpt_type: Custom
priority:
db: ChargebackVm
cols:
- start_date
- display_range
- vm_name
- cpu_allocated_cost
- cpu_used_metric
- cpu_used_cost
- memory_allocated_cost
- memory_used_metric
- memory_used_cost
- disk_io_used_metric
- disk_io_used_cost
- net_io_used_metric
- net_io_used_cost
- storage_allocated_metric
- storage_allocated_cost
- storage_used_metric
- storage_used_cost
include: {}
col_order:
- vm_name
- display_range
- cpu_allocated_cost
- cpu_used_metric
- cpu_used_cost
- memory_allocated_cost
- memory_used_metric
- memory_used_cost
- disk_io_used_metric
- disk_io_used_cost
- net_io_used_metric
- net_io_used_cost
- storage_allocated_metric
- storage_allocated_cost
- storage_used_metric
- storage_used_cost
headers:
- VM Name
- Date Range
- vCPUs Allocated Cost
- CPU Used
- CPU Used Cost
- Memory Allocated Cost
- Memory Used
- Memory Used Cost
- Disk I/O Used
- Disk I/O Used Cost
- Network I/O Used
- Network I/O Used Cost
- Storage Allocated
- Storage Allocated Cost
- Storage Used
- Storage Used Cost
conditions:
order: Ascending
sortby:
- vm_name
- start_date
group: y
template_type: report
db_options:
:rpt_type: ChargebackVm
:options:
:interval: monthly
:interval_size: 1
:end_interval_offset: 0
col_formats:
-
-
-
- :mhz
-
-
-
-
-
-
-
-
-
-
-
-
tz: UTC
col_options:
cpu_allocated_cost:
:grouping:
- :total
cpu_allocated_metric:
:grouping:
- :total
cpu_cost:
:grouping:
- :total
cpu_metric:
:grouping:
- :total
cpu_used_cost:
:grouping:
- :total
cpu_used_metric:
:grouping:
- :total
disk_io_cost:
:grouping:
- :total
disk_io_metric:
:grouping:
- :total
disk_io_used_cost:
:grouping:
- :total
disk_io_used_metric:
:grouping:
- :total
fixed_compute_metric:
:grouping:
- :total
fixed_compute_1_cost:
:grouping:
- :total
fixed_compute_2_cost:
:grouping:
- :total
fixed_cost:
:grouping:
- :total
fixed_storage_1_cost:
:grouping:
- :total
fixed_storage_2_cost:
:grouping:
- :total
memory_allocated_cost:
:grouping:
- :total
memory_allocated_metric:
:grouping:
- :total
memory_cost:
:grouping:
- :total
memory_metric:
:grouping:
- :total
memory_used_cost:
:grouping:
- :total
memory_used_metric:
:grouping:
- :total
net_io_cost:
:grouping:
- :total
net_io_metric:
:grouping:
- :total
net_io_used_cost:
:grouping:
- :total
net_io_used_metric:
:grouping:
- :total
storage_allocated_cost:
:grouping:
- :total
storage_allocated_metric:
:grouping:
- :total
storage_cost:
:grouping:
- :total
storage_metric:
:grouping:
- :total
storage_used_cost:
:grouping:
- :total
storage_used_metric:
:grouping:
- :total
total_cost:
:grouping:
- :total

77 changes: 76 additions & 1 deletion spec/models/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,6 @@
expect(@service.next_group_index(0, -1)).to be_nil
end


it "should not allow the same VM to be added to more than one services" do
vm = Vm.first
@service.save
Expand All @@ -264,6 +263,82 @@
end
end

context "Chargeback report generation" do
before do
@vm = FactoryGirl.create(:vm_vmware)
@vm_1 = FactoryGirl.create(:vm_vmware)

@service = FactoryGirl.create(:service)
@service.name = "Test_Service_1"
@service_c1 = FactoryGirl.create(:service, :service => @service)
@service_c1.name = "Test_Service_2"
@service << @vm
@service_c1 << @vm_1
@service.save
@service_c1.save
end

describe ".queue_chargeback_reports" do
it "queue request to generate chargeback report for each service" do
expect(MiqQueue).to receive(:put).twice
described_class.queue_chargeback_reports
end
end

describe "#chargeback_report_name" do
it "creates chargeback report's name" do
expect(@service.chargeback_report_name).to eq "Chargeback-Vm-Monthly-Test_Service_1"
expect(@service_c1.chargeback_report_name).to eq "Chargeback-Vm-Monthly-Test_Service_2"
end
end

describe "#queue_chargeback_report_generation" do
it "queue request to generate chargeback report" do
expect(MiqQueue).to receive(:put) do |args|
expect(args).to have_attributes(:class_name => described_class.name,
:method_name => "generate_chargeback_report",
:args => {:report_source => "Test Run"})
end
@service.queue_chargeback_report_generation(:report_source => "Test Run")
end
end

describe "#generate_chargeback_report" do
it "delete existing chargeback report result for service before generating new one" do
FactoryGirl.create(:miq_chargeback_report_result, :name => @service.chargeback_report_name)
expect(MiqReportResult.count).to eq 1

report = double("MiqReport")
allow(MiqReport).to receive(:new).and_return(report)
expect(report).to receive(:queue_generate_table)

@service.generate_chargeback_report
expect(MiqReportResult.count).to eq 0
end

it "loads report template and initiate generation" do
EvmSpecHelper.local_miq_server
@service.generate_chargeback_report
expect(MiqReportResult.count).to eq 1
expect(MiqReportResult.first.name).to eq @service.chargeback_report_name
end
end

describe "#chargeback_yaml" do
it "loads chargeback report template" do
@user = FactoryGirl.create(:user_with_group)
report_yaml = @service.chargeback_yaml

report = MiqReport.new(report_yaml)
allow(Chargeback).to receive(:build_results_for_report_chargeback)
report.generate_table(:userid => @user.userid)
cols_from_data = report.table.column_names.to_set
cols_from_yaml = report_yaml['col_order'].to_set
expect(cols_from_yaml).to be_subset(cols_from_data)
end
end
end

describe "#children" do
it "returns children" do
create_deep_tree
Expand Down

0 comments on commit d96040e

Please sign in to comment.