Class: LogBench::App::Sort

Inherits:
Object
  • Object
show all
Defined in:
lib/log_bench/app/sort.rb

Constant Summary collapse

MODES =
[:timestamp, :duration, :method, :status].freeze

Instance Method Summary collapse

Constructor Details

#initializeSort

Returns a new instance of Sort.



6
7
8
# File 'lib/log_bench/app/sort.rb', line 6

def initialize
  self.mode = :timestamp
end

Instance Method Details

#cycleObject



10
11
12
13
14
# File 'lib/log_bench/app/sort.rb', line 10

def cycle
  current_index = MODES.index(mode)
  next_index = (current_index + 1) % MODES.length
  self.mode = MODES[next_index]
end

#display_nameObject



16
17
18
19
20
21
22
23
# File 'lib/log_bench/app/sort.rb', line 16

def display_name
  case mode
  when :timestamp then "TIMESTAMP"
  when :duration then "DURATION"
  when :method then "METHOD"
  when :status then "STATUS"
  end
end

#duration?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/log_bench/app/sort.rb', line 44

def duration?
  mode == :duration
end

#method?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/log_bench/app/sort.rb', line 48

def method?
  mode == :method
end

#sort_requests(requests) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/log_bench/app/sort.rb', line 25

def sort_requests(requests)
  case mode
  when :timestamp
    requests.sort_by { |req| req.timestamp || Time.at(0) }
  when :duration
    requests.sort_by { |req| -(req.duration || 0) }  # Descending (slowest first)
  when :method
    requests.sort_by { |req| req.method || "" }
  when :status
    requests.sort_by { |req| -(req.status || 0) }  # Descending (errors first)
  else
    requests
  end
end

#status?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/log_bench/app/sort.rb', line 52

def status?
  mode == :status
end

#timestamp?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/log_bench/app/sort.rb', line 40

def timestamp?
  mode == :timestamp
end