Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Production < staging #727

Merged
merged 3 commits into from
Sep 17, 2024
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
38 changes: 31 additions & 7 deletions app/search/nomination_stats_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ class NominationStatsSearch < Search
]

FETCH_QUERY = %Q{
ceremonial_counties.id AS ceremonial_county_id,
CASE WHEN ceremonial_counties.name IS NULL THEN 'Not assigned' ELSE ceremonial_counties.name END AS ceremonial_county_name,
#{TRACKED_STATES.map { |s| "COUNT(CASE WHEN form_answers.state = '#{s}' THEN 1 END) AS #{s}_count" }.join(',')},
COUNT(CASE WHEN form_answers.state IN (#{TRACKED_STATES.map { |s| "'#{s}'" }.join(',')}) THEN 1 END) AS total_count
}.squish.freeze

def self.default_search
{
sort: "ceremonial_county_name",
sort: "name",
search_filter: {
year: "all_years",
assigned_ceremonial_county: FormAnswerStatus::AdminFilter.values('assigned county')
Expand All @@ -29,13 +30,36 @@ def self.default_search
end

def results
super
@search_results = scope

@search_results = @search_results
if !!filter_params[:year].presence
@search_results = filter_by_year(@search_results, filter_params[:year].presence)
end

cte_query = @search_results
.select(FETCH_QUERY)
.left_joins(:ceremonial_county)
.group("ceremonial_counties.name")
.group("ceremonial_counties.id, ceremonial_counties.name")

base_query = CeremonialCounty.select(:id, :name)
base_scope = base_query.from("(#{base_query.to_sql} UNION ALL (SELECT NULL AS id, 'Not assigned' AS name)) AS ceremonial_counties")

if !!filter_params[:assigned_ceremonial_county].presence
base_scope = filter_by_assigned_ceremonial_county(base_scope, filter_params[:assigned_ceremonial_county].presence)
end

desc = params.fetch(:sort, "").split(".")&.last == "desc"
base_scope = sort_by_ceremonial_county_name(base_scope, desc)

@search_results = base_scope.with(data: cte_query)
.select(%Q{
ceremonial_counties.id,
ceremonial_counties.name,
#{TRACKED_STATES.map { |s| "COALESCE(data.#{s}_count, 0) AS #{s}_count" }.join(',')},
COALESCE(data.total_count, 0) AS total_count
}.squish)
.joins("LEFT OUTER JOIN data ON data.ceremonial_county_id = ceremonial_counties.id")

@search_results
end

Expand All @@ -51,11 +75,11 @@ def filter_by_assigned_ceremonial_county(scoped_results, value)
value = value.map do |v|
v == "not_assigned" ? nil : v
end
scoped_results.where(ceremonial_county_id: value)

scoped_results.where(id: value)
end

def sort_by_ceremonial_county_name(scoped_results, desc = false)
scoped_results.order("ceremonial_counties.name #{sort_order(desc)}")
scoped_results.order("name #{sort_order(desc)}")
end

end
4 changes: 2 additions & 2 deletions app/views/admin/statistics/nominations/_list.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ div role="region" aria-labelledby="table-list-nomination-statistics-caption" tab
thead.govuk-table__head
tr.govuk-table__row
th.sortable.govuk-table__header.kavs-table__header--dense.kavs-table__header--border-right scope="col" rowspan="2"
= sort_link f, 'Lieutenancy', object, :ceremonial_county_name
= sort_link f, 'Lieutenancy', object, :name
th.govuk-table__header.kavs-table__header--dense.kavs-table__header--border-right scope="col" rowspan="2"
| Submitted nominations
th.govuk-table__header.kavs-table__header--dense.kavs-table__header--border-right.text-center scope="col" colspan="4"
Expand Down Expand Up @@ -55,7 +55,7 @@ div role="region" aria-labelledby="table-list-nomination-statistics-caption" tab
- results.each do |row|
tr.govuk-table__row
th.govuk-table__header.kavs-table__header--dense.kavs-table__header--border-right
= row[:ceremonial_county_name]
= row[:name]
td.govuk-table__cell.kavs-table__cell--dense.kavs-table__cell--border-right
= row[:submitted_count]
td.govuk-table__cell.kavs-table__cell--dense.kavs-table__cell--border-right
Expand Down
Loading