Class: Reviewer::Tool::Timing

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(history, key) ⇒ Timing

Creates a timing tracker for a specific tool

Parameters:

  • history (History)

    the persistence store for timing data

  • key (Symbol)

    the tool’s unique key



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

Parameters:

  • command (Command)

    the command to get timing for

Returns:

  • (Float)

    the average time in seconds or 0 if no history



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

Parameters:

  • command (Command)

    the command to look up

Returns:

  • (Array<Float>)

    the last few recorded execution times



53
54
55
# File 'lib/reviewer/tool/timing.rb', line 53

def get_timing(command)
  @history.get(@key, command.raw_string) || []
end

#last_prepared_atTime?

Specifies when the tool last had its prepare command run

Returns:

  • (Time, nil)

    timestamp of when the prepare command was last 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

Parameters:

  • timestamp (DateTime, Time)

    the value to record



35
36
37
# File 'lib/reviewer/tool/timing.rb', line 35

def last_prepared_at=(timestamp)
  @history.set(@key, :last_prepared_at, timestamp.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

Parameters:

  • command (Command)

    the command that was run

  • time (Float, nil)

    the execution time in seconds



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

Returns:

  • (Boolean)

    true if the timestamp is nil or older than six hours



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