Class: TimeLogger
- Inherits:
-
Object
- Object
- TimeLogger
- Defined in:
- lib/openstudio/workflow/time_logger.rb
Overview
Class to store run times in a useful structure. Data are stored in a hash based on a the channel name There is no concept of multi-levels. The onus is on the user to make sure that they don’t add a value to the logger that may be a level.
Instance Attribute Summary collapse
-
#channels ⇒ Object
readonly
Returns the value of attribute channels.
Instance Method Summary collapse
-
#delta(channel) ⇒ Object
this will report all the values for all the channels with this name.
-
#initialize ⇒ TimeLogger
constructor
A new instance of TimeLogger.
-
#report ⇒ Object
return the entire report.
-
#save(filename) ⇒ Object
save the data to a file.
-
#start(channel) ⇒ Object
name of the moniker that you are tracking.
- #stop(channel) ⇒ Object
- #stop_all ⇒ Object
Constructor Details
#initialize ⇒ TimeLogger
Returns a new instance of TimeLogger.
14 15 16 17 |
# File 'lib/openstudio/workflow/time_logger.rb', line 14 def initialize @logger = [] @channels = {} end |
Instance Attribute Details
#channels ⇒ Object (readonly)
Returns the value of attribute channels.
12 13 14 |
# File 'lib/openstudio/workflow/time_logger.rb', line 12 def channels @channels end |
Instance Method Details
#delta(channel) ⇒ Object
this will report all the values for all the channels with this name.
52 53 54 |
# File 'lib/openstudio/workflow/time_logger.rb', line 52 def delta(channel) @logger.map { |k| { channel.to_s => k[:delta] } if k[:channel] == channel }.compact end |
#report ⇒ Object
return the entire report
47 48 49 |
# File 'lib/openstudio/workflow/time_logger.rb', line 47 def report @logger end |
#save(filename) ⇒ Object
save the data to a file. This will overwrite the file if it already exists
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/openstudio/workflow/time_logger.rb', line 57 def save(filename) File.open(filename, 'w') do |f| f << JSON.pretty_generate(@logger) # make sure data is written to the disk one way or the other begin f.fsync rescue StandardError f.flush end end end |
#start(channel) ⇒ Object
name of the moniker that you are tracking. If the name is already in use, then it restarts the timer.
20 21 22 23 24 |
# File 'lib/openstudio/workflow/time_logger.rb', line 20 def start(channel) # warning -- "will reset timer for #{moniker}" if @monikers.key? moniker s = ::Time.now @channels[channel] = { start_time_str: s.to_s, start_time: s.to_f } end |
#stop(channel) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/openstudio/workflow/time_logger.rb', line 26 def stop(channel) end_time = ::Time.now.to_f @logger << { channel: channel, start_time: @channels[channel][:start_time], start_time_str: @channels[channel][:start_time_str], end_time: end_time, delta: end_time - @channels[channel][:start_time] } # remove the channel @channels.delete(channel) if @channels.key? channel end |
#stop_all ⇒ Object
40 41 42 43 44 |
# File 'lib/openstudio/workflow/time_logger.rb', line 40 def stop_all @channels.each_key do |channel| stop(channel) end end |