Class: Kameleoon::DataManager::VisitorManager

Inherits:
Object
  • Object
show all
Defined in:
lib/kameleoon/data/manager/visitor_manager.rb

Overview

VisitorManager stores Visitor instances and manages visitor sessions. It is thread-safe except ‘stop` method

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_manager, expiration_period, scheduler) ⇒ VisitorManager

Returns a new instance of VisitorManager.



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/kameleoon/data/manager/visitor_manager.rb', line 18

def initialize(data_manager, expiration_period, scheduler)
  Logging::KameleoonLogger.debug(
    'CALL: VisitorManager.new(data_manager, expiration_period: %s, scheduler)', expiration_period
  )
  @data_manager = data_manager
  @expiration_period = expiration_period
  @scheduler = scheduler
  @visitors = Concurrent::Map.new
  start
  Logging::KameleoonLogger.debug(
    'RETURN: VisitorManager.new(data_manager, expiration_period: %s, scheduler)', expiration_period
  )
end

Instance Attribute Details

#expiration_periodObject (readonly)

Returns the value of attribute expiration_period.



16
17
18
# File 'lib/kameleoon/data/manager/visitor_manager.rb', line 16

def expiration_period
  @expiration_period
end

Instance Method Details

#add_data(visitor_code, *args, overwrite: true) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/kameleoon/data/manager/visitor_manager.rb', line 73

def add_data(visitor_code, *args, overwrite: true)
  Logging::KameleoonLogger.debug("CALL: VisitorManager.add_data(visitor_code: '%s', args: %s, overwrite: %s)",
                                 visitor_code, args, overwrite)
  visitor = get_or_create_visitor(visitor_code)
  cdi = @data_manager.data_file.custom_data_info
  args.size.times do |i|
    custom_data = args[i]
    next unless custom_data.is_a?(Kameleoon::CustomData)

    # We shouldn't send custom data with local only type
    custom_data.mark_as_sent if cdi.local_only?(custom_data.id)
    # If mappingIdentifier is passed, we should link anonymous visitor with real unique userId.
    # After authorization, customer must be able to continue work with userId, but hash for variation
    # should be calculated based on anonymous visitor code, that's why set MappingIdentifier to visitor.
    if Configuration::CustomDataInfo.mapping_identifier?(cdi, custom_data)
      visitor.mapping_identifier = visitor_code
      user_id = custom_data.values[0]
      args[i] = MappingIdentifier.new(custom_data)
      if visitor_code != user_id
        @visitors[user_id] = visitor.clone
        Logging::KameleoonLogger.info(-> { "Linked anonymous visitor '#{visitor_code}' with user '#{user_id}'" })
      end
    end
  end
  visitor.add_data(*args, overwrite: overwrite)
  Logging::KameleoonLogger.debug(
    "RETURN: VisitorManager.add_data(visitor_code: '%s', args: %s, overwrite: %s) -> (visitor)",
    visitor_code, args, overwrite
  )
  visitor
end

#enumerate(&blk) ⇒ Object



65
66
67
# File 'lib/kameleoon/data/manager/visitor_manager.rb', line 65

def enumerate(&blk)
  @visitors.each_pair { |vc, v| blk.call(vc, v) }
end

#get_or_create_visitor(visitor_code) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/kameleoon/data/manager/visitor_manager.rb', line 50

def get_or_create_visitor(visitor_code)
  Logging::KameleoonLogger.debug("CALL: VisitorManager.get_or_create_visitor(visitor_code: '%s')",
                                 visitor_code)
  visitor = @visitors.compute(visitor_code) do |former_v|
    next DataManager::Visitor.new if former_v.nil?

    former_v.update_last_activity_time
    former_v
  end
  Logging::KameleoonLogger.debug(
    "RETURN: VisitorManager.get_or_create_visitor(visitor_code: '%s') -> (visitor)", visitor_code
  )
  visitor
end

#get_visitor(visitor_code) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/kameleoon/data/manager/visitor_manager.rb', line 39

def get_visitor(visitor_code)
  Logging::KameleoonLogger.debug("CALL: VisitorManager.get_visitor(visitor_code: '%s')", visitor_code)
  visitor = @visitors.compute_if_present(visitor_code) do |visitor|
    visitor.update_last_activity_time
    visitor
  end
  Logging::KameleoonLogger.debug("RETURN: VisitorManager.get_visitor(visitor_code: '%s') -> (visitor: %s)",
                        visitor_code, visitor)
  visitor
end

#sizeObject



69
70
71
# File 'lib/kameleoon/data/manager/visitor_manager.rb', line 69

def size
  @visitors.size
end

#stopObject



32
33
34
35
36
37
# File 'lib/kameleoon/data/manager/visitor_manager.rb', line 32

def stop
  Logging::KameleoonLogger.debug('CALL: VisitorManager.stop')
  @purge_job&.unschedule
  @purge_job = nil
  Logging::KameleoonLogger.debug('RETURN: VisitorManager.stop')
end