Skip to content

Commit

Permalink
Pass in references to rbac
Browse files Browse the repository at this point in the history
includes() are used in ruby
references() are used by sql

Tables used in the select or where clause need to be joined
using references, joins, or outer_joins.

Up until now, we've treated them as the same. But it is taxing to have
so many tables in the query. So they are split apart.

:include is used in the select column, so it is referenced
miq_expression is used in the where clause, so it is referenced
include_for_find is only used to preload models. 

---

If references are not passed in, it defaults to existing behavior.
Meaning, all includes are in references.

But if references are passed in, then only those tables are referenced()
  • Loading branch information
kbrock committed Sep 12, 2019
1 parent cd434a4 commit b3f5682
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 4 deletions.
7 changes: 4 additions & 3 deletions app/models/miq_report/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,13 +253,12 @@ def generate_daily_metric_rollup_results(options = {})
end

time_range = Metric::Helper.time_range_from_offset(interval, db_options[:start_offset], db_options[:end_offset], tz)
db_includes = get_include_for_find
results = Metric::Helper.find_for_interval_name('daily', time_profile || tz, db_klass)
.where(where_clause)
.where(options[:where_clause])
.where(:timestamp => time_range)
.includes(db_includes)
.references(Rbac.includes_to_references(db_klass, db_includes))
.includes(get_include_for_find)
.references(Rbac.includes_to_references(db_klass, get_include))
.limit(options[:limit])
results = Rbac.filtered(results, :class => db,
:filter => conditions,
Expand All @@ -276,6 +275,7 @@ def generate_interval_metric_results(options = {})
.where(where_clause)
.where(options[:where_clause])
.includes(get_include_for_find)
.references(Rbac.includes_to_references(db_klass, get_include))
.limit(options[:limit])

# Only build where clause from expression for hourly report. It will not work properly for daily because many values are rolled up from hourly.
Expand Down Expand Up @@ -308,6 +308,7 @@ def generate_basic_results(options = {})
:targets => targets,
:filter => conditions,
:include_for_find => get_include_for_find,
:references => get_include,
:where_clause => where_clause,
:skip_counts => true
)
Expand Down
1 change: 1 addition & 0 deletions app/models/miq_report/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ def paged_view_search(options = {})
search_options = options.merge(:class => db,
:conditions => conditions,
:include_for_find => includes,
:references => get_include,
:skip_references => skip_references
)
search_options.merge!(:limit => limit, :offset => offset, :order => order) if order
Expand Down
1 change: 1 addition & 0 deletions app/models/vim_performance_analysis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def get_targets
search_options = {
:class => topts[:compute_type].to_s,
:include_for_find => includes,
:references => includes,
:userid => @options[:userid],
:miq_group_id => @options[:miq_group_id],
}
Expand Down
5 changes: 4 additions & 1 deletion lib/rbac/filterer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ def self.accessible_tenant_ids_strategy(klass)
# @option options :where_clause []
# @option options :sub_filter
# @option options :include_for_find [Array<Symbol>, Hash{Symbol => Symbol,Hash,Array}] models included but not in query
# @option options :references [Array<Symbol>], models used by select and where. If not passed, uses include_for_find instead
# @option options :filter [MiqExpression] (optional)

# @option options :user [User] (default: current_user)
Expand Down Expand Up @@ -207,6 +208,7 @@ def search(options = {})
where_clause = options[:where_clause]
sub_filter = options[:sub_filter]
include_for_find = options[:include_for_find]
references = options.fetch(:references) { include_for_find }
search_filter = options[:filter]

limit = options[:limit] || targets.try(:limit_value)
Expand Down Expand Up @@ -266,7 +268,7 @@ def search(options = {})
.includes(include_for_find).includes(exp_includes)
.order(order)

scope = include_references(scope, klass, include_for_find, exp_includes, skip_references)
scope = include_references(scope, klass, references, exp_includes, skip_references)
scope = scope.limit(limit).offset(offset) if attrs[:apply_limit_in_sql]

if inline_view?(options, scope)
Expand Down Expand Up @@ -398,6 +400,7 @@ def skip_references?(scope, options, attrs, exp_sql, exp_includes)
return false if scope.singleton_class.included_modules.include?(ActiveRecord::NullRelation)
options[:skip_references] ||
(options[:extra_cols].blank? &&
!options.key?(:references) &&
(!attrs[:apply_limit_in_sql] && (exp_sql.nil? || exp_includes.nil?)))
end

Expand Down

0 comments on commit b3f5682

Please sign in to comment.