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.
44 45 46 47 |
# File 'lib/openstudio/workflow/time_logger.rb', line 44 def initialize @logger = [] @channels = {} end |
Instance Attribute Details
#channels ⇒ Object (readonly)
Returns the value of attribute channels.
42 43 44 |
# File 'lib/openstudio/workflow/time_logger.rb', line 42 def channels @channels end |
Instance Method Details
#delta(channel) ⇒ Object
this will report all the values for all the channels with this name.
82 83 84 |
# File 'lib/openstudio/workflow/time_logger.rb', line 82 def delta(channel) @logger.map { |k| { channel.to_s => k[:delta] } if k[:channel] == channel }.compact end |
#report ⇒ Object
return the entire report
77 78 79 |
# File 'lib/openstudio/workflow/time_logger.rb', line 77 def report @logger end |
#save(filename) ⇒ Object
save the data to a file. This will overwrite the file if it already exists
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/openstudio/workflow/time_logger.rb', line 87 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.
50 51 52 53 54 |
# File 'lib/openstudio/workflow/time_logger.rb', line 50 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
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/openstudio/workflow/time_logger.rb', line 56 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
70 71 72 73 74 |
# File 'lib/openstudio/workflow/time_logger.rb', line 70 def stop_all @channels.each_key do |channel| stop(channel) end end |