Class: Reviewer::Tool::Timing
- Inherits:
-
Object
- Object
- Reviewer::Tool::Timing
- Defined in:
- lib/reviewer/tool/timing.rb
Overview
Manages timing persistence for a tool — recording, retrieving, and averaging execution times, plus tracking when the prepare command was last run.
Constant Summary collapse
- SIX_HOURS_IN_SECONDS =
60 * 60 * 6
Instance Method Summary collapse
-
#average_time(command) ⇒ Float
Calculates the average execution time for a command.
-
#get_timing(command) ⇒ Array<Float>
Retrieves historical timing data for a command.
-
#initialize(history, key) ⇒ Timing
constructor
Creates a timing tracker for a specific tool.
-
#last_prepared_at ⇒ Time?
Specifies when the tool last had its
preparecommand run. -
#last_prepared_at=(timestamp) ⇒ void
Sets the timestamp for when the tool last ran its
preparecommand. -
#record_timing(command, time) ⇒ void
Records the execution time for a command to calculate running averages.
-
#stale? ⇒ Boolean
Determines whether the
preparecommand was run recently enough.
Constructor Details
#initialize(history, key) ⇒ Timing
Creates a timing tracker for a specific tool
17 18 19 20 |
# File 'lib/reviewer/tool/timing.rb', line 17 def initialize(history, key) @history = history @key = key end |
Instance Method Details
#average_time(command) ⇒ Float
Calculates the average execution time for a command
43 44 45 46 47 |
# File 'lib/reviewer/tool/timing.rb', line 43 def average_time(command) times = get_timing(command) times.any? ? times.sum / times.size : 0 end |
#get_timing(command) ⇒ Array<Float>
Retrieves historical timing data for a command
53 54 55 |
# File 'lib/reviewer/tool/timing.rb', line 53 def get_timing(command) @history.get(@key, command.raw_string) || [] end |
#last_prepared_at ⇒ Time?
Specifies when the tool last had its prepare command run
25 26 27 28 29 |
# File 'lib/reviewer/tool/timing.rb', line 25 def last_prepared_at date_string = @history.get(@key, :last_prepared_at).to_s date_string.empty? ? nil : DateTime.parse(date_string).to_time end |
#last_prepared_at=(timestamp) ⇒ void
This method returns an undefined value.
Sets the timestamp for when the tool last ran its prepare command
35 36 37 |
# File 'lib/reviewer/tool/timing.rb', line 35 def last_prepared_at=() @history.set(@key, :last_prepared_at, .to_s) end |
#record_timing(command, time) ⇒ void
This method returns an undefined value.
Records the execution time for a command to calculate running averages
62 63 64 65 66 67 68 |
# File 'lib/reviewer/tool/timing.rb', line 62 def record_timing(command, time) return unless time timing = get_timing(command).take(4) << time.round(2) @history.set(@key, command.raw_string, timing) end |
#stale? ⇒ Boolean
Determines whether the prepare command was run recently enough
73 74 75 |
# File 'lib/reviewer/tool/timing.rb', line 73 def stale? !last_prepared_at || last_prepared_at < Time.now - SIX_HOURS_IN_SECONDS end |