|
| 1 | +# Copyright 2022, Optimizely |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | + |
| 14 | +from __future__ import annotations |
| 15 | + |
| 16 | +from typing import Optional, Any |
| 17 | + |
| 18 | +from optimizely import exceptions as optimizely_exception |
| 19 | +from optimizely import logger as optimizely_logger |
| 20 | +from optimizely.helpers.enums import Errors, OdpManagerConfig, OdpSegmentsCacheConfig |
| 21 | +from optimizely.helpers.validator import are_odp_data_types_valid |
| 22 | +from optimizely.odp.lru_cache import OptimizelySegmentsCache, LRUCache |
| 23 | +from optimizely.odp.odp_config import OdpConfig, OdpConfigState |
| 24 | +from optimizely.odp.odp_event_manager import OdpEventManager |
| 25 | +from optimizely.odp.odp_segment_manager import OdpSegmentManager |
| 26 | +from optimizely.odp.zaius_graphql_api_manager import ZaiusGraphQLApiManager |
| 27 | + |
| 28 | + |
| 29 | +class OdpManager: |
| 30 | + """Orchestrates segment manager, event manager and odp config.""" |
| 31 | + |
| 32 | + def __init__( |
| 33 | + self, |
| 34 | + disable: bool, |
| 35 | + segments_cache: Optional[OptimizelySegmentsCache] = None, |
| 36 | + segment_manager: Optional[OdpSegmentManager] = None, |
| 37 | + event_manager: Optional[OdpEventManager] = None, |
| 38 | + logger: Optional[optimizely_logger.Logger] = None |
| 39 | + ) -> None: |
| 40 | + |
| 41 | + self.enabled = not disable |
| 42 | + self.odp_config = OdpConfig() |
| 43 | + self.logger = logger or optimizely_logger.NoOpLogger() |
| 44 | + |
| 45 | + self.segment_manager = segment_manager |
| 46 | + self.event_manager = event_manager |
| 47 | + |
| 48 | + if not self.enabled: |
| 49 | + self.logger.info('ODP is disabled.') |
| 50 | + return |
| 51 | + |
| 52 | + if not self.segment_manager: |
| 53 | + if not segments_cache: |
| 54 | + segments_cache = LRUCache( |
| 55 | + OdpSegmentsCacheConfig.DEFAULT_CAPACITY, |
| 56 | + OdpSegmentsCacheConfig.DEFAULT_TIMEOUT_SECS |
| 57 | + ) |
| 58 | + self.segment_manager = OdpSegmentManager( |
| 59 | + self.odp_config, |
| 60 | + segments_cache, |
| 61 | + ZaiusGraphQLApiManager(logger), logger |
| 62 | + ) |
| 63 | + else: |
| 64 | + self.segment_manager.odp_config = self.odp_config |
| 65 | + |
| 66 | + if event_manager: |
| 67 | + event_manager.odp_config = self.odp_config |
| 68 | + self.event_manager = event_manager |
| 69 | + else: |
| 70 | + self.event_manager = OdpEventManager(self.odp_config, logger) |
| 71 | + |
| 72 | + self.event_manager.start() |
| 73 | + |
| 74 | + def fetch_qualified_segments(self, user_id: str, options: list[str]) -> Optional[list[str]]: |
| 75 | + if not self.enabled or not self.segment_manager: |
| 76 | + self.logger.error(Errors.ODP_NOT_ENABLED) |
| 77 | + return None |
| 78 | + |
| 79 | + user_key = OdpManagerConfig.KEY_FOR_USER_ID |
| 80 | + user_value = user_id |
| 81 | + |
| 82 | + return self.segment_manager.fetch_qualified_segments(user_key, user_value, options) |
| 83 | + |
| 84 | + def identify_user(self, user_id: str) -> None: |
| 85 | + if not self.enabled or not self.event_manager: |
| 86 | + self.logger.debug('ODP identify event is not dispatched (ODP disabled).') |
| 87 | + return |
| 88 | + if self.odp_config.odp_state() == OdpConfigState.NOT_INTEGRATED: |
| 89 | + self.logger.debug('ODP identify event is not dispatched (ODP not integrated).') |
| 90 | + return |
| 91 | + |
| 92 | + self.event_manager.identify_user(user_id) |
| 93 | + |
| 94 | + def send_event(self, type: str, action: str, identifiers: dict[str, str], data: dict[str, Any]) -> None: |
| 95 | + """ |
| 96 | + Send an event to the ODP server. |
| 97 | +
|
| 98 | + Args: |
| 99 | + type: The event type. |
| 100 | + action: The event action name. |
| 101 | + identifiers: A dictionary for identifiers. |
| 102 | + data: A dictionary for associated data. The default event data will be added to this data |
| 103 | + before sending to the ODP server. |
| 104 | +
|
| 105 | + Raises custom exception if error is detected. |
| 106 | + """ |
| 107 | + if not self.enabled or not self.event_manager: |
| 108 | + raise optimizely_exception.OdpNotEnabled(Errors.ODP_NOT_ENABLED) |
| 109 | + |
| 110 | + if self.odp_config.odp_state() == OdpConfigState.NOT_INTEGRATED: |
| 111 | + raise optimizely_exception.OdpNotIntegrated(Errors.ODP_NOT_INTEGRATED) |
| 112 | + |
| 113 | + if not are_odp_data_types_valid(data): |
| 114 | + raise optimizely_exception.OdpInvalidData(Errors.ODP_INVALID_DATA) |
| 115 | + |
| 116 | + self.event_manager.send_event(type, action, identifiers, data) |
| 117 | + |
| 118 | + def update_odp_config(self, api_key: Optional[str], api_host: Optional[str], |
| 119 | + segments_to_check: list[str]) -> None: |
| 120 | + if not self.enabled: |
| 121 | + return |
| 122 | + |
| 123 | + config_changed = self.odp_config.update(api_key, api_host, segments_to_check) |
| 124 | + if not config_changed: |
| 125 | + self.logger.debug('Odp config was not changed.') |
| 126 | + return |
| 127 | + |
| 128 | + # reset segments cache when odp integration or segments to check are changed |
| 129 | + if self.segment_manager: |
| 130 | + self.segment_manager.reset() |
| 131 | + |
| 132 | + if self.event_manager: |
| 133 | + self.event_manager.update_config() |
0 commit comments