Skip to content

Commit 3579f5d

Browse files
feat: add odp event manager (#312)
* add odp event manager
1 parent c574289 commit 3579f5d

File tree

7 files changed

+875
-17
lines changed

7 files changed

+875
-17
lines changed

lib/optimizely/helpers/constants.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,8 @@ module Constants
385385
ODP_LOGS = {
386386
FETCH_SEGMENTS_FAILED: 'Audience segments fetch failed (%s).',
387387
ODP_EVENT_FAILED: 'ODP event send failed (%s).',
388-
ODP_NOT_ENABLED: 'ODP is not enabled.'
388+
ODP_NOT_ENABLED: 'ODP is not enabled.',
389+
ODP_NOT_INTEGRATED: 'ODP is not integrated.'
389390
}.freeze
390391

391392
DECISION_NOTIFICATION_TYPES = {
@@ -424,6 +425,19 @@ module Constants
424425
REQUEST_TIMEOUT: 10
425426
}.freeze
426427

428+
ODP_CONFIG_STATE = {
429+
UNDETERMINED: 'UNDETERMINED',
430+
INTEGRATED: 'INTEGRATED',
431+
NOT_INTEGRATED: 'NOT_INTEGRATED'
432+
}.freeze
433+
434+
ODP_EVENT_MANAGER = {
435+
DEFAULT_QUEUE_CAPACITY: 10_000,
436+
DEFAULT_BATCH_SIZE: 10,
437+
DEFAULT_FLUSH_INTERVAL_SECONDS: 1,
438+
DEFAULT_RETRY_COUNT: 3
439+
}.freeze
440+
427441
HTTP_HEADERS = {
428442
'IF_MODIFIED_SINCE' => 'If-Modified-Since',
429443
'LAST_MODIFIED' => 'Last-Modified'

lib/optimizely/helpers/validator.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@ def finite_number?(value)
178178

179179
value.is_a?(Numeric) && value.to_f.finite? && value.abs <= Constants::FINITE_NUMBER_LIMIT
180180
end
181+
182+
def odp_data_types_valid?(data)
183+
valid_types = [String, Float, Integer, TrueClass, FalseClass, NilClass]
184+
data.values.all? { |e| valid_types.member? e.class }
185+
end
181186
end
182187
end
183188
end

lib/optimizely/odp/odp_config.rb

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
#
1818

1919
require 'optimizely/logger'
20+
require_relative '../helpers/constants'
2021

2122
module Optimizely
2223
class OdpConfig
24+
ODP_CONFIG_STATE = Helpers::Constants::ODP_CONFIG_STATE
2325
# Contains configuration used for ODP integration.
2426
#
2527
# @param api_host - The host URL for the ODP audience segments API (optional).
@@ -30,6 +32,7 @@ def initialize(api_key = nil, api_host = nil, segments_to_check = [])
3032
@api_host = api_host
3133
@segments_to_check = segments_to_check
3234
@mutex = Mutex.new
35+
@odp_state = @api_host.nil? || @api_key.nil? ? ODP_CONFIG_STATE[:UNDETERMINED] : ODP_CONFIG_STATE[:INTEGRATED]
3336
end
3437

3538
# Replaces the existing configuration
@@ -41,14 +44,19 @@ def initialize(api_key = nil, api_host = nil, segments_to_check = [])
4144
# @return - True if the provided values were different than the existing values.
4245

4346
def update(api_key = nil, api_host = nil, segments_to_check = [])
47+
updated = false
4448
@mutex.synchronize do
45-
break false if @api_key == api_key && @api_host == api_host && @segments_to_check == segments_to_check
46-
47-
@api_key = api_key
48-
@api_host = api_host
49-
@segments_to_check = segments_to_check
50-
break true
49+
@odp_state = api_host.nil? || api_key.nil? ? ODP_CONFIG_STATE[:NOT_INTEGRATED] : ODP_CONFIG_STATE[:INTEGRATED]
50+
51+
if @api_key != api_key || @api_host != api_host || @segments_to_check != segments_to_check
52+
@api_key = api_key
53+
@api_host = api_host
54+
@segments_to_check = segments_to_check
55+
updated = true
56+
end
5157
end
58+
59+
updated
5260
end
5361

5462
# Returns the api host for odp connections
@@ -99,12 +107,12 @@ def segments_to_check=(segments_to_check)
99107
@mutex.synchronize { @segments_to_check = segments_to_check.clone }
100108
end
101109

102-
# Returns True if odp is integrated
110+
# Returns the state of odp integration (UNDETERMINED, INTEGRATED, NOT_INTEGRATED)
103111
#
104-
# @return - bool
112+
# @return - string
105113

106-
def odp_integrated?
107-
@mutex.synchronize { !@api_key.nil? && !@api_host.nil? }
114+
def odp_state
115+
@mutex.synchronize { @odp_state }
108116
end
109117
end
110118
end

lib/optimizely/odp/odp_event.rb

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# frozen_string_literal: true
2+
3+
#
4+
# Copyright 2022, Optimizely and contributors
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
19+
require 'json'
20+
21+
module Optimizely
22+
class OdpEvent
23+
# Representation of an odp event which can be sent to the Optimizely odp platform.
24+
def initialize(type:, action:, identifiers:, data:)
25+
@type = type
26+
@action = action
27+
@identifiers = identifiers
28+
@data = add_common_event_data(data)
29+
end
30+
31+
def add_common_event_data(custom_data)
32+
data = {
33+
idempotence_id: SecureRandom.uuid,
34+
data_source_type: 'sdk',
35+
data_source: 'ruby-sdk',
36+
data_source_version: VERSION
37+
}
38+
data.update(custom_data)
39+
data
40+
end
41+
42+
def to_json(*_args)
43+
{
44+
type: @type,
45+
action: @action,
46+
identifiers: @identifiers,
47+
data: @data
48+
}.to_json
49+
end
50+
51+
def ==(other)
52+
to_json == other.to_json
53+
end
54+
end
55+
end

0 commit comments

Comments
 (0)