Skip to content

Commit 1ab5224

Browse files
authored
Merge pull request #1991 from alphagov/ldeb-add-draft-form-feature-reports--sd
Add feature reports for draft forms
2 parents db1c43e + 84773bb commit 1ab5224

17 files changed

Lines changed: 389 additions & 171 deletions

app/controllers/reports_controller.rb

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,70 +5,58 @@ class ReportsController < ApplicationController
55
def index; end
66

77
def features
8-
forms = Reports::FormDocumentsService.live_form_documents
8+
tag = params[:tag]
9+
forms = Reports::FormDocumentsService.form_documents(tag:)
910
data = Reports::FeatureReportService.new(forms).report
1011

11-
render template: "reports/features", locals: { data: }
12+
render template: "reports/features", locals: { tag:, data: }
1213
end
1314

1415
def questions_with_answer_type
16+
tag = params[:tag]
1517
answer_type = params.require(:answer_type)
16-
forms = Reports::FormDocumentsService.live_form_documents
18+
forms = Reports::FormDocumentsService.form_documents(tag:)
1719
questions = Reports::FeatureReportService.new(forms).questions_with_answer_type(answer_type)
1820

19-
render template: "reports/questions_with_answer_type", locals: { answer_type:, questions: }
20-
end
21-
22-
def questions_with_add_another_answer
23-
forms = Reports::FormDocumentsService.live_form_documents
24-
questions = Reports::FeatureReportService.new(forms).questions_with_add_another_answer
25-
2621
if params[:format] == "csv"
2722
send_data Reports::QuestionsCsvReportService.new(questions).csv,
2823
type: "text/csv; charset=iso-8859-1",
29-
disposition: "attachment; filename=#{csv_filename('live_questions_with_add_another_answer_report')}"
24+
disposition: "attachment; filename=#{questions_csv_filename(tag, answer_type)}"
3025
else
31-
render template: "reports/feature_report", locals: { report: params[:action], records: questions }
26+
render template: "reports/questions_with_answer_type", locals: { tag:, answer_type:, questions: }
3227
end
3328
end
3429

30+
def questions_with_add_another_answer
31+
tag = params[:tag]
32+
forms = Reports::FormDocumentsService.form_documents(tag:)
33+
questions = Reports::FeatureReportService.new(forms).questions_with_add_another_answer
34+
35+
questions_feature_report(tag, params[:action], questions)
36+
end
37+
3538
def forms_with_routes
36-
forms = Reports::FormDocumentsService.live_form_documents
39+
tag = params[:tag]
40+
forms = Reports::FormDocumentsService.form_documents(tag:)
3741
forms = Reports::FeatureReportService.new(forms).forms_with_routes
3842

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

4846
def forms_with_payments
49-
forms = Reports::FormDocumentsService.live_form_documents
47+
tag = params[:tag]
48+
forms = Reports::FormDocumentsService.form_documents(tag:)
5049
forms = Reports::FeatureReportService.new(forms).forms_with_payments
5150

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

6154
def forms_with_csv_submission_enabled
62-
forms = Reports::FormDocumentsService.live_form_documents
55+
tag = params[:tag]
56+
forms = Reports::FormDocumentsService.form_documents(tag:)
6357
forms = Reports::FeatureReportService.new(forms).forms_with_csv_submission_enabled
6458

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

7462
def users
@@ -120,27 +108,42 @@ def live_forms_csv
120108
end
121109

122110
def live_questions_csv
123-
answer_type = params[:answer_type]
124111
forms = Reports::FormDocumentsService.live_form_documents
125-
questions = if answer_type
126-
Reports::FeatureReportService.new(forms).questions_with_answer_type(answer_type)
127-
else
128-
Reports::FeatureReportService.new(forms).questions
129-
end
112+
questions = Reports::FeatureReportService.new(forms).questions
130113

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

136119
private
137120

121+
def questions_feature_report(tag, report, questions)
122+
if params[:format] == "csv"
123+
send_data Reports::QuestionsCsvReportService.new(questions).csv,
124+
type: "text/csv; charset=iso-8859-1",
125+
disposition: "attachment; filename=#{csv_filename("#{tag}_#{report}_report")}"
126+
else
127+
render template: "reports/feature_report", locals: { tag:, report:, records: questions }
128+
end
129+
end
130+
131+
def forms_feature_report(tag, report, forms)
132+
if params[:format] == "csv"
133+
send_data Reports::FormsCsvReportService.new(forms).csv,
134+
type: "text/csv; charset=iso-8859-1",
135+
disposition: "attachment; filename=#{csv_filename("#{tag}_#{report}_report")}"
136+
else
137+
render template: "reports/feature_report", locals: { tag:, report:, records: forms }
138+
end
139+
end
140+
138141
def check_user_has_permission
139142
authorize Report, :can_view_reports?
140143
end
141144

142-
def questions_csv_filename(answer_type)
143-
base_name = "live_questions_report"
145+
def questions_csv_filename(tag, answer_type)
146+
base_name = "#{tag}_questions_report"
144147
base_name += "_#{answer_type}_answer_type" if answer_type.present?
145148
csv_filename(base_name)
146149
end

app/services/reports/form_documents_service.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,19 @@ class << self
88

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

11+
def draft_form_documents
12+
form_documents(tag: "draft")
13+
end
14+
1115
def live_form_documents
16+
form_documents(tag: "live")
17+
end
18+
19+
def form_documents(tag:)
1220
Enumerator.new do |yielder|
1321
page = 1
1422
loop do
15-
form_documents_response = live_form_documents_page(page)
23+
form_documents_response = form_documents_page(tag:, page:)
1624
form_documents_response.forms.each { |f| yielder << f unless is_in_internal_organisation?(f) }
1725

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

3745
private
3846

39-
def live_form_documents_page(page)
47+
def form_documents_page(tag:, page:)
4048
uri = URI(FORM_DOCUMENTS_URL)
41-
params = { tag: "live", page:, per_page: Settings.reports.forms_api_forms_per_request_page }
49+
params = { tag:, page:, per_page: Settings.reports.forms_api_forms_per_request_page }
4250
uri.query = URI.encode_www_form(params)
4351

4452
response = Net::HTTP.get_response(uri, REQUEST_HEADERS)
Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
<% set_page_title(t("reports.#{report}.heading"))%>
1+
<% set_page_title(t("reports.#{report}.heading", tag:).upcase_first)%>
22
<% content_for :back_link, govuk_back_link_to(report_features_path, t("reports.back_to_feature_usage")) %>
33
<div class="govuk-grid-row">
44
<div class="govuk-grid-column-two-thirds">
5-
<h1 class="govuk-heading-l"><%= t("reports.#{report}.heading")%></h1>
5+
<h1 class="govuk-heading-l"><%= t("reports.#{report}.heading", tag:).upcase_first %></h1>
66

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

1012
<div class="govuk-grid-column-full">
11-
<%= govuk_table(**report_table(records)) %>
13+
<% if records.empty? %>
14+
<p class="govuk-body"><%= t("reports.#{report}.empty", tag:) %></p>
15+
<% else %>
16+
<%= govuk_table(**report_table(records)) %>
17+
<% end %>
1218
</div>
1319
</div>

app/views/reports/features.html.erb

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
<% set_page_title(t(".title")) %>
1+
<% set_page_title(t(".title", tag:)) %>
22
<% content_for :back_link, govuk_back_link_to(reports_path, t("reports.back_link")) %>
33
<div class="govuk-grid-row">
44
<div class="govuk-grid-column-two-thirds">
5-
<h1 class="govuk-heading-l"><%= t(".title") %></h1>
5+
<h1 class="govuk-heading-l"><%= t(".title", tag:) %></h1>
66

77
<h2 class="govuk-heading-m"><%= t(".features.heading") %></h2>
88
<%= govuk_summary_list do |summary_list| %>
99
<%= summary_list.with_row do |row| %>
10-
<%= row.with_key(text: t(".features.total_live_forms")) %>
10+
<%= row.with_key(text: t(".features.total_forms", tag:).upcase_first) %>
1111
<%= row.with_value(text: data[:total_forms]) %>
1212
<% end %>
1313
<%= summary_list.with_row do |row| %>
14-
<%= row.with_key(text: t(".features.live_forms_with_routes")) %>
14+
<%= row.with_key(text: t(".features.forms_with_routes", tag:).upcase_first) %>
1515
<%= row.with_value(text: govuk_link_to(data[:forms_with_routing], report_forms_with_routes_path, no_visited_state: true)) %>
1616
<% end %>
1717
<%= summary_list.with_row do |row| %>
18-
<%= row.with_key(text: t(".features.live_forms_with_payments")) %>
18+
<%= row.with_key(text: t(".features.forms_with_payments", tag:).upcase_first) %>
1919
<%= row.with_value(text: govuk_link_to(data[:forms_with_payment], report_forms_with_payments_path, no_visited_state: true)) %>
2020
<% end %>
2121
<%= summary_list.with_row do |row| %>
22-
<%= row.with_key(text: t(".features.live_forms_with_add_another_answer")) %>
22+
<%= row.with_key(text: t(".features.forms_with_add_another_answer", tag:).upcase_first) %>
2323
<%= row.with_value(text: govuk_link_to(data[:forms_with_add_another_answer], report_questions_with_add_another_answer_path, no_visited_state: true)) %>
2424
<% end %>
2525
<%= summary_list.with_row do |row| %>
26-
<%= row.with_key(text: t(".features.live_forms_with_csv_submission_enabled")) %>
26+
<%= row.with_key(text: t(".features.forms_with_csv_submission_enabled", tag:).upcase_first) %>
2727
<%= row.with_value(text: govuk_link_to(data[:forms_with_csv_submission_enabled], report_forms_with_csv_submission_enabled_path, no_visited_state: true)) %>
2828
<% end %>
2929
<% end %>
@@ -35,12 +35,14 @@
3535
<%= table.with_head do |head| %>
3636
<%= head.with_row do |row| %>
3737
<%= row.with_cell(text: t(".answer_types.table_headings.answer_type")) %>
38-
<%= row.with_cell(text: t(".answer_types.table_headings.number_of_forms"), numeric: true) %>
39-
<%= row.with_cell(text: t(".answer_types.table_headings.number_of_pages"), numeric: true) %>
38+
<%= row.with_cell(text: t(".answer_types.table_headings.number_of_forms", tag:), numeric: true) %>
39+
<%= row.with_cell(text: t(".answer_types.table_headings.number_of_pages", tag:), numeric: true) %>
4040
<% end %>
4141
<% end %>
4242

43-
<% answer_type_links = { "selection" => report_selection_questions_summary_path } %>
43+
<% answer_type_links = {
44+
"selection" => tag == "live" ? report_selection_questions_summary_path : nil,
45+
} %>
4446
<%= table.with_body do |body| %>
4547
<% Page::ANSWER_TYPES.each do |answer_type| %>
4648
<%= body.with_row do |row| %>

app/views/reports/index.html.erb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
<h1 class="govuk-heading-l"><%= t(".title") %></h1>
55
<ul class="govuk-list">
66
<li><%= govuk_link_to t("reports.add_another_answer.title"), report_add_another_answer_path %></li>
7-
<li><%= govuk_link_to t("reports.features.title"), report_features_path %></li>
7+
<li><%= govuk_link_to t("reports.features.title", tag: "live"), report_features_path(tag: :live) %></li>
8+
<li><%= govuk_link_to t("reports.features.title", tag: "draft"), report_features_path(tag: :draft) %></li>
89
<li><%= govuk_link_to t("reports.features.long_list_usage.title"), report_selection_questions_summary_path %></li>
910
<li><%= govuk_link_to t("reports.users.title"), report_users_path %></li>
1011
<li><%= govuk_link_to t("reports.last_signed_in_at.title"), report_last_signed_in_at_path %></li>
Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1-
<% set_page_title(t(".heading", answer_type: t("helpers.label.page.answer_type_options.names.#{answer_type}").downcase)) %>
1+
<% set_page_title(t(".heading", answer_type: t("helpers.label.page.answer_type_options.names.#{answer_type}").downcase, tag:).upcase_first) %>
22
<% content_for :back_link, govuk_back_link_to(report_features_path, t("reports.back_to_feature_usage")) %>
33
<div class="govuk-grid-row">
44
<div class="govuk-grid-column-two-thirds">
5-
<h1 class="govuk-heading-l"><%= t(".heading", answer_type: t("helpers.label.page.answer_type_options.names.#{answer_type}").downcase) %></h1>
5+
<h1 class="govuk-heading-l"><%= t(".heading", answer_type: t("helpers.label.page.answer_type_options.names.#{answer_type}").downcase, tag:).upcase_first %></h1>
66

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

1012
<div class="govuk-grid-column-full">
11-
<%= govuk_table(
12-
head: report_questions_table_head,
13-
rows: report_questions_table_rows(questions),
14-
) %>
13+
<% if questions.empty? %>
14+
<p class="govuk-body"><%= t(".empty", tag:, answer_type:) %></p>
15+
<% else %>
16+
<%= govuk_table(
17+
head: report_questions_table_head,
18+
rows: report_questions_table_rows(questions),
19+
) %>
20+
<% end %>
1521
</div>
1622
</div>

app/views/reports/selection_questions/summary.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<% set_page_title(t(".title")) %>
2-
<% content_for :back_link, govuk_back_link_to(report_features_path, t(".back_link")) %>
2+
<% content_for :back_link, govuk_back_link_to(report_features_path(tag: :live), t(".back_link")) %>
33
<div class="govuk-grid-row">
44
<div class="govuk-grid-column-two-thirds">
55
<h1 class="govuk-heading-l"><%= t(".title") %></h1>

config/locales/en.yml

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,33 +1385,36 @@ en:
13851385
heading: Answer type usage
13861386
table_headings:
13871387
answer_type: Answer type
1388-
number_of_forms: Number of live forms with this answer type
1389-
number_of_pages: Number of uses of this answer type in live forms
1388+
number_of_forms: Number of %{tag} forms with this answer type
1389+
number_of_pages: Number of uses of this answer type in %{tag} forms
13901390
features:
1391+
forms_with_add_another_answer: "%{tag} forms with add another answer"
1392+
forms_with_csv_submission_enabled: "%{tag} forms with CSV submission enabled"
1393+
forms_with_payments: "%{tag} forms with payments"
1394+
forms_with_routes: "%{tag} forms with routes"
13911395
heading: Feature usage
1392-
live_forms_with_add_another_answer: Live forms with add another answer
1393-
live_forms_with_csv_submission_enabled: Live forms with CSV submission enabled
1394-
live_forms_with_payments: Live forms with payments
1395-
live_forms_with_routes: Live forms with routes
1396-
total_live_forms: Total live forms
1396+
total_forms: Total %{tag} forms
13971397
long_list_usage:
13981398
title: Selection from a list of options feature usage in live forms
1399-
title: Feature and answer type usage in live forms
1399+
title: Feature and answer type usage in %{tag} forms
14001400
form_or_questions_list_table:
14011401
headings:
14021402
form_name: Form name
14031403
number_of_routes: Number of routes
14041404
organisation: Organisation
14051405
question_text: Question text
14061406
forms_with_csv_submission_enabled:
1407-
download_csv: Download data about all live forms with CSV submission enabled as a CSV file
1408-
heading: Live forms with CSV submission enabled
1407+
download_csv: Download data about all %{tag} forms with CSV submission enabled as a CSV file
1408+
empty: There are no %{tag} forms with CSV submission enabled
1409+
heading: "%{tag} forms with CSV submission enabled"
14091410
forms_with_payments:
1410-
download_csv: Download data about all live forms with payments as a CSV file
1411-
heading: Live forms with payments
1411+
download_csv: Download data about all %{tag} forms with payments as a CSV file
1412+
empty: There are no %{tag} forms with payments
1413+
heading: "%{tag} forms with payments"
14121414
forms_with_routes:
1413-
download_csv: Download data about all live forms with routes as a CSV file
1414-
heading: Live forms with routes
1415+
download_csv: Download data about all %{tag} forms with routes as a CSV file
1416+
empty: There are no %{tag} forms with routes
1417+
heading: "%{tag} forms with routes"
14151418
index:
14161419
title: Reports
14171420
last_signed_in_at:
@@ -1421,11 +1424,13 @@ en:
14211424
not_since_last_signed_in_at_added: People who last signed in sometime between 3 November 2023 and 4 November 2024
14221425
title: When users last signed in
14231426
questions_with_add_another_answer:
1424-
download_csv: Download all questions with add another answer in live forms as a CSV file
1425-
heading: Questions with add another answer in live forms
1427+
download_csv: Download all questions with add another answer in %{tag} forms as a CSV file
1428+
empty: There are no questions with add another answer in %{tag} forms
1429+
heading: Questions with add another answer in %{tag} forms
14261430
questions_with_answer_type:
1427-
download_csv: Download all questions in live forms with this answer type as a CSV file
1428-
heading: Live questions with %{answer_type} answer type
1431+
download_csv: Download all questions in %{tag} forms with this answer type as a CSV file
1432+
empty: There are no %{tag} questions with %{answer_type} answer type
1433+
heading: "%{tag} questions with %{answer_type} answer type"
14291434
selection_questions:
14301435
autocomplete:
14311436
title: Questions where you can select one from over 30 options

config/routes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@
197197
scope "/reports" do
198198
get "/", to: "reports#index", as: :reports
199199

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

0 commit comments

Comments
 (0)