Class: Trainspotter::SessionRecord

Inherits:
Record
  • Object
show all
Defined in:
app/models/trainspotter/session_record.rb

Constant Summary

Constants inherited from Record

Record::SCHEMA_VERSION

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Record

ensure_connected, reset_connection!

Class Method Details

.expire_before(cutoff, log_file:) ⇒ Object



25
26
27
28
29
30
# File 'app/models/trainspotter/session_record.rb', line 25

def self.expire_before(cutoff, log_file:)
  ongoing
    .for_log_file(log_file)
    .where("ended_at < ?", cutoff)
    .update_all(end_reason: "timeout")
end

.find_active(ip:, after:, log_file:) ⇒ Object



18
19
20
21
22
23
# File 'app/models/trainspotter/session_record.rb', line 18

def self.find_active(ip:, after:, log_file:)
  where(ip: ip, log_file: log_file, end_reason: "ongoing")
    .where("started_at > ?", after)
    .order(started_at: :desc)
    .first
end

Instance Method Details

#anonymous?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'app/models/trainspotter/session_record.rb', line 34

def anonymous?
  email.nil?
end

#duration_displayObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/models/trainspotter/session_record.rb', line 55

def duration_display
  seconds = duration_seconds
  return "Unknown" unless seconds

  if seconds < 60
    "#{seconds}s"
  elsif seconds < 3600
    minutes = seconds / 60
    "#{minutes}m"
  else
    hours = seconds / 3600
    minutes = (seconds % 3600) / 60
    "#{hours}h #{minutes}m"
  end
end

#duration_secondsObject



49
50
51
52
53
# File 'app/models/trainspotter/session_record.rb', line 49

def duration_seconds
  return nil unless started_at
  end_time = ended_at || Time.current
  (end_time - started_at).to_i
end

#ongoing?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'app/models/trainspotter/session_record.rb', line 38

def ongoing?
  end_reason == "ongoing"
end

#time_range_displayObject



42
43
44
45
46
47
# File 'app/models/trainspotter/session_record.rb', line 42

def time_range_display
  return "Unknown" unless started_at
  start_str = started_at.strftime("%b %d %H:%M")
  end_str = ended_at&.strftime("%H:%M") || "now"
  "#{start_str} - #{end_str}"
end