Class: ActiveRecord::SqlAnalyzer::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/sql_analyzer/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



6
7
8
9
10
# File 'lib/active_record/sql_analyzer/cli.rb', line 6

def initialize
  @options = {
    concurrency: 6
  }
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/active_record/sql_analyzer/cli.rb', line 4

def options
  @options
end

#processorObject (readonly)

Returns the value of attribute processor.



4
5
6
# File 'lib/active_record/sql_analyzer/cli.rb', line 4

def processor
  @processor
end

Instance Method Details

#parse_options(args) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/active_record/sql_analyzer/cli.rb', line 38

def parse_options(args)
  opts = OptionParser.new

  opts.on("--log-dir [DIR]", String, "Directory that logs are in") do |val|
    unless Dir.exist?(val)
      raise ArgumentError, "log directory '#{val}' does not exist"
    end

    options[:log_dir] = val
  end

  opts.on("--dest-dir [DIR]", String, "Directory to dump logs to") do |val|
    unless Dir.exist?(val)
      raise ArgumentError, "dest directory '#{val}' does not exist"
    end

    options[:dest_dir] = val
  end

  opts.on("-c", "--concurrency", Integer, "How many threads to use for processing log files") do |val|
    if val <= 0
      raise ArgumentError, "Concurrency must be >0"
    end

    options[:concurrency] = val
  end

  opts.on_tail("-h", "--help") do
    puts opts
    exit
  end

  opts.parse(args)
end

#runObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/active_record/sql_analyzer/cli.rb', line 16

def run
  definition_logs = Dir["#{options[:log_dir]}/*_definitions.log*"].map do |path|
    [File.basename(path).gsub(/_definitions\.log.*/, ""), path]
  end

  if definition_logs.empty?
    raise ArgumentError, "Cannot find any log files in '#{options[:log_dir]}'"
  end

  # Process the definition logs
  processor.run_definition(definition_logs)

  # Process the usage logs
  usage_logs = Dir["#{options[:log_dir]}/{#{definition_logs.map(&:first).uniq.join(",")}}.log*"].map do |path|
    [File.basename(path).split(".", 2).first, path]
  end

  processor.run_usage(usage_logs)

  processor.dump(options[:dest_dir])
end