Class: Dexter::PgStatActivityParser

Inherits:
LogParser
  • Object
show all
Defined in:
lib/dexter/pg_stat_activity_parser.rb

Constant Summary

Constants inherited from LogParser

LogParser::REGEX

Constants included from Logging

Logging::COLOR_CODES

Instance Method Summary collapse

Methods inherited from LogParser

#initialize

Methods included from Logging

#colorize, #log, #output

Constructor Details

This class inherits a constructor from Dexter::LogParser

Instance Method Details

#performObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/dexter/pg_stat_activity_parser.rb', line 3

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

#stat_activityObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/dexter/pg_stat_activity_parser.rb', line 32

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