Class: Optimizely::OdpManager

Inherits:
Object
  • Object
show all
Defined in:
lib/optimizely/odp/odp_manager.rb

Constant Summary collapse

ODP_LOGS =
Helpers::Constants::ODP_LOGS
ODP_MANAGER_CONFIG =
Helpers::Constants::ODP_MANAGER_CONFIG
ODP_CONFIG_STATE =
Helpers::Constants::ODP_CONFIG_STATE

Instance Method Summary collapse

Constructor Details

#initialize(disable:, segments_cache: nil, segment_manager: nil, event_manager: nil, fetch_segments_timeout: nil, odp_event_timeout: nil, odp_flush_interval: nil, logger: nil) ⇒ OdpManager

update_odp_config must be called to complete initialization



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/optimizely/odp/odp_manager.rb', line 35

def initialize(
  disable:,
  segments_cache: nil,
  segment_manager: nil,
  event_manager: nil,
  fetch_segments_timeout: nil,
  odp_event_timeout: nil,
  odp_flush_interval: nil,
  logger: nil
)
  @enabled = !disable
  @segment_manager = segment_manager
  @event_manager = event_manager
  @logger = logger || NoOpLogger.new
  @odp_config = OdpConfig.new

  unless @enabled
    @logger.log(Logger::INFO, ODP_LOGS[:ODP_NOT_ENABLED])
    return
  end

  unless @segment_manager
    segments_cache ||= LRUCache.new(
      Helpers::Constants::ODP_SEGMENTS_CACHE_CONFIG[:DEFAULT_CAPACITY],
      Helpers::Constants::ODP_SEGMENTS_CACHE_CONFIG[:DEFAULT_TIMEOUT_SECONDS]
    )
    @segment_manager = Optimizely::OdpSegmentManager.new(segments_cache, nil, @logger, timeout: fetch_segments_timeout)
  end

  @event_manager ||= Optimizely::OdpEventManager.new(logger: @logger, request_timeout: odp_event_timeout, flush_interval: odp_flush_interval)

  @segment_manager.odp_config = @odp_config
end

Instance Method Details

#fetch_qualified_segments(user_id:, options:) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/optimizely/odp/odp_manager.rb', line 69

def fetch_qualified_segments(user_id:, options:)
  # Returns qualified segments for the user from the cache or the ODP server if not in the cache.
  #
  # @param user_id - The user id.
  # @param options - An array of OptimizelySegmentOptions used to ignore and/or reset the cache.
  #
  # @return - Array of qualified segments or nil.
  options ||= []
  unless @enabled
    @logger.log(Logger::ERROR, ODP_LOGS[:ODP_NOT_ENABLED])
    return nil
  end

  if @odp_config.odp_state == ODP_CONFIG_STATE[:UNDETERMINED]
    @logger.log(Logger::ERROR, 'Cannot fetch segments before the datafile has loaded.')
    return nil
  end

  @segment_manager.fetch_qualified_segments(ODP_MANAGER_CONFIG[:KEY_FOR_USER_ID], user_id, options)
end

#identify_user(user_id:) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/optimizely/odp/odp_manager.rb', line 90

def identify_user(user_id:)
  unless @enabled
    @logger.log(Logger::DEBUG, 'ODP identify event is not dispatched (ODP disabled).')
    return
  end

  case @odp_config.odp_state
  when ODP_CONFIG_STATE[:UNDETERMINED]
    @logger.log(Logger::DEBUG, 'ODP identify event is not dispatched (datafile not ready).')
    return
  when ODP_CONFIG_STATE[:NOT_INTEGRATED]
    @logger.log(Logger::DEBUG, 'ODP identify event is not dispatched (ODP not integrated).')
    return
  end

  @event_manager.send_event(
    type: ODP_MANAGER_CONFIG[:EVENT_TYPE],
    action: 'identified',
    identifiers: {ODP_MANAGER_CONFIG[:KEY_FOR_USER_ID] => user_id},
    data: {}
  )
end

#send_event(type:, action:, identifiers:, data:) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/optimizely/odp/odp_manager.rb', line 113

def send_event(type:, action:, identifiers:, data:)
  # Send an event to the ODP server.
  #
  # @param type - the event type.
  # @param action - the event action name.
  # @param identifiers - a hash for identifiers.
  # @param data - a hash for associated data. The default event data will be added to this data before sending to the ODP server.
  unless @enabled
    @logger.log(Logger::ERROR, ODP_LOGS[:ODP_NOT_ENABLED])
    return
  end

  unless Helpers::Validator.odp_data_types_valid?(data)
    @logger.log(Logger::ERROR, ODP_LOGS[:ODP_INVALID_DATA])
    return
  end

  @event_manager.send_event(type: type, action: action, identifiers: identifiers, data: data)
end

#stop!Object



153
154
155
156
157
# File 'lib/optimizely/odp/odp_manager.rb', line 153

def stop!
  return unless @enabled

  @event_manager.stop!
end

#update_odp_config(api_key, api_host, segments_to_check) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/optimizely/odp/odp_manager.rb', line 133

def update_odp_config(api_key, api_host, segments_to_check)
  # Update the odp config, reset the cache and send signal to the event processor to update its config.
  # Start the event manager if odp is integrated.
  return unless @enabled

  config_changed = @odp_config.update(api_key, api_host, segments_to_check)
  unless config_changed
    @logger.log(Logger::DEBUG, 'Odp config was not changed.')
    return
  end

  @segment_manager.reset

  if @event_manager.running?
    @event_manager.update_config
  elsif @odp_config.odp_state == ODP_CONFIG_STATE[:INTEGRATED]
    @event_manager.start!(@odp_config)
  end
end