abongo 1.0.6 → 1.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -243,7 +243,7 @@ class Abongo
243
243
  end
244
244
 
245
245
  def self.all_tests
246
- Abongo.experiments.find.to_a
246
+ Abongo.experiments.find.sort(:final).to_a
247
247
  end
248
248
 
249
249
  def self.get_test(test)
@@ -1,9 +1,15 @@
1
- class Abongo
2
- @@VERSION = '1.0.0'
3
- def self.VERSION; @@VERSION; end
4
- @@MAJOR_VERSION = '1.0'
5
- def self.MAJOR_VERSION; @@MAJOR_VERSION; end
1
+ if defined? Rails
2
+ require 'abongo/sugar'
3
+ require 'abongo/view_helper'
4
+ require 'abongo/controller/dashboard'
5
+ ActionController::Base.send :include, Abongo::Sugar
6
+ ActionView::Base.send :include, Abongo::ViewHelper
7
+ end
8
+
9
+ require 'abongo/version'
10
+ require 'abongo/statistics'
6
11
 
12
+ class Abongo
7
13
  @@options ||= {}
8
14
  def self.options; @@options; end
9
15
  def self.options=(options); @@options = options; end
@@ -29,7 +35,6 @@ class Abongo
29
35
  @@identity ||= rand(10 ** 10)
30
36
  end
31
37
 
32
- # TODO: add options
33
38
  def self.flip(test_name, options = {})
34
39
  if block_given?
35
40
  yield(self.test(test_name, [true, false], options))
@@ -65,6 +70,7 @@ class Abongo
65
70
  # Small timing issue in here
66
71
  if (!@@options[:count_humans_only] || participant['human'])
67
72
  Abongo.alternatives.update({:content => choice, :test => test['_id']}, {:$inc => {:participants => 1}})
73
+ Abongo.experiments.update({:_id => test['_id']}, {'$inc' => {:participants => 1}})
68
74
  end
69
75
  end
70
76
 
@@ -95,7 +101,6 @@ class Abongo
95
101
  self.score_conversion!(test)
96
102
  end
97
103
  else # No tests listening for this conversion. Assume it is just a test name
98
- puts name.inspect
99
104
  if name.kind_of? BSON::ObjectId
100
105
  self.score_conversion!(name)
101
106
  else
@@ -117,6 +122,7 @@ class Abongo
117
122
  test = Abongo.experiments.find_one(:_id => test_name)
118
123
  viewed_alternative = Abongo.find_alternative_for_user(Abongo.identity, test)
119
124
  Abongo.alternatives.update({:content => viewed_alternative, :test => test['_id']}, {'$inc' => {:conversions => 1}})
125
+ Abongo.experiments.update({:_id => test_name}, {'$inc' => {:conversions => 1}})
120
126
  end
121
127
  end
122
128
  end
@@ -153,16 +159,22 @@ class Abongo
153
159
  tests_and_alternatives
154
160
  end
155
161
 
162
+ def self.is_human?(identity = nil)
163
+ identity ||= Abongo.identity
164
+ participant = Abongo.participants.find_one(:identity => identity)
165
+ return !!(participant && participant["human"])
166
+ end
167
+
156
168
  def self.human!(identity = nil)
157
169
  identity ||= Abongo.identity
158
170
  begin
159
171
  previous = Abongo.participants.find_and_modify({'query' => {:identity => identity}, 'update' => {'$set' => {:human => true}}, 'upsert' => true})
160
172
  rescue Mongo::OperationFailure
161
- Abongo.participants.update({:identity => identity}, {'$set' => {:human => true}}, :upsert => true, :safe => true)
173
+ Abongo.participants.update({:identity => identity}, {'$set' => {:human => true}}, :upsert => true)
162
174
  previous = Abongo.participants.find_one(:identity => identity)
163
175
  end
164
176
 
165
- unless previous['human']
177
+ if !previous['human'] and options[:count_humans_only]
166
178
  if options[:expires_in_for_bots] and previous['tests']
167
179
  Abongo.set_expiration(Abongo.identity, expires_in(true))
168
180
  end
@@ -171,7 +183,8 @@ class Abongo
171
183
  previous['tests'].each do |test_id|
172
184
  test = Abongo.experiments.find_one(test_id)
173
185
  choice = Abongo.find_alternative_for_user(identity, test)
174
- Abongo.alternatives.update({:content => choice, :test => test['_id']}, {:$inc => {:participants => 1}})
186
+ Abongo.alternatives.update({:content => choice, :test => test_id}, {:$inc => {:participants => 1}})
187
+ Abongo.experiments.update({:_id => test_id}, {'$inc' => {:participants => 1}})
175
188
  end
176
189
  end
177
190
 
@@ -179,7 +192,8 @@ class Abongo
179
192
  previous['conversions'].each do |test_id|
180
193
  test = Abongo.experiments.find_one(:_id => test_id)
181
194
  viewed_alternative = Abongo.find_alternative_for_user(identity, test)
182
- Abongo.alternatives.update({:content => viewed_alternative, :test => test['_id']}, {'$inc' => {:conversions => 1}})
195
+ Abongo.alternatives.update({:content => viewed_alternative, :test => test_id}, {'$inc' => {:conversions => 1}})
196
+ Abongo.experiments.update({:_id => test_id}, {'$inc' => {:conversions => 1}})
183
197
  end
184
198
  end
185
199
  end
@@ -187,7 +201,9 @@ class Abongo
187
201
 
188
202
  def self.end_experiment!(test_name, final_alternative, conversion_name = nil)
189
203
  warn("conversion_name is deprecated") if conversion_name
190
- Abongo.experiments.update({:name => test_name}, {:$set => { :final => final_alternative}}, :upsert => true, :safe => true)
204
+ test = get_test(test_name)
205
+ Abongo.experiments.update({:name => test_name}, {'$set' => { :final => final_alternative}}, :upsert => true, :safe => true)
206
+ Abongo.conversions.update({'tests' => test['_id']}, {'$pull' => {'tests' => test['_id']}}, :multi => true)
191
207
  end
192
208
 
193
209
  protected
@@ -230,8 +246,16 @@ class Abongo
230
246
  Abongo.experiments.find.to_a
231
247
  end
232
248
 
233
- def self.get_test(test_name)
234
- Abongo.experiments.find_one({:name => test_name}) || nil
249
+ def self.get_test(test)
250
+ Abongo.experiments.find_one({:name => test}) || Abongo.experiments.find_one({:_id => test}) || nil
251
+ end
252
+
253
+ def self.get_alternatives(test_id)
254
+ Abongo.alternatives.find({:test => test_id})
255
+ end
256
+
257
+ def self.get_alternative(alternative_id)
258
+ Abongo.alternatives.find_one({:_id => BSON::ObjectId(alternative_id)})
235
259
  end
236
260
 
237
261
  def self.tests_listening_to_conversion(conversion)
@@ -243,7 +267,7 @@ class Abongo
243
267
  def self.start_experiment!(test_name, alternatives_array, conversion_name = nil)
244
268
  conversion_name ||= test_name
245
269
 
246
- Abongo.experiments.update({:name => test_name}, {:$set => { :alternatives => alternatives_array}}, :upsert => true, :safe => true)
270
+ Abongo.experiments.update({:name => test_name}, {:$set => {:alternatives => alternatives_array}, :$inc => {:participants => 0, :conversions => 0}}, :upsert => true, :safe => true)
247
271
  test = Abongo.experiments.find_one({:name => test_name})
248
272
 
249
273
  # This could be a lot more elegant
@@ -270,13 +294,13 @@ class Abongo
270
294
 
271
295
  def self.add_participation(identity, test_id, expires_in = nil)
272
296
  if expires_in.nil?
273
- Abongo.participants.update({:identity => identity}, {'$addToSet' => {:tests => test_id}}, :upsert => true, :safe => true)
297
+ Abongo.participants.update({:identity => identity}, {'$addToSet' => {:tests => test_id}}, :upsert => true)
274
298
  else
275
- Abongo.participants.update({:identity => identity}, {'$addToSet' => {:tests => test_id}, '$set' => {:expires => Time.now + expires_in}}, :upsert => true, :safe => true)
299
+ Abongo.participants.update({:identity => identity}, {'$addToSet' => {:tests => test_id}, '$set' => {:expires => Time.now + expires_in}}, :upsert => true)
276
300
  end
277
301
  end
278
302
 
279
303
  def self.set_expiration(identity, expires_in)
280
- Abongo.participants.update({:identity => identity}, {'$set' => {:expires => Time.now + expires_in}}, :upsert => true, :safe => true)
304
+ Abongo.participants.update({:identity => identity}, {'$set' => {:expires => Time.now + expires_in}}, :upsert => true)
281
305
  end
282
306
  end
@@ -1,4 +1,4 @@
1
1
  class Abongo
2
- VERSION = '1.0.6'
2
+ VERSION = '1.0.7'
3
3
  MAJOR_VERSION = '1'
4
4
  end
@@ -0,0 +1,4 @@
1
+ class Abongo
2
+ VERSION = '1.0.6'
3
+ MAJOR_VERSION = '1'
4
+ end
@@ -1,4 +1,4 @@
1
- <h3><%= experiment['name'].titleize %> <%= %Q|(Test completed)| if experiment['final'] %> </h3>
1
+ <h3><%= experiment['name'].titleize %> <%= %Q|(Test completed)| unless experiment['final'].nil? %> </h3>
2
2
  <table class="experiment" style="border: 1px black;">
3
3
  <tr class="header_row">
4
4
  <th>Name</th>
@@ -21,7 +21,7 @@
21
21
  <td><%= alternative['participants'] %></td>
22
22
  <td><%= alternative['conversions'] %> (<%= Abongo::Statistics.pretty_conversion_rate(alternative) %>)</td>
23
23
  <td>
24
- <% unless experiment['final'] %>
24
+ <% if experiment['final'].nil? %>
25
25
  <%= link_to("End experiment, picking this.", url_for(:id => alternative['_id'],
26
26
  :action => "end_experiment"),
27
27
  :method => :post,
@@ -0,0 +1,43 @@
1
+ <h3><%= experiment['name'].titleize %> <%= %Q|(Test completed)| if experiment['final'] %> </h3>
2
+ <table class="experiment" style="border: 1px black;">
3
+ <tr class="header_row">
4
+ <th>Name</th>
5
+ <th>Participants</th>
6
+ <th>Conversions</th>
7
+ <th>Notes</th>
8
+ </tr>
9
+ <tr class="experiment_row">
10
+ <td>Experiment Total: </td>
11
+ <td><%= experiment['participants'] %> </td>
12
+ <td><%= experiment['conversions'] %> (<%= Abongo::Statistics.pretty_conversion_rate(experiment) %>)</td>
13
+ <td></td>
14
+ </tr>
15
+ <% alternatives = Abongo.get_alternatives(experiment['_id']).to_a %>
16
+ <% alternatives.each do |alternative| %>
17
+ <tr class="alternative_row">
18
+ <td>
19
+ <%= h alternative['content'] %>
20
+ </td>
21
+ <td><%= alternative['participants'] %></td>
22
+ <td><%= alternative['conversions'] %> (<%= Abongo::Statistics.pretty_conversion_rate(alternative) %>)</td>
23
+ <td>
24
+ <% unless experiment['final'] %>
25
+ <%= link_to("End experiment, picking this.", url_for(:id => alternative['_id'],
26
+ :action => "end_experiment"),
27
+ :method => :post,
28
+ :confirm => "Are you sure you want to terminate this experiment? This is not reversible."
29
+ ) %>
30
+ <% else %>
31
+ <% if alternative['content'] == experiment['final'] %>
32
+ <b>(All users seeing this.)</b>
33
+ <% end %>
34
+ <% end %>
35
+ </td>
36
+ </tr>
37
+ <% end %>
38
+ <tr>
39
+ <td colspan="4">
40
+ <b>Significance test results: </b><%= Abongo::Statistics.describe_result_in_words(experiment, alternatives) %>
41
+ </td>
42
+ </tr>
43
+ </table>
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 0
8
- - 6
9
- version: 1.0.6
8
+ - 7
9
+ version: 1.0.7
10
10
  platform: ruby
11
11
  authors:
12
12
  - Michael Fairley
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-04-16 00:00:00 -07:00
17
+ date: 2011-04-28 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -57,8 +57,10 @@ files:
57
57
  - lib/abongo/statistics.rb
58
58
  - lib/abongo/sugar.rb
59
59
  - lib/abongo/version.rb
60
+ - lib/abongo/version.rb~
60
61
  - lib/abongo/view_helper.rb
61
62
  - lib/abongo/views/dashboard/_experiment.html.erb
63
+ - lib/abongo/views/dashboard/_experiment.html.erb~
62
64
  - lib/abongo/views/dashboard/index.html.erb
63
65
  - lib/abongo.rb
64
66
  - lib/abongo.rb~