Skip to content

Commit

Permalink
feat(cms): update case stats with new stages
Browse files Browse the repository at this point in the history
Jira: PWNN-1528
  • Loading branch information
EdwinKruglov committed Sep 5, 2023
1 parent bca0a7d commit d5e875f
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 28 deletions.
2 changes: 1 addition & 1 deletion app/models/support/tower_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ module Support
class TowerCase < ApplicationRecord
UNSPECIFIED_VALUE = 99
enum state: Case.states, _prefix: :state
enum stage: Procurement.stages.merge(unspecified: UNSPECIFIED_VALUE), _prefix: :stage
enum support_level: Case.support_levels.merge(unspecified: UNSPECIFIED_VALUE), _prefix: :support_level
belongs_to :support_tower, class_name: "Support::Tower", foreign_key: :tower_id
belongs_to :procurement_stage, class_name: "Support::ProcurementStage"
end
end
53 changes: 35 additions & 18 deletions app/models/support/tower_statistics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,49 @@ def open_cases = @tower_overview.open_cases
def on_hold_cases = @tower_overview.on_hold_cases
def new_cases = @tower_overview.new_cases
def missing_level_cases = TowerCase.where(tower_slug:, support_level: TowerCase::UNSPECIFIED_VALUE).count
def missing_stage_cases = TowerCase.where(tower_slug:, procurement_stage: TowerCase::UNSPECIFIED_VALUE).count
def missing_stage_cases = TowerCase.where(tower_slug:, procurement_stage_id: nil).count
def missing_value_cases = TowerCase.where(tower_slug:, value: nil).count
def missing_org_cases = TowerCase.where(tower_slug:, organisation_id: nil).count

def breakdown_of_cases_by_stage
sql = <<-SQL
SELECT
procurement_stage.stage,
SUM(CASE WHEN support_tower_cases.state = 1 THEN 1 ELSE 0 END) AS open_cases,
SUM(CASE WHEN support_tower_cases.state = 3 THEN 1 ELSE 0 END) AS on_hold_cases,
SUM(CASE WHEN support_tower_cases.state = 0 THEN 1 ELSE 0 END) AS new_cases,
SUM(COALESCE(support_tower_cases.value, 0.0)) AS live_value,
'' AS id
SELECT sub.procurement_stage_id, sub.procurement_stage_title, sub.open_cases, sub.on_hold_cases, sub.new_cases, sub.live_value, sub.id
FROM (
SELECT * FROM GENERATE_SERIES(0, 6) AS stage
UNION SELECT :unspecified AS stage
ORDER BY stage
) procurement_stage
LEFT JOIN support_tower_cases
ON support_tower_cases.procurement_stage = procurement_stage.stage
AND support_tower_cases.tower_slug = :tower_slug
GROUP BY procurement_stage.stage
ORDER BY procurement_stage.stage
SELECT
sps.id::text AS procurement_stage_id,
sps.title AS procurement_stage_title,
sps.stage AS procurement_stage_stage,
sps.updated_at AS procurement_stage_updated_at,
SUM(CASE WHEN stc.state = 1 THEN 1 ELSE 0 END) AS open_cases,
SUM(CASE WHEN stc.state = 3 THEN 1 ELSE 0 END) AS on_hold_cases,
SUM(CASE WHEN stc.state = 0 THEN 1 ELSE 0 END) AS new_cases,
SUM(COALESCE(stc.value, 0.0)) AS live_value,
'' AS id
FROM support_tower_cases stc
RIGHT JOIN support_procurement_stages sps
ON stc.procurement_stage_id = sps.id
AND stc.tower_slug = :tower_slug
GROUP BY sps.id
UNION
SELECT
'unspecified' AS procurement_stage_id,
'Unspecified' AS procurement_stage_title,
NULL AS procurement_stage_stage,
NULL AS procurement_stage_updated_at,
SUM(CASE WHEN stc.state = 1 THEN 1 ELSE 0 END) AS open_cases,
SUM(CASE WHEN stc.state = 3 THEN 1 ELSE 0 END) AS on_hold_cases,
SUM(CASE WHEN stc.state = 0 THEN 1 ELSE 0 END) AS new_cases,
SUM(COALESCE(stc.value, 0.0)) AS live_value,
'' AS id
FROM support_tower_cases stc
WHERE stc.procurement_stage_id IS NULL AND stc.tower_slug = :tower_slug
) AS sub
ORDER BY sub.procurement_stage_stage, sub.procurement_stage_updated_at
SQL

TowerCase.find_by_sql([sql, { tower_slug:, unspecified: TowerCase::UNSPECIFIED_VALUE }])
TowerCase.find_by_sql([sql, { tower_slug: }])
end

def breakdown_of_cases_by_level
Expand Down
10 changes: 5 additions & 5 deletions app/views/support/case_statistics/towers/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@
<tbody class="govuk-table__body">
<% @tower_statistics.breakdown_of_cases_by_stage.each do |stage| %>
<tr class="govuk-table__row">
<th scope="row" class="govuk-table__header"><%= link_to I18n.t("support.case_statistics.stages.#{stage.stage}"), filtered_tower_path(@tower, stage: stage.stage, state: "live"), class: "govuk-link govuk-link--no-visited-state" %></th>
<td class="govuk-table__cell result-value"><%= link_to stage.open_cases, filtered_tower_path(@tower, stage: stage.stage, state: "opened"), class: "govuk-link govuk-link--no-visited-state" %></td>
<td class="govuk-table__cell result-value"><%= link_to stage.on_hold_cases, filtered_tower_path(@tower, stage: stage.stage, state: "on_hold"), class: "govuk-link govuk-link--no-visited-state" %></td>
<td class="govuk-table__cell result-value"><%= link_to stage.new_cases, filtered_tower_path(@tower, stage: stage.stage, state: "initial"), class: "govuk-link govuk-link--no-visited-state" %></td>
<th scope="row" class="govuk-table__header"><%= link_to stage.procurement_stage_title, filtered_tower_path(@tower, procurement_stage: stage.procurement_stage, state: "live"), class: "govuk-link govuk-link--no-visited-state" %></th>
<td class="govuk-table__cell result-value"><%= link_to stage.open_cases, filtered_tower_path(@tower, procurement_stage: stage.procurement_stage, state: "opened"), class: "govuk-link govuk-link--no-visited-state" %></td>
<td class="govuk-table__cell result-value"><%= link_to stage.on_hold_cases, filtered_tower_path(@tower, procurement_stage: stage.procurement_stage, state: "on_hold"), class: "govuk-link govuk-link--no-visited-state" %></td>
<td class="govuk-table__cell result-value"><%= link_to stage.new_cases, filtered_tower_path(@tower, procurement_stage: stage.procurement_stage, state: "initial"), class: "govuk-link govuk-link--no-visited-state" %></td>
<%
=begin%>
# TODO: uncomment when ready to do case value work
Expand Down Expand Up @@ -137,6 +137,6 @@
<p><%= link_to I18n.t("support.case_statistics.missing_data.no_value", count: @tower_statistics.missing_value_cases), "#", class: "govuk-link govuk-link--no-visited-state" %></p>
<%
=end%>
<p><%= link_to I18n.t("support.case_statistics.missing_data.no_stage", count: @tower_statistics.missing_stage_cases).html_safe, filtered_tower_path(@tower, stage: :unspecified, state: "live"), class: "govuk-link govuk-link--no-visited-state" %></p>
<p><%= link_to I18n.t("support.case_statistics.missing_data.no_stage", count: @tower_statistics.missing_stage_cases).html_safe, filtered_tower_path(@tower, procurement_stage: :unspecified, state: "live"), class: "govuk-link govuk-link--no-visited-state" %></p>
<p><%= link_to I18n.t("support.case_statistics.missing_data.no_level", count: @tower_statistics.missing_level_cases).html_safe, filtered_tower_path(@tower, level: :unspecified, state: "live"), class: "govuk-link govuk-link--no-visited-state" %></p>
<p><%= link_to I18n.t("support.case_statistics.missing_data.no_org", count: @tower_statistics.missing_org_cases).html_safe, filtered_tower_path(@tower, has_org: :false, state: "live"), class: "govuk-link govuk-link--no-visited-state" %></p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class UpdateSupportTowerCasesToVersion4 < ActiveRecord::Migration[7.0]
def change
update_view :support_tower_cases, version: 4, revert_to_version: 3
end
end
7 changes: 3 additions & 4 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2023_08_25_101049) do
ActiveRecord::Schema[7.0].define(version: 2023_08_30_101649) do
# These are extensions that must be enabled in order to support this database
enable_extension "citext"
enable_extension "pg_trgm"
Expand Down Expand Up @@ -995,15 +995,14 @@
sc.value,
sc.procurement_id,
sc.organisation_id,
COALESCE(sp.stage, 99) AS procurement_stage,
sc.procurement_stage_id,
COALESCE(sc.support_level, 99) AS support_level,
COALESCE(tow.title, 'No Tower'::character varying) AS tower_name,
lower(replace((COALESCE(tow.title, 'No Tower'::character varying))::text, ' '::text, '-'::text)) AS tower_slug,
tow.id AS tower_id,
sc.created_at,
sc.updated_at
FROM (((support_cases sc
JOIN support_procurements sp ON ((sp.id = sc.procurement_id)))
FROM ((support_cases sc
LEFT JOIN support_categories cat ON ((sc.category_id = cat.id)))
LEFT JOIN support_towers tow ON ((cat.support_tower_id = tow.id)))
WHERE (sc.state = ANY (ARRAY[0, 1, 3]));
Expand Down
16 changes: 16 additions & 0 deletions db/views/support_tower_cases_v04.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
SELECT sc.id,
sc.state,
sc.value,
sc.procurement_id,
sc.organisation_id,
sc.procurement_stage_id AS procurement_stage_id,
COALESCE(sc.support_level, 99) AS support_level,
COALESCE(tow.title, 'No Tower') AS tower_name,
LOWER(REPLACE(COALESCE(tow.title, 'No Tower'), ' ', '-')) AS tower_slug,
tow.id AS tower_id,
sc.created_at,
sc.updated_at
FROM support_cases sc
LEFT JOIN support_categories AS cat ON sc.category_id = cat.id
LEFT JOIN support_towers AS tow ON cat.support_tower_id = tow.id
WHERE sc.state IN (0, 1, 3) -- new, opened, on-hold

0 comments on commit d5e875f

Please sign in to comment.