-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
13 changed files
with
351 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,29 @@ | ||
module Unleash | ||
class ActivationStrategy | ||
attr_accessor :name, :params | ||
attr_accessor :name, :params, :constraints | ||
|
||
def initialize(name, params) | ||
def initialize(name, params, constraints = []) | ||
self.name = name | ||
|
||
if params.is_a?(Hash) | ||
self.params = params | ||
elsif params.nil? | ||
self.params = {} | ||
else | ||
Unleash.logger.warn "Invalid params provided for ActivationStrategy #{params}" | ||
Unleash.logger.warn "Invalid params provided for ActivationStrategy (params:#{params})" | ||
self.params = {} | ||
end | ||
|
||
if constraints.is_a?(Array) && constraints.each{ |c| c.is_a?(Constraint) } | ||
self.constraints = constraints | ||
else | ||
Unleash.logger.warn "Invalid constraints provided for ActivationStrategy (contraints: #{constraints})" | ||
self.constraints = [] | ||
end | ||
end | ||
|
||
def matches_context?(context) | ||
self.constraints.any?{ |c| c.matches_context? context } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module Unleash | ||
class Constraint | ||
attr_accessor :context_name, :operator, :values | ||
|
||
VALID_OPERATORS = ['IN', 'NOT_IN'].freeze | ||
|
||
def initialize(context_name, operator, values = []) | ||
raise ArgumentError, "context_name is not a String" unless context_name.is_a?(String) | ||
raise ArgumentError, "operator does not hold a valid value:" + VALID_OPERATORS unless VALID_OPERATORS.include? operator | ||
raise ArgumentError, "values does not hold an Array" unless values.is_a?(Array) | ||
|
||
self.context_name = context_name | ||
self.operator = operator | ||
self.values = values | ||
end | ||
|
||
def matches_context?(context) | ||
Unleash.logger.debug "Unleash::Constraint matches_context? values: #{self.values} context.get_by_name(#{self.context_name})" \ | ||
" #{context.get_by_name(self.context_name)} " | ||
|
||
is_included = self.values.include? context.get_by_name(self.context_name) | ||
|
||
operator == 'IN' ? is_included : !is_included | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
require 'unleash/strategy/util' | ||
|
||
module Unleash | ||
module Strategy | ||
class FlexibleRollout < Base | ||
def name | ||
'flexibleRollout' | ||
end | ||
|
||
# need: params['percentage'] | ||
def is_enabled?(params = {}, context = nil) | ||
return false unless params.is_a?(Hash) | ||
return false unless context.class.name == 'Unleash::Context' | ||
|
||
stickiness = params.fetch('stickiness', 'default') | ||
stickiness_id = resolve_stickiness(stickiness, context) | ||
|
||
begin | ||
percentage = Integer(params.fetch('rollout', 0)) | ||
percentage = 0 if percentage > 100 || percentage.negative? | ||
rescue ArgumentError | ||
return false | ||
end | ||
|
||
group_id = params.fetch('groupId', '') | ||
normalized_user_id = Util.get_normalized_number(stickiness_id, group_id) | ||
|
||
return false if stickiness_id.nil? | ||
|
||
(percentage.positive? && normalized_user_id <= percentage) | ||
end | ||
|
||
private | ||
|
||
def random | ||
Random.rand(0..100) | ||
# (rand() * 100).to_s | ||
end | ||
|
||
def resolve_stickiness(stickiness, context) | ||
case stickiness | ||
when 'userId' | ||
context.user_id | ||
when 'sessionId' | ||
context.session_id | ||
when 'random' | ||
random | ||
when 'default' | ||
context.user_id || context.session_id || random | ||
else | ||
nil | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.