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.



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

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

Instance Attribute Details

#channelsObject (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

#reportObject

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_allObject



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