From 52b044d6a4278c229992404ad5801769c2d13363 Mon Sep 17 00:00:00 2001 From: Marius Peter Date: Sun, 24 Aug 2025 20:29:54 +0200 Subject: First commit. Vive le Castel Peter ! --- app/controllers/dashboard_controller.rb | 45 +++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 app/controllers/dashboard_controller.rb (limited to 'app/controllers/dashboard_controller.rb') diff --git a/app/controllers/dashboard_controller.rb b/app/controllers/dashboard_controller.rb new file mode 100644 index 0000000..4e9d560 --- /dev/null +++ b/app/controllers/dashboard_controller.rb @@ -0,0 +1,45 @@ +class DashboardController < ApplicationController + def index + # Raft allocation by crop type + @raft_data = raft_data_series + + # Nutrient target table + @latest_measurement = NutrientMeasurement.order(measured_on: :desc, created_at: :desc).first + @target = TargetNutrientCalculator.call + + # Measurement history table + @measurements = NutrientMeasurement.order(measured_on: :desc).limit(10) + + @npk_measurement_data = measurement_data_series(:nno3, :p, :k) + @ammonium_measurement_data = measurement_data_series(:nnh4) + end + + private + + def raft_data_series + data_series = [] + + counts = Raft.left_outer_joins(:crop) + .group("crops.name", "crops.crop_type") + .count + + counts.each do |(crop_name, crop_type), count| + name = (crop_name || "unassigned").titleize + type = (crop_type || "unassigned").titleize + data = { type => count } + data_series << { name:, data: } + end + + unassigned, assigned = data_series.partition { |s| s[:name].casecmp("unassigned").zero? } + assigned + unassigned + end + + def measurement_data_series(*nutrients) + nutrients.map do |formula| + { name: Nutrient.find_by!(formula:).name, + data: NutrientMeasurement + .order(:measured_on) + .pluck(:measured_on, formula) } + end + end +end -- cgit v1.2.3