Add stats for encart motivation

This commit is contained in:
gregoirenovel 2017-06-15 14:42:48 +02:00
parent 19696da736
commit d0b927857a
3 changed files with 93 additions and 0 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
@ -209,4 +212,52 @@ class StatsController < ApplicationController
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