diff --git a/app/jobs/cron/datagouv/file_by_month_job.rb b/app/jobs/cron/datagouv/file_by_month_job.rb index f4375679a..7cf7b0357 100644 --- a/app/jobs/cron/datagouv/file_by_month_job.rb +++ b/app/jobs/cron/datagouv/file_by_month_job.rb @@ -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 diff --git a/app/jobs/cron/datagouv/file_depose_by_month_job.rb b/app/jobs/cron/datagouv/file_depose_by_month_job.rb index c7fd2d828..4191356e9 100644 --- a/app/jobs/cron/datagouv/file_depose_by_month_job.rb +++ b/app/jobs/cron/datagouv/file_depose_by_month_job.rb @@ -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 diff --git a/app/models/deleted_dossier.rb b/app/models/deleted_dossier.rb index 429b89c6c..b8caead0e 100644 --- a/app/models/deleted_dossier.rb +++ b/app/models/deleted_dossier.rb @@ -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 diff --git a/app/models/stat.rb b/app/models/stat.rb index 9ce8744b6..d4cd331a2 100644 --- a/app/models/stat.rb +++ b/app/models/stat.rb @@ -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" diff --git a/app/tasks/maintenance/backfill_depose_at_on_deleted_dossiers_task.rb b/app/tasks/maintenance/backfill_depose_at_on_deleted_dossiers_task.rb new file mode 100644 index 000000000..2f8eb6178 --- /dev/null +++ b/app/tasks/maintenance/backfill_depose_at_on_deleted_dossiers_task.rb @@ -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 diff --git a/db/migrate/20240402212902_add_depose_at_to_deleted_dossiers.rb b/db/migrate/20240402212902_add_depose_at_to_deleted_dossiers.rb new file mode 100644 index 000000000..950684743 --- /dev/null +++ b/db/migrate/20240402212902_add_depose_at_to_deleted_dossiers.rb @@ -0,0 +1,5 @@ +class AddDeposeAtToDeletedDossiers < ActiveRecord::Migration[7.0] + def change + add_column :deleted_dossiers, :depose_at, :date + end +end diff --git a/db/schema.rb b/db/schema.rb index 866214bcd..2e8f741dc 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" diff --git a/spec/models/stat_spec.rb b/spec/models/stat_spec.rb index 0e2c7a26f..0898f7ef7 100644 --- a/spec/models/stat_spec.rb +++ b/spec/models/stat_spec.rb @@ -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)