Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 48 additions & 45 deletions app/controllers/reports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,70 +5,58 @@ class ReportsController < ApplicationController
def index; end

def features
forms = Reports::FormDocumentsService.live_form_documents
tag = params[:tag]
forms = Reports::FormDocumentsService.form_documents(tag:)
data = Reports::FeatureReportService.new(forms).report

render template: "reports/features", locals: { data: }
render template: "reports/features", locals: { tag:, data: }
end

def questions_with_answer_type
tag = params[:tag]
answer_type = params.require(:answer_type)
forms = Reports::FormDocumentsService.live_form_documents
forms = Reports::FormDocumentsService.form_documents(tag:)
questions = Reports::FeatureReportService.new(forms).questions_with_answer_type(answer_type)

render template: "reports/questions_with_answer_type", locals: { answer_type:, questions: }
end

def questions_with_add_another_answer
forms = Reports::FormDocumentsService.live_form_documents
questions = Reports::FeatureReportService.new(forms).questions_with_add_another_answer

if params[:format] == "csv"
send_data Reports::QuestionsCsvReportService.new(questions).csv,
type: "text/csv; charset=iso-8859-1",
disposition: "attachment; filename=#{csv_filename('live_questions_with_add_another_answer_report')}"
disposition: "attachment; filename=#{questions_csv_filename(tag, answer_type)}"
else
render template: "reports/feature_report", locals: { report: params[:action], records: questions }
render template: "reports/questions_with_answer_type", locals: { tag:, answer_type:, questions: }
end
end

def questions_with_add_another_answer
tag = params[:tag]
forms = Reports::FormDocumentsService.form_documents(tag:)
questions = Reports::FeatureReportService.new(forms).questions_with_add_another_answer

questions_feature_report(tag, params[:action], questions)
end

def forms_with_routes
forms = Reports::FormDocumentsService.live_form_documents
tag = params[:tag]
forms = Reports::FormDocumentsService.form_documents(tag:)
forms = Reports::FeatureReportService.new(forms).forms_with_routes

if params[:format] == "csv"
send_data Reports::FormsCsvReportService.new(forms).csv,
type: "text/csv; charset=iso-8859-1",
disposition: "attachment; filename=#{csv_filename('live_forms_with_routes_report')}"
else
render template: "reports/feature_report", locals: { report: params[:action], records: forms }
end
forms_feature_report(tag, params[:action], forms)
end

def forms_with_payments
forms = Reports::FormDocumentsService.live_form_documents
tag = params[:tag]
forms = Reports::FormDocumentsService.form_documents(tag:)
forms = Reports::FeatureReportService.new(forms).forms_with_payments

if params[:format] == "csv"
send_data Reports::FormsCsvReportService.new(forms).csv,
type: "text/csv; charset=iso-8859-1",
disposition: "attachment; filename=#{csv_filename('live_forms_with_payments_report')}"
else
render template: "reports/feature_report", locals: { report: params[:action], records: forms }
end
forms_feature_report(tag, params[:action], forms)
end

def forms_with_csv_submission_enabled
forms = Reports::FormDocumentsService.live_form_documents
tag = params[:tag]
forms = Reports::FormDocumentsService.form_documents(tag:)
forms = Reports::FeatureReportService.new(forms).forms_with_csv_submission_enabled

if params[:format] == "csv"
send_data Reports::FormsCsvReportService.new(forms).csv,
type: "text/csv; charset=iso-8859-1",
disposition: "attachment; filename=#{csv_filename('live_forms_with_csv_submission_enabled_report')}"
else
render template: "reports/feature_report", locals: { report: params[:action], records: forms }
end
forms_feature_report(tag, params[:action], forms)
end

def users
Expand Down Expand Up @@ -120,27 +108,42 @@ def live_forms_csv
end

def live_questions_csv
answer_type = params[:answer_type]
forms = Reports::FormDocumentsService.live_form_documents
questions = if answer_type
Reports::FeatureReportService.new(forms).questions_with_answer_type(answer_type)
else
Reports::FeatureReportService.new(forms).questions
end
questions = Reports::FeatureReportService.new(forms).questions

send_data Reports::QuestionsCsvReportService.new(questions).csv,
type: "text/csv; charset=iso-8859-1",
disposition: "attachment; filename=#{questions_csv_filename(answer_type)}"
disposition: "attachment; filename=#{csv_filename('live_questions_report')}"
end

private

def questions_feature_report(tag, report, questions)
if params[:format] == "csv"
send_data Reports::QuestionsCsvReportService.new(questions).csv,
type: "text/csv; charset=iso-8859-1",
disposition: "attachment; filename=#{csv_filename("#{tag}_#{report}_report")}"
else
render template: "reports/feature_report", locals: { tag:, report:, records: questions }
end
end

def forms_feature_report(tag, report, forms)
if params[:format] == "csv"
send_data Reports::FormsCsvReportService.new(forms).csv,
type: "text/csv; charset=iso-8859-1",
disposition: "attachment; filename=#{csv_filename("#{tag}_#{report}_report")}"
else
render template: "reports/feature_report", locals: { tag:, report:, records: forms }
end
end

def check_user_has_permission
authorize Report, :can_view_reports?
end

def questions_csv_filename(answer_type)
base_name = "live_questions_report"
def questions_csv_filename(tag, answer_type)
base_name = "#{tag}_questions_report"
base_name += "_#{answer_type}_answer_type" if answer_type.present?
csv_filename(base_name)
end
Expand Down
14 changes: 11 additions & 3 deletions app/services/reports/form_documents_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,19 @@ class << self

FormDocumentsResponse = Data.define(:forms, :has_more_results?)

def draft_form_documents
form_documents(tag: "draft")
end

def live_form_documents
form_documents(tag: "live")
end

def form_documents(tag:)
Enumerator.new do |yielder|
page = 1
loop do
form_documents_response = live_form_documents_page(page)
form_documents_response = form_documents_page(tag:, page:)
form_documents_response.forms.each { |f| yielder << f unless is_in_internal_organisation?(f) }

break unless form_documents_response.has_more_results?
Expand All @@ -36,9 +44,9 @@ def has_csv_submission_enabled?(form_document)

private

def live_form_documents_page(page)
def form_documents_page(tag:, page:)
uri = URI(FORM_DOCUMENTS_URL)
params = { tag: "live", page:, per_page: Settings.reports.forms_api_forms_per_request_page }
params = { tag:, page:, per_page: Settings.reports.forms_api_forms_per_request_page }
uri.query = URI.encode_www_form(params)

response = Net::HTTP.get_response(uri, REQUEST_HEADERS)
Expand Down
14 changes: 10 additions & 4 deletions app/views/reports/feature_report.html.erb
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
<% set_page_title(t("reports.#{report}.heading"))%>
<% set_page_title(t("reports.#{report}.heading", tag:).upcase_first)%>
<% content_for :back_link, govuk_back_link_to(report_features_path, t("reports.back_to_feature_usage")) %>
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<h1 class="govuk-heading-l"><%= t("reports.#{report}.heading")%></h1>
<h1 class="govuk-heading-l"><%= t("reports.#{report}.heading", tag:).upcase_first %></h1>

<p><%=govuk_link_to(t("reports.#{report}.download_csv"), url_for(format: :csv))%></p>
<% unless records.empty? %>
<p><%=govuk_link_to(t("reports.#{report}.download_csv", tag:).upcase_first, url_for(format: :csv))%></p>
<% end %>
</div>

<div class="govuk-grid-column-full">
<%= govuk_table(**report_table(records)) %>
<% if records.empty? %>
<p class="govuk-body"><%= t("reports.#{report}.empty", tag:) %></p>
<% else %>
<%= govuk_table(**report_table(records)) %>
<% end %>
</div>
</div>
22 changes: 12 additions & 10 deletions app/views/reports/features.html.erb
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
<% set_page_title(t(".title")) %>
<% set_page_title(t(".title", tag:)) %>
<% content_for :back_link, govuk_back_link_to(reports_path, t("reports.back_link")) %>
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<h1 class="govuk-heading-l"><%= t(".title") %></h1>
<h1 class="govuk-heading-l"><%= t(".title", tag:) %></h1>

<h2 class="govuk-heading-m"><%= t(".features.heading") %></h2>
<%= govuk_summary_list do |summary_list| %>
<%= summary_list.with_row do |row| %>
<%= row.with_key(text: t(".features.total_live_forms")) %>
<%= row.with_key(text: t(".features.total_forms", tag:).upcase_first) %>
<%= row.with_value(text: data[:total_forms]) %>
<% end %>
<%= summary_list.with_row do |row| %>
<%= row.with_key(text: t(".features.live_forms_with_routes")) %>
<%= row.with_key(text: t(".features.forms_with_routes", tag:).upcase_first) %>
<%= row.with_value(text: govuk_link_to(data[:forms_with_routing], report_forms_with_routes_path, no_visited_state: true)) %>
<% end %>
<%= summary_list.with_row do |row| %>
<%= row.with_key(text: t(".features.live_forms_with_payments")) %>
<%= row.with_key(text: t(".features.forms_with_payments", tag:).upcase_first) %>
<%= row.with_value(text: govuk_link_to(data[:forms_with_payment], report_forms_with_payments_path, no_visited_state: true)) %>
<% end %>
<%= summary_list.with_row do |row| %>
<%= row.with_key(text: t(".features.live_forms_with_add_another_answer")) %>
<%= row.with_key(text: t(".features.forms_with_add_another_answer", tag:).upcase_first) %>
<%= row.with_value(text: govuk_link_to(data[:forms_with_add_another_answer], report_questions_with_add_another_answer_path, no_visited_state: true)) %>
<% end %>
<%= summary_list.with_row do |row| %>
<%= row.with_key(text: t(".features.live_forms_with_csv_submission_enabled")) %>
<%= row.with_key(text: t(".features.forms_with_csv_submission_enabled", tag:).upcase_first) %>
<%= row.with_value(text: govuk_link_to(data[:forms_with_csv_submission_enabled], report_forms_with_csv_submission_enabled_path, no_visited_state: true)) %>
<% end %>
<% end %>
Expand All @@ -35,12 +35,14 @@
<%= table.with_head do |head| %>
<%= head.with_row do |row| %>
<%= row.with_cell(text: t(".answer_types.table_headings.answer_type")) %>
<%= row.with_cell(text: t(".answer_types.table_headings.number_of_forms"), numeric: true) %>
<%= row.with_cell(text: t(".answer_types.table_headings.number_of_pages"), numeric: true) %>
<%= row.with_cell(text: t(".answer_types.table_headings.number_of_forms", tag:), numeric: true) %>
<%= row.with_cell(text: t(".answer_types.table_headings.number_of_pages", tag:), numeric: true) %>
<% end %>
<% end %>

<% answer_type_links = { "selection" => report_selection_questions_summary_path } %>
<% answer_type_links = {
"selection" => tag == "live" ? report_selection_questions_summary_path : nil,
} %>
<%= table.with_body do |body| %>
<% Page::ANSWER_TYPES.each do |answer_type| %>
<%= body.with_row do |row| %>
Expand Down
3 changes: 2 additions & 1 deletion app/views/reports/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
<h1 class="govuk-heading-l"><%= t(".title") %></h1>
<ul class="govuk-list">
<li><%= govuk_link_to t("reports.add_another_answer.title"), report_add_another_answer_path %></li>
<li><%= govuk_link_to t("reports.features.title"), report_features_path %></li>
<li><%= govuk_link_to t("reports.features.title", tag: "live"), report_features_path(tag: :live) %></li>
<li><%= govuk_link_to t("reports.features.title", tag: "draft"), report_features_path(tag: :draft) %></li>
<li><%= govuk_link_to t("reports.features.long_list_usage.title"), report_selection_questions_summary_path %></li>
<li><%= govuk_link_to t("reports.users.title"), report_users_path %></li>
<li><%= govuk_link_to t("reports.last_signed_in_at.title"), report_last_signed_in_at_path %></li>
Expand Down
20 changes: 13 additions & 7 deletions app/views/reports/questions_with_answer_type.html.erb
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
<% set_page_title(t(".heading", answer_type: t("helpers.label.page.answer_type_options.names.#{answer_type}").downcase)) %>
<% set_page_title(t(".heading", answer_type: t("helpers.label.page.answer_type_options.names.#{answer_type}").downcase, tag:).upcase_first) %>
<% content_for :back_link, govuk_back_link_to(report_features_path, t("reports.back_to_feature_usage")) %>
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<h1 class="govuk-heading-l"><%= t(".heading", answer_type: t("helpers.label.page.answer_type_options.names.#{answer_type}").downcase) %></h1>
<h1 class="govuk-heading-l"><%= t(".heading", answer_type: t("helpers.label.page.answer_type_options.names.#{answer_type}").downcase, tag:).upcase_first %></h1>

<p><%=govuk_link_to(t(".download_csv"), report_live_questions_csv_path(answer_type:))%></p>
<% unless questions.empty? %>
<p><%=govuk_link_to(t(".download_csv", tag:).upcase_first, url_for(format: :csv))%></p>
<% end %>
</div>

<div class="govuk-grid-column-full">
<%= govuk_table(
head: report_questions_table_head,
rows: report_questions_table_rows(questions),
) %>
<% if questions.empty? %>
<p class="govuk-body"><%= t(".empty", tag:, answer_type:) %></p>
<% else %>
<%= govuk_table(
head: report_questions_table_head,
rows: report_questions_table_rows(questions),
) %>
<% end %>
</div>
</div>
2 changes: 1 addition & 1 deletion app/views/reports/selection_questions/summary.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<% set_page_title(t(".title")) %>
<% content_for :back_link, govuk_back_link_to(report_features_path, t(".back_link")) %>
<% content_for :back_link, govuk_back_link_to(report_features_path(tag: :live), t(".back_link")) %>
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<h1 class="govuk-heading-l"><%= t(".title") %></h1>
Expand Down
41 changes: 23 additions & 18 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1385,33 +1385,36 @@ en:
heading: Answer type usage
table_headings:
answer_type: Answer type
number_of_forms: Number of live forms with this answer type
number_of_pages: Number of uses of this answer type in live forms
number_of_forms: Number of %{tag} forms with this answer type
number_of_pages: Number of uses of this answer type in %{tag} forms
features:
forms_with_add_another_answer: "%{tag} forms with add another answer"
forms_with_csv_submission_enabled: "%{tag} forms with CSV submission enabled"
forms_with_payments: "%{tag} forms with payments"
forms_with_routes: "%{tag} forms with routes"
heading: Feature usage
live_forms_with_add_another_answer: Live forms with add another answer
live_forms_with_csv_submission_enabled: Live forms with CSV submission enabled
live_forms_with_payments: Live forms with payments
live_forms_with_routes: Live forms with routes
total_live_forms: Total live forms
total_forms: Total %{tag} forms
long_list_usage:
title: Selection from a list of options feature usage in live forms
title: Feature and answer type usage in live forms
title: Feature and answer type usage in %{tag} forms
form_or_questions_list_table:
headings:
form_name: Form name
number_of_routes: Number of routes
organisation: Organisation
question_text: Question text
forms_with_csv_submission_enabled:
download_csv: Download data about all live forms with CSV submission enabled as a CSV file
heading: Live forms with CSV submission enabled
download_csv: Download data about all %{tag} forms with CSV submission enabled as a CSV file
empty: There are no %{tag} forms with CSV submission enabled
heading: "%{tag} forms with CSV submission enabled"
forms_with_payments:
download_csv: Download data about all live forms with payments as a CSV file
heading: Live forms with payments
download_csv: Download data about all %{tag} forms with payments as a CSV file
empty: There are no %{tag} forms with payments
heading: "%{tag} forms with payments"
forms_with_routes:
download_csv: Download data about all live forms with routes as a CSV file
heading: Live forms with routes
download_csv: Download data about all %{tag} forms with routes as a CSV file
empty: There are no %{tag} forms with routes
heading: "%{tag} forms with routes"
index:
title: Reports
last_signed_in_at:
Expand All @@ -1421,11 +1424,13 @@ en:
not_since_last_signed_in_at_added: People who last signed in sometime between 3 November 2023 and 4 November 2024
title: When users last signed in
questions_with_add_another_answer:
download_csv: Download all questions with add another answer in live forms as a CSV file
heading: Questions with add another answer in live forms
download_csv: Download all questions with add another answer in %{tag} forms as a CSV file
empty: There are no questions with add another answer in %{tag} forms
heading: Questions with add another answer in %{tag} forms
questions_with_answer_type:
download_csv: Download all questions in live forms with this answer type as a CSV file
heading: Live questions with %{answer_type} answer type
download_csv: Download all questions in %{tag} forms with this answer type as a CSV file
empty: There are no %{tag} questions with %{answer_type} answer type
heading: "%{tag} questions with %{answer_type} answer type"
selection_questions:
autocomplete:
title: Questions where you can select one from over 30 options
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@
scope "/reports" do
get "/", to: "reports#index", as: :reports

scope "/features" do
scope "/features/:tag", constraints: { tag: /(draft|live)/ } do
get "/", to: "reports#features", as: :report_features
get "questions-with-answer-type/:answer_type", to: "reports#questions_with_answer_type", as: :report_questions_with_answer_type
get "questions-with-add-another-answer", to: "reports#questions_with_add_another_answer", as: :report_questions_with_add_another_answer
Expand Down
Loading