From 690f2db8637cbd6e97dbd17a17aa815579b98110 Mon Sep 17 00:00:00 2001 From: rebuilt Date: Thu, 3 Jul 2025 17:45:37 -0700 Subject: [PATCH] ECP-162 fix benefits so it gets scored correctly --- app/models/benefit.rb | 2 +- .../column/parent/socio_economic_status.rb | 2 +- .../graph/parents_by_socio_economic_status.rb | 3 +-- app/services/socio_economic_calculator.rb | 23 +++++++++++++++++++ app/services/survey_responses_data_loader.rb | 12 ++-------- .../20250704001027_add_benefit_to_parents.rb | 6 +++++ db/schema.rb | 8 +++---- .../survey_responses_data_loader_spec.rb | 12 +++++----- 8 files changed, 44 insertions(+), 24 deletions(-) create mode 100644 app/services/socio_economic_calculator.rb create mode 100644 db/migrate/20250704001027_add_benefit_to_parents.rb diff --git a/app/models/benefit.rb b/app/models/benefit.rb index 36273a25..5055e7ba 100644 --- a/app/models/benefit.rb +++ b/app/models/benefit.rb @@ -25,7 +25,7 @@ class Benefit < ApplicationRecord end def points - return 1 if designation == "Yes" + return 1 if designation == "No" 0 end diff --git a/app/presenters/analyze/graph/column/parent/socio_economic_status.rb b/app/presenters/analyze/graph/column/parent/socio_economic_status.rb index abe29f1b..a5875f6b 100644 --- a/app/presenters/analyze/graph/column/parent/socio_economic_status.rb +++ b/app/presenters/analyze/graph/column/parent/socio_economic_status.rb @@ -7,7 +7,7 @@ module Analyze class SocioEconomicStatus < Base attr_reader :socio_economic_status, :label - def initialize(socio_economic_status:, label:, show_irrelevancy_message:) + def initialize(socio_economic_status:, label:, show_irrelevancy_message: false) @socio_economic_status = socio_economic_status @label = label @show_irrelevancy_message = show_irrelevancy_message diff --git a/app/presenters/analyze/graph/parents_by_socio_economic_status.rb b/app/presenters/analyze/graph/parents_by_socio_economic_status.rb index 760ffba1..e1db0b60 100644 --- a/app/presenters/analyze/graph/parents_by_socio_economic_status.rb +++ b/app/presenters/analyze/graph/parents_by_socio_economic_status.rb @@ -13,8 +13,7 @@ module Analyze def columns [].tap do |array| - array << Analyze::Graph::Column::Parent::SocioEconomicStatus.new(socio_economic_status: 0, label: ["No Advantage"], show_irrelevancy_message: false) - array << Analyze::Graph::Column::Parent::SocioEconomicStatus.new(socio_economic_status: 1, label: ["Low Advantage"], show_irrelevancy_message: false) + array << Analyze::Graph::Column::Parent::SocioEconomicStatus.new(socio_economic_status: [0, 1], label: ["Low Advantage"], show_irrelevancy_message: false) array << Analyze::Graph::Column::Parent::SocioEconomicStatus.new(socio_economic_status: 2, label: ["Mediumn Advantage"], show_irrelevancy_message: false) array << Analyze::Graph::Column::Parent::SocioEconomicStatus.new(socio_economic_status: 3, label: ["High Advantage"], show_irrelevancy_message: false) diff --git a/app/services/socio_economic_calculator.rb b/app/services/socio_economic_calculator.rb new file mode 100644 index 00000000..b3162d1b --- /dev/null +++ b/app/services/socio_economic_calculator.rb @@ -0,0 +1,23 @@ +class SocioEconomicCalculator + def self.update_socio_economic_scores + parent_list = [].tap do |list| + Parent.all.each do |parent| + parent.socio_economic_status = socio_economic_score(parent.education, parent.benefit, parent.employments) + list << parent + end + end + + Parent.import( + parent_list, + batch_size: 500, + on_duplicate_key_update: :all + ) + end + + def self.socio_economic_score(education, benefits, employment) + employment_points = employment.map(&:points).sum.clamp(0, 1) + ed_points = education&.points || 0 + benefits_points = benefits&.points || 0 + ed_points + benefits_points + employment_points + end +end diff --git a/app/services/survey_responses_data_loader.rb b/app/services/survey_responses_data_loader.rb index 41667bd0..01338f56 100644 --- a/app/services/survey_responses_data_loader.rb +++ b/app/services/survey_responses_data_loader.rb @@ -56,13 +56,6 @@ class SurveyResponsesDataLoader SurveyItemResponse.import(survey_item_responses.compact.flatten, batch_size:, on_duplicate_key_update: :all) end - def socio_economic_score(education, benefits, employment) - employment_points = employment.map(&:points).sum.clamp(0, 1) - ed_points = education&.points || 0 - benefits_points = benefits&.points || 0 - ed_points + benefits_points + employment_points - end - private def schools @@ -134,8 +127,7 @@ class SurveyResponsesDataLoader parent = Parent.find_or_create_by(response_id: row.response_id) parent.number_of_children = row.number_of_children parent.education = educations[row.education] if row.education.present? - parent.benefits_id = benefits[row.benefits].id if row.benefits.present? - + parent.benefit = benefits[row.benefits] if row.benefits.present? tmp_languages = row.languages.map { |language| languages[language] }.reject(&:nil?) parent.languages.delete_all parent.languages.concat(tmp_languages) @@ -152,7 +144,7 @@ class SurveyResponsesDataLoader tmp_employments = row.employments.map { |employment| employments[employment] }.reject(&:nil?) parent.employments.concat(tmp_employments) - parent.socio_economic_status = socio_economic_score(educations[row.education], benefits[row.benefits], tmp_employments) + parent.socio_economic_status = SocioEconomicCalculator.socio_economic_score(educations[row.education], benefits[row.benefits], tmp_employments) parent.housing = housings[row.housing] if row.housing.present? parent.save diff --git a/db/migrate/20250704001027_add_benefit_to_parents.rb b/db/migrate/20250704001027_add_benefit_to_parents.rb new file mode 100644 index 00000000..11a21f2f --- /dev/null +++ b/db/migrate/20250704001027_add_benefit_to_parents.rb @@ -0,0 +1,6 @@ +class AddBenefitToParents < ActiveRecord::Migration[8.0] + def change + remove_reference :parents, :benefits, foreign_key: true + add_reference :parents, :benefit, foreign_key: true + end +end diff --git a/db/schema.rb b/db/schema.rb index b102eb23..90acf5e5 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.0].define(version: 2025_06_30_181104) do +ActiveRecord::Schema[8.0].define(version: 2025_07_04_001027) do # These are extensions that must be enabled in order to support this database enable_extension "pg_catalog.plpgsql" @@ -190,9 +190,9 @@ ActiveRecord::Schema[8.0].define(version: 2025_06_30_181104) do t.datetime "updated_at", null: false t.bigint "housing_id" t.bigint "education_id" - t.bigint "benefits_id" t.integer "socio_economic_status" - t.index ["benefits_id"], name: "index_parents_on_benefits_id" + t.bigint "benefit_id" + t.index ["benefit_id"], name: "index_parents_on_benefit_id" t.index ["education_id"], name: "index_parents_on_education_id" t.index ["housing_id"], name: "index_parents_on_housing_id" end @@ -386,7 +386,7 @@ ActiveRecord::Schema[8.0].define(version: 2025_06_30_181104) do add_foreign_key "parent_languages", "parents" add_foreign_key "parent_races", "parents" add_foreign_key "parent_races", "races" - add_foreign_key "parents", "benefits", column: "benefits_id" + add_foreign_key "parents", "benefits" add_foreign_key "parents", "educations" add_foreign_key "parents", "housings" add_foreign_key "respondents", "academic_years" diff --git a/spec/services/survey_responses_data_loader_spec.rb b/spec/services/survey_responses_data_loader_spec.rb index fb2be6de..327784c3 100644 --- a/spec/services/survey_responses_data_loader_spec.rb +++ b/spec/services/survey_responses_data_loader_spec.rb @@ -344,28 +344,28 @@ describe SurveyResponsesDataLoader do describe ".socio_economic_score" do it "returns 0 when none of the rubrics meet the standard for higher advantage" do - score = SurveyResponsesDataLoader.new.socio_economic_score(Education.new(designation: "No formal schooling completed"), Benefit.new(designation: "No"), [Employment.new(designation: "No full-time or part-time employment")]) + score = SocioEconomicCalculator.socio_economic_score(Education.new(designation: "No formal schooling completed"), Benefit.new(designation: "Yes"), [Employment.new(designation: "No full-time or part-time employment")]) expect(score).to eq 0 - score = SurveyResponsesDataLoader.new.socio_economic_score(Education.new(designation: "Unknown"), Benefit.new(designation: "Unknown"), [Employment.new(designation: "Unknown")]) + score = SocioEconomicCalculator.socio_economic_score(Education.new(designation: "Unknown"), Benefit.new(designation: "Unknown"), [Employment.new(designation: "Unknown")]) expect(score).to eq 0 end it "returns 1 when one of the rubrics meets the standard for higher advantage" do - score = SurveyResponsesDataLoader.new.socio_economic_score(Education.new(designation: "Associates Degree"), Benefit.new(designation: "No"), [Employment.new(designation: "No full-time or part-time employment")]) + score = SocioEconomicCalculator.socio_economic_score(Education.new(designation: "Associates Degree"), Benefit.new(designation: "Yes"), [Employment.new(designation: "No full-time or part-time employment")]) expect(score).to eq 1 end it "returns 2 when two of the rubrics meet the standard for higher advantage" do - score = SurveyResponsesDataLoader.new.socio_economic_score(Education.new(designation: "Associates Degree"), Benefit.new(designation: "Yes"), [Employment.new(designation: "No full-time or part-time employment")]) + score = SocioEconomicCalculator.socio_economic_score(Education.new(designation: "Associates Degree"), Benefit.new(designation: "No"), [Employment.new(designation: "No full-time or part-time employment")]) expect(score).to eq 2 end it "returns 3 when all three of the rubrics meet the standard for higher advantage" do - score = SurveyResponsesDataLoader.new.socio_economic_score(Education.new(designation: "Associates Degree"), Benefit.new(designation: "Yes"), [Employment.new(designation: "Two adults with full-time employment")]) + score = SocioEconomicCalculator.socio_economic_score(Education.new(designation: "Associates Degree"), Benefit.new(designation: "No"), [Employment.new(designation: "Two adults with full-time employment")]) expect(score).to eq 3 - score = SurveyResponsesDataLoader.new.socio_economic_score(Education.new(designation: "Associates Degree"), Benefit.new(designation: "Yes"), [Employment.new(designation: "One adult with full-time employment"), Employment.new(designation: "Two adults with full-time employment")]) + score = SocioEconomicCalculator.socio_economic_score(Education.new(designation: "Associates Degree"), Benefit.new(designation: "No"), [Employment.new(designation: "One adult with full-time employment"), Employment.new(designation: "Two adults with full-time employment")]) expect(score).to eq 3 end end