summaryrefslogtreecommitdiff
path: root/app/controllers/dashboard_controller.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/controllers/dashboard_controller.rb')
-rw-r--r--app/controllers/dashboard_controller.rb45
1 files changed, 45 insertions, 0 deletions
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
Copyright 2019--2025 Marius PETER