1
0

Implemented QuantityInput#lastBefore

This commit is contained in:
cryptogopher 2020-05-22 22:10:34 +02:00
parent 976271d6b4
commit 580e9156e6
2 changed files with 15 additions and 4 deletions

View File

@ -49,9 +49,20 @@ class Formula < ActiveRecord::Base
end end
def lastBefore(timepoints) def lastBefore(timepoints)
self.map{ BigDecimal(2000) } # NOTE: maybe optimize query, limiting range by min-max timepoints?
last_timepoint = timepoints.max # impact on caching?
@quantity.quantity_values.includes(:registry) values = @quantity.values.includes(:registry)
.map { |qv| [qv.registry.completed_at, qv.value] }.sort_by(&:first)
return [nil]*timepoints.length if values.empty?
vindex = 0
lastval = nil
timepoints.each_with_index.sort_by { |t, i| t || Time.current }.map do |time, index|
#lastval = values[vindex++].last while values[vindex].first <= time
vindex += values[vindex..-1].find_all { |vtime, v| lastval = v; vtime <= time }.length
[lastval, index]
end.sort_by(&:last).transpose.first
end end
end end

View File

@ -9,7 +9,7 @@ class Quantity < ActiveRecord::Base
belongs_to :project, required: false belongs_to :project, required: false
has_many :nutrients, dependent: :restrict_with_error has_many :nutrients, dependent: :restrict_with_error
has_many :readouts, dependent: :restrict_with_error has_many :readouts, dependent: :restrict_with_error
has_many :quantity_values, dependent: :restrict_with_error has_many :values, class_name: 'QuantityValue', dependent: :restrict_with_error
has_many :exposures, dependent: :destroy has_many :exposures, dependent: :destroy
has_one :formula, inverse_of: :quantity, dependent: :destroy, validate: true has_one :formula, inverse_of: :quantity, dependent: :destroy, validate: true