2020-10-02 16:29:34 +02:00
|
|
|
class Stat < ApplicationRecord
|
|
|
|
class << self
|
|
|
|
def update_stats
|
2021-11-25 09:11:05 +01:00
|
|
|
states = sum_hashes(dossiers_states, deleted_dossiers_states)
|
2020-10-02 16:29:34 +02:00
|
|
|
stat = Stat.first || Stat.new
|
|
|
|
|
|
|
|
stat.update(
|
|
|
|
dossiers_en_construction: states['en_construction'],
|
|
|
|
dossiers_en_instruction: states['en_instruction'],
|
|
|
|
dossiers_brouillon: states['brouillon'],
|
|
|
|
dossiers_depose_avant_30_jours: states['dossiers_depose_avant_30_jours'],
|
|
|
|
dossiers_deposes_entre_60_et_30_jours: states['dossiers_deposes_entre_60_et_30_jours'],
|
|
|
|
dossiers_not_brouillon: states['not_brouillon'],
|
|
|
|
dossiers_termines: states['termines'],
|
2021-12-03 02:46:45 +01:00
|
|
|
dossiers_cumulative: cumulative_month_serie([
|
2021-12-06 15:49:17 +01:00
|
|
|
[Dossier.state_not_brouillon, :depose_at],
|
2024-04-02 23:42:58 +02:00
|
|
|
[DeletedDossier.where.not(state: :brouillon), :depose_at]
|
2021-11-25 09:11:05 +01:00
|
|
|
]),
|
2021-12-03 02:46:45 +01:00
|
|
|
dossiers_in_the_last_4_months: last_four_months_serie([
|
2021-12-06 15:49:17 +01:00
|
|
|
[Dossier.state_not_brouillon, :depose_at],
|
2024-04-02 23:42:58 +02:00
|
|
|
[DeletedDossier.where.not(state: :brouillon), :depose_at]
|
2021-11-25 09:11:05 +01:00
|
|
|
]),
|
2020-10-06 14:23:15 +02:00
|
|
|
administrations_partenaires: AdministrateursProcedure.joins(:procedure).merge(Procedure.publiees_ou_closes).select('distinct administrateur_id').count
|
2020-10-02 16:29:34 +02:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def dossiers_states
|
2021-11-25 09:11:05 +01:00
|
|
|
sanitize_and_exec(Dossier, <<-EOF
|
|
|
|
SELECT
|
|
|
|
COUNT(*) FILTER ( WHERE state != 'brouillon' ) AS "not_brouillon",
|
2021-12-06 15:49:17 +01:00
|
|
|
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",
|
2023-11-10 15:51:24 +01:00
|
|
|
COUNT(*) FILTER ( WHERE state = 'brouillon' AND editing_fork_origin_id IS NULL AND for_procedure_preview = false) AS "brouillon",
|
2021-11-25 09:11:05 +01:00
|
|
|
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 dossiers
|
2024-04-02 23:42:58 +02:00
|
|
|
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')
|
2020-10-02 16:29:34 +02:00
|
|
|
EOF
|
2021-11-25 09:11:05 +01:00
|
|
|
)
|
|
|
|
end
|
2020-10-02 16:29:34 +02:00
|
|
|
|
2021-11-25 09:11:05 +01:00
|
|
|
def deleted_dossiers_states
|
|
|
|
sanitize_and_exec(DeletedDossier, <<-EOF
|
|
|
|
SELECT
|
2023-02-23 17:14:25 +01:00
|
|
|
COUNT(*) AS "not_brouillon",
|
2024-04-02 23:42:58 +02:00
|
|
|
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",
|
2021-11-25 09:11:05 +01:00
|
|
|
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
|
2023-02-23 17:14:25 +01:00
|
|
|
).merge('brouillon' => 0)
|
2020-10-02 16:29:34 +02:00
|
|
|
end
|
|
|
|
|
2021-12-03 02:46:45 +01:00
|
|
|
def last_four_months_serie(associations_with_date_attribute)
|
2021-11-25 09:11:05 +01:00
|
|
|
timeseries = associations_with_date_attribute.map do |association, date_attribute|
|
2021-12-03 02:46:45 +01:00
|
|
|
association.group_by_month(date_attribute, last: 4, current: false).count
|
2021-11-25 09:11:05 +01:00
|
|
|
end
|
2020-10-02 16:29:34 +02:00
|
|
|
|
2022-02-02 09:17:43 +01:00
|
|
|
sum_hashes(*timeseries).sort.to_h
|
2021-12-03 02:46:45 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def cumulative_month_serie(associations_with_date_attribute)
|
2021-11-25 09:11:05 +01:00
|
|
|
timeseries = associations_with_date_attribute.map do |association, date_attribute|
|
2021-12-03 02:46:45 +01:00
|
|
|
association.group_by_month(date_attribute, current: false).count
|
2021-11-25 09:11:05 +01:00
|
|
|
end
|
|
|
|
|
2021-12-03 02:46:45 +01:00
|
|
|
cumulative_serie(sum_hashes(*timeseries))
|
|
|
|
end
|
|
|
|
|
|
|
|
def cumulative_serie(sums)
|
2020-10-02 16:29:34 +02:00
|
|
|
sum = 0
|
2021-12-03 02:46:45 +01:00
|
|
|
sums.keys.sort.index_with { |date| sum += sums[date] }
|
2020-10-02 16:29:34 +02:00
|
|
|
end
|
|
|
|
|
2021-11-25 09:11:05 +01:00
|
|
|
def sum_hashes(*hashes)
|
2021-12-03 02:46:45 +01:00
|
|
|
{}.merge(*hashes) { |_k, v1, v2| v1 + v2 }
|
2021-11-25 09:11:05 +01:00
|
|
|
end
|
|
|
|
|
2020-10-02 16:29:34 +02:00
|
|
|
def max_date
|
|
|
|
Time.zone.now.beginning_of_month - 1.second
|
|
|
|
end
|
2021-11-25 09:11:05 +01:00
|
|
|
|
|
|
|
def sanitize_and_exec(model, query)
|
|
|
|
sanitized_query = ActiveRecord::Base.sanitize_sql([
|
|
|
|
query,
|
|
|
|
now: Time.zone.now,
|
|
|
|
one_month_ago: 1.month.ago,
|
|
|
|
two_months_ago: 2.months.ago
|
|
|
|
])
|
|
|
|
model.connection.select_all(sanitized_query).first
|
|
|
|
end
|
2020-10-02 16:29:34 +02:00
|
|
|
end
|
|
|
|
end
|