Class: RailsConsolePro::CompareResult

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_console_pro/compare_result.rb

Overview

Value object for query comparison results

Defined Under Namespace

Classes: Comparison

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(comparisons:, winner: nil, timestamp: Time.current) ⇒ CompareResult



19
20
21
22
23
# File 'lib/rails_console_pro/compare_result.rb', line 19

def initialize(comparisons:, winner: nil, timestamp: Time.current)
  @comparisons = Array(comparisons)
  @winner = winner
  @timestamp = timestamp
end

Instance Attribute Details

#comparisonsObject (readonly)

Returns the value of attribute comparisons.



17
18
19
# File 'lib/rails_console_pro/compare_result.rb', line 17

def comparisons
  @comparisons
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



17
18
19
# File 'lib/rails_console_pro/compare_result.rb', line 17

def timestamp
  @timestamp
end

#winnerObject (readonly)

Returns the value of attribute winner.



17
18
19
# File 'lib/rails_console_pro/compare_result.rb', line 17

def winner
  @winner
end

Instance Method Details

#error_countObject



37
38
39
# File 'lib/rails_console_pro/compare_result.rb', line 37

def error_count
  comparisons.count { |c| c.error }
end

#export_to_file(file_path, format: nil) ⇒ Object

Export to file



76
77
78
# File 'lib/rails_console_pro/compare_result.rb', line 76

def export_to_file(file_path, format: nil)
  FormatExporter.export_to_file(self, file_path, format: format)
end

#fastestObject



25
26
27
# File 'lib/rails_console_pro/compare_result.rb', line 25

def fastest
  comparisons.min_by { |c| c.duration_ms || Float::INFINITY }
end

#fastest_nameObject



45
46
47
# File 'lib/rails_console_pro/compare_result.rb', line 45

def fastest_name
  fastest&.name
end

#has_errors?Boolean



33
34
35
# File 'lib/rails_console_pro/compare_result.rb', line 33

def has_errors?
  comparisons.any? { |c| c.error }
end

#performance_ratioObject



53
54
55
56
57
58
# File 'lib/rails_console_pro/compare_result.rb', line 53

def performance_ratio
  return nil if comparisons.size < 2 || fastest.nil? || slowest.nil?
  return nil if fastest.duration_ms.nil? || slowest.duration_ms.nil? || fastest.duration_ms.zero?

  (slowest.duration_ms / fastest.duration_ms).round(2)
end

#slowestObject



29
30
31
# File 'lib/rails_console_pro/compare_result.rb', line 29

def slowest
  comparisons.max_by { |c| c.duration_ms || 0 }
end

#slowest_nameObject



49
50
51
# File 'lib/rails_console_pro/compare_result.rb', line 49

def slowest_name
  slowest&.name
end

#to_html(style: :default) ⇒ Object

Export to HTML



71
72
73
# File 'lib/rails_console_pro/compare_result.rb', line 71

def to_html(style: :default)
  FormatExporter.to_html(self, title: "Query Comparison", style: style)
end

#to_json(pretty: true) ⇒ Object

Export to JSON



61
62
63
# File 'lib/rails_console_pro/compare_result.rb', line 61

def to_json(pretty: true)
  FormatExporter.to_json(self, pretty: pretty)
end

#to_yamlObject

Export to YAML



66
67
68
# File 'lib/rails_console_pro/compare_result.rb', line 66

def to_yaml
  FormatExporter.to_yaml(self)
end

#total_queriesObject



41
42
43
# File 'lib/rails_console_pro/compare_result.rb', line 41

def total_queries
  comparisons.sum { |c| c.query_count || 0 }
end