Class: Bandit::BaseStorage

Inherits:
Object
  • Object
show all
Defined in:
lib/bandit/storage/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_storage(name, config) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# 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 :dalli then DalliStorage.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



105
106
107
# File 'lib/bandit/storage/base.rb', line 105

def alt_started_key(experiment, alternative)
  make_key [ "altstarted", experiment.name, alternative ]
end

#alternative_start_time(experiment, alternative) ⇒ Object



91
92
93
94
# File 'lib/bandit/storage/base.rb', line 91

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

Raises:

  • (NotImplementedError)


50
51
52
# File 'lib/bandit/storage/base.rb', line 50

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



111
112
113
114
115
# File 'lib/bandit/storage/base.rb', line 111

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



79
80
81
# File 'lib/bandit/storage/base.rb', line 79

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

Raises:

  • (NotImplementedError)


40
41
42
# File 'lib/bandit/storage/base.rb', line 40

def get(key, default=0)
  raise NotImplementedError
end

#incr(key, count) ⇒ Object

increment key by count

Raises:

  • (NotImplementedError)


30
31
32
# File 'lib/bandit/storage/base.rb', line 30

def incr(key, count)
  raise NotImplementedError
end

#incr_conversions(experiment, alternative, count = 1, date_hour = nil) ⇒ Object



65
66
67
68
69
# File 'lib/bandit/storage/base.rb', line 65

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



54
55
56
57
58
59
60
61
62
63
# File 'lib/bandit/storage/base.rb', line 54

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

Raises:

  • (NotImplementedError)


35
36
37
# File 'lib/bandit/storage/base.rb', line 35

def init(key, value)
  raise NotImplementedError
end

#make_key(parts) ⇒ Object



121
122
123
# File 'lib/bandit/storage/base.rb', line 121

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



98
99
100
101
102
# File 'lib/bandit/storage/base.rb', line 98

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



73
74
75
# File 'lib/bandit/storage/base.rb', line 73

def participant_count(experiment, alternative, date_hour=nil)
  get part_key(experiment, alternative, date_hour)
end

#player_state_get(experiment, player, name) ⇒ Object



87
88
89
# File 'lib/bandit/storage/base.rb', line 87

def player_state_get(experiment, player, name)
  get player_state_key(experiment, player, name), nil
end

#player_state_key(exp, player, varname) ⇒ Object



117
118
119
# File 'lib/bandit/storage/base.rb', line 117

def player_state_key(exp, player, varname)
  make_key [ "state", exp.name, player.name, varname ]
end

#player_state_set(experiment, player, name, value) ⇒ Object



83
84
85
# File 'lib/bandit/storage/base.rb', line 83

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

Raises:

  • (NotImplementedError)


45
46
47
# File 'lib/bandit/storage/base.rb', line 45

def set(key, value)
  raise NotImplementedError
end

#with_failure_grace(fail_default = 0) ⇒ Object



125
126
127
128
129
130
131
132
133
# File 'lib/bandit/storage/base.rb', line 125

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