From 323c7dc3ee340d0e918bbfb226af646b75ce701b Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Mon, 1 Dec 2025 19:41:28 +1030 Subject: [PATCH] Optimize Data Improvement Page (#4356) * feat: Add data improvement page to crops controller This commit introduces a new data improvement page to the crops controller. The page displays tabbed lists of crops with missing data, allowing users to easily identify areas for data quality improvement. The following data quality categories are included: - Crops without photos - Crops without descriptions - Crops without a youtube video - Crops without alternate names - Crops without a scientific name with a wikidata id - Crops without row spacing - Crops without sun requirements - Crops without height All lists are sorted by planting count in descending order. * refactor: Optimize data improvement page to load tab data on demand This commit refactors the data improvement page to load data for each tab on demand, rather than loading all queries at once. This improves the performance of the page by only executing the query for the currently active tab. The controller action now uses a `case` statement based on a `tab` URL parameter to execute the appropriate query. The view has been updated to pass this parameter when a tab is clicked. --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> --- app/controllers/crops_controller.rb | 26 ++++++++++++++++++++++ app/views/crops/_crop_list.html.haml | 10 +++++++++ app/views/crops/data_improvement.html.haml | 13 +++++++++++ app/views/crops/index.html.haml | 1 + config/routes.rb | 1 + 5 files changed, 51 insertions(+) create mode 100644 app/views/crops/_crop_list.html.haml create mode 100644 app/views/crops/data_improvement.html.haml diff --git a/app/controllers/crops_controller.rb b/app/controllers/crops_controller.rb index 96f042133..1d39ff328 100644 --- a/app/controllers/crops_controller.rb +++ b/app/controllers/crops_controller.rb @@ -149,6 +149,32 @@ class CropsController < ApplicationController respond_with @crop end + def data_improvement + @active_tab = params[:tab] || 'photos' + + @crops = case @active_tab + when 'photos' + Crop.approved.where(photo_associations_count: 0).order(plantings_count: :desc) + when 'descriptions' + Crop.approved.where(description: [nil, '']).order(plantings_count: :desc) + when 'youtube' + Crop.approved.where(en_youtube_url: [nil, '']).order(plantings_count: :desc) + when 'alternate_names' + Crop.approved.left_joins(:alternate_names).where(alternate_names: { id: nil }).order(plantings_count: :desc) + when 'wikidata' + crops_with_wikidata = Crop.joins(:scientific_names).where.not(scientific_names: { wikidata_id: nil }).distinct + Crop.approved.where.not(id: crops_with_wikidata).order(plantings_count: :desc) + when 'row_spacing' + Crop.approved.where(row_spacing: nil).order(plantings_count: :desc) + when 'sun_requirements' + Crop.approved.where(sun_requirements: [nil, '']).order(plantings_count: :desc) + when 'height' + Crop.approved.where(height: nil).order(plantings_count: :desc) + else + Crop.none + end + end + private def notifier diff --git a/app/views/crops/_crop_list.html.haml b/app/views/crops/_crop_list.html.haml new file mode 100644 index 000000000..12b98a4c0 --- /dev/null +++ b/app/views/crops/_crop_list.html.haml @@ -0,0 +1,10 @@ +%table.table.table-striped + %thead + %tr + %th Name + %th Plantings + %tbody + - crops.each do |crop| + %tr + %td= link_to crop.name, crop + %td= crop.plantings_count diff --git a/app/views/crops/data_improvement.html.haml b/app/views/crops/data_improvement.html.haml new file mode 100644 index 000000000..155ca422c --- /dev/null +++ b/app/views/crops/data_improvement.html.haml @@ -0,0 +1,13 @@ +%h1 Data Improvement + +- tabs = { photos: "Photos", descriptions: "Descriptions", youtube: "YouTube videos", alternate_names: "Alternate names", wikidata: "Wikidata ID", row_spacing: "Row spacing", sun_requirements: "Sun requirements", height: "Height" } + +%ul.nav.nav-tabs + - tabs.each do |key, value| + %li{class: ('active' if @active_tab == key.to_s)} + = link_to value, data_improvement_crops_path(tab: key) + +.tab-content + .tab-pane.active + %h2= "Crops without #{tabs[@active_tab.to_sym]}" + = render 'crop_list', crops: @crops diff --git a/app/views/crops/index.html.haml b/app/views/crops/index.html.haml index 12a5ebbbc..9d0425588 100644 --- a/app/views/crops/index.html.haml +++ b/app/views/crops/index.html.haml @@ -3,6 +3,7 @@ - content_for :buttonbar do - if can? :wrangle, Crop = link_to 'Wrangle Crops', wrangle_crops_path, class: 'btn btn-secondary' + = link_to 'Data Improvement', data_improvement_crops_path, class: 'btn btn-info' - if can? :create, Crop = link_to 'Add New Crop', new_crop_path, class: 'btn btn-primary' diff --git a/config/routes.rb b/config/routes.rb index 93d4d4b35..64266b4f8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -89,6 +89,7 @@ Rails.application.routes.draw do get 'wrangle' get 'hierarchy' get 'search' + get 'data_improvement' end end