2017-03-24 18:04:37 +01:00
|
|
|
class StatsController < ApplicationController
|
2020-11-05 15:09:11 +01:00
|
|
|
before_action :authenticate_super_admin!, only: [:download]
|
2018-08-13 14:46:22 +02:00
|
|
|
|
2017-05-26 17:27:23 +02:00
|
|
|
MEAN_NUMBER_OF_CHAMPS_IN_A_FORM = 24.0
|
|
|
|
|
2017-03-24 18:04:37 +01:00
|
|
|
def index
|
2020-10-02 16:29:34 +02:00
|
|
|
stat = Stat.first
|
2020-10-02 13:28:22 +02:00
|
|
|
|
2019-11-14 09:43:45 +01:00
|
|
|
procedures = Procedure.publiees_ou_closes
|
2017-04-03 15:00:26 +02:00
|
|
|
|
2018-08-29 14:54:06 +02:00
|
|
|
@procedures_numbers = procedures_numbers(procedures)
|
2020-10-02 14:14:32 +02:00
|
|
|
|
|
|
|
@dossiers_numbers = dossiers_numbers(
|
2020-10-02 16:29:34 +02:00
|
|
|
stat.dossiers_not_brouillon,
|
|
|
|
stat.dossiers_depose_avant_30_jours,
|
|
|
|
stat.dossiers_deposes_entre_60_et_30_jours
|
2020-10-02 14:14:32 +02:00
|
|
|
)
|
2017-05-29 17:16:26 +02:00
|
|
|
|
2021-12-01 17:34:00 +01:00
|
|
|
@contact_percentage = Rails.cache.fetch("stats.contact_percentage", expires_in: 1.day) do
|
|
|
|
contact_percentage
|
|
|
|
end
|
2018-12-18 10:35:23 +01:00
|
|
|
|
2020-10-02 16:29:34 +02:00
|
|
|
@dossiers_states_for_pie = {
|
|
|
|
"Brouillon" => stat.dossiers_brouillon,
|
|
|
|
"En construction" => stat.dossiers_en_construction,
|
|
|
|
"En instruction" => stat.dossiers_en_instruction,
|
|
|
|
"Terminé" => stat.dossiers_termines
|
|
|
|
}
|
2018-08-24 17:10:27 +02:00
|
|
|
|
2017-07-17 15:10:05 +02:00
|
|
|
@procedures_cumulative = cumulative_hash(procedures, :published_at)
|
|
|
|
@procedures_in_the_last_4_months = last_four_months_hash(procedures, :published_at)
|
2017-05-26 17:29:31 +02:00
|
|
|
|
2020-10-02 16:29:34 +02:00
|
|
|
@dossiers_cumulative = stat.dossiers_cumulative
|
|
|
|
@dossiers_in_the_last_4_months = stat.dossiers_in_the_last_4_months
|
2017-03-24 18:04:37 +01:00
|
|
|
end
|
|
|
|
|
2018-08-13 14:46:22 +02:00
|
|
|
def download
|
|
|
|
headers = [
|
|
|
|
'ID du dossier',
|
2018-09-05 14:48:42 +02:00
|
|
|
'ID de la démarche',
|
|
|
|
'Nom de la démarche',
|
2018-08-13 14:46:22 +02:00
|
|
|
'ID utilisateur',
|
|
|
|
'Etat du fichier',
|
|
|
|
'Durée en brouillon',
|
|
|
|
'Durée en construction',
|
|
|
|
'Durée en instruction'
|
|
|
|
]
|
|
|
|
|
|
|
|
data = Dossier
|
|
|
|
.includes(:procedure, :user)
|
|
|
|
.in_batches
|
|
|
|
.flat_map do |dossiers|
|
|
|
|
dossiers
|
|
|
|
.pluck(
|
|
|
|
"dossiers.id",
|
|
|
|
"procedures.id",
|
|
|
|
"procedures.libelle",
|
|
|
|
"users.id",
|
|
|
|
"dossiers.state",
|
2021-12-06 15:49:17 +01:00
|
|
|
"dossiers.depose_at - dossiers.created_at",
|
|
|
|
"dossiers.en_instruction_at - dossiers.depose_at",
|
2018-08-13 14:46:22 +02:00
|
|
|
"dossiers.processed_at - dossiers.en_instruction_at"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.csv { send_data(SpreadsheetArchitect.to_xlsx(headers: headers, data: data), filename: "statistiques.csv") }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-24 18:04:37 +01:00
|
|
|
private
|
|
|
|
|
2018-08-29 14:54:06 +02:00
|
|
|
def procedures_numbers(procedures)
|
|
|
|
total = procedures.count
|
2018-10-25 15:11:12 +02:00
|
|
|
last_30_days_count = procedures.where(published_at: 1.month.ago..Time.zone.now).count
|
2018-08-29 14:54:06 +02:00
|
|
|
previous_count = procedures.where(published_at: 2.months.ago..1.month.ago).count
|
2018-09-03 17:35:14 +02:00
|
|
|
if previous_count != 0
|
|
|
|
evolution = (((last_30_days_count.to_f / previous_count) - 1) * 100).round(0)
|
|
|
|
else
|
|
|
|
evolution = 0
|
|
|
|
end
|
2018-12-18 21:55:45 +01:00
|
|
|
formatted_evolution = format("%+d", evolution)
|
2018-08-29 14:54:06 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
total: total.to_s,
|
|
|
|
last_30_days_count: last_30_days_count.to_s,
|
|
|
|
evolution: formatted_evolution
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-10-02 14:14:32 +02:00
|
|
|
def dossiers_numbers(total, last_30_days_count, previous_count)
|
2018-09-03 17:35:14 +02:00
|
|
|
if previous_count != 0
|
|
|
|
evolution = (((last_30_days_count.to_f / previous_count) - 1) * 100).round(0)
|
|
|
|
else
|
|
|
|
evolution = 0
|
|
|
|
end
|
2018-12-18 21:55:45 +01:00
|
|
|
formatted_evolution = format("%+d", evolution)
|
2018-08-29 14:54:06 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
total: total.to_s,
|
|
|
|
last_30_days_count: last_30_days_count.to_s,
|
|
|
|
evolution: formatted_evolution
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2018-12-18 10:35:23 +01:00
|
|
|
def contact_percentage
|
2019-04-02 14:29:17 +02:00
|
|
|
number_of_months = 13
|
|
|
|
|
2019-11-25 14:39:42 +01:00
|
|
|
from = Time.zone.today.prev_month(number_of_months)
|
|
|
|
to = Time.zone.today.prev_month
|
2018-12-18 10:35:23 +01:00
|
|
|
|
2019-01-03 17:24:24 +01:00
|
|
|
adapter = Helpscout::UserConversationsAdapter.new(from, to)
|
|
|
|
if !adapter.can_fetch_reports?
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
adapter
|
2018-12-18 10:35:23 +01:00
|
|
|
.reports
|
|
|
|
.map do |monthly_report|
|
|
|
|
start_date = monthly_report[:start_date].to_time.localtime
|
|
|
|
end_date = monthly_report[:end_date].to_time.localtime
|
2019-09-11 17:16:48 +02:00
|
|
|
replies_count = monthly_report[:replies_sent]
|
2018-12-18 10:35:23 +01:00
|
|
|
|
2021-12-06 15:49:17 +01:00
|
|
|
dossiers_count = Dossier.where(depose_at: start_date..end_date).count
|
2018-12-18 10:35:23 +01:00
|
|
|
|
|
|
|
monthly_contact_percentage = replies_count.fdiv(dossiers_count || 1) * 100
|
|
|
|
[I18n.l(start_date, format: '%b %y'), monthly_contact_percentage.round(1)]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-06 09:34:02 +02:00
|
|
|
def max_date
|
2020-11-05 15:09:11 +01:00
|
|
|
if super_admin_signed_in?
|
2019-03-07 00:55:40 +01:00
|
|
|
Time.zone.now
|
2017-06-22 12:10:03 +02:00
|
|
|
else
|
2018-10-25 15:11:12 +02:00
|
|
|
Time.zone.now.beginning_of_month - 1.second
|
2017-06-22 12:10:03 +02:00
|
|
|
end
|
2017-10-06 09:34:02 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def last_four_months_hash(association, date_attribute)
|
|
|
|
min_date = 3.months.ago.beginning_of_month.to_date
|
2017-05-26 17:29:31 +02:00
|
|
|
|
2018-01-15 19:12:15 +01:00
|
|
|
association
|
2017-05-26 17:29:31 +02:00
|
|
|
.where(date_attribute => min_date..max_date)
|
2018-12-24 17:43:12 +01:00
|
|
|
.group("DATE_TRUNC('month', #{date_attribute})")
|
2017-05-26 17:29:31 +02:00
|
|
|
.count
|
|
|
|
.to_a
|
2017-11-20 18:55:59 +01:00
|
|
|
.sort_by { |a| a[0] }
|
2017-05-26 17:29:31 +02:00
|
|
|
.map { |e| [I18n.l(e.first, format: "%B %Y"), e.last] }
|
|
|
|
end
|
|
|
|
|
2017-07-17 15:12:05 +02:00
|
|
|
def cumulative_hash(association, date_attribute)
|
2017-03-28 15:42:48 +02:00
|
|
|
sum = 0
|
|
|
|
association
|
2018-12-24 17:43:12 +01:00
|
|
|
.where("#{date_attribute} < ?", max_date)
|
|
|
|
.group("DATE_TRUNC('month', #{date_attribute})")
|
2017-03-28 15:42:48 +02:00
|
|
|
.count
|
|
|
|
.to_a
|
2017-11-20 18:55:59 +01:00
|
|
|
.sort_by { |a| a[0] }
|
2018-01-16 13:34:24 +01:00
|
|
|
.map { |x, y| { x => (sum += y) } }
|
2017-03-28 15:42:48 +02:00
|
|
|
.reduce({}, :merge)
|
|
|
|
end
|
2017-03-24 18:04:37 +01:00
|
|
|
end
|