Class: TimeLogger

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

Instance Method Summary collapse

Constructor Details

#initializeTimeLogger

Returns a new instance of TimeLogger.



7
8
9
10
# File 'lib/openstudio/workflow/time_logger.rb', line 7

def initialize
  @logger = []
  @channels = {}
end

Instance Attribute Details

#channelsObject (readonly)

Returns the value of attribute channels.



5
6
7
# File 'lib/openstudio/workflow/time_logger.rb', line 5

def channels
  @channels
end

Instance Method Details

#delta(channel) ⇒ Object

this will report all the values for all the channels with this name.



45
46
47
# File 'lib/openstudio/workflow/time_logger.rb', line 45

def delta(channel)
  @logger.map { |k| { channel.to_s => k[:delta] } if k[:channel] == channel }.compact
end

#reportObject

return the entire report



40
41
42
# File 'lib/openstudio/workflow/time_logger.rb', line 40

def report
  @logger
end

#save(filename) ⇒ Object

save the data to a file. This will overwrite the file if it already exists



50
51
52
# File 'lib/openstudio/workflow/time_logger.rb', line 50

def save(filename)
  File.open(filename, 'w') { |f| f << JSON.pretty_generate(@logger) }
end

#start(channel) ⇒ Object

name of the moniker that you are tracking. If the name is already in use, then it restarts the timer.



13
14
15
16
17
# File 'lib/openstudio/workflow/time_logger.rb', line 13

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



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/openstudio/workflow/time_logger.rb', line 19

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_allObject



33
34
35
36
37
# File 'lib/openstudio/workflow/time_logger.rb', line 33

def stop_all
  @channels.each_key do |channel|
    stop(channel)
  end
end