Sha256: 3371c1665bf572908ef0b398fc344f67e6f6b5a1608c42d43cd70ecec674d32a

Contents?: true

Size: 1.21 KB

Versions: 6

Compression:

Stored size: 1.21 KB

Contents

module Dexter
  class PgStatActivityParser < LogParser
    def perform
      previous_queries = {}

      10.times do
        active_queries = {}
        processed_queries = {}

        stat_activity.each do |row|
          if row["state"] == "active"
            active_queries[row["id"]] = row
          else
            process_entry(row["query"], row["duration_ms"].to_f)
            processed_queries[row["id"]] = true
          end
        end

        # store queries after they complete
        previous_queries.each do |id, row|
          if !active_queries[id] && !processed_queries[id]
            process_entry(row["query"], row["duration_ms"].to_f)
          end
        end

        previous_queries = active_queries

        sleep(0.1)
      end
    end

    def stat_activity
      sql = <<~SQL
        SELECT
          pid || ':' || COALESCE(query_start, xact_start) AS id,
          query,
          state,
          EXTRACT(EPOCH FROM NOW() - COALESCE(query_start, xact_start)) * 1000.0 AS duration_ms
        FROM
          pg_stat_activity
        WHERE
          datname = current_database()
          AND pid != pg_backend_pid()
        ORDER BY
          1
      SQL
      @logfile.send(:execute, sql)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
pgdexter-0.5.5 lib/dexter/pg_stat_activity_parser.rb
pgdexter-0.5.4 lib/dexter/pg_stat_activity_parser.rb
pgdexter-0.5.3 lib/dexter/pg_stat_activity_parser.rb
pgdexter-0.5.2 lib/dexter/pg_stat_activity_parser.rb
pgdexter-0.5.1 lib/dexter/pg_stat_activity_parser.rb
pgdexter-0.5.0 lib/dexter/pg_stat_activity_parser.rb