Class: Bandit::BaseStorage
- Inherits:
-
Object
- Object
- Bandit::BaseStorage
- Defined in:
- lib/bandit/storage/base.rb
Direct Known Subclasses
Class Method Summary collapse
Instance Method Summary collapse
-
#alt_started_key(experiment, alternative) ⇒ Object
key for alternative start.
- #alternative_start_time(experiment, alternative) ⇒ Object
-
#clear! ⇒ Object
clear all stored values.
-
#conv_key(exp, alt, date_hour = nil) ⇒ Object
if date_hour is nil, create key for total otherwise, create key for hourly based.
-
#conversion_count(experiment, alternative, date_hour = nil) ⇒ Object
if date_hour isn’t specified, get total count if date_hour is specified, return count for DateHour.
-
#get(key, default = 0) ⇒ Object
get key if exists, otherwise 0.
-
#incr(key, count) ⇒ Object
increment key by count.
- #incr_conversions(experiment, alternative, count = 1, date_hour = nil) ⇒ Object
- #incr_participants(experiment, alternative, count = 1, date_hour = nil) ⇒ Object
-
#init(key, value) ⇒ Object
initialize key if not set.
- #make_key(parts) ⇒ Object
-
#part_key(exp, alt, date_hour = nil) ⇒ Object
if date_hour is nil, create key for total otherwise, create key for hourly based.
-
#participant_count(experiment, alternative, date_hour = nil) ⇒ Object
if date_hour isn’t specified, get total count if date_hour is specified, return count for DateHour.
- #player_state_get(experiment, player, name) ⇒ Object
- #player_state_key(exp, player, varname) ⇒ Object
- #player_state_set(experiment, player, name, value) ⇒ Object
-
#set(key, value) ⇒ Object
set key with value, regardless of whether it is set or not.
- #with_failure_grace(fail_default = 0) ⇒ Object
Class Method Details
.get_storage(name, config) ⇒ Object
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/bandit/storage/base.rb', line 17 def self.get_storage(name, config) config ||= {} case name when :memory then MemoryStorage.new(config) when :memcache then MemCacheStorage.new(config) when :redis then RedisStorage.new(config) else raise UnknownStorageEngineError, "#{name} not a known storage method" end end |
Instance Method Details
#alt_started_key(experiment, alternative) ⇒ Object
key for alternative start
104 105 106 |
# File 'lib/bandit/storage/base.rb', line 104 def alt_started_key(experiment, alternative) make_key [ "altstarted", experiment.name, alternative ] end |
#alternative_start_time(experiment, alternative) ⇒ Object
90 91 92 93 |
# File 'lib/bandit/storage/base.rb', line 90 def alternative_start_time(experiment, alternative) secs = get alt_started_key(experiment, alternative), nil secs.nil? ? nil : Time.at(secs).to_date_hour end |
#clear! ⇒ Object
clear all stored values
49 50 51 |
# File 'lib/bandit/storage/base.rb', line 49 def clear! raise NotImplementedError end |
#conv_key(exp, alt, date_hour = nil) ⇒ Object
if date_hour is nil, create key for total otherwise, create key for hourly based
110 111 112 113 114 |
# File 'lib/bandit/storage/base.rb', line 110 def conv_key(exp, alt, date_hour=nil) parts = [ "conversions", exp.name, alt ] parts += [ date_hour.date, date_hour.hour ] unless date_hour.nil? make_key parts end |
#conversion_count(experiment, alternative, date_hour = nil) ⇒ Object
if date_hour isn’t specified, get total count if date_hour is specified, return count for DateHour
78 79 80 |
# File 'lib/bandit/storage/base.rb', line 78 def conversion_count(experiment, alternative, date_hour=nil) get conv_key(experiment, alternative, date_hour) end |
#get(key, default = 0) ⇒ Object
get key if exists, otherwise 0
39 40 41 |
# File 'lib/bandit/storage/base.rb', line 39 def get(key, default=0) raise NotImplementedError end |
#incr(key, count) ⇒ Object
increment key by count
29 30 31 |
# File 'lib/bandit/storage/base.rb', line 29 def incr(key, count) raise NotImplementedError end |
#incr_conversions(experiment, alternative, count = 1, date_hour = nil) ⇒ Object
64 65 66 67 68 |
# File 'lib/bandit/storage/base.rb', line 64 def incr_conversions(experiment, alternative, count=1, date_hour=nil) # increment total count and per hour count incr conv_key(experiment, alternative), count incr conv_key(experiment, alternative, date_hour || DateHour.now), count end |
#incr_participants(experiment, alternative, count = 1, date_hour = nil) ⇒ Object
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/bandit/storage/base.rb', line 53 def incr_participants(experiment, alternative, count=1, date_hour=nil) date_hour ||= DateHour.now # initialize first start time for alternative if we haven't inited yet init alt_started_key(experiment, alternative), date_hour.to_i # increment total count and per hour count incr part_key(experiment, alternative), count incr part_key(experiment, alternative, date_hour), count end |
#init(key, value) ⇒ Object
initialize key if not set
34 35 36 |
# File 'lib/bandit/storage/base.rb', line 34 def init(key, value) raise NotImplementedError end |
#make_key(parts) ⇒ Object
120 121 122 |
# File 'lib/bandit/storage/base.rb', line 120 def make_key(parts) parts.join(":") end |
#part_key(exp, alt, date_hour = nil) ⇒ Object
if date_hour is nil, create key for total otherwise, create key for hourly based
97 98 99 100 101 |
# File 'lib/bandit/storage/base.rb', line 97 def part_key(exp, alt, date_hour=nil) parts = [ "participants", exp.name, alt ] parts += [ date_hour.date, date_hour.hour ] unless date_hour.nil? make_key parts end |
#participant_count(experiment, alternative, date_hour = nil) ⇒ Object
if date_hour isn’t specified, get total count if date_hour is specified, return count for DateHour
72 73 74 |
# File 'lib/bandit/storage/base.rb', line 72 def participant_count(experiment, alternative, date_hour=nil) get part_key(experiment, alternative, date_hour) end |
#player_state_get(experiment, player, name) ⇒ Object
86 87 88 |
# File 'lib/bandit/storage/base.rb', line 86 def player_state_get(experiment, player, name) get player_state_key(experiment, player, name), nil end |
#player_state_key(exp, player, varname) ⇒ Object
116 117 118 |
# File 'lib/bandit/storage/base.rb', line 116 def player_state_key(exp, player, varname) make_key [ "state", exp.name, player.name, varname ] end |
#player_state_set(experiment, player, name, value) ⇒ Object
82 83 84 |
# File 'lib/bandit/storage/base.rb', line 82 def player_state_set(experiment, player, name, value) set player_state_key(experiment, player, name), value end |
#set(key, value) ⇒ Object
set key with value, regardless of whether it is set or not
44 45 46 |
# File 'lib/bandit/storage/base.rb', line 44 def set(key, value) raise NotImplementedError end |
#with_failure_grace(fail_default = 0) ⇒ Object
124 125 126 127 128 129 130 131 132 |
# File 'lib/bandit/storage/base.rb', line 124 def with_failure_grace(fail_default=0) begin yield rescue Bandit.storage_failed! Rails.logger.error "Storage method #{self.class} failed. Falling back to memory storage." fail_default end end |