|
| 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 'optimizely/logger' |
| 20 | +require_relative '../helpers/constants' |
| 21 | +require_relative '../helpers/validator' |
| 22 | +require_relative '../exceptions' |
| 23 | +require_relative 'odp_config' |
| 24 | +require_relative 'lru_cache' |
| 25 | +require_relative 'odp_segment_manager' |
| 26 | +require_relative 'odp_event_manager' |
| 27 | + |
| 28 | +module Optimizely |
| 29 | + class OdpManager |
| 30 | + ODP_LOGS = Helpers::Constants::ODP_LOGS |
| 31 | + ODP_MANAGER_CONFIG = Helpers::Constants::ODP_MANAGER_CONFIG |
| 32 | + ODP_CONFIG_STATE = Helpers::Constants::ODP_CONFIG_STATE |
| 33 | + |
| 34 | + def initialize(disable:, segments_cache: nil, segment_manager: nil, event_manager: nil, logger: nil) |
| 35 | + @enabled = !disable |
| 36 | + @segment_manager = segment_manager |
| 37 | + @event_manager = event_manager |
| 38 | + @logger = logger || NoOpLogger.new |
| 39 | + @odp_config = OdpConfig.new |
| 40 | + |
| 41 | + unless @enabled |
| 42 | + @logger.log(Logger::INFO, ODP_LOGS[:ODP_NOT_ENABLED]) |
| 43 | + return |
| 44 | + end |
| 45 | + |
| 46 | + unless @segment_manager |
| 47 | + segments_cache ||= LRUCache.new( |
| 48 | + Helpers::Constants::ODP_SEGMENTS_CACHE_CONFIG[:DEFAULT_CAPACITY], |
| 49 | + Helpers::Constants::ODP_SEGMENTS_CACHE_CONFIG[:DEFAULT_TIMEOUT_SECONDS] |
| 50 | + ) |
| 51 | + @segment_manager = Optimizely::OdpSegmentManager.new(segments_cache, nil, @logger) |
| 52 | + end |
| 53 | + |
| 54 | + @event_manager ||= Optimizely::OdpEventManager.new(logger: @logger) |
| 55 | + |
| 56 | + @segment_manager.odp_config = @odp_config |
| 57 | + @event_manager.start!(@odp_config) |
| 58 | + end |
| 59 | + |
| 60 | + def fetch_qualified_segments(user_id:, options:) |
| 61 | + # Returns qualified segments for the user from the cache or the ODP server if not in the cache. |
| 62 | + # |
| 63 | + # @param user_id - The user id. |
| 64 | + # @param options - An array of OptimizelySegmentOptions used to ignore and/or reset the cache. |
| 65 | + # |
| 66 | + # @return - Array of qualified segments or nil. |
| 67 | + options ||= [] |
| 68 | + unless @enabled |
| 69 | + @logger.log(Logger::ERROR, ODP_LOGS[:ODP_NOT_ENABLED]) |
| 70 | + return nil |
| 71 | + end |
| 72 | + |
| 73 | + if @odp_config.odp_state == ODP_CONFIG_STATE[:UNDETERMINED] |
| 74 | + @logger.log(Logger::ERROR, 'Cannot fetch segments before the datafile has loaded.') |
| 75 | + return nil |
| 76 | + end |
| 77 | + |
| 78 | + @segment_manager.fetch_qualified_segments(ODP_MANAGER_CONFIG[:KEY_FOR_USER_ID], user_id, options) |
| 79 | + end |
| 80 | + |
| 81 | + def identify_user(user_id:) |
| 82 | + unless @enabled |
| 83 | + @logger.log(Logger::DEBUG, 'ODP identify event is not dispatched (ODP disabled).') |
| 84 | + return |
| 85 | + end |
| 86 | + |
| 87 | + case @odp_config.odp_state |
| 88 | + when ODP_CONFIG_STATE[:UNDETERMINED] |
| 89 | + @logger.log(Logger::DEBUG, 'ODP identify event is not dispatched (datafile not ready).') |
| 90 | + return |
| 91 | + when ODP_CONFIG_STATE[:NOT_INTEGRATED] |
| 92 | + @logger.log(Logger::DEBUG, 'ODP identify event is not dispatched (ODP not integrated).') |
| 93 | + return |
| 94 | + end |
| 95 | + |
| 96 | + @event_manager.send_event( |
| 97 | + type: ODP_MANAGER_CONFIG[:EVENT_TYPE], |
| 98 | + action: 'identified', |
| 99 | + identifiers: {ODP_MANAGER_CONFIG[:KEY_FOR_USER_ID] => user_id}, |
| 100 | + data: {} |
| 101 | + ) |
| 102 | + end |
| 103 | + |
| 104 | + def send_event(type:, action:, identifiers:, data:) |
| 105 | + # Send an event to the ODP server. |
| 106 | + # |
| 107 | + # @param type - the event type. |
| 108 | + # @param action - the event action name. |
| 109 | + # @param identifiers - a hash for identifiers. |
| 110 | + # @param data - a hash for associated data. The default event data will be added to this data before sending to the ODP server. |
| 111 | + unless @enabled |
| 112 | + @logger.log(Logger::ERROR, ODP_LOGS[:ODP_NOT_ENABLED]) |
| 113 | + return |
| 114 | + end |
| 115 | + |
| 116 | + unless Helpers::Validator.odp_data_types_valid?(data) |
| 117 | + @logger.log(Logger::ERROR, ODP_LOGS[:ODP_INVALID_DATA]) |
| 118 | + return |
| 119 | + end |
| 120 | + |
| 121 | + @event_manager.send_event(type: type, action: action, identifiers: identifiers, data: data) |
| 122 | + end |
| 123 | + |
| 124 | + def update_odp_config(api_key, api_host, segments_to_check) |
| 125 | + # Update the odp config, reset the cache and send signal to the event processor to update its config. |
| 126 | + return unless @enabled |
| 127 | + |
| 128 | + config_changed = @odp_config.update(api_key, api_host, segments_to_check) |
| 129 | + unless config_changed |
| 130 | + @logger.log(Logger::DEBUG, 'Odp config was not changed.') |
| 131 | + return |
| 132 | + end |
| 133 | + |
| 134 | + @segment_manager.reset |
| 135 | + @event_manager.update_config |
| 136 | + end |
| 137 | + |
| 138 | + def close! |
| 139 | + return unless @enabled |
| 140 | + |
| 141 | + @event_manager.stop! |
| 142 | + end |
| 143 | + end |
| 144 | +end |
0 commit comments