pg_qtop 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5b68b70f3b6f49e8bdde29887334cd1fcb64fa35
4
- data.tar.gz: 0275546c0da92a4372e28b30715a55e80ce32001
3
+ metadata.gz: dcaa78ecf82e15a38b2638837f8828833e49cab5
4
+ data.tar.gz: 2fb75d88dde0903495c9386ce80e0f1255e68001
5
5
  SHA512:
6
- metadata.gz: 8dae0bbf5bbf819d426ce4e478b663aa2dfaea1ba6a7ac918e6cb19605346323c44c2d0a05e149ee6bd64e6b669cc45fd755fa52684110c0b5e6ca77e8173139
7
- data.tar.gz: 854098cd74a01161f667224e1003a66f25820d1197cf7747c012c74cfd8d29427bf43ce9566a135d33fe4cd5710ca8d08a98f63d2d9b5e79eba58948b2dbd2d3
6
+ metadata.gz: c92a12099f7f5c7ce54c41ee4ae53d06353d0fce61ac57aea7bcc002d5d3e213985c18595246c4b9c90c9d2887a8be73a21d3f1e1f62aa278d6e3807b0f72268
7
+ data.tar.gz: 1e2bea6b72edd335ccc1706f10afab389c211740236e25220d171efb176d94e55802b6770922ffa9647d85465eeb649fb89323b6f081d9489b9fb8fbc280b533
@@ -1,2 +1,4 @@
1
+ require 'active_support'
2
+
1
3
  require 'pg_qtop/version'
2
4
  require 'pg_qtop/monitor'
@@ -8,16 +8,36 @@ module PgQtop
8
8
  class CLIHelper
9
9
  include Mixlib::CLI
10
10
 
11
- option :database,
11
+ option :dbname,
12
12
  short: "-d DATABASE",
13
13
  long: "--database DATABASE",
14
- description: "The database to be tracked",
14
+ description: "database name to connect to",
15
15
  required: true
16
16
 
17
+ option :host,
18
+ short: "-h HOSTNAME",
19
+ long: "--host HOSTNAME",
20
+ description: "database server host"
21
+
22
+ option :port,
23
+ short: "-p PORT",
24
+ long: "--port PORT",
25
+ description: "database server port (default: 5432)"
26
+
27
+ option :user,
28
+ short: "-U USERNAME",
29
+ long: "--username USERNAME",
30
+ description: "database user name"
31
+
17
32
  option :table,
18
33
  short: "-t TABLE",
19
34
  long: "--table TABLE",
20
35
  description: "Only show queries that use the specified table"
36
+
37
+ option :statement_type,
38
+ short: "-s TYPE",
39
+ long: "--statement-type TYPE",
40
+ description: "Only show queries of the specified type (SELECT, INSERT, UPDATE or DELETE)"
21
41
  end
22
42
 
23
43
  module Monitor
@@ -30,29 +50,50 @@ module PgQtop
30
50
  Curses.noecho
31
51
  Curses.init_screen
32
52
 
33
- conn = PG::Connection.open(dbname: cli.config[:database])
34
-
53
+ conn = PG::Connection.open cli.config.slice(:dbname, :host, :port, :user)
35
54
  conn.exec('SELECT pg_stat_statements_reset()')
36
55
 
37
56
  while true do
38
57
  Curses.setpos(0, 0)
39
- queries = conn.exec('SELECT query, calls, total_time FROM pg_stat_statements').to_a
58
+ queries = conn.exec('SELECT query, calls, total_time, shared_blks_read, shared_blks_hit FROM pg_stat_statements').to_a
40
59
 
41
60
  queries = queries.sort_by {|q| (q["total_time"].to_f / q["calls"].to_f) }.reverse
42
- Curses.addstr("AVG\t| QUERY\n")
61
+
62
+ Curses.addstr("AVG\t| CALLS\t| HIT RATE\t| QUERY\n")
43
63
  Curses.addstr("-" * 80 + "\n")
64
+
44
65
  queries.each do |query|
45
- if cli.config[:table]
46
- next unless PgQuery.parse(query["query"]).tables.include?(cli.config[:table])
66
+ parsed_query = PgQuery.parse(query["query"])
67
+
68
+ next if cli.config[:statement_type] && !matches_statement_type?(parsed_query, cli.config[:statement_type])
69
+ next if cli.config[:table] && !parsed_query.tables.include?(cli.config[:table])
70
+
71
+ hit_rate = 100.0 * query["shared_blks_hit"].to_f / (query["shared_blks_hit"].to_i + query["shared_blks_read"].to_i)
72
+
73
+ Curses.addstr format("%0.1fms\t", query["total_time"].to_f / query["calls"].to_f)
74
+ Curses.addstr format("| %d\t", query["calls"].to_i)
75
+
76
+ if hit_rate.nan?
77
+ Curses.addstr "| -\t\t"
78
+ else
79
+ Curses.addstr format("| %0.1f\t\t", hit_rate)
47
80
  end
48
- Curses.addstr("%0.1fms\t" % (query["total_time"].to_f / query["calls"].to_f))
49
- Curses.addstr("| " + query["query"].gsub(/\s+/, " ").strip + "\n")
81
+ Curses.addstr format("| %s\n", query["query"].gsub(/\s+/, " ").strip)
50
82
  end
83
+
51
84
  Curses.refresh
85
+
52
86
  sleep 1
53
87
  end
54
88
 
55
89
  Curses.close_screen
56
90
  end
91
+
92
+ def matches_statement_type?(parsed_query, filter)
93
+ filter = filter.upcase
94
+ filter = 'INSERT INTO' if filter == 'INSERT'
95
+ filter = 'DELETE FROM' if filter == 'DELETE'
96
+ parsed_query.parsetree.flat_map {|q| q.keys }.include?(filter)
97
+ end
57
98
  end
58
99
  end
@@ -1,3 +1,3 @@
1
1
  module PgQtop
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_qtop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lukas Fittl
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: activesupport
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  description: Shows the top queries running on your server using pg_stat_statements
84
98
  email: lukas@fittl.com
85
99
  executables: