vanity 1.1.1 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +31 -0
- data/lib/vanity/experiment/ab_test.rb +10 -6
- data/lib/vanity/experiment/base.rb +2 -5
- data/lib/vanity/helpers.rb +59 -0
- data/lib/vanity/metric.rb +92 -21
- data/lib/vanity/playground.rb +42 -22
- data/lib/vanity/rails/helpers.rb +7 -16
- data/lib/vanity/rails.rb +12 -5
- data/lib/vanity/templates/_experiments.erb +3 -3
- data/lib/vanity/templates/_metric.erb +1 -1
- data/lib/vanity/templates/_metrics.erb +3 -3
- data/lib/vanity/templates/vanity.js +7 -21
- data/lib/vanity.rb +3 -1
- data/test/metric_test.rb +442 -211
- data/test/test_helper.rb +64 -4
- data/vanity.gemspec +1 -1
- metadata +4 -3
data/test/metric_test.rb
CHANGED
@@ -1,287 +1,518 @@
|
|
1
1
|
require "test/test_helper"
|
2
2
|
|
3
|
-
class
|
4
|
-
|
3
|
+
class Sky < ActiveRecord::Base
|
4
|
+
connection.drop_table :skies if table_exists?
|
5
|
+
connection.create_table :skies do |t|
|
6
|
+
t.integer :height
|
7
|
+
t.timestamps
|
8
|
+
end
|
9
|
+
|
10
|
+
named_scope :high, lambda { { :conditions=>"height >= 4" } }
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
context "Metric" do
|
15
|
+
|
5
16
|
# -- Via the playground --
|
6
17
|
|
7
|
-
|
8
|
-
metric "Yawns/sec", "Cheers/sec"
|
9
|
-
assert Vanity.playground.metrics.keys.include?(:yawns_sec)
|
10
|
-
assert Vanity.playground.metrics.keys.include?(:cheers_sec)
|
11
|
-
end
|
18
|
+
context "playground" do
|
12
19
|
|
13
|
-
|
14
|
-
|
15
|
-
Vanity.playground.
|
20
|
+
test "knows all loaded metrics" do
|
21
|
+
metric "Yawns/sec", "Cheers/sec"
|
22
|
+
assert Vanity.playground.metrics.keys.include?(:yawns_sec)
|
23
|
+
assert Vanity.playground.metrics.keys.include?(:cheers_sec)
|
16
24
|
end
|
17
|
-
end
|
18
25
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
26
|
+
test "loads metric definitions" do
|
27
|
+
File.open "tmp/experiments/metrics/yawns_sec.rb", "w" do |f|
|
28
|
+
f.write <<-RUBY
|
29
|
+
metric "Yawns/sec" do
|
30
|
+
def xmts
|
31
|
+
"x"
|
32
|
+
end
|
25
33
|
end
|
26
|
-
|
27
|
-
|
34
|
+
RUBY
|
35
|
+
end
|
36
|
+
assert_equal "x", Vanity.playground.metric(:yawns_sec).xmts
|
28
37
|
end
|
29
|
-
assert_equal "x", Vanity.playground.metric(:yawns_sec).xmts
|
30
|
-
end
|
31
38
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
39
|
+
test "bubbles up loaded metrics" do
|
40
|
+
File.open "tmp/experiments/metrics/yawns_sec.rb", "w" do |f|
|
41
|
+
f.write "fail 'yawn!'"
|
42
|
+
end
|
43
|
+
assert_raises NameError do
|
44
|
+
Vanity.playground.metric(:yawns_sec)
|
45
|
+
end
|
38
46
|
end
|
39
|
-
assert metric = Vanity.playground.metric(:yawns_sec)
|
40
|
-
assert_equal "Yawns/sec", metric.name
|
41
|
-
end
|
42
47
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
48
|
+
test "map identifier from file name" do
|
49
|
+
File.open "tmp/experiments/metrics/yawns_sec.rb", "w" do |f|
|
50
|
+
f.write <<-RUBY
|
51
|
+
metric "yawns/hour" do
|
52
|
+
end
|
53
|
+
RUBY
|
54
|
+
end
|
55
|
+
assert Vanity.playground.metric(:yawns_sec)
|
49
56
|
end
|
50
|
-
end
|
51
57
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
end
|
57
|
-
RUBY
|
58
|
+
test "fails tracking unknown metric" do
|
59
|
+
assert_raises NameError do
|
60
|
+
Vanity.playground.track! :yawns_sec
|
61
|
+
end
|
58
62
|
end
|
59
|
-
|
63
|
+
|
64
|
+
test "reloading metrics" do
|
65
|
+
metric "Yawns/sec", "Cheers/sec"
|
60
66
|
Vanity.playground.metric(:yawns_sec)
|
67
|
+
Vanity.playground.metric(:cheers_sec)
|
68
|
+
assert 2, Vanity.playground.metrics.size
|
69
|
+
metrics = Vanity.playground.metrics.values
|
70
|
+
Vanity.playground.reload!
|
71
|
+
assert 2, Vanity.playground.metrics.size
|
72
|
+
assert_not_equal metrics, Vanity.playground.metrics.values
|
61
73
|
end
|
62
|
-
end
|
63
74
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
metrics = Vanity.playground.metrics.values
|
70
|
-
Vanity.playground.reload!
|
71
|
-
assert 2, Vanity.playground.metrics.size
|
72
|
-
assert_not_equal metrics, Vanity.playground.metrics.values
|
73
|
-
end
|
75
|
+
test "ignores undefined metrics in database" do
|
76
|
+
metric "Yawns/sec"
|
77
|
+
Vanity.playground.reload!
|
78
|
+
assert Vanity.playground.metrics.empty?
|
79
|
+
end
|
74
80
|
|
75
|
-
def test_undefined_metric_in_database
|
76
|
-
metric "Yawns/sec"
|
77
|
-
Vanity.playground.reload!
|
78
|
-
assert Vanity.playground.metrics.empty?
|
79
81
|
end
|
80
82
|
|
81
83
|
|
82
84
|
# -- Tracking --
|
83
85
|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
86
|
+
context "tracking" do
|
87
|
+
test "can count" do
|
88
|
+
metric "Yawns/sec", "Cheers/sec"
|
89
|
+
4.times { Vanity.playground.track! :yawns_sec }
|
90
|
+
2.times { Vanity.playground.track! :cheers_sec }
|
91
|
+
yawns = Vanity.playground.metric(:yawns_sec).values(today, today).first
|
92
|
+
cheers = Vanity.playground.metric(:cheers_sec).values(today, today).first
|
93
|
+
assert yawns = 2 * cheers
|
94
|
+
end
|
92
95
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
end
|
96
|
+
test "can tell the time" do
|
97
|
+
metric "Yawns/sec"
|
98
|
+
Timecop.travel(today - 4) { 4.times { Vanity.playground.track! :yawns_sec } }
|
99
|
+
Timecop.travel(today - 2) { 2.times { Vanity.playground.track! :yawns_sec } }
|
100
|
+
1.times { Vanity.playground.track! :yawns_sec }
|
101
|
+
boredom = Vanity.playground.metric(:yawns_sec).values(today - 5, today)
|
102
|
+
assert_equal [0,4,0,2,0,1], boredom
|
103
|
+
end
|
102
104
|
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
105
|
+
test "with no value" do
|
106
|
+
metric "Yawns/sec", "Cheers/sec", "Looks"
|
107
|
+
Vanity.playground.track! :yawns_sec, 0
|
108
|
+
Vanity.playground.track! :cheers_sec, -1
|
109
|
+
Vanity.playground.track! :looks, 10
|
110
|
+
assert_equal 0, Vanity.playground.metric(:yawns_sec).values(today, today).sum
|
111
|
+
assert_equal 0, Vanity.playground.metric(:cheers_sec).values(today, today).sum
|
112
|
+
assert_equal 10, Vanity.playground.metric(:looks).values(today, today).sum
|
113
|
+
end
|
111
114
|
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
115
|
+
test "with count" do
|
116
|
+
metric "Yawns/sec"
|
117
|
+
Timecop.travel(today - 4) { Vanity.playground.track! :yawns_sec, 4 }
|
118
|
+
Timecop.travel(today - 2) { Vanity.playground.track! :yawns_sec, 2 }
|
119
|
+
Vanity.playground.track! :yawns_sec
|
120
|
+
boredom = Vanity.playground.metric(:yawns_sec).values(today - 5, today)
|
121
|
+
assert_equal [0,4,0,2,0,1], boredom
|
122
|
+
end
|
120
123
|
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
124
|
+
test "runs hook" do
|
125
|
+
metric "Many Happy Returns"
|
126
|
+
total = 0
|
127
|
+
Vanity.playground.metric(:many_happy_returns).hook do |metric_id, timestamp, count|
|
128
|
+
assert_equal :many_happy_returns, metric_id
|
129
|
+
assert_in_delta Time.now.to_i, timestamp.to_i, 1
|
130
|
+
total += count
|
131
|
+
end
|
132
|
+
Vanity.playground.track! :many_happy_returns, 6
|
133
|
+
assert_equal 6, total
|
128
134
|
end
|
129
|
-
Vanity.playground.track! :many_happy_returns, 6
|
130
|
-
assert_equal 6, total
|
131
|
-
end
|
132
135
|
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
136
|
+
test "runs multiple hooks" do
|
137
|
+
metric "Many Happy Returns"
|
138
|
+
returns = 0
|
139
|
+
Vanity.playground.metric(:many_happy_returns).hook { returns += 1 }
|
140
|
+
Vanity.playground.metric(:many_happy_returns).hook { returns += 1 }
|
141
|
+
Vanity.playground.metric(:many_happy_returns).hook { returns += 1 }
|
142
|
+
Vanity.playground.track! :many_happy_returns
|
143
|
+
assert_equal 3, returns
|
144
|
+
end
|
142
145
|
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
146
|
+
test "destroy wipes metrics" do
|
147
|
+
metric "Many Happy Returns"
|
148
|
+
Vanity.playground.track! :many_happy_returns, 3
|
149
|
+
assert_equal [3], Vanity.playground.metric(:many_happy_returns).values(today, today)
|
150
|
+
Vanity.playground.metric(:many_happy_returns).destroy!
|
151
|
+
assert_equal [0], Vanity.playground.metric(:many_happy_returns).values(today, today)
|
152
|
+
end
|
149
153
|
end
|
150
154
|
|
151
155
|
|
152
156
|
# -- Metric name --
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
157
|
+
|
158
|
+
context "name" do
|
159
|
+
test "can be whatever" do
|
160
|
+
File.open "tmp/experiments/metrics/yawns_sec.rb", "w" do |f|
|
161
|
+
f.write <<-RUBY
|
162
|
+
metric "Yawns per second" do
|
163
|
+
end
|
164
|
+
RUBY
|
165
|
+
end
|
166
|
+
assert_equal "Yawns per second", Vanity.playground.metric(:yawns_sec).name
|
167
|
+
end
|
168
|
+
end
|
163
169
|
|
164
170
|
|
165
171
|
# -- Description helper --
|
166
172
|
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
173
|
+
context "description" do
|
174
|
+
test "metric with description" do
|
175
|
+
File.open "tmp/experiments/metrics/yawns_sec.rb", "w" do |f|
|
176
|
+
f.write <<-RUBY
|
177
|
+
metric "Yawns/sec" do
|
178
|
+
description "Am I that boring?"
|
179
|
+
end
|
180
|
+
RUBY
|
181
|
+
end
|
182
|
+
assert_equal "Am I that boring?", Vanity::Metric.description(Vanity.playground.metric(:yawns_sec))
|
174
183
|
end
|
175
|
-
assert_equal "Am I that boring?", Vanity::Metric.description(Vanity.playground.metric(:yawns_sec))
|
176
|
-
end
|
177
184
|
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
185
|
+
test "metric without description" do
|
186
|
+
File.open "tmp/experiments/metrics/yawns_sec.rb", "w" do |f|
|
187
|
+
f.write <<-RUBY
|
188
|
+
metric "Yawns/sec" do
|
189
|
+
end
|
190
|
+
RUBY
|
191
|
+
end
|
192
|
+
assert_nil Vanity::Metric.description(Vanity.playground.metric(:yawns_sec))
|
184
193
|
end
|
185
|
-
assert_nil Vanity::Metric.description(Vanity.playground.metric(:yawns_sec))
|
186
|
-
end
|
187
194
|
|
188
|
-
|
189
|
-
|
190
|
-
|
195
|
+
test "metric with no method description" do
|
196
|
+
metric = Object.new
|
197
|
+
assert_nil Vanity::Metric.description(metric)
|
198
|
+
end
|
191
199
|
end
|
192
200
|
|
193
201
|
|
194
202
|
# -- Metric bounds --
|
195
203
|
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
204
|
+
context "bounds" do
|
205
|
+
test "metric with bounds" do
|
206
|
+
File.open "tmp/experiments/metrics/sky_is_limit.rb", "w" do |f|
|
207
|
+
f.write <<-RUBY
|
208
|
+
metric "Sky is limit" do
|
209
|
+
def bounds
|
210
|
+
[6,12]
|
211
|
+
end
|
202
212
|
end
|
203
|
-
|
204
|
-
|
213
|
+
RUBY
|
214
|
+
end
|
215
|
+
assert_equal [6,12], Vanity::Metric.bounds(Vanity.playground.metric(:sky_is_limit))
|
205
216
|
end
|
206
|
-
assert_equal [6,12], Vanity::Metric.bounds(Vanity.playground.metric(:sky_is_limit))
|
207
|
-
end
|
208
217
|
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
218
|
+
test "metric without bounds" do
|
219
|
+
metric "Sky is limit"
|
220
|
+
assert_equal [nil, nil], Vanity::Metric.bounds(Vanity.playground.metric(:sky_is_limit))
|
221
|
+
end
|
213
222
|
|
214
|
-
|
215
|
-
|
216
|
-
|
223
|
+
test "metric with no method bounds" do
|
224
|
+
metric = Object.new
|
225
|
+
assert_equal [nil, nil], Vanity::Metric.bounds(metric)
|
226
|
+
end
|
217
227
|
end
|
218
228
|
|
219
229
|
|
220
230
|
# -- Timestamp --
|
221
|
-
|
222
|
-
def test_metric_has_created_timestamp
|
223
|
-
metric "Coolness"
|
224
|
-
metric = Vanity.playground.metric(:coolness)
|
225
|
-
assert_instance_of Time, metric.created_at
|
226
|
-
assert_in_delta metric.created_at.to_i, Time.now.to_i, 1
|
227
|
-
end
|
228
231
|
|
229
|
-
|
230
|
-
|
231
|
-
Timecop.travel past do
|
232
|
+
context "created_at" do
|
233
|
+
test "for new metric" do
|
232
234
|
metric "Coolness"
|
233
|
-
|
234
|
-
|
235
|
+
metric = Vanity.playground.metric(:coolness)
|
236
|
+
assert_instance_of Time, metric.created_at
|
237
|
+
assert_in_delta metric.created_at.to_i, Time.now.to_i, 1
|
235
238
|
end
|
236
239
|
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
240
|
+
test "across restarts" do
|
241
|
+
past = Date.today - 1
|
242
|
+
Timecop.travel past do
|
243
|
+
metric "Coolness"
|
244
|
+
coolness = Vanity.playground.metric(:coolness)
|
245
|
+
assert_in_delta coolness.created_at.to_i, past.to_time.to_i, 1
|
246
|
+
end
|
242
247
|
|
248
|
+
new_playground
|
249
|
+
metric "Coolness"
|
250
|
+
new_cool = Vanity.playground.metric(:coolness)
|
251
|
+
assert_in_delta new_cool.created_at.to_i, past.to_time.to_i, 1
|
252
|
+
end
|
253
|
+
end
|
243
254
|
|
244
|
-
# -- Data helper --
|
245
255
|
|
246
|
-
|
247
|
-
metric "Yawns/sec"
|
248
|
-
Timecop.travel(today - 4) { Vanity.playground.track! :yawns_sec, 4 }
|
249
|
-
Timecop.travel(today - 2) { Vanity.playground.track! :yawns_sec, 2 }
|
250
|
-
Vanity.playground.track! :yawns_sec
|
251
|
-
boredom = Vanity::Metric.data(Vanity.playground.metric(:yawns_sec), Date.today - 5, Date.today)
|
252
|
-
assert_equal [[today - 5, 0], [today - 4, 4], [today - 3, 0], [today - 2, 2], [today - 1, 0], [today, 1]], boredom
|
253
|
-
end
|
256
|
+
# -- Data --
|
254
257
|
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
258
|
+
context "data" do
|
259
|
+
test "explicit dates" do
|
260
|
+
metric "Yawns/sec"
|
261
|
+
Timecop.travel(today - 4) { Vanity.playground.track! :yawns_sec, 4 }
|
262
|
+
Timecop.travel(today - 2) { Vanity.playground.track! :yawns_sec, 2 }
|
263
|
+
Vanity.playground.track! :yawns_sec
|
264
|
+
boredom = Vanity::Metric.data(Vanity.playground.metric(:yawns_sec), Date.today - 5, Date.today)
|
265
|
+
assert_equal [[today - 5, 0], [today - 4, 4], [today - 3, 0], [today - 2, 2], [today - 1, 0], [today, 1]], boredom
|
266
|
+
end
|
263
267
|
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
268
|
+
test "start date only" do
|
269
|
+
metric "Yawns/sec"
|
270
|
+
Timecop.travel(today - 4) { Vanity.playground.track! :yawns_sec, 4 }
|
271
|
+
Timecop.travel(today - 2) { Vanity.playground.track! :yawns_sec, 2 }
|
272
|
+
Vanity.playground.track! :yawns_sec
|
273
|
+
boredom = Vanity::Metric.data(Vanity.playground.metric(:yawns_sec), Date.today - 4)
|
274
|
+
assert_equal [[today - 4, 4], [today - 3, 0], [today - 2, 2], [today - 1, 0], [today, 1]], boredom
|
275
|
+
end
|
276
|
+
|
277
|
+
test "start date and duration" do
|
278
|
+
metric "Yawns/sec"
|
279
|
+
Timecop.travel(today - 4) { Vanity.playground.track! :yawns_sec, 4 }
|
280
|
+
Timecop.travel(today - 2) { Vanity.playground.track! :yawns_sec, 2 }
|
281
|
+
Vanity.playground.track! :yawns_sec
|
282
|
+
boredom = Vanity::Metric.data(Vanity.playground.metric(:yawns_sec), 5)
|
283
|
+
assert_equal [[today - 4, 4], [today - 3, 0], [today - 2, 2], [today - 1, 0], [today, 1]], boredom
|
284
|
+
end
|
285
|
+
|
286
|
+
test "no data" do
|
287
|
+
metric "Yawns/sec"
|
288
|
+
boredom = Vanity::Metric.data(Vanity.playground.metric(:yawns_sec))
|
289
|
+
assert_equal 90, boredom.size
|
290
|
+
assert_equal [today - 89, 0], boredom.first
|
291
|
+
assert_equal [today, 0], boredom.last
|
292
|
+
end
|
293
|
+
|
294
|
+
test "using custom values method" do
|
295
|
+
File.open "tmp/experiments/metrics/hours_in_day.rb", "w" do |f|
|
296
|
+
f.write <<-RUBY
|
297
|
+
metric "Hours in day" do
|
298
|
+
def values(from, to)
|
299
|
+
(from..to).map { |d| 24 }
|
300
|
+
end
|
301
|
+
end
|
302
|
+
RUBY
|
303
|
+
end
|
304
|
+
data = Vanity::Metric.data(Vanity.playground.metric(:hours_in_day))
|
305
|
+
assert_equal [24] * 90, data.map(&:last)
|
306
|
+
end
|
272
307
|
|
273
|
-
def test_data_with_no_dates
|
274
|
-
metric "Yawns/sec"
|
275
|
-
boredom = Vanity::Metric.data(Vanity.playground.metric(:yawns_sec))
|
276
|
-
assert_equal [today - 90, 0], boredom.first
|
277
|
-
assert_equal [today, 0], boredom.last
|
278
308
|
end
|
279
309
|
|
280
310
|
|
311
|
+
# -- ActiveRecord --
|
312
|
+
|
313
|
+
context "ActiveRecord" do
|
314
|
+
|
315
|
+
test "record count" do
|
316
|
+
File.open "tmp/experiments/metrics/sky_is_limit.rb", "w" do |f|
|
317
|
+
f.write <<-RUBY
|
318
|
+
metric "Sky is limit" do
|
319
|
+
model Sky
|
320
|
+
end
|
321
|
+
RUBY
|
322
|
+
end
|
323
|
+
Vanity.playground.metrics
|
324
|
+
Sky.create!
|
325
|
+
assert_equal 1, Sky.count
|
326
|
+
assert_equal 1, Vanity::Metric.data(metric(:sky_is_limit)).last.last
|
327
|
+
end
|
328
|
+
|
329
|
+
test "record sum" do
|
330
|
+
File.open "tmp/experiments/metrics/sky_is_limit.rb", "w" do |f|
|
331
|
+
f.write <<-RUBY
|
332
|
+
metric "Sky is limit" do
|
333
|
+
model Sky, :sum=>:height
|
334
|
+
end
|
335
|
+
RUBY
|
336
|
+
end
|
337
|
+
Vanity.playground.metrics
|
338
|
+
Sky.create! :height=>4
|
339
|
+
Sky.create! :height=>2
|
340
|
+
assert_equal 6, Vanity::Metric.data(metric(:sky_is_limit)).last.last
|
341
|
+
end
|
342
|
+
|
343
|
+
test "record average" do
|
344
|
+
Sky.aggregates
|
345
|
+
File.open "tmp/experiments/metrics/sky_is_limit.rb", "w" do |f|
|
346
|
+
f.write <<-RUBY
|
347
|
+
metric "Sky is limit" do
|
348
|
+
model Sky, :average=>:height
|
349
|
+
end
|
350
|
+
RUBY
|
351
|
+
end
|
352
|
+
Vanity.playground.metrics
|
353
|
+
Sky.create! :height=>4
|
354
|
+
Sky.create! :height=>2
|
355
|
+
assert_equal 3, Vanity::Metric.data(metric(:sky_is_limit)).last.last
|
356
|
+
end
|
357
|
+
|
358
|
+
test "record minimum" do
|
359
|
+
Sky.aggregates
|
360
|
+
File.open "tmp/experiments/metrics/sky_is_limit.rb", "w" do |f|
|
361
|
+
f.write <<-RUBY
|
362
|
+
metric "Sky is limit" do
|
363
|
+
model Sky, :minimum=>:height
|
364
|
+
end
|
365
|
+
RUBY
|
366
|
+
end
|
367
|
+
Vanity.playground.metrics
|
368
|
+
Sky.create! :height=>4
|
369
|
+
Sky.create! :height=>2
|
370
|
+
assert_equal 2, Vanity::Metric.data(metric(:sky_is_limit)).last.last
|
371
|
+
end
|
372
|
+
|
373
|
+
test "record maximum" do
|
374
|
+
Sky.aggregates
|
375
|
+
File.open "tmp/experiments/metrics/sky_is_limit.rb", "w" do |f|
|
376
|
+
f.write <<-RUBY
|
377
|
+
metric "Sky is limit" do
|
378
|
+
model Sky, :maximum=>:height
|
379
|
+
end
|
380
|
+
RUBY
|
381
|
+
end
|
382
|
+
Vanity.playground.metrics
|
383
|
+
Sky.create! :height=>4
|
384
|
+
Sky.create! :height=>2
|
385
|
+
assert_equal 4, Vanity::Metric.data(metric(:sky_is_limit)).last.last
|
386
|
+
end
|
387
|
+
|
388
|
+
test "with conditions" do
|
389
|
+
File.open "tmp/experiments/metrics/sky_is_limit.rb", "w" do |f|
|
390
|
+
f.write <<-RUBY
|
391
|
+
metric "Sky is limit" do
|
392
|
+
model Sky, :sum=>:height, :conditions=>["height > 4"]
|
393
|
+
end
|
394
|
+
RUBY
|
395
|
+
end
|
396
|
+
Vanity.playground.metrics
|
397
|
+
high_skies = 0
|
398
|
+
metric(:sky_is_limit).hook do |metric_id, timestamp, height|
|
399
|
+
assert height > 4
|
400
|
+
high_skies += height
|
401
|
+
end
|
402
|
+
[nil,5,3,6].each do |height|
|
403
|
+
Sky.create! :height=>height
|
404
|
+
end
|
405
|
+
assert_equal 11, Vanity::Metric.data(metric(:sky_is_limit)).sum(&:last)
|
406
|
+
assert_equal 11, high_skies
|
407
|
+
end
|
408
|
+
|
409
|
+
test "with scope" do
|
410
|
+
Sky.aggregates
|
411
|
+
File.open "tmp/experiments/metrics/sky_is_limit.rb", "w" do |f|
|
412
|
+
f.write <<-RUBY
|
413
|
+
metric "Sky is limit" do
|
414
|
+
model Sky.high
|
415
|
+
end
|
416
|
+
RUBY
|
417
|
+
end
|
418
|
+
Vanity.playground.metrics
|
419
|
+
total = 0
|
420
|
+
metric(:sky_is_limit).hook do |metric_id, timestamp, count|
|
421
|
+
total += count
|
422
|
+
end
|
423
|
+
Sky.create! :height=>4
|
424
|
+
Sky.create! :height=>2
|
425
|
+
assert_equal 1, Vanity::Metric.data(metric(:sky_is_limit)).last.last
|
426
|
+
assert_equal 1, total
|
427
|
+
end
|
428
|
+
|
429
|
+
test "hooks" do
|
430
|
+
File.open "tmp/experiments/metrics/sky_is_limit.rb", "w" do |f|
|
431
|
+
f.write <<-RUBY
|
432
|
+
metric "Sky is limit" do
|
433
|
+
model Sky, :sum=>:height
|
434
|
+
end
|
435
|
+
RUBY
|
436
|
+
end
|
437
|
+
Vanity.playground.metrics
|
438
|
+
total = 0
|
439
|
+
metric(:sky_is_limit).hook do |metric_id, timestamp, count|
|
440
|
+
assert_equal :sky_is_limit, metric_id
|
441
|
+
assert_in_delta Time.now.to_i, timestamp.to_i, 1
|
442
|
+
total += count
|
443
|
+
end
|
444
|
+
Sky.create! :height=>4
|
445
|
+
assert_equal 4, total
|
446
|
+
end
|
447
|
+
|
448
|
+
test "after_create not after_save" do
|
449
|
+
File.open "tmp/experiments/metrics/sky_is_limit.rb", "w" do |f|
|
450
|
+
f.write <<-RUBY
|
451
|
+
metric "Sky is limit" do
|
452
|
+
model Sky
|
453
|
+
end
|
454
|
+
RUBY
|
455
|
+
end
|
456
|
+
Vanity.playground.metrics
|
457
|
+
once = nil
|
458
|
+
metric(:sky_is_limit).hook do
|
459
|
+
fail "Metric tracked twice" if once
|
460
|
+
once = true
|
461
|
+
end
|
462
|
+
Sky.create!
|
463
|
+
Sky.last.update_attributes :height=>4
|
464
|
+
end
|
465
|
+
|
466
|
+
test "with after_save" do
|
467
|
+
File.open "tmp/experiments/metrics/sky_is_limit.rb", "w" do |f|
|
468
|
+
f.write <<-RUBY
|
469
|
+
metric "Sky is limit" do
|
470
|
+
model Sky, :conditions=>["height > 3"]
|
471
|
+
Sky.after_save { |sky| track! if sky.height_changed? && sky.height > 3 }
|
472
|
+
end
|
473
|
+
RUBY
|
474
|
+
end
|
475
|
+
Vanity.playground.metrics
|
476
|
+
times = 0
|
477
|
+
metric(:sky_is_limit).hook do
|
478
|
+
times += 1
|
479
|
+
end
|
480
|
+
Sky.create!
|
481
|
+
(1..5).each do |height|
|
482
|
+
Sky.last.update_attributes! :height=>height
|
483
|
+
end
|
484
|
+
assert_equal 2, times
|
485
|
+
end
|
486
|
+
|
487
|
+
test "do it youself" do
|
488
|
+
File.open "tmp/experiments/metrics/sky_is_limit.rb", "w" do |f|
|
489
|
+
f.write <<-RUBY
|
490
|
+
metric "Sky is limit" do
|
491
|
+
Sky.after_save { |sky| track! if sky.height_changed? && sky.height > 3 }
|
492
|
+
end
|
493
|
+
RUBY
|
494
|
+
end
|
495
|
+
Vanity.playground.metrics
|
496
|
+
(1..5).each do |height|
|
497
|
+
Sky.create! :height=>height
|
498
|
+
end
|
499
|
+
Sky.first.update_attributes! :height=>4
|
500
|
+
assert_equal 3, Vanity::Metric.data(metric(:sky_is_limit)).last.last
|
501
|
+
end
|
502
|
+
|
503
|
+
end
|
504
|
+
|
505
|
+
|
281
506
|
# -- Helper methods --
|
282
507
|
|
283
508
|
def today
|
284
509
|
@today ||= Date.today
|
285
510
|
end
|
286
511
|
|
512
|
+
teardown do
|
513
|
+
Sky.delete_all
|
514
|
+
Sky.after_create.clear
|
515
|
+
Sky.after_save.clear
|
516
|
+
end
|
517
|
+
|
287
518
|
end
|