chore(perf): improve perf of Dossier#visible_by_user up to 32%

This commit is contained in:
Colin Darie 2023-11-10 15:51:24 +01:00
parent 06a8684bfd
commit 154a993794
3 changed files with 3 additions and 4 deletions

View file

@ -223,7 +223,7 @@ class Dossier < ApplicationRecord
scope :prefilled, -> { where(prefilled: true) }
scope :hidden_by_user, -> { where.not(hidden_by_user_at: nil) }
scope :hidden_by_administration, -> { where.not(hidden_by_administration_at: nil) }
scope :visible_by_user, -> { where(for_procedure_preview: false).or(where(for_procedure_preview: nil)).where(hidden_by_user_at: nil, editing_fork_origin_id: nil) }
scope :visible_by_user, -> { where(for_procedure_preview: false).where(hidden_by_user_at: nil, editing_fork_origin_id: nil) }
scope :visible_by_administration, -> {
state_not_brouillon
.where(hidden_by_administration_at: nil)

View file

@ -32,7 +32,7 @@ class Stat < ApplicationRecord
COUNT(*) FILTER ( WHERE state != 'brouillon' ) AS "not_brouillon",
COUNT(*) FILTER ( WHERE state != 'brouillon' and depose_at BETWEEN :one_month_ago AND :now ) AS "dossiers_depose_avant_30_jours",
COUNT(*) FILTER ( WHERE state != 'brouillon' and depose_at BETWEEN :two_months_ago AND :one_month_ago ) AS "dossiers_deposes_entre_60_et_30_jours",
COUNT(*) FILTER ( WHERE state = 'brouillon' AND editing_fork_origin_id IS NULL AND (for_procedure_preview IS NULL OR for_procedure_preview = false)) AS "brouillon",
COUNT(*) FILTER ( WHERE state = 'brouillon' AND editing_fork_origin_id IS NULL AND for_procedure_preview = false) AS "brouillon",
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"

View file

@ -27,7 +27,6 @@ describe Stat, type: :model do
create_list(:dossier, 2, :en_construction, depose_at: 10.days.ago, procedure:)
create_list(:dossier, 3, :en_construction, depose_at: 40.days.ago, procedure:)
create_list(:dossier, 3, :brouillon, procedure:, for_procedure_preview: nil)
create_list(:dossier, 1, :brouillon, procedure:, for_procedure_preview: false)
create_list(:dossier, 6, :en_instruction, procedure:)
@ -48,7 +47,7 @@ describe Stat, type: :model do
expect(stats["not_brouillon"]).to eq(18)
expect(stats["dossiers_depose_avant_30_jours"]).to eq(2)
expect(stats["dossiers_deposes_entre_60_et_30_jours"]).to eq(3)
expect(stats["brouillon"]).to eq(4)
expect(stats["brouillon"]).to eq(1)
expect(stats["en_construction"]).to eq(5)
expect(stats["en_instruction"]).to eq(6)
expect(stats["termines"]).to eq(7)