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