Skip to content

Commit

Permalink
Merge pull request #14312 from kbrock/report_only_cols
Browse files Browse the repository at this point in the history
Screens now display include columns
  • Loading branch information
martinpovolny authored Jul 4, 2017
2 parents d0cf206 + 4e3afb7 commit af4ff1d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
13 changes: 10 additions & 3 deletions app/models/miq_report/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,8 @@ def build_table(data, db, options = {})
self.col_order = (col_order + res_cols).uniq
end

only_cols = options[:only] || ((cols || []) + build_cols_from_include(include)).uniq + ['id']
self.col_order = ((cols || []) + build_cols_from_include(include)).uniq if col_order.blank?
only_cols = options[:only] || cols_for_report(['id'])
self.col_order = cols_for_report if col_order.blank?

build_trend_data(objs)
build_trend_limits(objs)
Expand Down Expand Up @@ -417,7 +417,7 @@ def build_table_from_report(options = {})
result = generate_rows_from_data(get_data_from_report(db_options[:report]))

self.cols ||= []
only_cols = options[:only] || (self.cols + generate_cols + build_cols_from_include(include)).uniq
only_cols = options[:only] || cols_for_report(generate_cols)
column_names = result.empty? ? self.cols : result.first.keys
@table = Ruport::Data::Table.new(:data => result, :column_names => column_names)
@table.reorder(only_cols) unless @table.data.empty?
Expand Down Expand Up @@ -611,6 +611,13 @@ def build_pivot(data)
end
end

# the columns that are needed for this report.
# there may be some columns that are used to derive columns,
# so we currently include '*'
def cols_for_report(extra_cols = [])
((cols || []) + (col_order || []) + (extra_cols || []) + build_cols_from_include(include)).uniq
end

def build_cols_from_include(hash, parent_association = nil)
return [] if hash.blank?
hash.inject([]) do |a, (k, v)|
Expand Down
2 changes: 1 addition & 1 deletion app/models/miq_report/generator/trend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def build_results_for_report_trend(options)
klass = db.kind_of?(Class) ? db : Object.const_get(db)
self.title = klass.report_title(db_options)
self.cols, self.headers = klass.report_cols(db_options)
options[:only] ||= (cols + build_cols_from_include(include) + ['id']).uniq
options[:only] ||= cols_for_report

# Build and filter trend data from performance data
build_apply_time_profile(results)
Expand Down
31 changes: 31 additions & 0 deletions spec/models/miq_report/generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,35 @@ def create_rollup(host, profile, used_mem)
end
end
end

describe "#cols_for_report" do
it "uses cols" do
rpt = MiqReport.new(:db => "VmOrTemplate", :cols => %w(vendor version name))
expect(rpt.cols_for_report).to eq(%w(vendor version name))
end

it "uses include" do
rpt = MiqReport.new(:db => "VmOrTemplate", :include => {"host" => { "columns" => %w(name hostname guid)}})
expect(rpt.cols_for_report).to eq(%w(host.name host.hostname host.guid))
end

it "uses extra_cols" do
rpt = MiqReport.new(:db => "VmOrTemplate")
expect(rpt.cols_for_report(%w(vendor))).to eq(%w(vendor))
end

it "derives include" do
rpt = MiqReport.new(:db => "VmOrTemplate", :cols => %w(vendor), :col_order =>%w(host.name vendor))
expect(rpt.cols_for_report).to match_array(%w(vendor host.name))
end

it "works with col, col_order and include together" do
rpt = MiqReport.new(:db => "VmOrTemplate",
:cols => %w(vendor),
:col_order => %w(host.name host.hostname vendor),
:include => {"host" => { "columns" => %w(name hostname)}}
)
expect(rpt.cols_for_report).to match_array(%w(vendor host.name host.hostname))
end
end
end

0 comments on commit af4ff1d

Please sign in to comment.