From c7892bcb86ae77828bb1ff09421be0a377cb47ab Mon Sep 17 00:00:00 2001 From: Jared Cosulich Date: Fri, 21 Dec 2018 13:36:04 -0500 Subject: [PATCH] faster SchoolQuestion creation --- lib/tasks/data.rake | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/tasks/data.rake b/lib/tasks/data.rake index 6680f41b..d3894691 100644 --- a/lib/tasks/data.rake +++ b/lib/tasks/data.rake @@ -16,7 +16,7 @@ # sudo heroku run rake data:move_likert_to_submeasures -a mciea-beta # sudo heroku run:detached rake data:sync -a mciea-beta --size performance-l -# sudo heroku run:detached rake data:create_school_questions -a mciea-beta --size performance-l +# sudo heroku run:detached rake data:create_school_questions -a mciea-dashboard --size performance-l # Add: # @@ -459,10 +459,13 @@ namespace :data do category.questions.created_in(school_category.year).each do |question| school = school_category.school next if school.district.name != "Boston" - attempts = Attempt. + + attempt_data = Attempt. created_in(school_category.year). for_question(question). - for_school(school) + for_school(school). + select('count(attempts.answer_index) as response_count'). + select('sum(case when questions.reverse then 6 - attempts.answer_index else attempts.answer_index end) as answer_index_total')[0] available_responders = school.available_responders_for(question) @@ -476,11 +479,9 @@ namespace :data do school_category: school_category, year: school_category.year, attempt_count: available_responders, - response_count: attempts.count, - response_rate: attempts.count.to_f / available_responders.to_f, - response_total: attempts.sum do |a| - question.reverse? ? 6 - a.answer_index : a.answer_index - end + response_count: attempt_data.response_count, + response_rate: attempt_data.response_count.to_f / available_responders.to_f, + response_total: attempt_data.answer_index_total ) new_school_questions << school_question school_questions << school_question