Skip to content

Commit

Permalink
Merge pull request #1228 from kbrock/tree_reports_report_results
Browse files Browse the repository at this point in the history
TreeBuilderSavedReports reduce queries
  • Loading branch information
martinpovolny authored May 4, 2017
2 parents faa2eb5 + 83dafbb commit a26db2c
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions app/presenters/tree_builder_report_saved_reports.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,26 @@ def root_options
end

# Get root nodes count/array for explorer tree
def x_get_tree_roots(count_only, _options)
folder_ids = {}
def x_get_tree_roots(_count_only, _options)
u = User.current_user
user_groups = u.admin_user? ? nil : u.miq_groups
MiqReport.having_report_results(:miq_groups => user_groups, :select => [:id, :name]).each do |r|
folder_ids[r.name] = to_cid(r.id.to_i)
having_report_results(user_groups).pluck(:name, :id).sort.map do |name, id|
{:id => to_cid(id.to_i), :text => name, :icon => 'fa fa-file-text-o', :tip => name}
end
objects = []
folder_ids.sort.each_with_index do |p|
objects.push(:id => p[1], :text => p[0], :icon => 'fa fa-file-text-o', :tip => p[0])
end
count_only_or_objects(count_only, objects)
end

def x_get_tree_custom_kids(object, count_only, _options)
scope = MiqReportResult.with_current_user_groups_and_report(from_cid(object[:id].split('-').last))
count_only ? scope.size : scope.order("last_run_on DESC").includes(:miq_task).to_a
count_only ? 1 : scope.order("last_run_on DESC").includes(:miq_task).to_a
end

# Scope on reports that have report results.
def having_report_results(miq_groups)
miq_group_relation = MiqReport.joins(:miq_report_results).distinct
if miq_groups.nil? # u.admin_user?
miq_group_relation.where.not(:miq_report_results => {:miq_group_id => nil})
else
miq_group_relation.where(:miq_report_results => {:miq_group_id => miq_groups})
end
end
end

0 comments on commit a26db2c

Please sign in to comment.