working on yearly aggregation

pull/1/head
Jared Cosulich 7 years ago
parent 9734b7dc06
commit 10945d6c71

@ -18,7 +18,7 @@ class SchoolCategory < ApplicationRecord
answer_index_total.to_f / response_count.to_f answer_index_total.to_f / response_count.to_f
end end
def aggregated_responses(year) def aggregated_responses
attempt_data = Attempt. attempt_data = Attempt.
created_in(year). created_in(year).
for_category(category). for_category(category).
@ -39,8 +39,8 @@ class SchoolCategory < ApplicationRecord
} }
end end
def chained_aggregated_responses(year) def chained_aggregated_responses
_aggregated_responses = aggregated_responses(year) _aggregated_responses = aggregated_responses
child_school_categories = category.child_categories.collect do |cc| child_school_categories = category.child_categories.collect do |cc|
SchoolCategory.for(school, cc).valid SchoolCategory.for(school, cc).valid
@ -67,15 +67,15 @@ class SchoolCategory < ApplicationRecord
} }
end end
def sync_aggregated_responses(year) def sync_aggregated_responses
return if ENV['BULK_PROCESS'] return if ENV['BULK_PROCESS']
update_attributes(chained_aggregated_responses(year)) update_attributes(chained_aggregated_responses)
if category.parent_category.present? if category.parent_category.present?
parent_school_category = SchoolCategory.for(school, category.parent_category).in(year).first parent_school_category = SchoolCategory.for(school, category.parent_category).in(year).first
if parent_school_category.nil? if parent_school_category.nil?
parent_school_category = SchoolCategory.create(school: school, category: category.parent_category, year: year) parent_school_category = SchoolCategory.create(school: school, category: category.parent_category, year: year)
end end
parent_school_category.sync_aggregated_responses(year) parent_school_category.sync_aggregated_responses
end end
end end
end end

@ -426,14 +426,14 @@ namespace :data do
end end
def sync_school_category_aggregates def sync_school_category_aggregates
year = '2018' year = 2018
School.all.each do |school| School.all.each do |school|
Category.all.each do |category| Category.all.each do |category|
school_category = SchoolCategory.for(school, category).in(year).first school_category = SchoolCategory.for(school, category).in(year).first
if school_category.nil? if school_category.nil?
school_category = school.school_categories.create(category: category, year: year) school_category = school.school_categories.create(category: category, year: year)
end end
school_category.sync_aggregated_responses(year) school_category.sync_aggregated_responses
end end
end end
end end

Loading…
Cancel
Save