Merge pull request #443 from sgmap/decision-stats

Add stats for encart motivation
This commit is contained in:
gregoirenovel 2017-06-19 18:19:58 +02:00 committed by GitHub
commit f68cf3f236
3 changed files with 99 additions and 2 deletions

View file

@ -29,6 +29,9 @@ class StatsController < ApplicationController
@avis_usage = avis_usage
@avis_average_answer_time = avis_average_answer_time
@avis_answer_percentages = avis_answer_percentages
@motivation_usage_dossier = motivation_usage_dossier
@motivation_usage_procedure = motivation_usage_procedure
end
private
@ -70,6 +73,10 @@ class StatsController < ApplicationController
(collection.sum.to_f / collection.size).round(2)
end
def percentage(numerator, denominator)
((numerator.to_f / denominator) * 100).round(2)
end
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
@ -166,7 +173,7 @@ class StatsController < ApplicationController
result = 0
else
weekly_dossier_with_avis_count = weekly_dossiers.select { |dossier| dossier.avis.present? }.count
result = ((weekly_dossier_with_avis_count.to_f / weekly_dossiers_count) * 100).round(2)
result = percentage(weekly_dossier_with_avis_count, weekly_dossiers_count)
end
[min_date.to_i, result]
@ -199,10 +206,58 @@ class StatsController < ApplicationController
[min_date.to_i, 0]
else
answered_weekly_avis_count = weekly_avis.with_answer.count
result = ((answered_weekly_avis_count.to_f / weekly_avis_count) * 100).round(2)
result = percentage(answered_weekly_avis_count, weekly_avis_count)
[min_date.to_i, result]
end
end
end
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
end

View file

@ -88,3 +88,16 @@
= line_chart @avis_answer_percentages, ytitle: 'avis avec réponse / total avis', xtitle: 'semaines'
.clearfix
%h2.new-h2 Encart motivation
.stat-cards
.stat-card.stat-card-half.pull-left
%span.stat-card-title Taux d'utilisation des motivations (par dossier)
= column_chart @motivation_usage_dossier, ytitle: 'dossiers avec motivation / total dossiers', xtitle: 'semaines'
.stat-card.stat-card-half.pull-left
%span.stat-card-title Taux d'utilisation des motivations (par procédure)
= column_chart @motivation_usage_procedure, ytitle: 'procedures avec motivation / total procedures', xtitle: 'semaines'
.clearfix

View file

@ -267,4 +267,33 @@ describe StatsController, type: :controller do
it { is_expected.to match [[3.week.ago.to_i, 0], [2.week.ago.to_i, 0], [1.week.ago.to_i, 66.67]] }
end
describe '#motivation_usage_dossier' do
let!(:dossier) { create(:dossier, processed_at: 1.week.ago, motivation: "Motivation") }
let!(:dossier2) { create(:dossier, processed_at: 1.week.ago) }
let!(:dossier3) { create(:dossier, processed_at: 1.week.ago) }
before do
Timecop.freeze(Time.now)
end
subject { StatsController.new.send(:motivation_usage_dossier) }
it { expect(subject).to match([[I18n.l(3.week.ago.end_of_week, format: '%d/%m/%Y'), 0], [I18n.l(2.week.ago.end_of_week, format: '%d/%m/%Y'), 0], [I18n.l(1.week.ago.end_of_week, format: '%d/%m/%Y'), 33.33]]) }
end
describe '#motivation_usage_procedure' do
let!(:dossier) { create(:dossier, processed_at: 1.week.ago, motivation: "Motivation" ) }
let!(:dossier1) { create(:dossier, processed_at: 1.week.ago, motivation: "Motivation", procedure: dossier.procedure) }
let!(:dossier2) { create(:dossier, processed_at: 1.week.ago) }
let!(:dossier3) { create(:dossier, processed_at: 1.week.ago) }
before do
Timecop.freeze(Time.now)
end
subject { StatsController.new.send(:motivation_usage_procedure) }
it { expect(subject).to match([[I18n.l(3.week.ago.end_of_week, format: '%d/%m/%Y'), 0], [I18n.l(2.week.ago.end_of_week, format: '%d/%m/%Y'), 0], [I18n.l(1.week.ago.end_of_week, format: '%d/%m/%Y'), 33.33]]) }
end
end