fix(stats): more accurat stats about deleted dossiers

This commit is contained in:
Paul Chavard 2024-04-02 23:42:58 +02:00
parent bb88be7d9c
commit 282ce1322c
8 changed files with 38 additions and 17 deletions

View file

@ -14,6 +14,6 @@ class Cron::Datagouv::FileByMonthJob < Cron::CronJob
end
def data
Dossier.where(created_at: 1.month.ago.all_month).count
Dossier.visible_by_user_or_administration.where(created_at: 1.month.ago.all_month).count
end
end

View file

@ -14,6 +14,8 @@ class Cron::Datagouv::FileDeposeByMonthJob < Cron::CronJob
end
def data
Dossier.where(depose_at: 1.month.ago.all_month).count
Dossier.visible_by_user_or_administration
.where(depose_at: 1.month.ago.all_month).count + DeletedDossier
.where(depose_at: 1.month.ago.all_month).count
end
end

View file

@ -36,6 +36,7 @@ class DeletedDossier < ApplicationRecord
user_id: dossier.user_id,
procedure: dossier.procedure,
state: dossier.state,
depose_at: dossier.depose_at,
deleted_at: Time.zone.now
).create_or_find_by!(dossier_id: dossier.id)
end

View file

@ -14,11 +14,11 @@ class Stat < ApplicationRecord
dossiers_termines: states['termines'],
dossiers_cumulative: cumulative_month_serie([
[Dossier.state_not_brouillon, :depose_at],
[DeletedDossier.where.not(state: :brouillon), :deleted_at]
[DeletedDossier.where.not(state: :brouillon), :depose_at]
]),
dossiers_in_the_last_4_months: last_four_months_serie([
[Dossier.state_not_brouillon, :depose_at],
[DeletedDossier.where.not(state: :brouillon), :deleted_at]
[DeletedDossier.where.not(state: :brouillon), :depose_at]
]),
administrations_partenaires: AdministrateursProcedure.joins(:procedure).merge(Procedure.publiees_ou_closes).select('distinct administrateur_id').count
)
@ -37,8 +37,7 @@ class Stat < ApplicationRecord
COUNT(*) FILTER ( WHERE state = 'en_instruction' ) AS "en_instruction",
COUNT(*) FILTER ( WHERE state in ('accepte', 'refuse', 'sans_suite') ) AS "termines"
FROM dossiers
WHERE hidden_at IS NULL
AND revision_id NOT IN (SELECT r.id FROM procedure_revisions r LEFT JOIN procedures p ON r.procedure_id=p.id WHERE p.aasm_state = 'brouillon')
WHERE revision_id NOT IN (SELECT r.id FROM procedure_revisions r LEFT JOIN procedures p ON r.procedure_id=p.id WHERE p.aasm_state = 'brouillon')
EOF
)
end
@ -47,8 +46,8 @@ class Stat < ApplicationRecord
sanitize_and_exec(DeletedDossier, <<-EOF
SELECT
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 depose_at BETWEEN :one_month_ago AND :now ) AS "dossiers_depose_avant_30_jours",
COUNT(*) FILTER ( WHERE depose_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"

View file

@ -0,0 +1,13 @@
# frozen_string_literal: true
module Maintenance
class BackfillDeposeAtOnDeletedDossiersTask < MaintenanceTasks::Task
def collection
DeletedDossier.where(depose_at: nil)
end
def process(element)
element.update_column(:depose_at, element.deleted_at)
end
end
end

View file

@ -0,0 +1,5 @@
class AddDeposeAtToDeletedDossiers < ActiveRecord::Migration[7.0]
def change
add_column :deleted_dossiers, :depose_at, :date
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2024_03_25_161743) do
ActiveRecord::Schema[7.0].define(version: 2024_04_02_212902) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_buffercache"
enable_extension "pg_stat_statements"
@ -359,6 +359,7 @@ ActiveRecord::Schema[7.0].define(version: 2024_03_25_161743) do
create_table "deleted_dossiers", force: :cascade do |t|
t.datetime "created_at", precision: nil, null: false
t.datetime "deleted_at", precision: nil
t.date "depose_at"
t.bigint "dossier_id"
t.bigint "groupe_instructeur_id"
t.bigint "procedure_id"

View file

@ -2,14 +2,14 @@ describe Stat, type: :model 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: :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: :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)
create(:deleted_dossier, dossier_id: create(:dossier).id, state: :refuse, deleted_at: 3.months.ago)
create(:deleted_dossier, dossier_id: create(:dossier).id, state: :sans_suite, deleted_at: 3.months.ago)
create(:deleted_dossier, dossier_id: create(:dossier).id, state: :accepte, deleted_at: Time.zone.now, depose_at: Time.zone.now)
create(:deleted_dossier, dossier_id: create(:dossier).id, state: :en_construction, deleted_at: 33.days.ago, depose_at: 33.days.ago)
create(:deleted_dossier, dossier_id: create(:dossier).id, state: :en_construction, deleted_at: 66.days.ago, depose_at: 66.days.ago)
create(:deleted_dossier, dossier_id: create(:dossier).id, state: :en_construction, deleted_at: 3.months.ago, depose_at: 3.months.ago)
create(:deleted_dossier, dossier_id: create(:dossier).id, state: :en_instruction, deleted_at: 3.months.ago, depose_at: 3.months.ago)
create(:deleted_dossier, dossier_id: create(:dossier).id, state: :accepte, deleted_at: 3.months.ago, depose_at: 3.months.ago)
create(:deleted_dossier, dossier_id: create(:dossier).id, state: :refuse, deleted_at: 3.months.ago, depose_at: 3.months.ago)
create(:deleted_dossier, dossier_id: create(:dossier).id, state: :sans_suite, deleted_at: 3.months.ago, depose_at: 3.months.ago)
expect(subject["not_brouillon"]).to eq(8)
expect(subject["dossiers_depose_avant_30_jours"]).to eq(1)