From 28ce02702521b0e99427afc4a156e1a2b452d867 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Thu, 12 Apr 2018 18:36:09 +0200 Subject: [PATCH] [fix #1832] Procedure Stat: display procedure cloned ratio --- app/controllers/stats_controller.rb | 18 ++++++++++++++++++ app/models/procedure.rb | 2 ++ app/views/stats/index.html.haml | 9 +++++++++ spec/controllers/stats_controller_spec.rb | 21 +++++++++++++++++++++ 4 files changed, 50 insertions(+) diff --git a/app/controllers/stats_controller.rb b/app/controllers/stats_controller.rb index c0e7c37b7..62094ec17 100644 --- a/app/controllers/stats_controller.rb +++ b/app/controllers/stats_controller.rb @@ -32,10 +32,28 @@ class StatsController < ApplicationController @motivation_usage_dossier = motivation_usage_dossier @motivation_usage_procedure = motivation_usage_procedure + + @cloned_from_library_procedures_ratio = cloned_from_library_procedures_ratio end private + def cloned_from_library_procedures_ratio + [3.weeks.ago, 2.weeks.ago, 1.week.ago].map do |date| + min_date = date.beginning_of_week + max_date = min_date.end_of_week + + all_procedures = Procedure.created_during(min_date..max_date) + cloned_from_library_procedures = all_procedures.cloned_from_library + + denominator = [1, all_procedures.count].max + + ratio = percentage(cloned_from_library_procedures.count, denominator) + + [l(max_date, format: '%d/%m/%Y'), ratio] + end + end + def max_date if administration_signed_in? Time.now.to_date diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 630f0feb9..c224ecf17 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -39,6 +39,8 @@ class Procedure < ApplicationRecord scope :archivees, -> { where.not(archived_at: nil) } scope :publiees_ou_archivees, -> { where.not(published_at: nil) } scope :by_libelle, -> { order(libelle: :asc) } + scope :created_during, -> (range) { where(created_at: range) } + scope :cloned_from_library, -> { where(cloned_from_library: true) } validates :libelle, presence: true, allow_blank: false, allow_nil: false validates :description, presence: true, allow_blank: false, allow_nil: false diff --git a/app/views/stats/index.html.haml b/app/views/stats/index.html.haml index dec7d83b8..8267176d3 100644 --- a/app/views/stats/index.html.haml +++ b/app/views/stats/index.html.haml @@ -103,3 +103,12 @@ = column_chart @motivation_usage_procedure, ytitle: 'procedures avec motivation / total procedures', xtitle: 'semaines' .clearfix + + %h2.new-h2 Utilisation de la bibliothèque + + .stat-cards + .stat-card.stat-card-half.pull-left + %span.stat-card-title Taux d'utilisation de la bibliothèque + = column_chart @cloned_from_library_procedures_ratio, ytitle: 'procédures clonées / total procédure', xtitle: 'semaines' + + .clearfix diff --git a/spec/controllers/stats_controller_spec.rb b/spec/controllers/stats_controller_spec.rb index d82a8ce11..1ca4a7182 100644 --- a/spec/controllers/stats_controller_spec.rb +++ b/spec/controllers/stats_controller_spec.rb @@ -310,4 +310,25 @@ describe StatsController, type: :controller do it { expect(subject).to match([[I18n.l(3.weeks.ago.end_of_week, format: '%d/%m/%Y'), 0], [I18n.l(2.weeks.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 "#cloned_from_library_procedures_ratio" do + let!(:procedure1) { create(:procedure, created_at: 3.weeks.ago) } + let!(:procedure2) { create(:procedure, created_at: 2.weeks.ago) } + let!(:procedure3) { create(:procedure, created_at: 2.weeks.ago, cloned_from_library: true) } + + before { Timecop.freeze(Time.now) } + after { Timecop.return } + + subject { StatsController.new.send(:cloned_from_library_procedures_ratio) } + + let(:result) do + [ + [I18n.l(3.weeks.ago.end_of_week, format: '%d/%m/%Y'), 0], + [I18n.l(2.weeks.ago.end_of_week, format: '%d/%m/%Y'), 50.0], + [I18n.l(1.week.ago.end_of_week, format: '%d/%m/%Y'), 0] + ] + end + + it { expect(subject).to match(result) } + end end