Sha256: 7babe874c9d4af6b19326f714e3050eb1cc50f5d2276cbf883f6649572ff3c03

Contents?: true

Size: 1.73 KB

Versions: 11

Compression:

Stored size: 1.73 KB

Contents

module PgHero
  module Methods
    module Connections
      def total_connections
        select_all("SELECT COUNT(*) FROM pg_stat_activity WHERE pid <> pg_backend_pid()").first["count"].to_i
      end

      def connection_sources(options = {})
        if options[:by_database_and_user]
          select_all <<-SQL
            SELECT
              datname AS database,
              usename AS user,
              application_name AS source,
              client_addr AS ip,
              COUNT(*) AS total_connections
            FROM
              pg_stat_activity
            WHERE
              pid <> pg_backend_pid()
            GROUP BY
              1, 2, 3, 4
            ORDER BY
              5 DESC, 1, 2, 3, 4
          SQL
        elsif options[:by_database]
          select_all <<-SQL
            SELECT
              application_name AS source,
              client_addr AS ip,
              datname AS database,
              COUNT(*) AS total_connections
            FROM
              pg_stat_activity
            WHERE
              pid <> pg_backend_pid()
            GROUP BY
              1, 2, 3
            ORDER BY
              COUNT(*) DESC,
              application_name ASC,
              client_addr ASC
          SQL
        else
          select_all <<-SQL
            SELECT
              application_name AS source,
              client_addr AS ip,
              COUNT(*) AS total_connections
            FROM
              pg_stat_activity
            WHERE
              pid <> pg_backend_pid()
            GROUP BY
              application_name,
              ip
            ORDER BY
              COUNT(*) DESC,
              application_name ASC,
              client_addr ASC
          SQL
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
pghero-1.7.0 lib/pghero/methods/connections.rb
pghero-1.6.5 lib/pghero/methods/connections.rb
pghero-1.6.4 lib/pghero/methods/connections.rb
pghero-1.6.3 lib/pghero/methods/connections.rb
pghero-1.6.2 lib/pghero/methods/connections.rb
pghero-1.6.1 lib/pghero/methods/connections.rb
pghero-1.6.0 lib/pghero/methods/connections.rb
pghero-1.5.3 lib/pghero/methods/connections.rb
pghero-1.5.2 lib/pghero/methods/connections.rb
pghero-1.5.1 lib/pghero/methods/connections.rb
pghero-1.5.0 lib/pghero/methods/connections.rb