Commit a937413c56b617f6c925f6eb19b3f38dc42ec3f3
1 parent
47c8d62e
Exists in
master
and in
1 other branch
re-indent prune_db rake file
Showing
1 changed file
with
115 additions
and
115 deletions
Show diff stats
lib/tasks/prune_db.rake
1 | 1 | namespace :prune_db do |
2 | - | |
2 | + | |
3 | 3 | task :all => [:invalidate_votes_with_bad_response_times] |
4 | 4 | |
5 | 5 | task(:invalidate_votes_with_bad_response_times => :environment) do |
6 | - badvotes = [] | |
7 | - #might want to optimize later to not start from the beginning each time | |
8 | - STDOUT.sync = true | |
9 | - Vote.find_each(:batch_size => 10000, :include => :appearance) do |v| | |
10 | - next if v.nil? || v.appearance.nil? | |
11 | - server_response_time = v.created_at.to_f - v.appearance.created_at.to_f | |
12 | - if v.time_viewed && v.time_viewed/1000 > server_response_time | |
13 | - badvotes << v | |
14 | - print "." | |
15 | - end | |
16 | - end | |
17 | - puts "\n" | |
18 | - | |
19 | - if badvotes.any? | |
20 | - | |
21 | - badvotes.each do |v| | |
22 | - v.time_viewed = nil | |
23 | - v.missing_response_time_exp = "invalid" | |
24 | - v.save! | |
25 | - end | |
26 | - else | |
27 | - puts "Could not find any bad votes. Yay." | |
28 | - end | |
6 | + badvotes = [] | |
7 | + #might want to optimize later to not start from the beginning each time | |
8 | + STDOUT.sync = true | |
9 | + Vote.find_each(:batch_size => 10000, :include => :appearance) do |v| | |
10 | + next if v.nil? || v.appearance.nil? | |
11 | + server_response_time = v.created_at.to_f - v.appearance.created_at.to_f | |
12 | + if v.time_viewed && v.time_viewed/1000 > server_response_time | |
13 | + badvotes << v | |
14 | + print "." | |
15 | + end | |
16 | + end | |
17 | + puts "\n" | |
18 | + | |
19 | + if badvotes.any? | |
20 | + | |
21 | + badvotes.each do |v| | |
22 | + v.time_viewed = nil | |
23 | + v.missing_response_time_exp = "invalid" | |
24 | + v.save! | |
25 | + end | |
26 | + else | |
27 | + puts "Could not find any bad votes. Yay." | |
28 | + end | |
29 | 29 | end |
30 | - | |
30 | + | |
31 | 31 | task(:associate_skips_with_appearances => :environment) do |
32 | - skips_to_fix = Skip.find(:all, :conditions => {:appearance_id => nil}) | |
33 | - skips_to_fix.each do |skip| | |
34 | - puts "Skip #{skip.id} : " | |
35 | - possible_appearances = skip.skipper.appearances.find(:all, :conditions => {:prompt_id => skip.prompt_id}) | |
36 | - if possible_appearances.nil? || possible_appearances.empty? | |
37 | - puts " I couldn't find any matches!" | |
38 | - skip.delete | |
39 | - next | |
40 | - end | |
41 | - if possible_appearances.size > 1 | |
42 | - puts " More than one possible appearance" | |
43 | - possible_appearances.delete_if{|a| a.answered?} | |
44 | - if possible_appearances.size > 1 || possible_appearances.size == 0 | |
45 | - puts" And I couldn't narrow it down.... moving on" | |
46 | - skip.delete | |
47 | - next | |
48 | - end | |
49 | - end | |
50 | - possible_appearance = possible_appearances.first | |
51 | - if possible_appearance.answered? | |
52 | - puts " This appearance has been answered already! Moving on" | |
53 | - skip.delete | |
54 | - else | |
55 | - puts " MATCH" | |
56 | - skip.appearance_id = possible_appearance.id | |
57 | - skip.save! | |
58 | - end | |
59 | - end | |
32 | + skips_to_fix = Skip.find(:all, :conditions => {:appearance_id => nil}) | |
33 | + skips_to_fix.each do |skip| | |
34 | + puts "Skip #{skip.id} : " | |
35 | + possible_appearances = skip.skipper.appearances.find(:all, :conditions => {:prompt_id => skip.prompt_id}) | |
36 | + if possible_appearances.nil? || possible_appearances.empty? | |
37 | + puts " I couldn't find any matches!" | |
38 | + skip.delete | |
39 | + next | |
40 | + end | |
41 | + if possible_appearances.size > 1 | |
42 | + puts " More than one possible appearance" | |
43 | + possible_appearances.delete_if{|a| a.answered?} | |
44 | + if possible_appearances.size > 1 || possible_appearances.size == 0 | |
45 | + puts" And I couldn't narrow it down.... moving on" | |
46 | + skip.delete | |
47 | + next | |
48 | + end | |
49 | + end | |
50 | + possible_appearance = possible_appearances.first | |
51 | + if possible_appearance.answered? | |
52 | + puts " This appearance has been answered already! Moving on" | |
53 | + skip.delete | |
54 | + else | |
55 | + puts " MATCH" | |
56 | + skip.appearance_id = possible_appearance.id | |
57 | + skip.save! | |
58 | + end | |
59 | + end | |
60 | 60 | end |
61 | - | |
61 | + | |
62 | 62 | task(:move_vote_and_skip_ids_to_appearance => :environment) do |
63 | - #Vote.find_each do |v| | |
64 | - # @appearance = Appearance.find(v.appearance_id) | |
65 | -# @appearance.answerable = v | |
66 | -# @appearance.save | |
67 | -# if v.id % 1000 == 0 | |
68 | -# puts v.id | |
69 | -# end | |
70 | -# end | |
71 | - Skip.find_each do |s| | |
72 | - if s.appearance_id | |
73 | - @appearance = Appearance.find(s.appearance_id) | |
74 | - | |
75 | - if @appearance.answerable | |
76 | - puts "Appearance #{@appearance.id} has more than one skip!" | |
77 | - else | |
78 | - @appearance.answerable = s | |
79 | - @appearance.save | |
80 | - end | |
81 | - end | |
63 | + #Vote.find_each do |v| | |
64 | + # @appearance = Appearance.find(v.appearance_id) | |
65 | + # @appearance.answerable = v | |
66 | + # @appearance.save | |
67 | + # if v.id % 1000 == 0 | |
68 | + # puts v.id | |
69 | + # end | |
70 | + # end | |
71 | + Skip.find_each do |s| | |
72 | + if s.appearance_id | |
73 | + @appearance = Appearance.find(s.appearance_id) | |
74 | + | |
75 | + if @appearance.answerable | |
76 | + puts "Appearance #{@appearance.id} has more than one skip!" | |
77 | + else | |
78 | + @appearance.answerable = s | |
79 | + @appearance.save | |
80 | + end | |
82 | 81 | end |
82 | + end | |
83 | 83 | end |
84 | 84 | |
85 | 85 | task(:remove_double_counted_votes_with_same_appearance => :environment) do |
86 | 86 | |
87 | - votes_with_no_appearance = [] | |
88 | - Vote.find_each(:include => :appearance) do |v| | |
89 | - puts v.id if v.id % 1000 == 0 | |
87 | + votes_with_no_appearance = [] | |
88 | + Vote.find_each(:include => :appearance) do |v| | |
89 | + puts v.id if v.id % 1000 == 0 | |
90 | + | |
91 | + votes_with_no_appearance << v if v.appearance.nil? | |
92 | + end | |
90 | 93 | |
91 | - votes_with_no_appearance << v if v.appearance.nil? | |
92 | - end | |
93 | - | |
94 | - skips_with_no_appearance = [] | |
95 | - Skip.find_each(:include => :appearance) do |s| | |
96 | - puts s.id if s.id % 1000 == 0 | |
94 | + skips_with_no_appearance = [] | |
95 | + Skip.find_each(:include => :appearance) do |s| | |
96 | + puts s.id if s.id % 1000 == 0 | |
97 | 97 | |
98 | - skips_with_no_appearance << s if s.appearance.nil? | |
99 | - end | |
98 | + skips_with_no_appearance << s if s.appearance.nil? | |
99 | + end | |
100 | 100 | |
101 | 101 | |
102 | - puts "#{votes_with_no_appearance.size} Votes" | |
103 | - puts "#{skips_with_no_appearance.size} Skips" | |
102 | + puts "#{votes_with_no_appearance.size} Votes" | |
103 | + puts "#{skips_with_no_appearance.size} Skips" | |
104 | 104 | |
105 | - votes_with_no_appearance.each do |v| | |
106 | - v.valid_record = false | |
107 | - v.validity_information = "No associated appearance object" | |
108 | - v.save! | |
109 | - end | |
105 | + votes_with_no_appearance.each do |v| | |
106 | + v.valid_record = false | |
107 | + v.validity_information = "No associated appearance object" | |
108 | + v.save! | |
109 | + end | |
110 | 110 | |
111 | - skips_with_no_appearance.each do |s| | |
112 | - s.valid_record = false | |
113 | - s.validity_information = "No associated appearance object" | |
114 | - s.save! | |
115 | - end | |
111 | + skips_with_no_appearance.each do |s| | |
112 | + s.valid_record = false | |
113 | + s.validity_information = "No associated appearance object" | |
114 | + s.save! | |
115 | + end | |
116 | 116 | |
117 | 117 | end |
118 | 118 | |
119 | 119 | #call this by doing rake prune_db:populate_seed_ideas['blahblah',questionnum], where blahblah is the filename |
120 | 120 | task(:populate_seed_ideas, :args1, :args2, :needs => :environment) do | task, arguments| |
121 | - filename = arguments[:args1] | |
122 | - question_num = arguments[:args2] | |
121 | + filename = arguments[:args1] | |
122 | + question_num = arguments[:args2] | |
123 | 123 | |
124 | - puts filename | |
125 | - puts question_num | |
124 | + puts filename | |
125 | + puts question_num | |
126 | 126 | |
127 | - q = Question.find(question_num) | |
128 | - creator_id = q.creator_id | |
127 | + q = Question.find(question_num) | |
128 | + creator_id = q.creator_id | |
129 | 129 | |
130 | - File.open(filename, "r") do |infile| | |
131 | - while( data= infile.gets) | |
132 | - c = Choice.new(:creator_id => creator_id, | |
133 | - :question_id => q.id, | |
134 | - :active => true, | |
135 | - :data => data.chomp) | |
130 | + File.open(filename, "r") do |infile| | |
131 | + while( data= infile.gets) | |
132 | + c = Choice.new(:creator_id => creator_id, | |
133 | + :question_id => q.id, | |
134 | + :active => true, | |
135 | + :data => data.chomp) | |
136 | 136 | |
137 | - c.save | |
138 | - end | |
139 | - end | |
137 | + c.save | |
138 | + end | |
139 | + end | |
140 | 140 | |
141 | 141 | end |
142 | 142 | |
... | ... | @@ -147,12 +147,12 @@ namespace :prune_db do |
147 | 147 | question = Question.find(question_id) |
148 | 148 | |
149 | 149 | orphaned_votes = Vote.find(:all, |
150 | - :select => "votes.id", | |
151 | - :joins => "LEFT JOIN appearances ON (votes.id = appearances.answerable_id AND answerable_type <> 'Skip')", | |
152 | - :conditions => ["answerable_id IS NULL AND votes.valid_record = 1 AND votes.question_id = ?", question.id]) | |
150 | + :select => "votes.id", | |
151 | + :joins => "LEFT JOIN appearances ON (votes.id = appearances.answerable_id AND answerable_type <> 'Skip')", | |
152 | + :conditions => ["answerable_id IS NULL AND votes.valid_record = 1 AND votes.question_id = ?", question.id]) | |
153 | 153 | puts "Question ##{question.id} has #{orphaned_votes.count} orphaned votes" |
154 | 154 | orphaned_votes.each do |orphaned_vote_id| |
155 | - orphaned_vote = Vote.find(orphaned_vote_id.id) | |
155 | + orphaned_vote = Vote.find(orphaned_vote_id.id) | |
156 | 156 | |
157 | 157 | # attempt to find sibling vote |
158 | 158 | # sibling vote is one that is valid has the same voter and prompt, | ... | ... |