From 21f47dcb0185d2fd2b6281d0b3c04bd8c3c7b799 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Thu, 23 Feb 2023 17:14:25 +0100 Subject: [PATCH] feat(procedure): include deleted dossiers in stats --- app/models/concerns/procedure_stats_concern.rb | 12 ++++++++---- app/models/deleted_dossier.rb | 9 +++++++++ app/models/stat.rb | 9 ++++----- spec/models/stat_spec.rb | 7 +++---- 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/app/models/concerns/procedure_stats_concern.rb b/app/models/concerns/procedure_stats_concern.rb index 74e7a12fa..74f7dacd7 100644 --- a/app/models/concerns/procedure_stats_concern.rb +++ b/app/models/concerns/procedure_stats_concern.rb @@ -22,9 +22,9 @@ module ProcedureStatsConcern def stats_dossiers_funnel Rails.cache.fetch("#{cache_key_with_version}/stats_dossiers_funnel", expires_in: 12.hours) do [ - ['Démarrés', dossiers.visible_by_user_or_administration.count], - ['Déposés', dossiers.visible_by_administration.count], - ['Instruction débutée', dossiers.visible_by_administration.state_instruction_commencee.count], + ['Démarrés', dossiers.visible_by_user_or_administration.count + nb_dossiers_termines_supprimes], + ['Déposés', dossiers.visible_by_administration.count + nb_dossiers_termines_supprimes], + ['Instruction débutée', dossiers.visible_by_administration.state_instruction_commencee.count + nb_dossiers_termines_supprimes], ['Traités', nb_dossiers_termines] ] end @@ -96,7 +96,11 @@ module ProcedureStatsConcern private def nb_dossiers_termines - @nb_dossiers_termines ||= dossiers.visible_by_administration.state_termine.count + @nb_dossiers_termines ||= dossiers.visible_by_administration.state_termine.count + nb_dossiers_termines_supprimes + end + + def nb_dossiers_termines_supprimes + @nb_dossiers_termines_supprimes ||= deleted_dossiers.state_termine.count end def first_processed_at diff --git a/app/models/deleted_dossier.rb b/app/models/deleted_dossier.rb index 61bcd66f6..edb9632ac 100644 --- a/app/models/deleted_dossier.rb +++ b/app/models/deleted_dossier.rb @@ -20,6 +20,7 @@ class DeletedDossier < ApplicationRecord scope :order_by_updated_at, -> (order = :desc) { order(created_at: order) } scope :deleted_since, -> (since) { where('deleted_dossiers.deleted_at >= ?', since) } + scope :state_termine, -> { where(state: [states.fetch(:accepte), states.fetch(:refuse), states.fetch(:sans_suite)]) } enum reason: { user_request: 'user_request', @@ -30,6 +31,14 @@ class DeletedDossier < ApplicationRecord instructeur_request: 'instructeur_request' } + enum state: { + en_construction: 'en_construction', + en_instruction: 'en_instruction', + accepte: 'accepte', + refuse: 'refuse', + sans_suite: 'sans_suite' + } + def self.create_from_dossier(dossier, reason) return if !dossier.log_operations? diff --git a/app/models/stat.rb b/app/models/stat.rb index 097c0c592..d4662a410 100644 --- a/app/models/stat.rb +++ b/app/models/stat.rb @@ -63,16 +63,15 @@ class Stat < ApplicationRecord def deleted_dossiers_states sanitize_and_exec(DeletedDossier, <<-EOF SELECT - COUNT(*) FILTER ( WHERE state != 'brouillon' ) AS "not_brouillon", - COUNT(*) FILTER ( WHERE state != 'brouillon' and deleted_at BETWEEN :one_month_ago AND :now ) AS "dossiers_depose_avant_30_jours", - COUNT(*) FILTER ( WHERE state != 'brouillon' and deleted_at BETWEEN :two_months_ago AND :one_month_ago ) AS "dossiers_deposes_entre_60_et_30_jours", - COUNT(*) FILTER ( WHERE state = 'brouillon' ) AS "brouillon", + COUNT(*) AS "not_brouillon", + COUNT(*) FILTER ( WHERE deleted_at BETWEEN :one_month_ago AND :now ) AS "dossiers_depose_avant_30_jours", + COUNT(*) FILTER ( WHERE deleted_at BETWEEN :two_months_ago AND :one_month_ago ) AS "dossiers_deposes_entre_60_et_30_jours", COUNT(*) FILTER ( WHERE state = 'en_construction' ) AS "en_construction", COUNT(*) FILTER ( WHERE state = 'en_instruction' ) AS "en_instruction", COUNT(*) FILTER ( WHERE state in ('accepte', 'refuse', 'sans_suite') ) AS "termines" FROM deleted_dossiers EOF - ) + ).merge('brouillon' => 0) end def last_four_months_serie(associations_with_date_attribute) diff --git a/spec/models/stat_spec.rb b/spec/models/stat_spec.rb index 41138d59a..726d67819 100644 --- a/spec/models/stat_spec.rb +++ b/spec/models/stat_spec.rb @@ -4,10 +4,9 @@ describe Stat do describe '.deleted_dossiers_states' do subject { Stat.send(:deleted_dossiers_states) } it 'find counts for columns' do - create(:deleted_dossier, dossier_id: create(:dossier).id, state: :termine) + create(:deleted_dossier, dossier_id: create(:dossier).id, state: :accepte) create(:deleted_dossier, dossier_id: create(:dossier).id, state: :en_construction, deleted_at: 1.month.ago) create(:deleted_dossier, dossier_id: create(:dossier).id, state: :en_construction, deleted_at: 2.months.ago) - create(:deleted_dossier, dossier_id: create(:dossier).id, state: :brouillon, deleted_at: 3.months.ago) create(:deleted_dossier, dossier_id: create(:dossier).id, state: :en_construction, deleted_at: 3.months.ago) create(:deleted_dossier, dossier_id: create(:dossier).id, state: :en_instruction, deleted_at: 3.months.ago) create(:deleted_dossier, dossier_id: create(:dossier).id, state: :accepte, deleted_at: 3.months.ago) @@ -17,10 +16,10 @@ describe Stat do expect(subject["not_brouillon"]).to eq(8) expect(subject["dossiers_depose_avant_30_jours"]).to eq(1) expect(subject["dossiers_deposes_entre_60_et_30_jours"]).to eq(1) - expect(subject["brouillon"]).to eq(1) + expect(subject["brouillon"]).to eq(0) expect(subject["en_construction"]).to eq(3) expect(subject["en_instruction"]).to eq(1) - expect(subject["termines"]).to eq(3) + expect(subject["termines"]).to eq(4) end end