Created
April 1, 2014 05:36
-
-
Save dollschasingmen/9908269 to your computer and use it in GitHub Desktop.
personalization_9
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
include ScriptUtils | |
require File.join(Rails.root, 'lib/reports/upr/split_test_proxy.rb') | |
class PersonalizationResult < ActiveRecord::Base | |
data_fabric :shard_by => 'reports' | |
File.join(BASE_REPORTS_DIR, 'app/models/personalization_result.rb') | |
end | |
class PersonalizationNineAnalysis | |
BAC_KEYS = [:memory, :attention, :flexibility, :problemsolving, :speed] | |
def initialize | |
@logger = ScriptLogger.new(STDOUT) | |
@split_test_proxy = Reports::Upr::SplitTestProxy.new(@logger) | |
@split_test_proxy.load! | |
@rate_area_hash = {} | |
@control_hash = {} | |
end | |
def increment_key(m, k) | |
if m.include?(k) | |
m[k] += 1 | |
else | |
m[k] = 1 | |
end | |
end | |
def increment_map(m, answer_data, &proc) | |
increment_key(m, :user_count) | |
BAC_KEYS.each { |k| proc.call(k, answer_data) } | |
end | |
def convert_for_rate_area(arr) | |
if arr == [0,0,0,0] | |
:not_important | |
elsif arr == [1,0,0,0] | |
:important | |
elsif arr == [1,1,1,0] | |
:very_important | |
else | |
:unknown | |
end | |
end | |
def execute(min_id = 11295637, max_id = 11651559) | |
#11295637 is the id at '2014-03-06' | |
count = 0 | |
PersonalizationResult.where('id >= ? and id <= ?', min_id, max_id).find_in_batches do |batch| | |
batch.each do |pr| | |
answer_data = YAML.load(pr.answer_data) | |
user = User.find_by_id(pr.user_id) | |
variant = @split_test_proxy.active_assignments_for_user(user)['personalization_9'] | |
if variant == 'rate_area' | |
increment_map(@rate_area_hash, answer_data) do |bac_key, answer_data| | |
comp_key = (bac_key.to_s + "_" + convert_for_rate_area(answer_data[bac_key]).to_s).to_sym | |
increment_key(@rate_area_hash, comp_key) | |
end | |
elsif variant == 'control' | |
increment_map(@control_hash, answer_data) do |bac_key, answer_data| | |
sum = answer_data[bac_key].inject(:+) | |
increment_key(@control_hash, bac_key) if sum == 4 | |
end | |
end | |
count += 1 | |
end | |
end | |
@logger.info("count = #{count}") | |
@logger.info("@rate_area_hash = #{@rate_area_hash.pretty_inspect}") | |
@logger.info("@control_hash = #{@control_hash.pretty_inspect}") | |
end | |
end | |
id = ARGV[2] | |
puts "personalization result start id = #{id}" | |
PersonalizationNineAnalysis.new.execute(id) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment