Sha256: a3efa6eba4fdcd55fd20538e417c81ac622cf2a4084fe52bf0fcd1322663afe4

Contents?: true

Size: 915 Bytes

Versions: 5

Compression:

Stored size: 915 Bytes

Contents

module PgHero
  module Methods
    module Connections
      def total_connections
        select_one("SELECT COUNT(*) FROM pg_stat_activity")
      end

      def connection_states
        states = select_all <<-SQL
          SELECT
            state,
            COUNT(*) AS connections
          FROM
            pg_stat_activity
          GROUP BY
            1
          ORDER BY
            2 DESC, 1
        SQL

        Hash[states.map { |s| [s[:state], s[:connections]] }]
      end

      def connection_sources
        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
          GROUP BY
            1, 2, 3, 4
          ORDER BY
            5 DESC, 1, 2, 3, 4
        SQL
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
pghero-2.4.1 lib/pghero/methods/connections.rb
pghero-2.4.0 lib/pghero/methods/connections.rb
pghero-2.3.0 lib/pghero/methods/connections.rb
pghero-2.2.1 lib/pghero/methods/connections.rb
pghero-2.2.0 lib/pghero/methods/connections.rb