Optimized formula computation to run 'eval' once
This commit is contained in:
parent
a4ec6a0c12
commit
b437bee18b
@ -7,7 +7,7 @@ module IngredientsHelper
|
||||
|
||||
def toggle_column_options
|
||||
disabled = []
|
||||
enabled_columns = @project.nutrients_column_view.quantities
|
||||
enabled_columns = @project.nutrients_column_view.quantities.to_a
|
||||
options = nested_set_options(@project.quantities.diet) do |q|
|
||||
disabled << q.id if enabled_columns.include?(q)
|
||||
raw("#{' ' * q.level}#{q.name}")
|
||||
|
@ -89,11 +89,13 @@ module BodyTracking
|
||||
@quantities = quantities
|
||||
@paramed_formula = Ripper.lex(@formula).map do |*, ttype, token|
|
||||
if QUANTITY_TTYPES.include?(ttype) && quantities_names.include?(token)
|
||||
"params['#{token}']"
|
||||
"params['#{token}'][_index]"
|
||||
else
|
||||
token
|
||||
end
|
||||
end.join
|
||||
@paramed_formula =
|
||||
"params.values.first.each_with_index.map { |*, _index| #{@paramed_formula} }"
|
||||
end
|
||||
|
||||
errors
|
||||
@ -112,14 +114,13 @@ module BodyTracking
|
||||
def calculate(inputs)
|
||||
raise RuntimeError, 'Invalid formula' unless self.valid?
|
||||
|
||||
inputs.map do |i, values|
|
||||
puts values.inspect
|
||||
begin
|
||||
[i, [get_binding(values).eval(@paramed_formula), nil]]
|
||||
rescue Exception => e
|
||||
puts e.message
|
||||
[i, [nil, nil]]
|
||||
end
|
||||
values = inputs.map { |q, v| [q.name, v.transpose[0]] }.to_h
|
||||
puts values.inspect
|
||||
begin
|
||||
get_binding(values).eval(@paramed_formula).map { |x| [x, nil] }
|
||||
rescue Exception => e
|
||||
puts e.message
|
||||
[[nil, nil]] * inputs.values.first.length
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -69,6 +69,7 @@ module BodyTracking
|
||||
# quantity not computable (no formula) or not requiring calculation/computed
|
||||
if q.formula.blank? || (subitems[q.name].length == items.count)
|
||||
completed_q[q.name] = subitems.delete(q.name) { {} }
|
||||
completed_q[q.name].default = [nil, nil]
|
||||
next
|
||||
end
|
||||
|
||||
@ -81,17 +82,12 @@ module BodyTracking
|
||||
|
||||
# quantity with formula has all dependencies satisfied, requires calculation
|
||||
if deps.empty?
|
||||
output_ids = items.select { |i| subitems[q.name][i.id].nil? }.map(&:id)
|
||||
input_q = q.formula_quantities
|
||||
inputs = items.select { |i| subitems[q.name][i.id].nil? }.map do |i|
|
||||
[
|
||||
i,
|
||||
input_q.map do |i_q|
|
||||
subitem_data = completed_q[i_q.name][i.id] || [nil, nil]
|
||||
[i_q.name, subitem_data[0]]
|
||||
end.to_h
|
||||
]
|
||||
inputs = input_q.map { |i_q| [i_q, completed_q[i_q.name].values_at(*output_ids)] }
|
||||
q.calculate(inputs.to_h).each_with_index do |result, index|
|
||||
subitems[q.name][output_ids[index]] = result
|
||||
end
|
||||
q.calculate(inputs).each { |i, result| subitems[q.name][i.id] = result }
|
||||
unchecked_q.unshift([q, deps])
|
||||
next
|
||||
end
|
||||
|
Reference in New Issue
Block a user