Skip to content

Commit

Permalink
Cache MiqExpression.get_col_type in MiqReport::Formatting
Browse files Browse the repository at this point in the history
This call will make the same call to the DB over and over again on
certain attributes, and is incredibly "N+1y" on large reports.

This adds a simple cache for the data return from the DB calls to make
sure they are only executed once per column.  Moved into a separate
method to break up the bulk of the current method.

Note:  An extra `to_sym` was removed when in the new method since it
would always get executed on the next line if the value existed.
Probably could remove the `unless` as well since at that point, we
should be clear... 🤷
  • Loading branch information
NickLaMuro committed Mar 23, 2018
1 parent 388f266 commit f20a3aa
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions app/models/miq_report/formatting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,7 @@ def format(col, value, options = {})

# Use default format for column stil nil
if format.nil? || format == :_default_
expression_col = col_to_expression_col(col)
dt = MiqExpression.get_col_type(expression_col)
dt = value.class.to_s.downcase.to_sym if dt.nil?
dt = dt.to_sym unless dt.nil?
format = MiqReport::Formats.default_format_details_for(expression_col, col, dt)
format = format_from_miq_expression(col, value)
else
format = format.deep_clone # Make sure we don't taint the original
end
Expand Down Expand Up @@ -247,4 +243,19 @@ def format_large_number_to_exponential_form(val, _options = {})
def format_model_name(val, _options = {})
ui_lookup(:model => val)
end

private

def format_from_miq_expression(col, value)
@miq_exp_dt_map ||= {}
expression_col = col_to_expression_col(col)

unless @miq_exp_dt_map.has_key?(col)
@miq_exp_dt_map[col] = MiqExpression.get_col_type(expression_col)
end
dt = @miq_exp_dt_map[col]
dt = value.class.to_s.downcase if dt.nil?
dt = dt.to_sym unless dt.nil?
MiqReport::Formats.default_format_details_for(expression_col, col, dt)
end
end

0 comments on commit f20a3aa

Please sign in to comment.