summaryrefslogtreecommitdiff
path: root/app/services/target_nutrient_calculator.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/services/target_nutrient_calculator.rb')
-rw-r--r--app/services/target_nutrient_calculator.rb33
1 files changed, 0 insertions, 33 deletions
diff --git a/app/services/target_nutrient_calculator.rb b/app/services/target_nutrient_calculator.rb
deleted file mode 100644
index e6cd378..0000000
--- a/app/services/target_nutrient_calculator.rb
+++ /dev/null
@@ -1,33 +0,0 @@
-class TargetNutrientCalculator
- # Derive nutrient columns from the NutrientMeasurement table
- NUTRIENT_COLUMNS = (NutrientMeasurement.column_names - %w[id measured_on created_at updated_at])
- .map!(&:to_sym)
- .freeze
-
- # Returns an unsaved NutrientMeasurement with target concentrations (e.g., mg/L)
- def self.call
- rafts = Raft.includes(:crop).where.not(crop_id: nil)
- total = rafts.count
- return empty_measurement if total.zero?
-
- sums = Hash.new(0.0)
-
- rafts.each do |raft|
- NUTRIENT_COLUMNS.each do |col|
- v = raft.crop.public_send(col)
- sums[col] += v.to_f if v
- end
- end
-
- targets = sums.transform_values { |s| s / total }
- NutrientMeasurement.new({ measured_on: Date.current }.merge(targets))
- end
-
- private
-
- def empty_measurement
- NutrientMeasurement.new(
- { measured_on: Date.current }.merge(NUTRIENT_COLUMNS.index_with { 0.0 })
- )
- end
-end
Copyright 2019--2025 Marius PETER