class Laboratory::Adapters::RedisAdapter

Constants

ALL_EXPERIMENTS_KEYS_KEY

Attributes

redis[R]

Public Class Methods

new(url:) click to toggle source
# File lib/laboratory/adapters/redis_adapter.rb, line 8
def initialize(url:)
  @redis = Redis.new(url: url)
end

Public Instance Methods

delete(experiment_id) click to toggle source
# File lib/laboratory/adapters/redis_adapter.rb, line 43
def delete(experiment_id)
  key = redis_key(experiment_id: experiment_id)
  redis.del(key)

  # Remove from ALL_EXPERIMENTS_KEY_KEY
  experiment_ids = fetch_all_experiment_ids
  experiment_ids.delete(experiment_id)
  redis.set(ALL_EXPERIMENTS_KEYS_KEY, experiment_ids.to_json)
end
delete_all() click to toggle source
# File lib/laboratory/adapters/redis_adapter.rb, line 53
def delete_all
  experiment_ids = fetch_all_experiment_ids
  experiment_ids.each { |experiment_id| delete(experiment_id) }
end
read(experiment_id) click to toggle source
# File lib/laboratory/adapters/redis_adapter.rb, line 27
def read(experiment_id)
  key = redis_key(experiment_id: experiment_id)
  response = redis.get(key)

  return nil if response.nil?

  parse_json_to_experiment(JSON.parse(response))
end
read_all() click to toggle source
# File lib/laboratory/adapters/redis_adapter.rb, line 36
def read_all
  experiment_ids = fetch_all_experiment_ids
  experiment_ids.map do |experiment_id|
    read(experiment_id)
  end
end
write(experiment) click to toggle source
# File lib/laboratory/adapters/redis_adapter.rb, line 12
def write(experiment)
  key = redis_key(experiment_id: experiment.id)
  json = experiment_to_json(experiment)
  redis.set(key, json)

  # Write to ALL_EXPERIMENTS_KEYS_KEY if it isn't already there.
  experiment_ids = fetch_all_experiment_ids

  unless experiment_ids.include?(experiment.id)
    experiment_ids << experiment.id
  end

  redis.set(ALL_EXPERIMENTS_KEYS_KEY, experiment_ids.to_json)
end

Private Instance Methods

experiment_event_recordings_to_hash(event_recordings) click to toggle source
# File lib/laboratory/adapters/redis_adapter.rb, line 104
def experiment_event_recordings_to_hash(event_recordings)
  event_recordings.map do |event_recording|
    {
      user_id: event_recording.user_id,
      timestamp: event_recording.timestamp
    }
  end
end
experiment_events_to_hash(events) click to toggle source
# File lib/laboratory/adapters/redis_adapter.rb, line 93
def experiment_events_to_hash(events)
  events.map do |event|
    event_recordings =
      experiment_event_recordings_to_hash(event.event_recordings)
    {
      id: event.id,
      event_recordings: event_recordings
    }
  end
end
experiment_to_json(experiment) click to toggle source
# File lib/laboratory/adapters/redis_adapter.rb, line 73
def experiment_to_json(experiment)
  {
    id: experiment.id,
    algorithm: experiment.algorithm.id,
    variants: experiment_variants_to_hash(experiment.variants),
    changelog: experiment.changelog
  }.to_json
end
experiment_variants_to_hash(variants) click to toggle source
# File lib/laboratory/adapters/redis_adapter.rb, line 82
def experiment_variants_to_hash(variants)
  variants.map do |variant|
    {
      id: variant.id,
      percentage: variant.percentage,
      participant_ids: variant.participant_ids,
      events: experiment_events_to_hash(variant.events)
    }
  end
end
fetch_all_experiment_ids() click to toggle source
# File lib/laboratory/adapters/redis_adapter.rb, line 60
def fetch_all_experiment_ids
  response = redis.get(ALL_EXPERIMENTS_KEYS_KEY)
  if response
    JSON.parse(response)
  else
    []
  end
end
parse_json_to_experiment(json) click to toggle source
# File lib/laboratory/adapters/redis_adapter.rb, line 113
def parse_json_to_experiment(json)
  Experiment.new(
    id: json['id'],
    algorithm: Algorithms.to_class(json['algorithm']),
    variants: parse_json_to_experiment_variants(json['variants']),
    changelog: parse_json_to_experiment_changelog_items(json['changelog'])
  )
end
parse_json_to_experiment_changelog_items(changelog_json) click to toggle source
# File lib/laboratory/adapters/redis_adapter.rb, line 133
def parse_json_to_experiment_changelog_items(changelog_json)
  changelog_json.map do |json|
    Experiment::ChangelogItem.new(
      changes: json['changes'],
      timestamp: json['timestamp'],
      actor: json['actor']
    )
  end
end
parse_json_to_experiment_event_recordings(event_recordings_json) click to toggle source
# File lib/laboratory/adapters/redis_adapter.rb, line 156
def parse_json_to_experiment_event_recordings(event_recordings_json)
  event_recordings_json.map do |json|
    Experiment::Event::Recording.new(
      user_id: json['user_id'],
      timestamp: json['timestamp']
    )
  end
end
parse_json_to_experiment_events(events_json) click to toggle source
# File lib/laboratory/adapters/redis_adapter.rb, line 143
def parse_json_to_experiment_events(events_json)
  events_json.map do |json|
    event_recordings = parse_json_to_experiment_event_recordings(
      json['event_recordings']
    )

    Experiment::Event.new(
      id: json['id'],
      event_recordings: event_recordings
    )
  end
end
parse_json_to_experiment_variants(variants_json) click to toggle source
# File lib/laboratory/adapters/redis_adapter.rb, line 122
def parse_json_to_experiment_variants(variants_json)
  variants_json.map do |json|
    Experiment::Variant.new(
      id: json['id'],
      percentage: json['percentage'],
      participant_ids: json['participant_ids'],
      events: parse_json_to_experiment_events(json['events'])
    )
  end
end
redis_key(experiment_id:) click to toggle source
# File lib/laboratory/adapters/redis_adapter.rb, line 69
def redis_key(experiment_id:)
  "laboratory_#{experiment_id}"
end