Class: WhereIsWaldo::Adapters::DatabaseAdapter

Inherits:
BaseAdapter
  • Object
show all
Defined in:
app/services/where_is_waldo/adapters/database_adapter.rb

Instance Method Summary collapse

Instance Method Details

#cleanup(timeout: nil) ⇒ Object



86
87
88
89
# File 'app/services/where_is_waldo/adapters/database_adapter.rb', line 86

def cleanup(timeout: nil)
  threshold = (timeout || default_timeout).seconds.ago
  Presence.where(last_heartbeat: ...threshold).delete_all
end

#connect(session_id:, subject_id:, metadata: {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/services/where_is_waldo/adapters/database_adapter.rb', line 6

def connect(session_id:, subject_id:, metadata: {})
  now = Time.current

  attrs = {
    session_column => session_id,
    subject_column => subject_id,
    connected_at: now,
    last_heartbeat: now,
    tab_visible: true,
    subject_active: true,
    last_activity: now,
    metadata: ,
    created_at: now,
    updated_at: now
  }

  # rubocop:disable Rails/SkipsModelValidations -- intentional for performance
  Presence.upsert(attrs, unique_by: session_column)
  # rubocop:enable Rails/SkipsModelValidations
  true
rescue StandardError => e
  Rails.logger.error "[WhereIsWaldo] Connect failed: #{e.message}"
  false
end

#disconnect(session_id: nil, subject_id: nil) ⇒ Object



31
32
33
34
35
36
37
38
# File 'app/services/where_is_waldo/adapters/database_adapter.rb', line 31

def disconnect(session_id: nil, subject_id: nil)
  scope = build_lookup_scope(session_id: session_id, subject_id: subject_id)
  scope.delete_all
  true
rescue StandardError => e
  Rails.logger.error "[WhereIsWaldo] Disconnect failed: #{e.message}"
  false
end

#heartbeat(session_id:, tab_visible: true, subject_active: true, last_activity_at: nil, metadata: {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/services/where_is_waldo/adapters/database_adapter.rb', line 40

def heartbeat(session_id:, tab_visible: true, subject_active: true, last_activity_at: nil, metadata: {})
  now = Time.current
  updates = {
    last_heartbeat: now,
    tab_visible: tab_visible,
    subject_active: subject_active,
    updated_at: now
  }
  # Update last_activity: use JS timestamp if provided, otherwise use current time when active
  if last_activity_at
    updates[:last_activity] = Time.zone.at(last_activity_at / 1000.0)
  elsif subject_active
    updates[:last_activity] = now
  end
  updates[:metadata] =  if .present?

  scope = Presence.where(session_column => session_id)

  # rubocop:disable Rails/SkipsModelValidations -- intentional for performance
  scope.update_all(updates).positive?
  # rubocop:enable Rails/SkipsModelValidations
rescue StandardError => e
  Rails.logger.error "[WhereIsWaldo] Heartbeat failed: #{e.message}"
  false
end

#online_subject_ids(timeout: nil) ⇒ Object



66
67
68
69
70
71
72
# File 'app/services/where_is_waldo/adapters/database_adapter.rb', line 66

def online_subject_ids(timeout: nil)
  threshold = (timeout || default_timeout).seconds.ago

  Presence.where("last_heartbeat > ?", threshold)
          .distinct
          .pluck(subject_column)
end

#session_status(session_id) ⇒ Object



80
81
82
83
84
# File 'app/services/where_is_waldo/adapters/database_adapter.rb', line 80

def session_status(session_id)
  scope = Presence.where(session_column => session_id)
  scope = scope.includes(:subject) if config.subject_class_constant
  scope.first&.as_presence_hash
end

#sessions_for_subject(subject_id) ⇒ Object



74
75
76
77
78
# File 'app/services/where_is_waldo/adapters/database_adapter.rb', line 74

def sessions_for_subject(subject_id)
  scope = Presence.where(subject_column => subject_id)
  scope = scope.includes(:subject) if config.subject_class_constant
  scope.map(&:as_presence_hash)
end