-
Notifications
You must be signed in to change notification settings - Fork 28
feat: add odp segment manager #310
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
49458ba
add odp segment manager
andrewleap-optimizely 0f74578
return nil on graphql error
andrewleap-optimizely 48d5b02
add odp config
andrewleap-optimizely a96ade4
cleanup tests
andrewleap-optimizely f1dfef8
address comments
andrewleap-optimizely File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
# frozen_string_literal: true | ||
|
||
# | ||
# Copyright 2022, Optimizely and contributors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
require 'optimizely/logger' | ||
|
||
module Optimizely | ||
class OdpConfig | ||
# Contains configuration used for ODP integration. | ||
# | ||
# @param api_host - The host URL for the ODP audience segments API (optional). If not provided, SDK will use the default host in datafile. | ||
# @param api_key - The public API key for the ODP account from which the audience segments will be fetched (optional). If not provided, SDK will use the default publicKey in datafile. | ||
andrewleap-optimizely marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# @param segments_to_check - An array of all ODP segments used in the current datafile (associated with api_host/api_key). | ||
# @param odp_enabled - A boolean value indicating whether odp is enabled for the project or not. | ||
def initialize(api_key = nil, api_host = nil, segments_to_check = []) | ||
@api_key = api_key | ||
@api_host = api_host | ||
@segments_to_check = segments_to_check | ||
@odp_enabled = !api_key.nil? && !api_host.nil? ? true : false | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We'll have a similar flag (odpEnabled) in the OdpManager level, which disable all ODP functions including segments and events. This may be conflict with that. We can clean it up here and visit again at the top level. |
||
@mutex = Mutex.new | ||
end | ||
|
||
# Replaces the existing configuration | ||
# | ||
# @param api_host - The host URL for the ODP audience segments API (optional). If not provided, SDK will use the default host in datafile. | ||
andrewleap-optimizely marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# @param api_key - The public API key for the ODP account from which the audience segments will be fetched (optional). If not provided, SDK will use the default publicKey in datafile. | ||
andrewleap-optimizely marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# @param segments_to_check - An array of all ODP segments used in the current datafile (associated with api_host/api_key). | ||
# | ||
# @return - True if the provided values were different than the existing values. | ||
|
||
def update(api_key = nil, api_host = nil, segments_to_check = []) | ||
@mutex.synchronize do | ||
@odp_enabled = !api_key.nil? && !api_host.nil? ? true : false | ||
andrewleap-optimizely marked this conversation as resolved.
Show resolved
Hide resolved
andrewleap-optimizely marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return false if @api_key == api_key && @api_host == api_host && @segments_to_check == segments_to_check | ||
andrewleap-optimizely marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@api_key = api_key | ||
@api_host = api_host | ||
@segments_to_check = segments_to_check | ||
true | ||
end | ||
end | ||
|
||
# Returns the api host for odp connections | ||
# | ||
# @return - The api host. | ||
< 8000 /td> | ||
def api_host | ||
@mutex.synchronize { @api_host.clone } | ||
end | ||
|
||
# Returns the api host for odp connections | ||
# | ||
# @return - The api host. | ||
|
||
def api_host=(api_host) | ||
@mutex.synchronize { @api_host = api_host.clone } | ||
end | ||
|
||
# Returns the api key for odp connections | ||
# | ||
# @return - The api key. | ||
|
||
def api_key | ||
@mutex.synchronize { @api_key.clone } | ||
end | ||
|
||
# Replace the api key with the provided string | ||
# | ||
# @param api_key - An api key | ||
|
||
def api_key=(api_key) | ||
@mutex.synchronize { @api_key = api_key.clone } | ||
end | ||
|
||
# Returns An array of qualified segments for this user | ||
# | ||
# @return - An array of segments names. | ||
|
||
def segments_to_check | ||
@mutex.synchronize { @segments_to_check.clone } | ||
end | ||
|
||
# Replace qualified segments with provided segments | ||
# | ||
# @param segments - An array of segment names | ||
|
||
def segments_to_check=(segments_to_check) | ||
@mutex.synchronize { @segments_to_check = segments_to_check.clone } | ||
end | ||
|
||
# Returns True if odp is enabled and ready | ||
# | ||
# @return - bool | ||
|
||
def odp_enabled | ||
@mutex.synchronize { @odp_enabled.clone } | ||
end | ||
|
||
# Enable/disable the odp integration | ||
# | ||
# @param odp_enabled - bool | ||
|
||
def odp_enabled=(odp_enabled) | ||
@mutex.synchronize { @odp_enabled = odp_enabled.clone } | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
# frozen_string_literal: true | ||
|
||
# | ||
# Copyright 2022, Optimizely and contributors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
require 'optimizely/logger' | ||
require_relative 'zaius_graphql_api_manager' | ||
|
||
module Optimizely | ||
class OdpSegmentManager | ||
# Schedules connections to ODP for audience segmentation and caches the results | ||
attr_reader :odp_config, :segments_cache, :zaius_manager, :logger | ||
|
||
def initialize(cache_size, cache_timeout_in_secs, odp_config, api_manager = nil, logger = nil, proxy_config = nil) | ||
@odp_config = odp_config | ||
@logger = logger || NoOpLogger.new | ||
@zaius_manager = api_manager || ZaiusGraphQLApiManager.new(logger: @logger, proxy_config: proxy_config) | ||
@segments_cache = Optimizely::LRUCache.new(cache_size, cache_timeout_in_secs) | ||
andrewleap-optimizely marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
|
||
def fetch_qualified_segments(segment_request) | ||
odp_api_key = @odp_config&.api_key | ||
andrewleap-optimizely marked this conversation as resolved.
Show resolved
Hide resolved
|
||
odp_api_host = @odp_config&.api_host | ||
|
||
unless odp_api_host && odp_api_key | ||
@logger.log(Logger::ERROR, 'api_key/api_host not defined') | ||
andrewleap-optimizely marked this conversation as resolved.
Show resolved
Hide resolved
|
||
segment_request.segments = nil | ||
return | ||
end | ||
segments_to_check = @odp_config&.segments_to_check | ||
|
||
unless segments_to_check&.size&.positive? | ||
segment_request.segments = [] | ||
andrewleap-optimizely marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return | ||
end | ||
|
||
cache_key = make_cache_key(segment_request.user_key, segment_request.user_value) | ||
|
||
ignore_cache = segment_request.options.include?(OptimizelySegmentOption::IGNORE_CACHE) | ||
reset_cache = segment_request.options.include?(OptimizelySegmentOption::RESET_CACHE) | ||
|
||
reset if reset_cache | ||
|
||
unless ignore_cache || reset_cache | ||
segments = @segments_cache.lookup(cache_key) | ||
unless segments.nil? | ||
segment_request.segments = segments | ||
return | ||
end | ||
8000 end | ||
|
||
Thread.new do | ||
segments = @zaius_manager.fetch_segments(odp_api_key, odp_api_host, segment_request.user_key, segment_request.user_value, segments_to_check) | ||
@segments_cache.save(cache_key, segments) unless segments.nil? || ignore_cache | ||
segment_request.segments = segments | ||
end | ||
andrewleap-optimizely marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
nil | ||
end | ||
|
||
def reset | ||
@segments_cache.reset | ||
nil | ||
end | ||
|
||
private | ||
|
||
def make_cache_key(user_key, user_value) | ||
"#{user_key}-$-#{user_value}" | ||
end | ||
end | ||
|
||
class OptimizelySegmentOption | ||
IGNORE_CACHE = :IGNORE_CACHE | ||
RESET_CACHE = :RESET_CACHE | ||
end | ||
|
||
class OdpSegmentRequest | ||
# Allows asynchronous communication between OptimizelyUserContext and OdpSegmentManger | ||
attr_reader :user_key, :user_value, :options | ||
|
||
def initialize(user_key, user_value, options) | ||
@user_key = user_key | ||
@user_value = user_value | ||
@options = options | ||
@queue = Thread::SizedQueue.new(1) | ||
@segments = nil | ||
end | ||
|
||
# If this method is called without a corresponding call to segments=, it will wait indefinitely | ||
def wait_for_segments | ||
return @segments if @queue.closed? | ||
|
||
@segments = @queue.pop | ||
@queue.close | ||
@segments | ||
end | ||
|
||
def segments=(segments) | ||
if @queue.closed? | ||
@segments = segments | ||
return | ||
end | ||
@queue.push(segments, non_block: true) | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.