Skip to content

Commit

Permalink
stats: compute stats from production with reusable classes
Browse files Browse the repository at this point in the history
  • Loading branch information
JeSuisUnCaillou authored and freesteph committed Mar 4, 2024
1 parent 26f3492 commit 1f5e272
Show file tree
Hide file tree
Showing 27 changed files with 1,013 additions and 2 deletions.
2 changes: 2 additions & 0 deletions app/models/pfmp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class Pfmp < ApplicationRecord

validates :day_count, numericality: { only_integer: true, allow_nil: true, greater_than: 0 }

scope :finished, -> { where("pfmps.end_date <= (?)", Time.zone.today) }

include Statesman::Adapters::ActiveRecordQueries[
transition_class: PfmpTransition,
initial_state: PfmpStateMachine.initial_state,
Expand Down
65 changes: 65 additions & 0 deletions app/models/stats/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# frozen_string_literal: true

module Stats
class Base
def title
raise NotImplementedError
end

def with_mef_and_establishment
raise NotImplementedError
end

def with_establishment
raise NotImplementedError
end

def global_data
raise NotImplementedError
end

def bops_data
raise NotImplementedError
end

def menj_academies_data
raise NotImplementedError
end

def establishments_data
raise NotImplementedError
end

def bop
Arel.sql(
%(
CASE mefs.ministry
WHEN 0 THEN (CASE private_contract_type_code WHEN '99' THEN 'ENPU' ELSE 'ENPR' END)
ELSE mefs.ministry::text END
)
)
end

def bop_key_map(bop)
%w[ENPU ENPR].include?(bop) ? bop : Mef.ministries.invert[bop.to_i].upcase
end

def group_per_bop(collection)
collection.merge(with_mef_and_establishment)
.group(bop)
end

def group_per_menj_academy(collection)
collection.merge(with_mef_and_establishment)
.where("mefs.ministry": :menj)
.order(:academy_label)
.group(:academy_label)
end

def group_per_establishment(collection)
collection.merge(with_establishment)
.order(:uai)
.group(:uai)
end
end
end
28 changes: 28 additions & 0 deletions app/models/stats/count.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

module Stats
class Count < Base
attr_reader :all

def initialize(all: 0)
@all = all
super()
end

def global_data
all.count
end

def bops_data
@bops_data ||= group_per_bop(all).count.transform_keys { |bop| bop_key_map(bop) }
end

def menj_academies_data
@menj_academies_data ||= group_per_menj_academy(all).count
end

def establishments_data
@establishments_data ||= group_per_establishment(all).count
end
end
end
26 changes: 26 additions & 0 deletions app/models/stats/indicator/attributive_decisions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

module Stats
module Indicator
class AttributiveDecisions < Ratio
def initialize
super(
subset: Schooling.with_attributive_decisions,
all: Schooling.all
)
end

def title
"Décisions d'attributions éditées"
end

def with_mef_and_establishment
Schooling.joins(classe: %i[mef establishment])
end

def with_establishment
Schooling.joins(classe: :establishment)
end
end
end
end
30 changes: 30 additions & 0 deletions app/models/stats/indicator/payments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

module Stats
module Indicator
class Payments < Sum
def initialize
super(
column: :amount,
all: Payment.joins(pfmp: { schooling: { student: :rib } })
.merge(Pfmp.finished)
.merge(Pfmp.in_state(:validated))
.merge(Schooling.with_attributive_decisions)
.merge(Student.asp_ready)
)
end

def title
"Somme des PFMPs terminées validées avec RIB, DA & données élèves"
end

def with_mef_and_establishment
Payment.joins(schooling: { classe: %i[mef establishment] })
end

def with_establishment
Payment.joins(schooling: { classe: :establishment })
end
end
end
end
25 changes: 25 additions & 0 deletions app/models/stats/indicator/pfmps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

module Stats
module Indicator
class Pfmps < Count
def initialize
super(
all: Pfmp.all
)
end

def title
"Toutes les PFMPs (même non complétées)"
end

def with_mef_and_establishment
Pfmp.joins(schooling: { classe: %i[mef establishment] })
end

def with_establishment
Pfmp.joins(schooling: { classe: :establishment })
end
end
end
end
26 changes: 26 additions & 0 deletions app/models/stats/indicator/ribs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

module Stats
module Indicator
class Ribs < Ratio
def initialize
super(
subset: Student.joins(:rib),
all: Student.all
)
end

def title
"Coordonnées bancaires saisies"
end

def with_mef_and_establishment
Student.joins(schoolings: { classe: %i[mef establishment] })
end

def with_establishment
Student.joins(schoolings: { classe: :establishment })
end
end
end
end
25 changes: 25 additions & 0 deletions app/models/stats/indicator/schoolings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

module Stats
module Indicator
class Schoolings < Count
def initialize
super(
all: Schooling.all
)
end

def title
"Scolarités concernées"
end

def with_mef_and_establishment
Schooling.joins(classe: %i[mef establishment])
end

def with_establishment
Schooling.joins(classe: :establishment)
end
end
end
end
26 changes: 26 additions & 0 deletions app/models/stats/indicator/students_data.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

module Stats
module Indicator
class StudentsData < Ratio
def initialize
super(
subset: Student.asp_ready,
all: Student.all
)
end

def title
"Données d'élèves nécessaires présentes"
end

def with_mef_and_establishment
Student.joins(schoolings: { classe: %i[mef establishment] })
end

def with_establishment
Student.joins(schoolings: { classe: :establishment })
end
end
end
end
28 changes: 28 additions & 0 deletions app/models/stats/indicator/validated_pfmps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

module Stats
module Indicator
class ValidatedPfmps < Ratio
def initialize
finished_pfmps = Pfmp.finished

super(
subset: finished_pfmps.in_state(:validated),
all: finished_pfmps
)
end

def title
"PFMPs terminées et validées"
end

def with_mef_and_establishment
Pfmp.joins(schooling: { classe: %i[mef establishment] })
end

def with_establishment
Pfmp.joins(schooling: { classe: :establishment })
end
end
end
end
27 changes: 27 additions & 0 deletions app/models/stats/indicator/yearly_amounts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

module Stats
module Indicator
class YearlyAmounts < Sum
def initialize
super(
column: :yearly_cap,
all: Schooling.joins(classe: :mef)
.merge(Mef.with_wages)
)
end

def title
"Somme des montants annuels"
end

def with_mef_and_establishment
Schooling.joins(classe: %i[mef establishment])
end

def with_establishment
Schooling.joins(classe: :establishment)
end
end
end
end
Loading

0 comments on commit 1f5e272

Please sign in to comment.