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.



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

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.



18
19
20
# File 'lib/kameleoon/data/manager/visitor_manager.rb', line 18

def expiration_period
  @expiration_period
end

Instance Method Details

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



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

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|
    data = args[i]
    case data
    when CustomData
      data = process_custom_data(cdi, visitor_code, visitor, data)
    when Conversion
      data = process_conversion(data, cdi)
    end
    args[i] = data
  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



67
68
69
# File 'lib/kameleoon/data/manager/visitor_manager.rb', line 67

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

#get_or_create_visitor(visitor_code) ⇒ Object



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

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



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

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



71
72
73
# File 'lib/kameleoon/data/manager/visitor_manager.rb', line 71

def size
  @visitors.size
end

#stopObject



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

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