Permalink
Browse files

make script/db_activity more pasteable

  • Loading branch information...
1 parent f8cefa1 commit 4ac1db3d65d85c13e98ac58b240edc15a0b91b5d @svenfuchs svenfuchs committed Feb 10, 2013
Showing with 16 additions and 12 deletions.
  1. +16 −12 script/db_activity
View
28 script/db_activity
@@ -4,19 +4,23 @@ require 'bundler/setup'
require 'travis'
Travis::Database.connect
-cols = %w(procpid current_query query_start)
-sql = "SELECT #{cols.join(', ')} FROM pg_stat_activity WHERE current_query <> '<IDLE>'"
-rows = ActiveRecord::Base.connection.select_all(sql).map do |row|
- row['current_query'] = row['current_query'][0..120] if row['current_query']
- row['query_start'] = row['query_start'].split('.')[0..-2].join if row['query_start']
- row
-end
+def list_queries
+ cols = %w(procpid current_query query_start)
+ sql = "SELECT #{cols.join(', ')} FROM pg_stat_activity WHERE current_query <> '<IDLE>'"
+ rows = ActiveRecord::Base.connection.select_all(sql).map do |row|
+ row['current_query'] = row['current_query'][0..120] if row['current_query']
+ row['query_start'] = row['query_start'].split('.')[0..-2].join if row['query_start']
+ row
+ end
+
+ widths = Hash[*cols.map { |col| [col, rows.map { |row| row[col].to_s.size }.max] }.flatten]
-widths = Hash[*cols.map { |col| [col, rows.map { |row| row[col].to_s.size }.max] }.flatten]
+ out = rows.map do |row|
+ '| ' + widths.map { |col, width| row[col].to_s.ljust(width) }.join(' | ') + ' |'
+ end
-out = rows.map do |row|
- '| ' + widths.map { |col, width| row[col].to_s.ljust(width) }.join(' | ') + ' |'
+ line = "+#{widths.values.map { |width| '-' * (width + 2) }.join('+')}+"
+ puts line, out, line
end
-line = "+#{widths.values.map { |width| '-' * (width + 2) }.join('+')}+"
-puts line, out, line
+list_queries

0 comments on commit 4ac1db3

Please sign in to comment.