Sha256: 0c6c6647d898b7ad5c02f70d989b07940d24df98a2cf887a81611cb3c05a9f72

Contents?: true

Size: 1.84 KB

Versions: 4

Compression:

Stored size: 1.84 KB

Contents

#!/usr/bin/env ruby

require 'bundler/setup'
require 'shiba'
require 'shiba/analyzer'
require 'shiba/table_stats'
require 'shiba/configure'
require 'shiba/output'

require 'optionparser'

options = {}

parser = Shiba::Configure.make_options_parser(options)
parser.banner = "Run a list of queries through shiba's analyzer."
parser.parse!


if options['json'] && options['html']
  $stderr.puts "Can only output to json or html, not both"
  $stderr.puts parser.banner
  exit 2
end

file = options.delete("file")
file = File.open(file, "r") if file

begin
  Shiba.configure(options)
rescue Shiba::ConfigError => e
  $stderr.puts(e.message)
  $stderr.puts(parser)
  exit 2
end

schema_stats_fname = options["stats"]

if schema_stats_fname && !File.exist?(schema_stats_fname)
  $stderr.puts "No such file: #{schema_stats_fname}"
  exit 2
end

file = $stdin if file.nil?

if options["verbose"]
  $stderr.puts "Reading queries from '#{file.inspect}'..."
  $stderr.puts "Analyzing SQL to '#{json.inspect}'..."
end

table_stats = Shiba::TableStats.new(Shiba.index_config, Shiba.connection, {})
queries = Shiba::Analyzer.analyze(file, table_stats, options)

if options['example_data_json']
  output = Shiba::Output.new(queries)
  File.open(options['example_data_json'], "w+") do |f|
    f.write(JSON.dump(output.as_json))
  end
elsif options['json']
  queries.each do |q|
    options['json'].puts(JSON.dump(q))
  end
else
  problems = queries.select { |q| q[:severity] && q[:severity] != 'none' }

  if problems.any?
    query_word = problems.size == 1 ? 'query' : 'queries'
    $stderr.puts "#{problems.size} problematic #{query_word} detected"

    page = Shiba::Output.new(queries, { 'output' => options['html'] }).make_web!

    if !File.exist?(page)
      $stderr.puts("Failed to generate #{page}")
      exit 2
    end

    $stderr.puts "Report available at #{page}"
    exit 1
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
shiba-0.6.4 bin/explain
shiba-0.6.3 bin/explain
shiba-0.6.2 bin/explain
shiba-0.6.1 bin/explain