From 4f12b5a6bc37f03bee4b01c6160b9147e3cae82a Mon Sep 17 00:00:00 2001 From: Leigh Dodds Date: Thu, 7 Dec 2023 17:11:25 +0000 Subject: [PATCH] Initial version of chart preview tool --- .../admin/chart_previews_controller.rb | 50 ++++++++++++++ app/views/admin/chart_previews/index.html.erb | 65 +++++++++++++++++++ config/routes.rb | 2 + 3 files changed, 117 insertions(+) create mode 100644 app/controllers/admin/chart_previews_controller.rb create mode 100644 app/views/admin/chart_previews/index.html.erb diff --git a/app/controllers/admin/chart_previews_controller.rb b/app/controllers/admin/chart_previews_controller.rb new file mode 100644 index 0000000000..00a7b2ed4e --- /dev/null +++ b/app/controllers/admin/chart_previews_controller.rb @@ -0,0 +1,50 @@ +require 'dashboard' + +module Admin + class ChartPreviewsController < AdminController + before_action :set_school + before_action :load_chart_list + before_action :set_chart_type + before_action :set_chart_titles + before_action :set_controls + + def index + @schools = School.data_enabled.by_name + end + + private + + def set_school + return nil unless chart_params[:school_id].present? + @preview_school = School.friendly.find(chart_params[:school_id]) + end + + def set_chart_type + return nil unless chart_params[:chart_type].present? + @chart_type = chart_params[:chart_type].to_sym + end + + def set_chart_titles + @title = chart_params[:title] || @preview_school.present? ? "School: #{@preview_school.name}" : '' + @subtitle = chart_params[:subtitle] || @chart_type.present? ? "Chart: #{@chart_type}" : '' + @footer = chart_params[:footer] + end + + def set_controls + @axis_controls = (chart_params[:axis_controls].nil? || chart_params[:axis_controls] == '1') + @analysis_controls = (chart_params[:analysis_controls].nil? || chart_params[:analysis_controls] == '1') + end + + def chart_params + params[:preview_chart] || {} + end + + def load_chart_list + analysis_charts = DashboardConfiguration::DASHBOARD_PAGE_GROUPS.except(:simulator, :simulator_detail, :simulator_debug, :test, :pupil_analysis_page, :heating_model_fitting, :cost_unused).map do |top_level_key, config| + ["#{config[:name]} (#{top_level_key})", config.fetch(:charts) {[]}] + end + custom_activity_charts = [['Activity charts (custom)', ChartManager::STANDARD_CHART_CONFIGURATION.keys.grep(/^activities/)]] + @chart_list = custom_activity_charts + analysis_charts + end + end +end diff --git a/app/views/admin/chart_previews/index.html.erb b/app/views/admin/chart_previews/index.html.erb new file mode 100644 index 0000000000..640a23aad0 --- /dev/null +++ b/app/views/admin/chart_previews/index.html.erb @@ -0,0 +1,65 @@ +

Chart Previews

+ +

+This page is intended to help with interactive testing and debugging of chart configurations. Using the options below you can select a school and +run a specific chart using their data. +

+ +<%= simple_form_for :preview_chart, url: admin_chart_previews_path, method: "GET", html: { class: 'form' } do |f| %> + <%= f.input :school_id, + collection: @schools, + label: 'Choose school', + label_method: lambda {|k| k.name }, + include_blank: false, + selected: ( params[:preview_chart].present? ? params[:preview_chart]['school_id'] : nil ), + input_html: { class: 'form-control select2' } + %> + + <%= f.input :chart_type, + as: :grouped_select, + collection: @chart_list, + label: 'Choose chart type', + group_method: :last, + group_label_method: :first, + include_blank: false, + selected: ( params[:preview_chart].present? ? params[:preview_chart]["chart_type"] : nil), + input_html: { class: 'form-control select2'} + %> + +

+ You can optionally provide a title, subtitle and footer for the chart to see + how the chart will look when embedded into a page with other text around it. +

+ + <%= f.input :title, as: :string, input_html: {value: @title || ''} %> + <%= f.input :subtitle, as: :string, input_html: {value: @subtitle || ''} %> + <%= f.input :footer, as: :string, input_html: {value: @footer || ''} %> + +

+ You can optionally indicate whether the chart should have controls to switch axis and to + navigate through the database. However these may be overridden by the chart configuration + as some charts do not support navigation or switching axes. +

+ + <%= f.input :axis_controls, as: :boolean, input_html: { :checked => @axis_controls } %> + <%= f.input :analysis_controls, as: :boolean, input_html: { :checked => @analysis_controls } %> + + <%= f.submit "Run chart", class: "btn btn-primary" %> + <%= link_to 'Clear', admin_chart_previews_path, class: "btn btn-primary" %> +<% end %> + +
+ <% if @preview_school.present? && @chart_type.present? %> + <%= component 'chart', chart_type: @chart_type, axis_controls: @axis_controls, analysis_controls: @analysis_controls, school: @preview_school do |c| %> + <% if @title.present? %> + <% c.with_title { @title } %> + <% end %> + <% if @subtitle.present? %> + <% c.with_subtitle { @subtitle } %> + <% end %> + <% if @footer.present? %> + <% c.with_footer { @footer } %> + <% end %> + <% end %> + <% end %> +
diff --git a/config/routes.rb b/config/routes.rb index f1e94076d4..5e07f04ca2 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -479,6 +479,8 @@ end end + resources :chart_previews + namespace :emails do resources :alert_mailers, only: :show end