Sha256: 1a76dea5ad350a6bb518f11a69e8e31a3bfebe823cf4028a384567fdae3953c6

Contents?: true

Size: 1.82 KB

Versions: 3

Compression:

Stored size: 1.82 KB

Contents

#!/usr/bin/env ruby

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

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

3 entries across 3 versions & 1 rubygems

Version Path
shiba-0.9.1 bin/explain
shiba-0.9.0 bin/explain
shiba-0.8.1 bin/explain