2017-03-24 18:04:37 +01:00
|
|
|
class StatsController < ApplicationController
|
2017-05-22 16:56:09 +02:00
|
|
|
layout "new_application"
|
2017-03-24 18:04:37 +01: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
|
2017-07-10 16:05:02 +02:00
|
|
|
procedures = Procedure.published
|
2017-04-03 15:02:12 +02:00
|
|
|
dossiers = Dossier.where.not(:state => :draft)
|
2017-04-03 15:00:26 +02:00
|
|
|
|
2017-05-29 17:16:26 +02:00
|
|
|
@procedures_count = procedures.count
|
|
|
|
@dossiers_count = dossiers.count
|
|
|
|
|
2017-05-29 17:47:55 +02:00
|
|
|
@procedures_cumulative = cumulative_hash(procedures)
|
2017-05-26 17:29:31 +02:00
|
|
|
@procedures_in_the_last_4_months = last_four_months_hash(procedures)
|
|
|
|
|
2017-04-26 14:29:22 +02:00
|
|
|
@dossiers_cumulative = cumulative_hash(dossiers, :initiated_at)
|
2017-05-29 17:47:55 +02:00
|
|
|
@dossiers_in_the_last_4_months = last_four_months_hash(dossiers, :initiated_at)
|
2017-04-03 17:04:31 +02:00
|
|
|
|
2017-05-30 15:11:18 +02:00
|
|
|
@procedures_count_per_administrateur = procedures_count_per_administrateur(procedures)
|
|
|
|
|
2017-06-01 09:44:22 +02:00
|
|
|
@dossier_instruction_mean_time = Rails.cache.fetch("dossier_instruction_mean_time", expires_in: 1.day) do
|
|
|
|
dossier_instruction_mean_time(dossiers)
|
|
|
|
end
|
|
|
|
|
|
|
|
@dossier_filling_mean_time = Rails.cache.fetch("dossier_filling_mean_time", expires_in: 1.day) do
|
|
|
|
dossier_filling_mean_time(dossiers)
|
|
|
|
end
|
2017-05-09 17:09:35 +02:00
|
|
|
|
|
|
|
@avis_usage = avis_usage
|
2017-05-09 18:11:58 +02:00
|
|
|
@avis_average_answer_time = avis_average_answer_time
|
2017-05-10 11:07:38 +02:00
|
|
|
@avis_answer_percentages = avis_answer_percentages
|
2017-06-15 14:42:48 +02:00
|
|
|
|
|
|
|
@motivation_usage_dossier = motivation_usage_dossier
|
|
|
|
@motivation_usage_procedure = motivation_usage_procedure
|
2017-03-24 18:04:37 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-05-26 17:29:31 +02:00
|
|
|
def last_four_months_hash(association, date_attribute = :created_at)
|
|
|
|
min_date = 3.months.ago.beginning_of_month.to_date
|
2017-06-22 12:10:03 +02:00
|
|
|
if administration_signed_in?
|
|
|
|
max_date = Time.now.to_date
|
|
|
|
else
|
|
|
|
max_date = Time.now.beginning_of_month - 1.second
|
|
|
|
end
|
2017-05-26 17:29:31 +02:00
|
|
|
|
|
|
|
association
|
|
|
|
.where(date_attribute => min_date..max_date)
|
|
|
|
.group("DATE_TRUNC('month', #{date_attribute.to_s})")
|
|
|
|
.count
|
|
|
|
.to_a
|
|
|
|
.sort{ |x, y| x[0] <=> y[0] }
|
|
|
|
.map { |e| [I18n.l(e.first, format: "%B %Y"), e.last] }
|
|
|
|
end
|
|
|
|
|
2017-04-26 14:28:39 +02:00
|
|
|
def cumulative_hash(association, date_attribute = :created_at)
|
2017-03-28 15:42:48 +02:00
|
|
|
sum = 0
|
|
|
|
association
|
2017-04-26 14:28:39 +02:00
|
|
|
.group("DATE_TRUNC('month', #{date_attribute.to_s})")
|
2017-03-28 15:42:48 +02:00
|
|
|
.count
|
|
|
|
.to_a
|
|
|
|
.sort{ |x, y| x[0] <=> y[0] }
|
|
|
|
.map { |x, y| { x => (sum += y)} }
|
|
|
|
.reduce({}, :merge)
|
|
|
|
end
|
2017-04-11 16:55:13 +02:00
|
|
|
|
2017-05-30 15:11:18 +02:00
|
|
|
def procedures_count_per_administrateur(procedures)
|
|
|
|
count_per_administrateur = procedures.group(:administrateur_id).count.values
|
|
|
|
{
|
|
|
|
'Une procédure' => count_per_administrateur.select { |count| count == 1 }.count,
|
2017-06-12 16:12:03 +02:00
|
|
|
'Entre deux et cinq procédures' => count_per_administrateur.select { |count| 2 <= count && count <= 5 }.count,
|
2017-05-30 15:11:18 +02:00
|
|
|
'Plus de cinq procédures' => count_per_administrateur.select { |count| 5 < count }.count
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2017-05-29 17:19:50 +02:00
|
|
|
def mean(collection)
|
|
|
|
(collection.sum.to_f / collection.size).round(2)
|
|
|
|
end
|
|
|
|
|
2017-06-19 17:29:20 +02:00
|
|
|
def percentage(numerator, denominator)
|
|
|
|
((numerator.to_f / denominator) * 100).round(2)
|
|
|
|
end
|
|
|
|
|
2017-05-29 17:19:50 +02:00
|
|
|
def dossier_instruction_mean_time(dossiers)
|
|
|
|
# In the 12 last months, we compute for each month
|
|
|
|
# the average time it took to instruct a dossier
|
|
|
|
# We compute monthly averages by first making an average per procedure
|
|
|
|
# and then computing the average for all the procedures
|
|
|
|
|
|
|
|
min_date = 11.months.ago
|
|
|
|
max_date = Time.now.to_date
|
|
|
|
|
|
|
|
processed_dossiers = dossiers
|
|
|
|
.where(:processed_at => min_date..max_date)
|
|
|
|
.pluck(:procedure_id, :initiated_at, :processed_at)
|
|
|
|
|
|
|
|
# Group dossiers by month
|
|
|
|
processed_dossiers_by_month = processed_dossiers
|
|
|
|
.group_by do |dossier|
|
|
|
|
dossier[2].beginning_of_month.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
processed_dossiers_by_month.map do |month, value|
|
|
|
|
# Group the dossiers for this month by procedure
|
|
|
|
dossiers_grouped_by_procedure = value.group_by { |dossier| dossier[0] }
|
|
|
|
|
|
|
|
# Compute the mean time for this procedure
|
|
|
|
procedure_processing_times = dossiers_grouped_by_procedure.map do |procedure_id, procedure_dossiers|
|
|
|
|
procedure_dossiers_processing_time = procedure_dossiers.map do |dossier|
|
|
|
|
(dossier[2] - dossier[1]).to_f / (3600 * 24)
|
|
|
|
end
|
|
|
|
|
|
|
|
mean(procedure_dossiers_processing_time)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Compute the average mean time for all the procedures of this month
|
|
|
|
month_average = mean(procedure_processing_times)
|
|
|
|
|
|
|
|
[month, month_average]
|
|
|
|
end.to_h
|
|
|
|
end
|
2017-05-26 17:27:23 +02:00
|
|
|
|
|
|
|
def dossier_filling_mean_time(dossiers)
|
|
|
|
# In the 12 last months, we compute for each month
|
|
|
|
# the average time it took to fill a dossier
|
|
|
|
# We compute monthly averages by first making an average per procedure
|
|
|
|
# and then computing the average for all the procedures
|
|
|
|
# For each procedure, we normalize the data: the time is calculated
|
|
|
|
# for a 24 champs form (the current form mean length)
|
|
|
|
|
|
|
|
min_date = 11.months.ago
|
|
|
|
max_date = Time.now.to_date
|
|
|
|
|
|
|
|
processed_dossiers = dossiers
|
|
|
|
.where(:processed_at => min_date..max_date)
|
|
|
|
.pluck(:procedure_id, :created_at, :initiated_at, :processed_at)
|
|
|
|
|
|
|
|
# Group dossiers by month
|
|
|
|
processed_dossiers_by_month = processed_dossiers
|
|
|
|
.group_by do |e|
|
|
|
|
e[3].beginning_of_month.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
processed_dossiers_by_month.map do |month, value|
|
|
|
|
# Group the dossiers for this month by procedure
|
|
|
|
dossiers_grouped_by_procedure = value.group_by { |dossier| dossier[0] }
|
|
|
|
|
|
|
|
# Compute the mean time for this procedure
|
|
|
|
procedure_processing_times = dossiers_grouped_by_procedure.map do |procedure_id, procedure_dossiers|
|
|
|
|
procedure_dossiers_processing_time = procedure_dossiers.map do |dossier|
|
|
|
|
(dossier[2] - dossier[1]).to_f / 60
|
|
|
|
end
|
|
|
|
|
|
|
|
procedure_mean = mean(procedure_dossiers_processing_time)
|
|
|
|
|
|
|
|
# We normalize the data for 24 fields
|
|
|
|
procedure_fields_count = Procedure.find(procedure_id).types_de_champ.count
|
|
|
|
procedure_mean * (MEAN_NUMBER_OF_CHAMPS_IN_A_FORM / procedure_fields_count)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Compute the average mean time for all the procedures of this month
|
|
|
|
month_average = mean(procedure_processing_times)
|
|
|
|
|
|
|
|
[month, month_average]
|
|
|
|
end.to_h
|
|
|
|
end
|
2017-05-09 17:09:35 +02:00
|
|
|
|
|
|
|
def avis_usage
|
|
|
|
[3.week.ago, 2.week.ago, 1.week.ago].map do |min_date|
|
|
|
|
max_date = min_date + 1.week
|
|
|
|
|
|
|
|
weekly_dossiers = Dossier.includes(:avis).where(created_at: min_date..max_date).to_a
|
|
|
|
|
|
|
|
weekly_dossiers_count = weekly_dossiers.count
|
|
|
|
|
|
|
|
if weekly_dossiers_count == 0
|
|
|
|
result = 0
|
|
|
|
else
|
|
|
|
weekly_dossier_with_avis_count = weekly_dossiers.select { |dossier| dossier.avis.present? }.count
|
2017-06-19 17:29:20 +02:00
|
|
|
result = percentage(weekly_dossier_with_avis_count, weekly_dossiers_count)
|
2017-05-09 17:09:35 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
[min_date.to_i, result]
|
|
|
|
end
|
|
|
|
end
|
2017-05-09 18:11:58 +02:00
|
|
|
|
|
|
|
def avis_average_answer_time
|
|
|
|
[3.week.ago, 2.week.ago, 1.week.ago].map do |min_date|
|
|
|
|
max_date = min_date + 1.week
|
|
|
|
|
|
|
|
average = Avis.with_answer
|
|
|
|
.where(created_at: min_date..max_date)
|
|
|
|
.average("EXTRACT(EPOCH FROM updated_at - created_at) / 86400")
|
|
|
|
|
|
|
|
result = average ? average.to_f.round(2) : 0
|
|
|
|
|
|
|
|
[min_date.to_i, result]
|
|
|
|
end
|
|
|
|
end
|
2017-05-10 11:07:38 +02:00
|
|
|
|
|
|
|
def avis_answer_percentages
|
|
|
|
[3.week.ago, 2.week.ago, 1.week.ago].map do |min_date|
|
|
|
|
max_date = min_date + 1.week
|
|
|
|
|
|
|
|
weekly_avis = Avis.where(created_at: min_date..max_date)
|
|
|
|
|
|
|
|
weekly_avis_count = weekly_avis.count
|
|
|
|
|
|
|
|
if weekly_avis_count == 0
|
|
|
|
[min_date.to_i, 0]
|
|
|
|
else
|
|
|
|
answered_weekly_avis_count = weekly_avis.with_answer.count
|
2017-06-19 17:29:20 +02:00
|
|
|
result = percentage(answered_weekly_avis_count, weekly_avis_count)
|
2017-05-10 11:07:38 +02:00
|
|
|
|
|
|
|
[min_date.to_i, result]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-06-15 14:42:48 +02:00
|
|
|
|
|
|
|
def motivation_usage_dossier
|
|
|
|
[3.week.ago, 2.week.ago, 1.week.ago].map do |date|
|
|
|
|
min_date = date.beginning_of_week
|
|
|
|
max_date = date.end_of_week
|
|
|
|
|
|
|
|
weekly_termine_dossiers = Dossier.where(processed_at: min_date..max_date)
|
|
|
|
weekly_termine_dossiers_count = weekly_termine_dossiers.count
|
|
|
|
weekly_termine_dossiers_with_motivation_count = weekly_termine_dossiers.where.not(motivation: nil).count
|
|
|
|
|
|
|
|
if weekly_termine_dossiers_count == 0
|
|
|
|
result = 0
|
|
|
|
else
|
|
|
|
result = percentage(weekly_termine_dossiers_with_motivation_count, weekly_termine_dossiers_count)
|
|
|
|
end
|
|
|
|
|
|
|
|
[l(max_date, format: '%d/%m/%Y'), result]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def motivation_usage_procedure
|
|
|
|
[3.week.ago, 2.week.ago, 1.week.ago].map do |date|
|
|
|
|
min_date = date.beginning_of_week
|
|
|
|
max_date = date.end_of_week
|
|
|
|
|
|
|
|
procedures_with_dossier_processed_this_week = Procedure
|
|
|
|
.joins(:dossiers)
|
|
|
|
.where(dossiers: { processed_at: min_date..max_date })
|
|
|
|
|
|
|
|
procedures_with_dossier_processed_this_week_count = procedures_with_dossier_processed_this_week
|
|
|
|
.uniq
|
|
|
|
.count
|
|
|
|
|
|
|
|
procedures_with_dossier_processed_this_week_and_with_motivation_count = procedures_with_dossier_processed_this_week
|
|
|
|
.where
|
|
|
|
.not(dossiers: { motivation: nil })
|
|
|
|
.uniq
|
|
|
|
.count
|
|
|
|
|
|
|
|
if procedures_with_dossier_processed_this_week_count == 0
|
|
|
|
result = 0
|
|
|
|
else
|
|
|
|
result = percentage(procedures_with_dossier_processed_this_week_and_with_motivation_count, procedures_with_dossier_processed_this_week_count)
|
|
|
|
end
|
|
|
|
|
|
|
|
[l(max_date, format: '%d/%m/%Y'), result]
|
|
|
|
end
|
|
|
|
end
|
2017-03-24 18:04:37 +01:00
|
|
|
end
|