-
Notifications
You must be signed in to change notification settings - Fork 28
feat: add odp manager #314
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 all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
707f0f0
remove odp_config from event manager constructor
andrewleap-optimizely 1a82033
remove odp_config from segment manager constructor
andrewleap-optimizely aed8336
add odp manager
andrewleap-optimizely fe4bbd5
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
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
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,144 @@ | ||
# 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 '../helpers/constants' | ||
require_relative '../helpers/validator' | ||
require_relative '../exceptions' | ||
require_relative 'odp_config' | ||
require_relative 'lru_cache' | ||
require_relative 'odp_segment_manager' | ||
require_relative 'odp_event_manager' | ||
|
||
module Optimizely | ||
class OdpManager | ||
ODP_LOGS = Helpers::Constants::ODP_LOGS | ||
ODP_MANAGER_CONFIG = Helpers::Constants::ODP_MANAGER_CONFIG | ||
ODP_CONFIG_STATE = Helpers::Constants::ODP_CONFIG_STATE | ||
|
||
def initialize(disable:, segments_cache: nil, segment_manager: nil, event_manager: 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) | ||
end | ||
|
||
@event_manager ||= Optimizely::OdpEventManager.new(logger: @logger) | ||
|
||
@segment_manager.odp_config = @odp_config | ||
@event_manager.start!(@odp_config) | ||
end | ||
|
||
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 | ||
|
||
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 | ||
|
||
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 | ||
|
||
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. | ||
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 | ||
@event_manager.update_config | ||
end | ||
|
||
def close! | ||
return unless @enabled | ||
|
||
@event_manager.stop! | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great! I am just curios who will call this function? should we add a notification listener here and make odpmanager self update itself? Or are we expecting to do something like that and trigger updates from the outside by calling this method?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea the idea is to register the listener at client initialization and have the callback be a client method, thus avoiding having to pass in the
NotificationListener
and theProjectConfig
. Seems to make sense because the callback needs access to bothProjectConfig
andOdpConfig
. Thoughts/opinions?