comparison lucene/src/luan/modules/lucene/Ab_testing.luan @ 503:92c3d22745b8

make _ENV optional
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 20 May 2015 23:24:46 -0600
parents 598123096772
children 7bc63886d4f2
comparison
equal deleted inserted replaced
502:d3183a330ff5 503:92c3d22745b8
7 local Table = require "luan:Table" 7 local Table = require "luan:Table"
8 local Io = require "luan:Io" 8 local Io = require "luan:Io"
9 local Http = require "luan:http/Http" 9 local Http = require "luan:http/Http"
10 local Logging = require "luan:logging/Logging" 10 local Logging = require "luan:logging/Logging"
11 11
12 local M = {}
12 13
13 local logger = Logging.logger "Ab_testing" 14 local logger = Logging.logger "Ab_testing"
14 15
15 function of(index) 16 function M.of(index)
16 17
17 local ab_testing = {} 18 local ab_testing = {}
18 19
19 ab_testing.test_map = {} 20 ab_testing.test_map = {}
20 21
89 event = events[i] 90 event = events[i]
90 fancy[event] = {} 91 fancy[event] = {}
91 for value, count in pairs(results[event]) do 92 for value, count in pairs(results[event]) do
92 fancy[event][value] = {} 93 fancy[event][value] = {}
93 fancy[event][value].count = count 94 fancy[event][value].count = count
94 fancy[event][value].pct_of_total = percent(count,all[value]) 95 fancy[event][value].pct_of_total = M.percent(count,all[value])
95 fancy[event][value].pct_of_prev = percent(count,prev[value]) 96 fancy[event][value].pct_of_prev = M.percent(count,prev[value])
96 end 97 end
97 prev = results[event] 98 prev = results[event]
98 end 99 end
99 return fancy 100 return fancy
100 end 101 end
138 local test = ab_testing.test_map[name] 139 local test = ab_testing.test_map[name]
139 test or error("test not found: "..name) 140 test or error("test not found: "..name)
140 results[name] = test.fancy_results() 141 results[name] = test.fancy_results()
141 end 142 end
142 Io.stdout = Http.response.text_writer() 143 Io.stdout = Http.response.text_writer()
143 html(test_names,ab_testing.test_map,results) 144 M.html(test_names,ab_testing.test_map,results)
144 end } 145 end }
145 end 146 end
146 147
147 return ab_testing 148 return ab_testing
148 end 149 end
149 150
150 151
151 -- aggregator factories 152 -- aggregator factories
152 153
153 -- fn(doc) should return boolean whether doc should be counted 154 -- fn(doc) should return boolean whether doc should be counted
154 function count(fn) 155 function M.count(fn)
155 return function() 156 return function()
156 local aggregator = {} 157 local aggregator = {}
157 aggregator.result = 0 158 aggregator.result = 0
158 function aggregator.aggregate(doc) 159 function aggregator.aggregate(doc)
159 if fn(doc) then 160 if fn(doc) then
162 end 163 end
163 return aggregator 164 return aggregator
164 end 165 end
165 end 166 end
166 167
167 count_all = count( function(doc) return true end ) 168 M.count_all = M.count( function(doc) return true end )
168 169
169 -- fn(doc) should return number to add to result, return 0 for nothing 170 -- fn(doc) should return number to add to result, return 0 for nothing
170 function sum(fn) 171 function M.sum(fn)
171 return function() 172 return function()
172 local aggregator = {} 173 local aggregator = {}
173 aggregator.result = 0 174 aggregator.result = 0
174 function aggregator.aggregate(doc) 175 function aggregator.aggregate(doc)
175 aggregator.result = aggregator.result + fn(doc) 176 aggregator.result = aggregator.result + fn(doc)
178 end 179 end
179 end 180 end
180 181
181 182
182 183
183 function percent(x,total) 184 function M.percent(x,total)
184 if total==0 then 185 if total==0 then
185 return 0 186 return 0
186 else 187 else
187 return 100 * x / total 188 return 100 * x / total
188 end 189 end
202 local function format(v) 203 local function format(v)
203 v = v .. '' 204 v = v .. ''
204 return v.gsub([[(\d+\.\d{1})\d+]],'$1') 205 return v.gsub([[(\d+\.\d{1})\d+]],'$1')
205 end 206 end
206 207
207 function html(test_names,tests,results) %> 208 function M.html(test_names,tests,results) %>
208 <!DOCTYPE html> 209 <!DOCTYPE html>
209 <html lang="en"> 210 <html lang="en">
210 <head> 211 <head>
211 <title>A/B Test Results</title> 212 <title>A/B Test Results</title>
212 <style><% basic_style() %></style> 213 <style><% basic_style() %></style>
263 end 264 end
264 %> 265 %>
265 </body> 266 </body>
266 </html> 267 </html>
267 <% end 268 <% end
269
270 return M