Created
February 3, 2009 23:02
-
-
Save schacon/57815 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "rubygems" | |
require "activerecord" | |
require '../../munger/lib/munger' | |
ActiveRecord::Base.establish_connection( | |
:adapter => "postgresql", | |
:host => "localhost", | |
:database => "testing", | |
:username => "postgres", | |
:password => "postgres" | |
) | |
class Participant < ActiveRecord::Base | |
has_many :demographics | |
has_many :assessments | |
end | |
class Demographic < ActiveRecord::Base | |
belongs_to :participant | |
has_one :assessment | |
end | |
class Assessment < ActiveRecord::Base | |
belongs_to :participant | |
belongs_to :demographic | |
end | |
# original code | |
class DemographicReport | |
include Munger | |
def initialize(program_ids, start_date, end_date) | |
date_range = "assessments.created_at #{(start_date..end_date).to_s(:db)}" | |
participant_result = Participant.find(:all, | |
:conditions => "program_id in (#{program_ids}) AND #{date_range}", | |
:joins => :assessments ) | |
@participant_data = Munger::Data.load_data(participant_result) | |
demographic_result = Demographic.find(:all, | |
:conditions => "program_id in (#{program_ids}) AND #{date_range}", | |
:joins => :assessment ) | |
@demographic_data = Munger::Data.load_data(demographic_result) | |
end | |
def gender_report | |
data = munge_data(:gender, @participant_data) | |
#data = translate_column(data, :gender, Gender::GENDER['en-US']) | |
report = munge_report(data, :gender) | |
puts Munger::Render.to_text(report) | |
end | |
def race_report | |
data = munge_data(:race, @participant_data) | |
#data = translate_column(data, :race, Race::RACES['en-US']) | |
report = munge_report(data, :race) | |
puts Munger::Render.to_text(report) | |
end | |
AGE_RANGES = [ | |
['<18', [0, 18]], | |
['>18', [19, 100]], | |
] | |
def age_report | |
demographic_result = Demographic.find(:all, :joins => :assessment) | |
data = Munger::Data.load_data(demographic_result) | |
data.add_column('age') do |r| | |
AGE_RANGES.select { |a| (a[1][0] <= r.age_when_taken.to_i) && (a[1][1] >= r.age_when_taken.to_i) }.first[0] | |
end | |
munge_data(:age, data) | |
report = munge_report(data, :age) | |
puts Munger::Render.to_text(report) | |
end | |
private | |
def munge_data(field, data) | |
rows = data.size | |
data.group(field, :count => :id) | |
data.add_column('percent') do |r| | |
(r.count_id.to_f / rows.to_f) * 100 | |
end | |
data | |
end | |
def translate_column(data, column_name, translation_hash) | |
data.transform_column(column_name) do |r| | |
eval("translation_hash[r.#{column_name}]") | |
end | |
end | |
def munge_report(data, column_name) | |
report = Munger::Report.from_data(data) | |
report.columns(:count_id => 'count') | |
report.columns([column_name, :count_id, :percent]) | |
report.aggregate(:sum => [:count_id, :percent]) | |
report.process | |
end | |
end | |
dr = DemographicReport.new('1,15,13,27', 1.year.ago, 1.day.ago) | |
dr.gender_report | |
puts | |
dr.race_report | |
puts | |
dr.age_report |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment