Commit 4b729ebe557d3dd1cb79f11c0374621e1f75b8b9
1 parent
8356a94a
Exists in
master
and in
1 other branch
Rake tasks to generate appearances and past density info
Showing
1 changed file
with
99 additions
and
0 deletions
Show diff stats
lib/tasks/test_api.rake
... | ... | @@ -20,6 +20,105 @@ namespace :test_api do |
20 | 20 | |
21 | 21 | end |
22 | 22 | |
23 | + desc "Generate appearances for any votes that have no current appearance, should only need to be run once" | |
24 | + task(:generate_appearances_for_existing_votes => :environment) do | |
25 | + votes = Vote.all | |
26 | + | |
27 | + count = 0 | |
28 | + votes.each do |v| | |
29 | + if v.appearance.nil? | |
30 | + print "." | |
31 | + a = Appearance.create(:voter_id => v.voter_id, :site_id => v.site_id, :prompt_id => v.prompt_id, :question_id => v.question_id, :created_at => v.created_at, :updated_at => v.updated_at) | |
32 | + v.appearance = a | |
33 | + v.save | |
34 | + | |
35 | + count += 1 | |
36 | + end | |
37 | + end | |
38 | + | |
39 | + print count | |
40 | + end | |
41 | + | |
42 | + | |
43 | + desc "Generate past density information" | |
44 | + task(:generate_past_densities => :environment) do | |
45 | + #this is not elegant, but should only be run once, so quick and dirty wins | |
46 | + | |
47 | + start_date = Vote.find(:all, :conditions => 'loser_choice_id IS NOT NULL', :order => :created_at, :limit => 1).first.created_at.to_date | |
48 | + end_date = Appearance.first.created_at.to_date | |
49 | + | |
50 | + start_date.upto(end_date-1) do |the_date| | |
51 | + questions = Question.find(:all) | |
52 | + | |
53 | + print the_date.to_s | |
54 | + questions.each do |q| | |
55 | + relevant_votes = q.votes.find(:all, :conditions => ['loser_choice_id IS NOT NULL AND created_at < ?', the_date]) | |
56 | + relevant_choices = q.choices.find(:all, :conditions => ['created_at < ?', the_date]) | |
57 | + | |
58 | + seed_choices = 0 | |
59 | + | |
60 | + relevant_choices.each do |c| | |
61 | + if !c.user_created | |
62 | + seed_choices+=1 | |
63 | + end | |
64 | + | |
65 | + end | |
66 | + | |
67 | + nonseed_choices = relevant_choices.size - seed_choices | |
68 | + | |
69 | + seed_seed_total = seed_choices **2 - seed_choices | |
70 | + nonseed_nonseed_total = nonseed_choices **2 - nonseed_choices | |
71 | + seed_nonseed_total = seed_choices * nonseed_choices | |
72 | + nonseed_seed_total = seed_choices * nonseed_choices | |
73 | + | |
74 | + seed_seed_sum = 0 | |
75 | + seed_nonseed_sum= 0 | |
76 | + nonseed_seed_sum= 0 | |
77 | + nonseed_nonseed_sum= 0 | |
78 | + | |
79 | + relevant_votes.each do |v| | |
80 | + | |
81 | + p = v.prompt | |
82 | + if p.left_choice.user_created == false && p.right_choice.user_created == false | |
83 | + seed_seed_sum += 1 | |
84 | + elsif p.left_choice.user_created == false && p.right_choice.user_created == true | |
85 | + seed_nonseed_sum += 1 | |
86 | + elsif p.left_choice.user_created == true && p.right_choice.user_created == false | |
87 | + nonseed_seed_sum += 1 | |
88 | + elsif p.left_choice.user_created == true && p.right_choice.user_created == true | |
89 | + nonseed_nonseed_sum += 1 | |
90 | + end | |
91 | + end | |
92 | + | |
93 | + densities = {} | |
94 | + densities[:seed_seed] = seed_seed_sum.to_f / seed_seed_total.to_f | |
95 | + densities[:seed_nonseed] = seed_nonseed_sum.to_f / seed_nonseed_total.to_f | |
96 | + densities[:nonseed_seed] = nonseed_seed_sum.to_f / nonseed_seed_total.to_f | |
97 | + densities[:nonseed_nonseed] = nonseed_nonseed_sum.to_f / nonseed_nonseed_total.to_f | |
98 | + | |
99 | + densities.each do |type, average| | |
100 | + d = Density.new | |
101 | + d.created_at = the_date | |
102 | + d.question_id = q.id | |
103 | + d.prompt_type = type.to_s | |
104 | + d.value = average.nan? ? nil : average | |
105 | + d.save! | |
106 | + end | |
107 | + | |
108 | + puts "Seed_seed sum: #{seed_seed_sum}, seed_seed total num: #{seed_seed_total}" | |
109 | + puts "Seed_nonseed sum: #{seed_nonseed_sum}, seed_nonseed total num: #{seed_nonseed_total}" | |
110 | + puts "Nonseed_seed sum: #{nonseed_seed_sum}, nonseed_seed total num: #{nonseed_seed_total}" | |
111 | + puts "Nonseed_nonseed sum: #{nonseed_nonseed_sum}, nonseed_nonseed total num: #{nonseed_nonseed_total}" | |
112 | + | |
113 | + | |
114 | + end | |
115 | + | |
116 | + end | |
117 | + | |
118 | + end | |
119 | + | |
120 | + task(:generate_historical_density_data) | |
121 | + | |
23 | 122 | desc "Should only need to be run once" |
24 | 123 | task(:generate_all_possible_prompts => :environment) do |
25 | 124 | inserts = [] | ... | ... |