Class: Dexter::Client

Inherits:
Object
  • Object
show all
Extended by:
Logging
Includes:
Logging
Defined in:
lib/dexter/client.rb

Constant Summary

Constants included from Logging

Logging::COLOR_CODES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

colorize, log, output

Constructor Details

#initialize(args) ⇒ Client

Returns a new instance of Client.



14
15
16
# File 'lib/dexter/client.rb', line 14

def initialize(args)
  @arguments, @options = parse_args(args)
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



6
7
8
# File 'lib/dexter/client.rb', line 6

def arguments
  @arguments
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/dexter/client.rb', line 6

def options
  @options
end

Class Method Details

.startObject



8
9
10
11
12
# File 'lib/dexter/client.rb', line 8

def self.start
  Client.new(ARGV).perform
rescue Error => e
  abort colorize(e.message.strip, :red)
end

Instance Method Details

#parse_args(args) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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
120
121
122
123
124
125
126
127
128
# File 'lib/dexter/client.rb', line 54

def parse_args(args)
  opts = Slop.parse(args) do |o|
    o.banner = "      Usage:\n          dexter [options]\n    BANNER\n\n    o.separator \"Input options:\"\n    o.string \"--input-format\", \"input format\"\n    o.boolean \"--pg-stat-activity\", \"use pg_stat_activity\", default: false\n    o.boolean \"--pg-stat-statements\", \"use pg_stat_statements\", default: false, help: false\n    o.boolean \"--stdin\", \"use stdin\", default: false\n    o.string \"-s\", \"--statement\", \"process a single statement\"\n    o.separator \"\"\n\n    o.separator \"Connection options:\"\n    o.string \"-d\", \"--dbname\", \"database name\"\n    o.string \"-h\", \"--host\", \"database host\"\n    o.integer \"-p\", \"--port\", \"database port\"\n    o.string \"-U\", \"--username\", \"database user\"\n    o.separator \"\"\n\n    o.separator \"Processing options:\"\n    o.integer \"--interval\", \"time to wait between processing queries, in seconds\", default: 60\n    o.integer \"--min-calls\", \"only process queries that have been called a certain number of times\", default: 0\n    o.float \"--min-time\", \"only process queries that have consumed a certain amount of DB time, in minutes\", default: 0\n    o.separator \"\"\n\n    o.separator \"Indexing options:\"\n    o.boolean \"--analyze\", \"analyze tables that haven't been analyzed in the past hour\", default: false\n    o.boolean \"--create\", \"create indexes\", default: false\n    o.boolean \"--enable-hypopg\", \"enable the HypoPG extension\", default: false\n    o.array \"--exclude\", \"prevent specific tables from being indexed\"\n    o.string \"--include\", \"only include specific tables\"\n    o.integer \"--min-cost\", default: 100, help: false\n    o.integer \"--min-cost-savings-pct\", default: 50, help: false\n    o.string \"--tablespace\", \"tablespace to create indexes\"\n    o.separator \"\"\n\n    o.separator \"Logging options:\"\n    o.boolean \"--log-explain\", \"log explain\", default: false, help: false\n    o.string \"--log-level\", \"log level\", default: \"info\"\n    o.boolean \"--log-sql\", \"log sql\", default: false\n    o.separator \"\"\n\n    o.separator \"Other options:\"\n    o.on \"-v\", \"--version\", \"print the version\" do\n      log Dexter::VERSION\n      exit\n    end\n    o.on \"--help\", \"prints help\" do\n      log o\n      exit\n    end\n  end\n\n  arguments = opts.arguments\n  options = opts.to_hash\n\n  options[:dbname] = arguments.shift unless options[:dbname]\n\n  # TODO remove global variable\n  $log_level = options[:log_level].to_s.downcase\n  unless [\"error\", \"info\", \"debug\", \"debug2\", \"debug3\"].include?($log_level)\n    raise Error, \"Unknown log level\"\n  end\n\n  unless [nil, \"csv\", \"json\", \"sql\"].include?(options[:input_format])\n    raise Error, \"Unknown input format\"\n  end\n\n  [arguments, options]\nrescue Slop::Error => e\n  raise Error, e.message\nend\n"

#performObject



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
# File 'lib/dexter/client.rb', line 18

def perform
  STDOUT.sync = true
  STDERR.sync = true

  connection = Connection.new(**options.slice(:dbname, :host, :port, :username, :log_sql))
  connection.setup(options[:enable_hypopg])

  source =
    if options[:statement]
      # TODO raise error for --interval, --min-calls, --min-time
      StatementSource.new(options[:statement])
    elsif options[:pg_stat_statements]
      # TODO support streaming option
      PgStatStatementsSource.new(connection)
    elsif options[:pg_stat_activity]
      PgStatActivitySource.new(connection)
    elsif arguments.any?
      ARGV.replace(arguments)
      if !options[:input_format]
        ext = ARGV.map { |v| File.extname(v) }.uniq
        options[:input_format] = ext.first[1..-1] if ext.size == 1
      end
      LogSource.new(ARGF, options[:input_format])
    elsif options[:stdin]
      LogSource.new(STDIN, options[:input_format])
    else
      raise Error, "Specify a source of queries: --pg-stat-statements, --pg-stat-activity, --stdin, or a path"
    end

  collector = Collector.new(**options.slice(:min_time, :min_calls))

  indexer = Indexer.new(connection: connection, **options)

  Processor.new(source, collector, indexer, **options.slice(:interval)).perform
end