comparison src/luan/modules/lucene/Ab_testing.luan @ 1088:bae2d0c2576c

change module naming convention
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 26 Dec 2016 22:29:36 -0700
parents 1a68fc55a80c
children 5dbb552075ff
comparison
equal deleted inserted replaced
1087:4aab4dd3ac9c 1088:bae2d0c2576c
10 local Io = require "luan:Io.luan" 10 local Io = require "luan:Io.luan"
11 local Http = require "luan:http/Http.luan" 11 local Http = require "luan:http/Http.luan"
12 local Logging = require "luan:logging/Logging.luan" 12 local Logging = require "luan:logging/Logging.luan"
13 local Lucene = require "luan:lucene/Lucene.luan" 13 local Lucene = require "luan:lucene/Lucene.luan"
14 14
15 local M = {} 15 local Ab_testing = {}
16 16
17 local logger = Logging.logger "Ab_testing" 17 local logger = Logging.logger "Ab_testing"
18 18
19 function M.of(index) 19 function Ab_testing.of(index)
20 20
21 local ab_testing = {} 21 local ab_testing = {}
22 22
23 ab_testing.test_map = {} 23 ab_testing.test_map = {}
24 24
92 event = events[i] 92 event = events[i]
93 fancy[event] = {} 93 fancy[event] = {}
94 for value, count in pairs(results[event]) do 94 for value, count in pairs(results[event]) do
95 fancy[event][value] = {} 95 fancy[event][value] = {}
96 fancy[event][value].count = count 96 fancy[event][value].count = count
97 fancy[event][value].pct_of_total = M.percent(count,all[value]) 97 fancy[event][value].pct_of_total = Ab_testing.percent(count,all[value])
98 fancy[event][value].pct_of_prev = M.percent(count,prev[value]) 98 fancy[event][value].pct_of_prev = Ab_testing.percent(count,prev[value])
99 end 99 end
100 prev = results[event] 100 prev = results[event]
101 end 101 end
102 return fancy 102 return fancy
103 end 103 end
141 local test = ab_testing.test_map[name] 141 local test = ab_testing.test_map[name]
142 test or error("test not found: "..name) 142 test or error("test not found: "..name)
143 results[name] = test.fancy_results() 143 results[name] = test.fancy_results()
144 end 144 end
145 Io.stdout = Http.response.text_writer() 145 Io.stdout = Http.response.text_writer()
146 M.html(test_names,ab_testing.test_map,results) 146 Ab_testing.html(test_names,ab_testing.test_map,results)
147 end 147 end
148 end 148 end
149 149
150 return ab_testing 150 return ab_testing
151 end 151 end
152 152
153 153
154 -- aggregator factories 154 -- aggregator factories
155 155
156 -- fn(doc) should return boolean whether doc should be counted 156 -- fn(doc) should return boolean whether doc should be counted
157 function M.count(fn) 157 function Ab_testing.count(fn)
158 return function() 158 return function()
159 local aggregator = {} 159 local aggregator = {}
160 aggregator.result = 0 160 aggregator.result = 0
161 function aggregator.aggregate(doc) 161 function aggregator.aggregate(doc)
162 if fn(doc) then 162 if fn(doc) then
165 end 165 end
166 return aggregator 166 return aggregator
167 end 167 end
168 end 168 end
169 169
170 M.count_all = M.count( function(doc) return true end ) 170 Ab_testing.count_all = Ab_testing.count( function(doc) return true end )
171 171
172 -- fn(doc) should return number to add to result, return 0 for nothing 172 -- fn(doc) should return number to add to result, return 0 for nothing
173 function M.sum(fn) 173 function Ab_testing.sum(fn)
174 return function() 174 return function()
175 local aggregator = {} 175 local aggregator = {}
176 aggregator.result = 0 176 aggregator.result = 0
177 function aggregator.aggregate(doc) 177 function aggregator.aggregate(doc)
178 aggregator.result = aggregator.result + fn(doc) 178 aggregator.result = aggregator.result + fn(doc)
181 end 181 end
182 end 182 end
183 183
184 184
185 185
186 function M.percent(x,total) 186 function Ab_testing.percent(x,total)
187 if total==0 then 187 if total==0 then
188 return 0 188 return 0
189 else 189 else
190 return 100 * x / total 190 return 100 * x / total
191 end 191 end
205 local function format(v) 205 local function format(v)
206 v = v .. '' 206 v = v .. ''
207 return gsub( v, [[(\d+\.\d{1})\d+]], '$1' ) 207 return gsub( v, [[(\d+\.\d{1})\d+]], '$1' )
208 end 208 end
209 209
210 function M.html(test_names,tests,results) %> 210 function Ab_testing.html(test_names,tests,results) %>
211 <!DOCTYPE html> 211 <!DOCTYPE html>
212 <html lang="en"> 212 <html lang="en">
213 <head> 213 <head>
214 <title>A/B Test Results</title> 214 <title>A/B Test Results</title>
215 <style><% basic_style() %></style> 215 <style><% basic_style() %></style>
267 %> 267 %>
268 </body> 268 </body>
269 </html> 269 </html>
270 <% end 270 <% end
271 271
272 return M 272 return Ab_testing