-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ability to allocate traffic for variant by weights #20
Changes from all commits
f21b9f5
6191bb3
d872f31
ccb25aa
3a829e0
4227b75
ac29ff2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,17 @@ class Utils | |
return Math.random() unless seed | ||
# a MUCH simplified version inspired by PlanOut.js | ||
parseInt(@sha1(seed).substr(0, 13), 16) / 0xFFFFFFFFFFFFF | ||
@checkWeights: (variants) -> | ||
contains_weight = [] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would personally write this as variants_with_weights = variants.filter((variant) -> variant.weight).length
variants_with_weights == variants.length
|
||
contains_weight.push(value.weight?) for key, value of variants | ||
contains_weight.every (has_weight) -> has_weight | ||
@sumWeights: (variants) -> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would probably express this as variants.reduce ((total, variant) -> total + (variant.weight || 0)), 0 it's slightly more dense, but it's "just" a sum of weights... |
||
sum = 0 | ||
for key, value of variants | ||
sum += value.weight if value.weight? | ||
sum | ||
@validateWeights: (variants) -> | ||
contains_weight = [] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. again, it's a matter of personal preference, but I would find it clearer to write something like variants_with_weights = variants.filter((variant) -> variant.weight).length
return true if variants_with_weights == 0 || variants.length == variants_with_weights
false (then you can also re-use |
||
contains_weight.push(value.weight?) for key, value of variants | ||
contains_weight.every (has_weight) -> has_weight or contains_weight.every (has_weight) -> !has_weight | ||
module.exports = Utils |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
test = require('tape') | ||
sinon = require('sinon') | ||
_ = require('lodash') | ||
Utils = require('../src/utils') | ||
|
||
setup = -> | ||
utils = new Utils(); | ||
|
||
describe = (description, fn) -> | ||
test description, (t) -> | ||
setup() | ||
fn(t) | ||
|
||
describe 'utils - checkWeights', (t) -> | ||
variants = { | ||
a: | ||
weight: 50 | ||
b: | ||
weight: 50 | ||
} | ||
all_has_weight = Utils.checkWeights(variants); | ||
variants = { | ||
a: {} | ||
b: {} | ||
} | ||
none_has_weight = Utils.checkWeights(variants); | ||
variants = { | ||
a: | ||
weight: 20 | ||
b: {} | ||
} | ||
some_has_weight = Utils.checkWeights(variants); | ||
t.plan(3) | ||
t.assert(all_has_weight == true, 'all variants contains weight') | ||
t.assert(none_has_weight == false, 'variants does not contain weights') | ||
t.assert(some_has_weight == false, 'only some variants does not have weight') | ||
|
||
describe 'utils - sumWeights', (t) -> | ||
variants = { | ||
a: | ||
weight: 55 | ||
b: | ||
weight: 35 | ||
} | ||
sum = Utils.sumWeights(variants); | ||
t.plan(1) | ||
t.assert(sum == 90, 'sum should be equal 90') | ||
|
||
describe 'utils - validateWeights', (t) -> | ||
variants = { | ||
a: | ||
weight: 55 | ||
b: | ||
weight: 35 | ||
} | ||
all_valid = Utils.validateWeights(variants); | ||
variants = { | ||
a: | ||
weight: 55 | ||
b: {} | ||
} | ||
some_valid = Utils.validateWeights(variants); | ||
variants = { | ||
a: {} | ||
b: {} | ||
} | ||
all_not_have_weight = Utils.validateWeights(variants); | ||
t.plan(3) | ||
t.assert(all_valid == true, 'all should be valid') | ||
t.assert(some_valid == false, 'only some are valid') | ||
t.assert(all_not_have_weight == true, 'all does not contain weight but are valid') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why
+ 1
here?