lib/timify.rb
###############################################################
# returns all data for this instance
#
#   input:
#     json: (boolean) in case of true the output will be in json format instead of a hash
#
#   output: (Hash or json string)
#     name: (String) name given for this instance
#     total_time: (float) total elapsed time from initialization to last 'add' call
#     started: (Time)
#     finished: (Time)
#     message: (String) a printable friendly message giving all information
#     locations, labels, ranges: (Hash) the resultant hash contains:
#       secs: (float) number of seconds
#       percent: (integer) percentage in reference to the total time
#       count: (integer) number of times
#     locations: (Hash) All summary data by location where was called
#     labels: (Hash) All summary data by label given on 'add' method
#     ranges: (Hash) All summary data by ranges where was called, from last 'add' call to current 'add' call
###############################################################
returns all data for this instance
input:
json: (boolean) in case of true the output will be in json format instead of a hash


returns all data for this instance input : json : ( boolean ) in case of true the output will be in json format instead of a hash
def totals(json: false)
  	return {} if @status==:off
    require 'json' if json
    output={
        name: @name,
        total_time: @total.to_f,
        started: @initial_time,
        finished: @timify_prev,
        locations: {},
        labels: {},
        ranges: {}
    }
    message="\n\nTotal time <#{@name}>:#{@total.to_f.round(2)}"
    message+="\nTotal time by location:\n"
    @timify.each {|location, secs|
      if @total==0 then
        percent=0
      else
        percent=(secs*100/(@total).to_f).round(0)
      end
      message+= "\t#{location}: #{secs.round(2)} (#{percent}%) ##{@count[location]}\n"
      output[:locations][location]={
          secs: secs,
          percent: percent,
          count: @count[location]
      }
    }
    if !@timify_by_label.empty?
      message+= "\nTotal time by label:\n"
      @timify_by_label.each {|label, secs|
        if @total==0 then
          percent=0
        else
          percent=(secs*100/(@total).to_f).round(0)
        end
        message+= "\t#{label}: #{secs.round(2)} (#{percent}%) ##{@count[label]}\n"
        output[:labels][label]={
            secs: secs,
            percent: percent,
            count: @count[label]
        }
      }
    end
    if !@timify_by_range.empty?
      message+= "\nTotal time by range:\n"
      @timify_by_range.each {|range, secs|
        if @total==0 then
          percent=0
        else
          percent=(secs*100/(@total).to_f).round(0)
        end
        message+= "\t#{range}: #{secs.round(2)} (#{percent}%) ##{@count[range]}\n"
        output[:ranges][range]={
            secs: secs,
            percent: percent,
            count: @count[range]
        }
      }
    end
    message+= "\n\n"
    output[:message]=message
    puts message if @show
    output=output.to_json if json
    return output
  end
def totals ( json : false ) return { } if @status == :off require 'json' if json output = { name : @name , total_time : @total . to_f , started : @initial_time , finished : @timify_prev , locations : { } , labels : { } , ranges : { } } message = "\n\nTotal time <#{@name}>:#{@total.to_f.round(2)}" message += "\nTotal time by location:\n" @timify . each { | location , secs | if @total == 0 then percent = 0 else percent = ( secs * 100 / ( @total ) . to_f ) . round ( 0 ) end message += "\t#{location}: #{secs.round(2)} (#{percent}%) ##{@count[location]}\n" output [ :locations ] [ location ] = { secs : secs , percent : percent , count : @count [ location ] } } if ! @timify_by_label . empty? message += "\nTotal time by label:\n" @timify_by_label . each { | label , secs | if @total == 0 then percent = 0 else percent = ( secs * 100 / ( @total ) . to_f ) . round ( 0 ) end message += "\t#{label}: #{secs.round(2)} (#{percent}%) ##{@count[label]}\n" output [ :labels ] [ label ] = { secs : secs , percent : percent , count : @count [ label ] } } end if ! @timify_by_range . empty? message += "\nTotal time by range:\n" @timify_by_range . each { | range , secs | if @total == 0 then percent = 0 else percent = ( secs * 100 / ( @total ) . to_f ) . round ( 0 ) end message += "\t#{range}: #{secs.round(2)} (#{percent}%) ##{@count[range]}\n" output [ :ranges ] [ range ] = { secs : secs , percent : percent , count : @count [ range ] } } end message += "\n\n" output [ :message ] = message puts message if @show output = output . to_json if json return output end
returns all data for this instance
input:
json: (boolean) in case of true the output will be in json format instead of a hash
returns all data for this instance input : json : ( boolean ) in case of true the output will be in json format instead of a hash
json