Sha256: ab8baeb6f0aef25599e8e2f1c6cc41a7225e3ad1056164acf0d0e312e6173e26

Contents?: true

Size: 1.25 KB

Versions: 8

Compression:

Stored size: 1.25 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]
          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

8 entries across 8 versions & 1 rubygems

Version Path
pghero-1.4.2 lib/pghero/methods/connections.rb
pghero-1.4.1 lib/pghero/methods/connections.rb
pghero-1.4.0 lib/pghero/methods/connections.rb
pghero-1.3.2 lib/pghero/methods/connections.rb
pghero-1.3.1 lib/pghero/methods/connections.rb
pghero-1.3.0 lib/pghero/methods/connections.rb
pghero-1.2.4 lib/pghero/methods/connections.rb
pghero-1.2.3 lib/pghero/methods/connections.rb