annotate lucene/src/luan/modules/lucene/Ab_testing.luan @ 274:8afe9f2fdfec

AB testing, not fully tested git-svn-id: https://luan-java.googlecode.com/svn/trunk@275 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Mon, 10 Nov 2014 03:28:32 +0000
parents
children a35d1177bbf0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
274
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
1 import "luan:Math"
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
2 import "luan:Table"
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
3
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
4
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
5 function of(index)
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
6
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
7 local ab_testing = {}
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
8
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
9 ab_testing.test_map = {}
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
10 ab_testing.test_list = {}
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
11
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
12 function ab_testing.test(test)
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
13 test.name or error "name not defined"
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
14 test.values or error "values not defined"
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
15 -- test.date_field is optional
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
16
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
17 local field = "ab_test_" .. test.name
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
18 index.fields[field] == nil or error("test "+test.name+" already defined")
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
19 index.fields[field] = field .. " index"
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
20 test.field = field
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
21
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
22 -- pass in map of name to aggregator factory
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
23 -- returns map of name to (map of value to result) and "start_date"
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
24 function test.results(aggregator_factories)
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
25 return index.Searcher( function(searcher)
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
26 local results = {}
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
27 for name in pairs(aggregator_factories) do
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
28 results[name] = {}
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
29 end
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
30 local date_field = test.date_field
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
31 local start_date = nil
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
32 for _, value in ipairs(test.values) do
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
33 local aggregators = {}
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
34 for name, factory in pairs(aggregator_factories) do
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
35 aggregators[name] = factory()
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
36 end
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
37 local query = { [field] = value }
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
38 searcher.search(query, function(doc)
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
39 for _, aggregator in pairs(aggregators) do
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
40 aggregator.aggregate(doc)
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
41 end
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
42 if date_field ~= nil then
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
43 local date = doc[date_field]
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
44 if date ~= nil and (start_date==nil or start_date > date) then
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
45 start_date = date
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
46 end
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
47 end
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
48 end)
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
49 for name, aggregator in pairs(aggregators) do
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
50 results[name][value] = aggregator.result
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
51 end
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
52 end
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
53 results.start_date = start_date
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
54 return results
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
55 end )
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
56 end
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
57
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
58 ab_testing.test_map[test.name] = test
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
59 ab_testing.test_list[#ab_testing.test_list + 1] = test
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
60
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
61 return test
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
62 end
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
63
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
64 function ab_testing.value(test_name,values)
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
65 return values[test_name] or ab_testing.test_map[test_name].values[1]
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
66 end
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
67
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
68 -- returns map from test name to value
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
69 function ab_testing.from_doc(doc)
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
70 local tests = ab_testing.test_list
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
71 local values = {}
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
72 for _, test in ipairs(tests) do
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
73 values[test.name] = doc[test.field]
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
74 end
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
75 return values
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
76 end
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
77
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
78 function ab_testing.to_doc(doc,values,tests)
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
79 tests = tests or ab_testing.test_list
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
80 if values == nil then
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
81 for _, test in ipairs(tests) do
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
82 doc[test.field] = test.values[Math.random(#test.values)]
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
83 end
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
84 else
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
85 for _, test in ipairs(tests) do
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
86 doc[test.field] = values[test.name]
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
87 end
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
88 end
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
89 end
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
90
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
91 return ab_testing
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
92 end
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
93
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
94
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
95 -- aggregator factories
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
96
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
97 -- fn(doc) should return boolean whether doc should be counted
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
98 function count(fn)
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
99 return function()
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
100 local aggregator = {}
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
101 aggregator.result = 0
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
102 function aggregator.aggregate(doc)
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
103 if fn(doc) then
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
104 aggregator.result = aggregator.result + 1
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
105 end
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
106 end
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
107 return aggregator
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
108 end
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
109 end
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
110
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
111 count_all = count( function() return true end )
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
112
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
113 -- fn(doc) should return number to add to result, return 0 for nothing
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
114 function sum(fn)
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
115 return function()
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
116 local aggregator = {}
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
117 aggregator.result = 0
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
118 function aggregator.aggregate(doc)
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
119 aggregator.result = aggregator.result + fn(doc)
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
120 end
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
121 return aggregator
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
122 end
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
123 end
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
124
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
125
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
126
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
127 local function percent(x,total)
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
128 if total==0 then
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
129 return 0
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
130 else
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
131 return 100 * x / total
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
132 end
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
133 end
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
134
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
135 function fancy(results,names)
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
136 local fancy = {}
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
137 fancy.start_date = results.start_date
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
138 local name = names[1]
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
139 fancy[name] = {}
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
140 for value, count in pairs(result[name]) do
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
141 fancy[name][value] = {}
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
142 fancy[name][value].count = count
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
143 fancy[name][value].pct_of_total = 100
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
144 fancy[name][value].pct_of_prev = 100
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
145 end
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
146 local all = result[name]
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
147 local prev = all
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
148 for i in range(2,#names) do
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
149 name = names[i]
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
150 fancy[name] = {}
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
151 for value, count in pairs(result[name]) do
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
152 fancy[name][value] = {}
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
153 fancy[name][value].count = count
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
154 fancy[name][value].pct_of_total = percent(count,all[value])
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
155 fancy[name][value].pct_of_prev = percent(count,prev[value])
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
156 end
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
157 prev = result[name]
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
158 end
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
159 return fancy
8afe9f2fdfec AB testing, not fully tested
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
160 end