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



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

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

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



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/piggly/command/report.rb', line 155

def configure(argv, config = Config.new)
  io = $stdin
  sonar_path = nil
  schema_csv_path = nil
  html_report_requested = false

  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") do |path|
      config.report_root = path
      html_report_requested = true
    end
    o.on("-x", "--sonar-report-path PATH",  "generate Sonar coverage XML report at PATH") do |path|
      sonar_path = path
    end
    o.on("--schema-csv-path PATH", "generate schema-level CSV coverage report at PATH") do |path|
      schema_csv_path = path
    end
    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

    unless html_report_requested || sonar_path || schema_csv_path
      raise OptionParser::MissingArgument,
        "at least one report type required: use -o for HTML reports, -x for Sonar report, or --schema-csv-path for CSV report"
    end
    
    if io.eql?($stdin) and $stdin.tty?
      raise OptionParser::MissingArgument,
        "stdin must be a pipe, or use --input PATH"
    end

    return io, config, sonar_path, schema_csv_path, html_report_requested
  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



96
97
98
99
100
101
# File 'lib/piggly/command/report.rb', line 96

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



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/piggly/command/report.rb', line 105

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

.create_schema_csv_report(config, procedures, profile, output_path) ⇒ Object

Create schema-level CSV coverage report



148
149
150
151
152
153
# File 'lib/piggly/command/report.rb', line 148

def create_schema_csv_report(config, procedures, profile, output_path)
  puts "creating schema CSV coverage report"
  reporter = Reporter::SchemaCsv.new(config, profile, output_path)
  path = reporter.report(procedures)
  puts "Schema CSV coverage report written to: #{path}"
end

.create_sonar_report(config, procedures, profile, output_path) ⇒ Object

Create Sonar coverage XML report



139
140
141
142
143
144
# File 'lib/piggly/command/report.rb', line 139

def create_sonar_report(config, procedures, profile, output_path)
  puts "creating Sonar coverage report"
  reporter = Reporter::Sonar.new(config, profile, output_path)
  path = reporter.report(procedures)
  puts "Sonar coverage report written to: #{path}"
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/piggly/command/report.rb', line 14

def main(argv)
  require "pp"
  io, config, sonar_path, schema_csv_path, html_report_requested = 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)

  # Generate HTML coverage report if requested
  if html_report_requested
    create_index(config, index, procedures, profile)
    create_reports(config, procedures, profile)
  end
  
  # Generate Sonar coverage report if requested
  if sonar_path
    create_sonar_report(config, procedures, profile, sonar_path)
  end

  # Generate schema-level CSV report if requested
  if schema_csv_path
    create_schema_csv_report(config, procedures, profile, schema_csv_path)
  end

  unless html_report_requested || sonar_path || schema_csv_path
    puts "Warning: No report output specified. Use -o for HTML reports, -x for Sonar report, or --schema-csv-path for CSV report."
  end
end

.profile_procedures(config, procedures, profile) ⇒ Object

Adds the given procedures to Profile



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

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



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

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



89
90
91
92
# File 'lib/piggly/command/report.rb', line 89

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