Class: Statsig::UserPersistentStorageUtils

Inherits:
Object
  • Object
show all
Defined in:
lib/user_persistent_storage_utils.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ UserPersistentStorageUtils

Returns a new instance of UserPersistentStorageUtils.



10
11
12
13
# File 'lib/user_persistent_storage_utils.rb', line 10

def initialize(options)
  @storage = options.user_persistent_storage
  @cache = {}
end

Instance Attribute Details

#cacheObject

Returns the value of attribute cache.



6
7
8
# File 'lib/user_persistent_storage_utils.rb', line 6

def cache
  @cache
end

#storageObject

Returns the value of attribute storage.



8
9
10
# File 'lib/user_persistent_storage_utils.rb', line 8

def storage
  @storage
end

Class Method Details

.get_storage_key(user, id_type) ⇒ Object



97
98
99
# File 'lib/user_persistent_storage_utils.rb', line 97

def self.get_storage_key(user, id_type)
  "#{user.get_unit_id(id_type)}:#{id_type}"
end

.parse(values_string) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/user_persistent_storage_utils.rb', line 77

def self.parse(values_string)
  return nil if values_string.nil?

  parsed = JSON.parse(values_string)
  return nil if parsed.nil?

  parsed.each do |config_name, config_value|
    if config_value.is_a?(Hash) && config_value.key?('json_value')
      config_value['json_value'] = symbolize_keys(config_value['json_value'])
    end
  end
  parsed
end

.stringify(values_object) ⇒ Object



91
92
93
94
95
# File 'lib/user_persistent_storage_utils.rb', line 91

def self.stringify(values_object)
  return JSON.generate(values_object)
rescue StandardError
  return nil
end

.symbolize_keys(hash) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/user_persistent_storage_utils.rb', line 101

def self.symbolize_keys(hash)
  return hash unless hash.is_a?(Hash)

  symbolized = {}
  hash.each do |key, value|
    symbolized[key.to_sym] = value.is_a?(Hash) ? symbolize_keys(value) : value
  end
  symbolized
end

Instance Method Details

#add_evaluation_to_user_persisted_values(user_persisted_values, config_name, evaluation) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/user_persistent_storage_utils.rb', line 64

def add_evaluation_to_user_persisted_values(user_persisted_values, config_name, evaluation)
  if user_persisted_values.nil?
    user_persisted_values = {}
  end
  hash = evaluation.to_hash
  if hash['json_value'].is_a?(Hash)
    hash['json_value'] = self.class.symbolize_keys(hash['json_value'])
  end
  user_persisted_values[config_name] = hash
end

#get_user_persisted_values(user, id_type) ⇒ Object



15
16
17
18
19
20
# File 'lib/user_persistent_storage_utils.rb', line 15

def get_user_persisted_values(user, id_type)
  key = self.class.get_storage_key(user, id_type)
  return @cache[key] unless @cache[key].nil?

  return load_from_storage(key)
end

#load_from_storage(key) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/user_persistent_storage_utils.rb', line 22

def load_from_storage(key)
  return if @storage.nil?

  begin
    storage_values = @storage.load(key)
  rescue StandardError => e
    puts "Failed to load key (#{key}) from user_persisted_storage (#{e.message})"
    return nil
  end

  unless storage_values.nil?
    parsed_values = self.class.parse(storage_values)
    unless parsed_values.nil?
      @cache[key] = parsed_values
      return @cache[key]
    end
  end
  return nil
end

#remove_experiment_from_storage(user, id_type, config_name) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/user_persistent_storage_utils.rb', line 56

def remove_experiment_from_storage(user, id_type, config_name)
  persisted_values = get_user_persisted_values(user, id_type)
  unless persisted_values.nil?
    persisted_values.delete(config_name)
    save_to_storage(user, id_type, persisted_values)
  end
end

#save_to_storage(user, id_type, user_persisted_values) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/user_persistent_storage_utils.rb', line 42

def save_to_storage(user, id_type, user_persisted_values)
  return if @storage.nil?

  key = self.class.get_storage_key(user, id_type)
  stringified = self.class.stringify(user_persisted_values)
  return if stringified.nil?

  begin
    @storage.save(key, stringified)
  rescue StandardError => e
    puts "Failed to save key (#{key}) to user_persisted_storage (#{e.message})"
  end
end