pg_qtop 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5b68b70f3b6f49e8bdde29887334cd1fcb64fa35
4
+ data.tar.gz: 0275546c0da92a4372e28b30715a55e80ce32001
5
+ SHA512:
6
+ metadata.gz: 8dae0bbf5bbf819d426ce4e478b663aa2dfaea1ba6a7ac918e6cb19605346323c44c2d0a05e149ee6bd64e6b669cc45fd755fa52684110c0b5e6ca77e8173139
7
+ data.tar.gz: 854098cd74a01161f667224e1003a66f25820d1197cf7747c012c74cfd8d29427bf43ce9566a135d33fe4cd5710ca8d08a98f63d2d9b5e79eba58948b2dbd2d3
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task default: :spec
7
+ task test: :spec
data/bin/pg_qtop ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'pg_qtop'
4
+
5
+ PgQtop::Monitor.call
data/lib/pg_qtop.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'pg_qtop/version'
2
+ require 'pg_qtop/monitor'
@@ -0,0 +1,58 @@
1
+ require 'rubygems'
2
+ require 'mixlib/cli'
3
+ require 'pg'
4
+ require 'pg_query'
5
+ require 'curses'
6
+
7
+ module PgQtop
8
+ class CLIHelper
9
+ include Mixlib::CLI
10
+
11
+ option :database,
12
+ short: "-d DATABASE",
13
+ long: "--database DATABASE",
14
+ description: "The database to be tracked",
15
+ required: true
16
+
17
+ option :table,
18
+ short: "-t TABLE",
19
+ long: "--table TABLE",
20
+ description: "Only show queries that use the specified table"
21
+ end
22
+
23
+ module Monitor
24
+ extend self
25
+
26
+ def call
27
+ cli = CLIHelper.new
28
+ cli.parse_options
29
+
30
+ Curses.noecho
31
+ Curses.init_screen
32
+
33
+ conn = PG::Connection.open(dbname: cli.config[:database])
34
+
35
+ conn.exec('SELECT pg_stat_statements_reset()')
36
+
37
+ while true do
38
+ Curses.setpos(0, 0)
39
+ queries = conn.exec('SELECT query, calls, total_time FROM pg_stat_statements').to_a
40
+
41
+ queries = queries.sort_by {|q| (q["total_time"].to_f / q["calls"].to_f) }.reverse
42
+ Curses.addstr("AVG\t| QUERY\n")
43
+ Curses.addstr("-" * 80 + "\n")
44
+ queries.each do |query|
45
+ if cli.config[:table]
46
+ next unless PgQuery.parse(query["query"]).tables.include?(cli.config[:table])
47
+ 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")
50
+ end
51
+ Curses.refresh
52
+ sleep 1
53
+ end
54
+
55
+ Curses.close_screen
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,3 @@
1
+ module PgQtop
2
+ VERSION = '0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pg_qtop
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Lukas Fittl
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pg
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pg_query
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: curses
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: mixlib-cli
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Shows the top queries running on your server using pg_stat_statements
84
+ email: lukas@fittl.com
85
+ executables:
86
+ - pg_qtop
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - Rakefile
91
+ - bin/pg_qtop
92
+ - lib/pg_qtop.rb
93
+ - lib/pg_qtop/monitor.rb
94
+ - lib/pg_qtop/version.rb
95
+ homepage: http://github.com/lfittl/pg_qtop
96
+ licenses:
97
+ - BSD-3-Clause
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.4.5
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: PostgreSQL query monitor
119
+ test_files: []