lib/context.rb in absmartly-sdk-0.1.2 vs lib/context.rb in absmartly-sdk-1.0.5

- old
+ new

@@ -1,25 +1,26 @@ # frozen_string_literal: true require_relative "hashing" require_relative "variant_assigner" +require_relative "context_event_logger" require_relative "json/unit" require_relative "json/attribute" require_relative "json/exposure" require_relative "json/publish_event" require_relative "json/goal_achievement" class Context attr_reader :data, :pending_count - def self.create(clock, config, scheduler, data_future, data_provider, + def self.create(clock, config, data_future, data_provider, event_handler, event_logger, variable_parser, audience_matcher) - Context.new(clock, config, scheduler, data_future, data_provider, + Context.new(clock, config, data_future, data_provider, event_handler, event_logger, variable_parser, audience_matcher) end - def initialize(clock, config, scheduler, data_future, data_provider, + def initialize(clock, config, data_future, data_provider, event_handler, event_logger, variable_parser, audience_matcher) @index = [] @achievements = [] @assignment_cache = {} @assignments = {} @@ -29,11 +30,10 @@ @event_handler = event_handler @event_logger = !config.event_logger.nil? ? config.event_logger : event_logger @data_provider = data_provider @variable_parser = variable_parser @audience_matcher = audience_matcher - @scheduler = scheduler @closed = false @units = {} @attributes = [] @overrides = {} @@ -47,12 +47,14 @@ set_attributes(config.attributes) if config.attributes set_overrides(config.overrides) if config.overrides set_custom_assignments(config.custom_assignments) if config.custom_assignments if data_future.success? assign_data(data_future.data_future) + log_event(ContextEventLogger::EVENT_TYPE::READY, data_future.data_future) else set_data_failed(data_future.exception) + log_error(data_future.exception) end end def ready? !@data.nil? @@ -155,24 +157,25 @@ def queue_exposure(assignment) unless assignment.exposed assignment.exposed = true exposure = Exposure.new - exposure.id = assignment.id + exposure.id = assignment.id || 0 exposure.name = assignment.name exposure.unit = assignment.unit_type exposure.variant = assignment.variant - exposure.exposed_at = @clock + exposure.exposed_at = @clock.to_i exposure.assigned = assignment.assigned exposure.eligible = assignment.eligible exposure.overridden = assignment.overridden exposure.full_on = assignment.full_on exposure.custom = assignment.custom exposure.audience_mismatch = assignment.audience_mismatch @pending_count += 1 @exposures.push(exposure) + log_event(ContextEventLogger::EVENT_TYPE::EXPOSURE, exposure) end end def peek_treatment(experiment_name) check_ready?(true) @@ -219,10 +222,11 @@ achievement.name = goal_name achievement.properties = properties @pending_count += 1 @achievements.push(achievement) + log_event(ContextEventLogger::EVENT_TYPE::GOAL, achievement) end def publish check_not_closed? @@ -234,22 +238,25 @@ unless @failed data_future = @data_provider.context_data if data_future.success? assign_data(data_future.data_future) + log_event(ContextEventLogger::EVENT_TYPE::REFRESH, data_future.data_future) else set_data_failed(data_future.exception) + log_error(data_future.exception) end end end def close unless @closed if @pending_count > 0 flush end @closed = true + log_event(ContextEventLogger::EVENT_TYPE::CLOSE, nil) end end def data check_ready?(true) @@ -280,22 +287,24 @@ event = PublishEvent.new event.hashed = true event.published_at = @clock.to_i event.units = @units.map do |key, value| - Unit.new(key, unit_hash(key, value)) + Unit.new(key.to_s, unit_hash(key, value)) end - event.attributes = @attributes.empty? ? nil : @attributes event.exposures = exposures - event.goals = achievements - return @event_handler.publish(self, event) + event.attributes = @attributes unless @attributes.empty? + event.goals = achievements unless achievements.nil? + log_event(ContextEventLogger::EVENT_TYPE::PUBLISH, event) + @event_handler.publish(self, event) end end else @exposures = [] @achievements = [] @pending_count = 0 + @data_failed end end def check_not_closed? if @closed @@ -363,11 +372,11 @@ attrs = @attributes.inject({}) do |hash, attr| hash[attr.name] = attr.value hash end match = @audience_matcher.evaluate(experiment.data.audience, attrs) - if match.nil? || !match.result + if match && !match.result assignment.audience_mismatch = true end end if experiment.data.audience_strict && assignment.audience_mismatch @@ -469,17 +478,28 @@ @index_variables = {} @data = ContextData.new @failed = true end + def log_event(event, data) + unless @event_logger.nil? + @event_logger.handle_event(event, data) + end + end + + def log_error(error) + unless @event_logger.nil? + @event_logger.handle_event(ContextEventLogger::EVENT_TYPE::ERROR, error.message) + end + end + attr_accessor :clock, :publish_delay, :event_handler, :event_logger, :data_provider, :variable_parser, :audience_matcher, - :scheduler, :units, :failed, :data_lock, :index, :index_variables,