Class: Jets::Timing::Report

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Defined in:
lib/jets/timing/report.rb

Instance Method Summary collapse

Constructor Details

#initialize(log = Jets::Timing::RECORD_LOG_PATH) ⇒ Report

Returns a new instance of Report.



7
8
9
# File 'lib/jets/timing/report.rb', line 7

def initialize(log=Jets::Timing::RECORD_LOG_PATH)
  @log = log
end

Instance Method Details

#dataObject



54
55
56
# File 'lib/jets/timing/report.rb', line 54

def data
  IO.readlines(@log).map { |l| JSON.load(l) }
end

#get_time(timings, type) ⇒ Object

Input: timings is a pair of data. The start and end time.



76
77
78
79
80
# File 'lib/jets/timing/report.rb', line 76

def get_time(timings, type)
  # reverse so we get the last ship
  time = timings.reverse.find { |l| l['type'] == type }['time']
  time.to_f # in milloseconds
end

#processObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/jets/timing/report.rb', line 19

def process
  deploy = times_for("Jets::Commands::Deploy")
  deploy[:rest] = rest_time(deploy[:run], deploy[:build_code], deploy[:ship])
  build = times_for("Jets::Commands::Build")
  cfn_ship = times_for("Jets::Cfn::Ship")
  code_builder = times_for("Jets::Builders::CodeBuilder")

  # Two ships screw this up.
  # The first ship creates the base parent stack with the s3 bucket.
  # The second ship creates the app.
  # Once the first stack exists, it is left alone and ship does not get called
  # twice.
  # This reporting logic currently only reports the last ship.
  # Refer to timings.reverse.find in get_time method.
  results = {
    overall: deploy[:run],
    "commands/deploy.rb": deploy,
    "commands/build.rb": build,
    "builders/code_builder.rb": code_builder,
    "cfn/ship.rb": cfn_ship,
  }
  text = YAML.dump(results.deep_stringify_keys)
  text.sub!('---', 'Timing report:')
  text
end

#rest_time(total, *times) ⇒ Object



59
60
61
62
63
# File 'lib/jets/timing/report.rb', line 59

def rest_time(total, *times)
  result = total.to_f
  times.each { |time| result -= time.to_f }
  '%.3f' % result + 's'
end

#resultsObject



11
12
13
14
15
16
17
# File 'lib/jets/timing/report.rb', line 11

def results
  unless File.exist?(@log)
    puts "WARN: Timing #{@log} does not exist"
    return
  end
  puts process
end

#time_for(klass, meth) ⇒ Object

Returns String Example: 88.8s



67
68
69
70
71
72
73
# File 'lib/jets/timing/report.rb', line 67

def time_for(klass, meth)
  timings = data.select { |l| l['class'] == klass && l['meth'] == meth }
  start_time = get_time(timings, 'start')
  finish_time = get_time(timings, 'finish')
  diff = ( finish_time - start_time ) / 1000.0
  '%.3f' % diff + 's'
end

#times_for(class_name) ⇒ Object

All times for a class



46
47
48
49
50
51
52
# File 'lib/jets/timing/report.rb', line 46

def times_for(class_name)
  times = data.select { |i| i['class'] == class_name }
  meths = times.map { |i| i['meth'] }.uniq
  meths.inject({}) do |result, meth|
    result.merge(meth => time_for(class_name, meth))
  end.deep_symbolize_keys
end