1
0

Displaying Measurement readouts, WIP

This commit is contained in:
cryptogopher 2019-12-10 20:37:36 +01:00
parent 40e9c6f8ae
commit 98564be4b5
7 changed files with 200 additions and 11 deletions

View File

@ -71,7 +71,7 @@ class IngredientsController < ApplicationController
warnings = []
if params.has_key?(:file)
quantities = @project.quantities.map { |q| [q.name, q] }.to_h
quantities = @project.quantities.diet.map { |q| [q.name, q] }.to_h
units = @project.units.map { |u| [u.shortname, u] }.to_h
sources = @project.sources.map { |s| [s.name, s] }.to_h
ingredients_params = []
@ -207,7 +207,7 @@ class IngredientsController < ApplicationController
end
def prepare_nutrients
@quantities = @project.quantities.where(primary: true)
@quantities = @project.quantities.diet.where(primary: true)
ingredients, requested_n, extra_n, @formula_q = @project.ingredients
.filter(@project, session[:i_filters], @quantities)

View File

@ -1,12 +1,13 @@
class MeasurementsController < ApplicationController
menu_item :body_trackers
before_action :init_session_filters
before_action :find_project_by_project_id, only: [:index, :new, :create]
before_action :find_measurement, only: [:edit, :update, :destroy, :retake, :readouts]
before_action :authorize
def index
session[:m_scope] = {}
session[:m_filters][:scope] = {}
prepare_measurements
end
@ -55,12 +56,16 @@ class MeasurementsController < ApplicationController
end
def readouts
session[:m_scope] = {name: @measurement.name}
prepare_measurements
session[:m_filters][:scope] = {name: @measurement.name}
prepare_readouts
end
private
def init_session_filters
session[:m_filters] ||= {}
end
def measurement_params
params.require(:measurement).permit(
:name,
@ -87,6 +92,28 @@ class MeasurementsController < ApplicationController
def prepare_measurements
@measurements = @project.measurements.includes(:source, :readouts)
.where(session[:m_scope])
.filter(session[:m_filters])
end
def prepare_readouts
@quantities = @project.quantities.measurement.where(primary: true)
measurements, requested_r, extra_r, @formula_q = @project.measurements.includes(:source)
.filter(@project, session[:i_filters], @quantities)
@nutrients = {}
@extra_nutrients = {}
ingredients.each_with_index do |i, index|
@nutrients[i] = []
requested_n[index].each do |q_name, value|
amount, unitname = value
@nutrients[i] << [q_name, amount.nil? ? '-' : "#{amount} [#{unitname || '-'}]"]
end
@extra_nutrients[i] = []
extra_n[index].each do |q_name, value|
amount, unitname = value
@extra_nutrients[i] << [q_name, amount.nil? ? '-' : "#{amount} [#{unitname || '-'}]"]
end
end
end
end

View File

@ -1,4 +1,12 @@
module MeasurementsHelper
def format_datetime(m)
m.taken_at.getlocal.strftime("%F <small>%R</small>").html_safe
end
def format_time(m)
m.taken_at.getlocal.strftime("%R")
end
def quantity_options
nested_set_options(@project.quantities.measurement) do |q|
raw("#{'&ensp;' * q.level}#{q.name}")

View File

@ -45,4 +45,92 @@ class Measurement < ActiveRecord::Base
def taken_at_time=(value)
self.taken_at = Time.parse(value, self.taken_at)
end
def self.filter(filters, requested_q = Quantity.none)
measurements = all.where(filters[:scope])
if filters[:name].present?
measurements = measurements.where("name LIKE ?", "%#{filters[:name]}%")
end
project = proxy_association.owner
formula_q = if filters[:readouts].present?
project.quantities.new(name: '__internal_q',
formula: filters[:readouts],
domain: :measurement)
end
apply_formula = formula_q.present? && formula_q.valid?
result =
if !requested_q.empty? || apply_formula
computed = measurements.compute_nutrients(requested_q, apply_formula && formula_q)
requested_q.present? ? computed : [computed[0]]
else
[measurements]
end
result.push(formula_q)
end
def self.compute_nutrients(requested_q, filter_q = nil)
ingredients = all
unchecked_q = requested_q.map { |q| [q, nil] }
unchecked_q << [filter_q, nil] if filter_q
nutrients = Hash.new { |h,k| h[k] = {} }
Nutrient.where(ingredient: ingredients).includes(:quantity, :unit)
.order('quantities.lft')
.pluck('quantities.name', :ingredient_id, :amount, 'units.shortname')
.each { |q_name, i_id, a, u_id| nutrients[q_name][i_id] = [a, u_id] }
extra_q = nutrients.keys - requested_q.pluck(:name)
completed_q = {}
# FIXME: loop should finish unless there is circular dependency in formulas
# for now we don't guard against that
while !unchecked_q.empty?
q, deps = unchecked_q.shift
# quantity not computable (no formula) or not requiring calculation/computed
if q.formula.blank? || (nutrients[q.name].length == ingredients.count)
completed_q[q.name] = nutrients.delete(q.name) { {} }
next
end
# quantity with formula requires refresh of dependencies availability
if deps.nil? || !deps.empty?
deps ||= q.formula_quantities
deps.reject! { |q| completed_q.has_key?(q.name) }
deps.each { |q| unchecked_q << [q, nil] unless unchecked_q.index { |u| u[0] == q } }
end
# quantity with formula has all dependencies satisfied, requires calculation
if deps.empty?
input_q = q.formula_quantities
inputs = ingredients.select { |i| nutrients[q.name][i.id].nil? }.map do |i|
[
i,
input_q.map do |i_q|
nutrient_data = completed_q[i_q.name][i.id] || [nil, nil]
[i_q.name, nutrient_data[0]]
end.to_h
]
end
q.calculate(inputs).each { |i, result| nutrients[q.name][i.id] = result }
unchecked_q.unshift([q, deps])
next
end
# quantity still has unsatisfied dependencies, move to the end of queue
unchecked_q << [q, deps]
end
all_q = nutrients.merge(completed_q)
[
filter_q ? ingredients.to_a.keep_if { |i| all_q[filter_q.name][i.id][0] } : ingredients,
ingredients.map { |i| requested_q.map { |q| [q.name, all_q[q.name][i.id]] } },
ingredients.map do |i|
extra_q.map { |q_name| [q_name, all_q[q_name][i.id]] if all_q[q_name][i.id] }
end
]
end
end

View File

@ -6,8 +6,8 @@
<p><%= f.text_field :name, size: 40, required: true %></p>
<p>
<%= f.date_field :taken_at_date, required: true %>
<%= f.time_field :taken_at_time, value: @measurement.taken_at.getlocal.strftime("%R"),
required: true, label: '' %>
<%= f.time_field :taken_at_time, value: format_time(@measurement), required: true,
label: '' %>
</p>
<% @measurement.readouts.each_with_index do |r, index| %>
<%= f.fields_for 'readouts_attributes', r, index: '' do |ff| %>

View File

@ -12,9 +12,7 @@
<% @measurements.each do |m| %>
<% next if m.new_record? %>
<tr id="measurement-<%= m.id %>" class="measurement <%= 'hidden' if m.hidden %>">
<td class="date">
<%= m.taken_at.getlocal.strftime("%F <small>%R</small>").html_safe %>
</td>
<td class="date"><%= format_datetime(m) %></td>
<td class="name">
<div style="float:left;">
<%= link_to m.name, readouts_measurement_path(m) %>

View File

@ -0,0 +1,68 @@
<% if @readouts.any? %>
<%= render partial: 'measurements/options' %>
<table class="readouts list odd-even">
<thead>
<tr>
<% total_width = 3 + @quantities.length %>
<th style="width:<%= 3 * 100/total_width%>%"><%= l(:field_name) %></th>
<% @quantities.each do |q| %>
<th style="width:<%= 100/total_width %>%"><%= q.name %></th>
<% end %>
</tr>
</thead>
<tbody>
<% @readouts.each do |m, values| %>
<% row_class = "measurement #{cycle('odd', 'even')}" %>
<tr id="measurement-<%= m.id %>" class="primary <%= row_class %>">
<td class="date" style="cursor: pointer;" onclick="$(this).closest('tr').toggle(); $(this).closest('tr').nextUntil('tr.primary', 'tr').toggle(); return false;">
<span class="icon icon-bullet-closed"><%= format_datetime(m) %></span>
</td>
<% values.each do |*, value| %>
<td class="primary value"><%= value %></td>
<% end %>
</tr>
<tr class="<%= row_class %>" style="display:none">
<td class="date" style="cursor: pointer;" onclick="$(this).closest('tr').prev('tr.primary').toggle(); $(this).closest('tr').prev('tr.primary').nextUntil('tr.primary', 'tr').toggle(); return false;">
<span class="icon icon-bullet-closed"><%= format_datetime(m) %></span>
</td>
<% values.each do |q_name, *| %>
<td class="primary quantity"><%= q_name %></td>
<% end %>
</tr>
<tr class="<%= row_class %>" style="display:none">
<td class="space"></td>
<% values.each do |*, value| %>
<td class="primary value"><%= value %></td>
<% end %>
</tr>
<% extras = @extra_readouts[i] %>
<% extras.each_slice(@quantities.length).with_index do |values, index| %>
<tr class="extra <%= row_class %>" style="display:none">
<td class="space"></td>
<% values.each do |q_name, *| %>
<td class="extra quantity"><%= q_name %></td>
<% end %>
<% if @quantities.length > values.length %>
<td class="space" colspan="<%= @quantities.length-values.length %>"></td>
<% end %>
</tr>
<tr class="extra <%= row_class %>" style="display:none">
<td class="space"></td>
<% values.each do |*, value| %>
<td class="extra value"><%= value %></td>
<% end %>
<% if @quantities.length > values.length %>
<td class="space" colspan="<%= @quantities.length-values.length %>"></td>
<% end %>
</tr>
<% end %>
<% end %>
</tbody>
</table>
<% else %>
<p class="nodata"><%= l(:label_no_data) %></p>
<% end %>