-
Notifications
You must be signed in to change notification settings - Fork 36
feat: add odp manager #405
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 17 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
f97f940
bump up py version in gitactions to 3.10
Mat001 a26e459
merge conflict resolve
Mat001 d2ed75e
Merge branch 'master' of github.com:optimizely/python-sdk
Mat001 77dfec4
feat: add first draft of odp_manager
Mat001 05b0d70
add tests
Mat001 e1a2de2
use unittest style of asserting exception
Mat001 2bb6c32
remove import
Mat001 32167fe
make tests more concise
Mat001 19492f9
fixed some pr comments
Mat001 baff0c4
de-indent event manager init
Mat001 0789c7c
add update config event manager signal
andrewleap-optimizely 4b5989b
Merge branch 'mpirnovar/odp_manager' of github.com:optimizely/python-…
Mat001 4b0b338
updated tests
Mat001 ec96869
commit odp_manager updates and cleanup
Mat001 e53eaa3
addres PR comments
Mat001 7c7535b
refactored tests for fetch segments
Mat001 0a2be6d
update cache default timeout
Mat001 de18508
fix pypy failed test
andrewleap-optimizely 6b7385f
hardcode cache settings
andrewleap-optimizely 18fbf64
assert error log not called
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
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,133 @@ | ||
# Copyright 2022, Optimizely | ||
# 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. | ||
|
||
from __future__ import annotations | ||
|
||
from typing import Optional, Any | ||
|
||
from optimizely import exceptions as optimizely_exception | ||
from optimizely import logger as optimizely_logger | ||
from optimizely.helpers.enums import Errors, OdpManagerConfig, OdpSegmentsCacheConfig | ||
from optimizely.helpers.validator import are_odp_data_types_valid | ||
from optimizely.odp.lru_cache import OptimizelySegmentsCache, LRUCache | ||
from optimizely.odp.odp_config import OdpConfig, OdpConfigState | ||
from optimizely.odp.odp_event_manager import OdpEventManager | ||
from optimizely.odp.odp_segment_manager import OdpSegmentManager | ||
from optimizely.odp.zaius_graphql_api_manager import ZaiusGraphQLApiManager | ||
|
||
|
||
class OdpManager: | ||
"""Orchestrates segment manager, event manager and odp config.""" | ||
|
||
def __init__( | ||
self, | ||
disable: bool, | ||
segments_cache: Optional[OptimizelySegmentsCache] = None, | ||
segment_manager: Optional[OdpSegmentManager] = None, | ||
event_manager: Optional[OdpEventManager] = None, | ||
logger: Optional[optimizely_logger.Logger] = None | ||
) -> None: | ||
|
||
self.enabled = not disable | ||
self.odp_config = OdpConfig() | ||
self.logger = logger or optimizely_logger.NoOpLogger() | ||
|
||
self.segment_manager = segment_manager | ||
self.event_manager = event_manager | ||
|
||
if not self.enabled: | ||
self.logger.info('ODP is disabled.') | ||
return | ||
|
||
if not self.segment_manager: | ||
if not segments_cache: | ||
segments_cache = LRUCache( | ||
OdpSegmentsCacheConfig.DEFAULT_CAPACITY, | ||
OdpSegmentsCacheConfig.DEFAULT_TIMEOUT_SECS | ||
) | ||
self.segment_manager = OdpSegmentManager( | ||
self.odp_config, | ||
segments_cache, | ||
ZaiusGraphQLApiManager(logger), logger | ||
) | ||
else: | ||
self.segment_manager.odp_config = self.odp_config | ||
|
||
if event_manager: | ||
event_manager.odp_config = self.odp_config | ||
self.event_manager = event_manager | ||
else: | ||
self.event_manager = OdpEventManager(self.odp_config, logger) | ||
|
||
self.event_manager.start() | ||
|
||
def fetch_qualified_segments(self, user_id: str, options: list[str]) -> Optional[list[str]]: | ||
if not self.enabled or not self.segment_manager: | ||
self.logger.error(Errors.ODP_NOT_ENABLED) | ||
return None | ||
|
||
user_key = OdpManagerConfig.KEY_FOR_USER_ID | ||
user_value = user_id | ||
|
||
return self.segment_manager.fetch_qualified_segments(user_key, user_value, options) | ||
|
||
def identify_user(self, user_id: str) -> None: | ||
if not self.enabled or not self.event_manager: | ||
self.logger.debug('ODP identify event is not dispatched (ODP disabled).') | ||
return | ||
if self.odp_config.odp_state() == OdpConfigState.NOT_INTEGRATED: | ||
self.logger.debug('ODP identify event is not dispatched (ODP not integrated).') | ||
return | ||
|
||
self.event_manager.identify_user(user_id) | ||
|
||
def send_event(self, type: str, action: str, identifiers: dict[str, str], data: dict[str, Any]) -> None: | ||
""" | ||
Send an event to the ODP server. | ||
|
||
Args: | ||
type: The event type. | ||
action: The event action name. | ||
identifiers: A dictionary for identifiers. | ||
data: A dictionary for associated data. The default event data will be added to this data | ||
before sending to the ODP server. | ||
|
||
Raises custom exception if error is detected. | ||
""" | ||
if not self.enabled or not self.event_manager: | ||
raise optimizely_exception.OdpNotEnabled(Errors.ODP_NOT_ENABLED) | ||
|
||
if self.odp_config.odp_state() == OdpConfigState.NOT_INTEGRATED: | ||
raise optimizely_exception.OdpNotIntegrated(Errors.ODP_NOT_INTEGRATED) | ||
|
||
if not are_odp_data_types_valid(data): | ||
raise optimizely_exception.OdpInvalidData(Errors.ODP_INVALID_DATA) | ||
|
||
self.event_manager.send_event(type, action, identifiers, data) | ||
|
||
def update_odp_config(self, api_key: Optional[str], api_host: Optional[str], | ||
segments_to_check: list[str]) -> None: | ||
if not self.enabled: | ||
return | ||
|
||
config_changed = self.odp_config.update(api_key, api_host, segments_to_check) | ||
if not config_changed: | ||
self.logger.debug('Odp config was not changed.') | ||
return | ||
|
||
# reset segments cache when odp integration or segments to check are changed | ||
if self.segment_manager: | ||
self.segment_manager.reset() | ||
|
||
if self.event_manager: | ||
self.event_manager.update_config() |
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
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.
Line 107-114, send_event raises multiple exceptions. They should be processed separately at the top level. We can discuss again when integrated.