Skip to content

Commit

Permalink
Merge pull request #4903 from h-kataria/bottlenecks_report_download_fix
Browse files Browse the repository at this point in the history
Fixed code to show Bottlenecks report download buttons.
  • Loading branch information
martinpovolny authored Nov 13, 2018
2 parents 42cd639 + 30ed201 commit e1e7030
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 13 deletions.
9 changes: 8 additions & 1 deletion app/controllers/bottlenecks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def change_tab

# build timeline data when coming back to Summary tab for bottlenecks
displaying_timeline = @sb[:active_tab] == "summary"
bottleneck_get_node_info(x_node) if displaying_timeline
get_node_info(x_node) if displaying_timeline

render :update do |page|
page << javascript_prologue
Expand All @@ -35,6 +35,13 @@ def change_tab
end
end

# Send the current utilization report data in text, CSV, or PDF
def report_download
filename = "Bottlenecks Event Details"
disable_client_cache
download_file(params[:typ], @sb[:report], filename)
end

def tree_select
if params[:id] # First time thru async method, grab id parm info
@refresh = (x_node == "")
Expand Down
11 changes: 1 addition & 10 deletions app/controllers/planning_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,7 @@ def report_download
@sb[:rpt].title = _("Counts of VMs (%{profile})") % {:profile => profile.join(", ")}
filename = "VM Counts per #{ui_lookup(:model => @sb[:options][:target_typ])}"
disable_client_cache
case params[:typ]
when "txt"
send_data(@sb[:rpt].to_text,
:filename => "#{filename}.txt")
when "csv"
send_data(@sb[:rpt].to_csv,
:filename => "#{filename}.csv")
when "pdf"
render_pdf(@sb[:rpt])
end
download_file(params[:typ], @sb[:rpt], filename)
end

def reset
Expand Down
3 changes: 3 additions & 0 deletions app/helpers/application_helper/button/utilization_download.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ def disabled?
# b) we are in the "Utilization" and have trend report and summary
return false if @sb.fetch_path(:util, :trend_rpt) && @sb.fetch_path(:util, :summary)

# c) we are in the "Bottlenecks" on 'Report' tab and have report data available
return false if @layout == 'miq_capacity_bottlenecks' && @sb[:active_tab] == 'report' && !@sb[:report].table.data.empty?

# otherwise the button is off
@error_message = _('No records found for this report')
@error_message.present?
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/application_helper/toolbar_chooser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def history_toolbar_filename
def x_view_toolbar_filename
if x_gtl_view_tb_render?
'x_gtl_view_tb'
elsif %w(miq_capacity_planning miq_capacity_utilization).include?(@layout)
elsif %w(miq_capacity_bottlenecks miq_capacity_planning miq_capacity_utilization).include?(@layout)
'miq_capacity_view_tb'
elsif @record && @explorer && (%w(services catalogs).include?(@layout) || %w(performance timeline).include?(@display))
nil
Expand Down
13 changes: 13 additions & 0 deletions app/helpers/optimize_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,17 @@ def get_nodetype_and_record(treenodeid)
@record = Storage.find_by(:id => nodeid)
end
end

def download_file(typ, report, filename)
case typ
when "txt"
send_data(report.to_text,
:filename => "#{filename}.txt")
when "csv"
send_data(report.to_csv,
:filename => "#{filename}.csv")
when "pdf"
render_pdf(report)
end
end
end
2 changes: 1 addition & 1 deletion app/views/bottlenecks/_bottlenecks_tabs.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
= render :partial => "bottlenecks_report"

:javascript
miq_tabs_init('bottlenecks_tabs', '/bottlenecks/change_tab/');
miq_tabs_init('#bottlenecks_tabs', '/bottlenecks/change_tab/');
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2238,9 +2238,11 @@
:bottlenecks => {
:get => %w(
index
report_download
timeline_data
),
:post => %w(
change_tab
reload
tl_chooser
tree_autoload
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
describe ApplicationHelper::Button::UtilizationDownload do
let(:view_context) { setup_view_context_with_sandbox({}) }
let(:report) { FactoryGirl.create(:miq_report, :miq_report_results => []) }
let(:button) { described_class.new(view_context, {}, {'layout' => 'miq_capacity_bottlenecks'}, {}) }

before do
button.instance_variable_set(:@sb, :active_tab => "report", :report => report)
allow(view_context).to receive(:x_active_tree).and_return(:bottlenecks_tree)
end

context '#disabled?' do
it 'when report tab has no data available' do
report.table = OpenStruct.new(:data => [])
expect(button.disabled?).to be_truthy
end

it 'when report tab has data' do
report.table = OpenStruct.new(:data => [:foo => 'bar'])
expect(button.disabled?).to be_falsey
end

it 'when on summary tab' do
button.instance_variable_set(:@sb, :active_tab => "summary", :report => report)
report.table = OpenStruct.new(:data => [:foo => 'bar'])
expect(button.disabled?).to be_truthy
end
end
end
7 changes: 7 additions & 0 deletions spec/helpers/application_helper/toolbar_chooser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,5 +172,12 @@
expect(ApplicationHelper::ToolbarChooser.new(nil, nil, :record => @record, :explorer => true, :layout => 'automation_manager', :sb => @sb).send(:x_view_toolbar_filename)).to eq("x_gtl_view_tb")
end
end

context 'when in bottlenecks explorer' do
it "displays toolbar" do
@sb = {:active_tab => 'summary'}
expect(ApplicationHelper::ToolbarChooser.new(nil, nil, :record => @record, :explorer => true, :layout => 'miq_capacity_bottlenecks', :sb => @sb).send(:x_view_toolbar_filename)).to eq("miq_capacity_view_tb")
end
end
end
end
12 changes: 12 additions & 0 deletions spec/routing/bottlenecks_routing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
end
end

describe '#report_download' do
it 'routes with GET' do
expect(get('/bottlenecks/report_download')).to route_to('bottlenecks#report_download')
end
end

describe '#timeline_data' do
it 'routes with GET' do
expect(get('/bottlenecks/timeline_data')).to route_to('bottlenecks#timeline_data')
Expand All @@ -33,6 +39,12 @@
end
end

describe '#change_tab' do
it 'routes with POST' do
expect(post('/bottlenecks/change_tab')).to route_to('bottlenecks#change_tab')
end
end

describe '#wait_for_task' do
it 'routes with POST' do
expect(post('/bottlenecks/wait_for_task')).to route_to('bottlenecks#wait_for_task')
Expand Down

0 comments on commit e1e7030

Please sign in to comment.