Merge pull request #8686 from tchak/fix-procedure-stats

fix(demarche): exclude preview and deleted dossiers from stats
This commit is contained in:
Paul Chavard 2023-02-23 16:22:19 +00:00 committed by GitHub
commit c4746099fa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 42 additions and 22 deletions

View file

@ -22,21 +22,20 @@ 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.count],
['Déposés', dossiers.state_not_brouillon.count],
['Instruction débutée', dossiers.state_instruction_commencee.count],
['Traités', dossiers.state_termine.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
end
def stats_termines_states
nb_dossiers_termines = dossiers.state_termine.count
Rails.cache.fetch("#{cache_key_with_version}/stats_termines_states", expires_in: 12.hours) do
[
['Acceptés', percentage(dossiers.where(state: :accepte).count, nb_dossiers_termines)],
['Refusés', percentage(dossiers.where(state: :refuse).count, nb_dossiers_termines)],
['Classés sans suite', percentage(dossiers.where(state: :sans_suite).count, nb_dossiers_termines)]
['Acceptés', percentage(dossiers.visible_by_administration.state_accepte.count, nb_dossiers_termines)],
['Refusés', percentage(dossiers.visible_by_administration.state_refuse.count, nb_dossiers_termines)],
['Classés sans suite', percentage(dossiers.visible_by_administration.state_sans_suite.count, nb_dossiers_termines)]
]
end
end
@ -45,6 +44,7 @@ module ProcedureStatsConcern
Rails.cache.fetch("#{cache_key_with_version}/stats_termines_by_week", expires_in: 12.hours) do
now = Time.zone.now
chart_data = dossiers.includes(:traitements)
.visible_by_administration
.state_termine
.where(traitements: { processed_at: (now.beginning_of_week - 6.months)..now.end_of_week })
@ -95,6 +95,14 @@ module ProcedureStatsConcern
private
def nb_dossiers_termines
@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
Traitement.for_traitement_time_stats(self).pick(:processed_at)
end

View file

@ -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?

View file

@ -224,6 +224,9 @@ class Dossier < ApplicationRecord
scope :state_instruction_commencee, -> { where(state: INSTRUCTION_COMMENCEE) }
scope :state_termine, -> { where(state: TERMINE) }
scope :state_not_termine, -> { where.not(state: TERMINE) }
scope :state_accepte, -> { where(state: states.fetch(:accepte)) }
scope :state_refuse, -> { where(state: states.fetch(:refuse)) }
scope :state_sans_suite, -> { where(state: states.fetch(:sans_suite)) }
scope :archived, -> { where(archived: true) }
scope :not_archived, -> { where(archived: false) }

View file

@ -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)

View file

@ -21,7 +21,7 @@ class Traitement < ApplicationRecord
scope :for_traitement_time_stats, -> (procedure) do
includes(:dossier)
.termine
.where(dossier: procedure.dossiers)
.where(dossier: procedure.dossiers.visible_by_administration)
.where.not('dossiers.depose_at' => nil, processed_at: nil)
.order(:processed_at)
end

View file

@ -7,15 +7,17 @@ describe ProcedureStatsConcern do
before do
create_list(:dossier, 2, :brouillon, procedure: procedure)
create(:dossier, :en_instruction, procedure: procedure)
create(:dossier, procedure: procedure, for_procedure_preview: true)
create(:dossier, :accepte, procedure: procedure, hidden_by_administration_at: Time.zone.now)
end
it "returns the funnel stats" do
expect(stats_dossiers_funnel).to match(
[
['Démarrés', procedure.dossiers.count],
['Déposés', procedure.dossiers.state_not_brouillon.count],
['Instruction débutée', procedure.dossiers.state_instruction_commencee.count],
['Traités', procedure.dossiers.state_termine.count]
['Démarrés', procedure.dossiers.visible_by_user_or_administration.count],
['Déposés', procedure.dossiers.visible_by_administration.count],
['Instruction débutée', procedure.dossiers.visible_by_administration.state_instruction_commencee.count],
['Traités', procedure.dossiers.visible_by_administration.state_termine.count]
]
)
end

View file

@ -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