Changed indexing from q_name -> q in compute
This commit is contained in:
parent
cfe225c014
commit
6919f9910c
@ -50,9 +50,9 @@
|
|||||||
onclick="$(this).closest('tr').prev('tr.primary').toggle(); $(this).closest('tr').prev('tr.primary').nextUntil('tr.primary', '.ingredient').toggle(); return false;">
|
onclick="$(this).closest('tr').prev('tr.primary').toggle(); $(this).closest('tr').prev('tr.primary').nextUntil('tr.primary', '.ingredient').toggle(); return false;">
|
||||||
<span class="icon icon-bullet-open"><%= i.name %></span>
|
<span class="icon icon-bullet-open"><%= i.name %></span>
|
||||||
</td>
|
</td>
|
||||||
<% @requested_n[index].each do |q_name, value| %>
|
<% @requested_n[index].each do |q, value| %>
|
||||||
<td class="primary quantity ellipsible">
|
<td class="primary quantity ellipsible">
|
||||||
<%= q_name %>
|
<%= q.name %>
|
||||||
<p class="value"><%= format_value(value) %></p>
|
<p class="value"><%= format_value(value) %></p>
|
||||||
</td>
|
</td>
|
||||||
<% end %>
|
<% end %>
|
||||||
@ -62,9 +62,9 @@
|
|||||||
<% next unless @quantities.length > 0 %>
|
<% next unless @quantities.length > 0 %>
|
||||||
<% @extra_n[index].each_slice(@quantities.length) do |values| %>
|
<% @extra_n[index].each_slice(@quantities.length) do |values| %>
|
||||||
<tr class="extra <%= row_class %>" style="display:none">
|
<tr class="extra <%= row_class %>" style="display:none">
|
||||||
<% values.each do |q_name, value| %>
|
<% values.each do |q, value| %>
|
||||||
<td class="extra quantity ellipsible">
|
<td class="extra quantity ellipsible">
|
||||||
<%= q_name %>
|
<%= q.name if value %>
|
||||||
<p class="value"><%= format_value(value) %></p>
|
<p class="value"><%= format_value(value) %></p>
|
||||||
</td>
|
</td>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
@ -49,9 +49,9 @@
|
|||||||
onclick="$(this).closest('tr').prev('tr.primary').toggle(); $(this).closest('tr').prev('tr.primary').nextUntil('tr.primary', '.measurement').toggle(); return false;">
|
onclick="$(this).closest('tr').prev('tr.primary').toggle(); $(this).closest('tr').prev('tr.primary').nextUntil('tr.primary', '.measurement').toggle(); return false;">
|
||||||
<span class="icon icon-bullet-open"><%= format_datetime(m) %></span>
|
<span class="icon icon-bullet-open"><%= format_datetime(m) %></span>
|
||||||
</td>
|
</td>
|
||||||
<% @requested_r[index].each do |q_name, value| %>
|
<% @requested_r[index].each do |q, value| %>
|
||||||
<td class="primary quantity ellipsible">
|
<td class="primary quantity ellipsible">
|
||||||
<%= q_name %>
|
<%= q.name %>
|
||||||
<p class="value"><%= format_value(value) %></p>
|
<p class="value"><%= format_value(value) %></p>
|
||||||
</td>
|
</td>
|
||||||
<% end %>
|
<% end %>
|
||||||
@ -61,9 +61,9 @@
|
|||||||
<% next unless @quantities.length > 0 %>
|
<% next unless @quantities.length > 0 %>
|
||||||
<% @extra_r[index].each_slice(@quantities.length).with_index do |values| %>
|
<% @extra_r[index].each_slice(@quantities.length).with_index do |values| %>
|
||||||
<tr class="extra <%= row_class %>" style="display:none">
|
<tr class="extra <%= row_class %>" style="display:none">
|
||||||
<% values.each do |q_name, value| %>
|
<% values.each do |q, value| %>
|
||||||
<td class="extra quantity ellipsible">
|
<td class="extra quantity ellipsible">
|
||||||
<%= q_name %>
|
<%= q.name if value %>
|
||||||
<p class="value"><%= format_value(value) %></p>
|
<p class="value"><%= format_value(value) %></p>
|
||||||
</td>
|
</td>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
@ -45,20 +45,21 @@ module BodyTracking
|
|||||||
unchecked_q = requested_q.map { |q| [q, nil] }
|
unchecked_q = requested_q.map { |q| [q, nil] }
|
||||||
unchecked_q << [filter_q, nil] if filter_q
|
unchecked_q << [filter_q, nil] if filter_q
|
||||||
|
|
||||||
subitems = Hash.new { |h,k| h[k] = {} }
|
|
||||||
item_class = proxy_association.klass
|
item_class = proxy_association.klass
|
||||||
subitem_type = item_class.nested_attributes_options.keys.first.to_s
|
subitem_type = item_class.nested_attributes_options.keys.first.to_s
|
||||||
subitem_reflection = item_class.reflections[subitem_type]
|
subitem_reflection = item_class.reflections[subitem_type]
|
||||||
subitem_class = subitem_reflection.klass
|
subitem_class = subitem_reflection.klass
|
||||||
subitems_scope = subitem_class.where(subitem_reflection.options[:inverse_of] => items)
|
subitems_scope = subitem_class.where(subitem_reflection.options[:inverse_of] => items)
|
||||||
item_foreign_key = subitem_reflection.foreign_key
|
item_foreign_key = subitem_reflection.foreign_key
|
||||||
|
subitems = Hash.new { |h,k| h[k] = {} }
|
||||||
subitems_scope.includes(:quantity, :unit)
|
subitems_scope.includes(:quantity, :unit)
|
||||||
.order('quantities.lft')
|
.order('quantities.lft')
|
||||||
.pluck(item_foreign_key, 'quantities.name', VALUE_COLUMNS[item_class],
|
.pluck('quantities.id', item_foreign_key, VALUE_COLUMNS[item_class],
|
||||||
'units.shortname')
|
'units.shortname')
|
||||||
.each { |item_id, q_name, a, u_id| subitems[q_name][item_id] = [a, u_id] }
|
.each { |q_id, item_id, a, u_id| subitems[q_id][item_id] = [a, u_id] }
|
||||||
|
Quantity.find(subitems.keys).each { |q| subitems[q] = subitems.delete(q.id) }
|
||||||
|
|
||||||
extra_q = subitems.keys - requested_q.pluck(:name)
|
extra_q = subitems.keys - requested_q
|
||||||
|
|
||||||
completed_q = {}
|
completed_q = {}
|
||||||
# FIXME: loop should finish unless there is circular dependency in formulas
|
# FIXME: loop should finish unless there is circular dependency in formulas
|
||||||
@ -69,42 +70,42 @@ module BodyTracking
|
|||||||
# quantity not computable: no formula/invalid formula (syntax error/runtime error)
|
# quantity not computable: no formula/invalid formula (syntax error/runtime error)
|
||||||
# or not requiring calculation/computed
|
# or not requiring calculation/computed
|
||||||
if !q.formula || q.formula.errors.any? || !q.formula.valid? ||
|
if !q.formula || q.formula.errors.any? || !q.formula.valid? ||
|
||||||
(subitems[q.name].length == items.count)
|
(subitems[q].length == items.count)
|
||||||
completed_q[q.name] = subitems.delete(q.name) { {} }
|
completed_q[q] = subitems.delete(q) { {} }
|
||||||
completed_q[q.name].default = [nil, nil]
|
completed_q[q].default = [nil, nil]
|
||||||
next
|
next
|
||||||
end
|
end
|
||||||
|
|
||||||
# quantity with formula requires refresh of dependencies availability
|
# quantity with formula requires refresh of dependencies availability
|
||||||
if deps.nil? || !deps.empty?
|
if deps.nil? || !deps.empty?
|
||||||
deps ||= q.formula.quantities.clone
|
deps ||= q.formula.quantities.clone
|
||||||
deps.reject! { |d| completed_q.has_key?(d.name) }
|
deps.reject! { |d| completed_q.has_key?(d) }
|
||||||
deps.each { |d| unchecked_q << [d, nil] unless unchecked_q.index { |u| u[0] == d } }
|
deps.each { |d| unchecked_q << [d, nil] unless unchecked_q.index { |u| u[0] == d } }
|
||||||
end
|
end
|
||||||
|
|
||||||
# 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)
|
output_ids = items.select { |i| subitems[q][i.id].nil? }.map(&:id)
|
||||||
input_q = q.formula.quantities
|
input_q = q.formula.quantities
|
||||||
inputs = input_q.map do |i_q|
|
inputs = input_q.map do |i_q|
|
||||||
values = completed_q[i_q.name].values_at(*output_ids)
|
values = completed_q[i_q].values_at(*output_ids)
|
||||||
values.map! { |v, u| [v || BigDecimal(0), u] } if q.formula.zero_nil
|
values.map! { |v, u| [v || BigDecimal(0), u] } if q.formula.zero_nil
|
||||||
[i_q, values]
|
[i_q, values]
|
||||||
end
|
end
|
||||||
begin
|
begin
|
||||||
calculated = q.formula.calculate(inputs.to_h)
|
calculated = q.formula.calculate(inputs.to_h)
|
||||||
rescue Exception => e
|
rescue Exception => e
|
||||||
output_ids.each { |oid| subitems[q.name][oid] = [BigDecimal::NAN, nil] }
|
output_ids.each { |oid| subitems[q][oid] = [BigDecimal::NAN, nil] }
|
||||||
q.formula.errors.add(
|
q.formula.errors.add(
|
||||||
:code, :computation_failed,
|
:code, :computation_failed,
|
||||||
{
|
{
|
||||||
quantity: q.name,
|
quantity: q.name,
|
||||||
description: e.message,
|
description: e.message,
|
||||||
count: output_ids.size == subitems[q.name].size ? 'all' : output_ids.size
|
count: output_ids.size == subitems[q].size ? 'all' : output_ids.size
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
else
|
else
|
||||||
output_ids.each_with_index { |oid, idx| subitems[q.name][oid] = calculated[idx] }
|
output_ids.each_with_index { |oid, idx| subitems[q][oid] = calculated[idx] }
|
||||||
end
|
end
|
||||||
unchecked_q.unshift([q, deps])
|
unchecked_q.unshift([q, deps])
|
||||||
next
|
next
|
||||||
@ -116,11 +117,9 @@ module BodyTracking
|
|||||||
|
|
||||||
all_q = subitems.merge(completed_q)
|
all_q = subitems.merge(completed_q)
|
||||||
[
|
[
|
||||||
filter_q ? items.to_a.keep_if { |i| all_q[filter_q.name][i.id][0] } : items,
|
filter_q ? items.to_a.keep_if { |i| all_q[filter_q][i.id][0] } : items,
|
||||||
items.map { |i| requested_q.map { |q| [q.name, all_q[q.name][i.id]] } },
|
items.map { |i| requested_q.map { |q| [q, all_q[q][i.id]] } },
|
||||||
items.map do |i|
|
items.map { |i| extra_q.map { |q| [q, all_q[q][i.id]] } }
|
||||||
extra_q.map { |q_name| [q_name, all_q[q_name][i.id]] if all_q[q_name][i.id] }
|
|
||||||
end
|
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user