Class: Piggly::Command::Report

Inherits:
Base show all
Defined in:
lib/piggly/command/report.rb

Overview

This command reads a given file (or STDIN) which is expected to contain messages like the pattern Profile::PATTERN, which is probbaly “WARNING: PIGGLY 0123456789abcdef”.

Lines in the input that match this pattern are profiled and used to generate a report

Class Method Summary collapse

Methods inherited from Base

command, connect, filter, o_accumulate, o_cache_root, o_connection_name, o_database_yml, o_dry_run, o_include_paths, o_reject, o_report_root, o_select, o_version

Class Method Details

.clear_coverage(config, profile) ⇒ Object

Clear coverage after procedures have been loaded



57
58
59
60
61
62
# File 'lib/piggly/command/report.rb', line 57

def clear_coverage(config, profile)
  unless config.accumulate?
    puts "clearing previous coverage"
    profile.clear
  end
end

.configure(argv, config = Config.new) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/piggly/command/report.rb', line 121

def configure(argv, config = Config.new)
  io = $stdin
  p  = OptionParser.new do |o|
    o.on("-t", "--dry-run",           "only print the names of matching procedures", &o_dry_run(config))
    o.on("-s", "--select PATTERN",    "select procedures matching PATTERN", &o_select(config))
    o.on("-r", "--reject PATTERN",    "ignore procedures matching PATTERN", &o_reject(config))
    o.on("-c", "--cache-root PATH",   "local cache directory", &o_cache_root(config))
    o.on("-o", "--report-root PATH",  "report output directory", &o_report_root(config))
    o.on("-a", "--accumulate",        "accumulate data from the previous run", &o_accumulate(config))
    o.on("-V", "--version",           "show version", &o_version(config))
    o.on("-h", "--help",              "show this message") { abort o.to_s }
    o.on("-f", "--input PATH",        "read trace messages from PATH") do |path|
      io = if path == "-"
             $stdin
           else
             File.open(path, "rb")
           end
    end
  end

  begin
    p.parse! argv
    
    if io.eql?($stdin) and $stdin.tty?
      raise OptionParser::MissingArgument,
        "stdin must be a pipe, or use --input PATH"
    end

    return io, config
  rescue OptionParser::InvalidOption,
         OptionParser::InvalidArgument,
         OptionParser::MissingArgument
    puts p
    puts
    puts $!
    exit! 1
  end
end

.create_index(config, index, procedures, profile) ⇒ Object

Create the report’s index.html



80
81
82
83
84
85
# File 'lib/piggly/command/report.rb', line 80

def create_index(config, index, procedures, profile)
  puts "creating index"
  reporter = Reporter::Index.new(config, profile)
  reporter.install("resources/piggly.css", "resources/sortable.js", "resources/highlight.js")
  reporter.report(procedures, index)
end

.create_reports(config, procedures, profile) ⇒ Object

Create each procedures’ HTML report page



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/piggly/command/report.rb', line 89

def create_reports(config, procedures, profile)
  puts "creating reports"
  queue = Util::ProcessQueue.new

  compiler = Compiler::TraceCompiler.new(config)
  reporter = Reporter::Procedure.new(config, profile)

  Parser.parser

  procedures.each do |p|
    queue.add do
      unless compiler.stale?(p)
        data = compiler.compile(p)
        path = reporter.report_path(p.source_path(config), ".html")

        unless profile.empty?(data[:tags])
          changes = ": #{profile.difference(p, data[:tags])}"
        end

        puts "reporting coverage for #{p.name}#{changes}"
      # pp data[:tags]
      # pp profile[p]
      # puts

        reporter.report(p)
      end
    end
  end

  queue.execute
end

.main(argv) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/piggly/command/report.rb', line 14

def main(argv)
  require "pp"
  io, config = configure(argv)

  profile = Profile.new
  index   = Dumper::Index.new(config)

  procedures = filter(config, index)

  if procedures.empty?
    if filters.empty?
      abort "no stored procedures in the cache"
    else
      abort "no stored procedures in the cache matched your criteria"
    end
  elsif config.dry_run?
    puts procedures.map{|p| p.signature }
    exit 0
  end

  profile_procedures(config, procedures, profile)
  clear_coverage(config, profile)

  read_profile(config, io, profile)
  store_coverage(profile)

  create_index(config, index, procedures, profile)
  create_reports(config, procedures, profile)
end

.profile_procedures(config, procedures, profile) ⇒ Object

Adds the given procedures to Profile



46
47
48
49
50
51
52
53
# File 'lib/piggly/command/report.rb', line 46

def profile_procedures(config, procedures, profile)
  # register each procedure in the Profile
  compiler = Compiler::TraceCompiler.new(config)
  procedures.each do |p|
    result = compiler.compile(p)
    profile.add(p, result[:tags], result)
  end
end

.read_profile(config, io, profile) ⇒ Object

Reads io for lines matching Profile::PATTERN and records coverage



66
67
68
69
# File 'lib/piggly/command/report.rb', line 66

def read_profile(config, io, profile)
  np = profile.notice_processor(config)
  io.each{|line| np.call(line) }
end

.store_coverage(profile) ⇒ Object

Store the coverage Profile on disk



73
74
75
76
# File 'lib/piggly/command/report.rb', line 73

def store_coverage(profile)
  puts "storing coverage profile"
  profile.store
end