/
select-expt.js
52 lines (45 loc) · 1.12 KB
/
select-expt.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
'use strict';
function selectExpt(expts) {
expts = expts.filter(function(expt) {
var acType = typeof expt.ac;
return acType === 'undefined' ||
acType === 'function' && expt.ac() || acType !== 'function' && expt.ac;
});
if (!expts.length) {
return null;
}
if (expts.length === 1) {
return expts[0];
}
var allocatedTraffic = 0;
var exptWoAllocation = [];
expts.forEach(function(expt) {
if (typeof expt.traffic === 'number') {
allocatedTraffic += expt.traffic;
} else {
exptWoAllocation.push(expt);
}
});
if (exptWoAllocation.length) {
var avgRest = allocatedTraffic >= 1 ?
0 : (1 - allocatedTraffic) / exptWoAllocation.length;
exptWoAllocation.forEach(function(expt) {
expt.traffic = avgRest;
});
}
var epsilon = Math.random();
var cursor = 0;
for (var i in expts) {
var expt = expts[i];
if (!expt.traffic) {
continue;
}
var next = cursor + expt.traffic;
if (epsilon >= cursor && epsilon < next || expt.traffic === 1) {
return expt;
}
cursor = next;
}
return null;
}
module.exports = selectExpt;