Sha256: 56662eadead45f2fc101e009cb1ea5d3ced7d0bc55bbb8eeb22a3267ddb2c102

Contents?: true

Size: 1.25 KB

Versions: 15

Compression:

Stored size: 1.25 KB

Contents

module Dexter
  class Collector
    def initialize(options = {})
      @top_queries = {}
      @new_queries = Set.new
      @mutex = Mutex.new
      @min_time = options[:min_time] * 60000 # convert minutes to ms
      @min_calls = options[:min_calls]
    end

    def add(query, duration)
      fingerprint =
        begin
          PgQuery.fingerprint(query)
        rescue PgQuery::ParseError
          # do nothing
        end

      return unless fingerprint

      @top_queries[fingerprint] ||= {calls: 0, total_time: 0}
      @top_queries[fingerprint][:calls] += 1
      @top_queries[fingerprint][:total_time] += duration
      @top_queries[fingerprint][:query] = query
      @mutex.synchronize do
        @new_queries << fingerprint
      end
    end

    def fetch_queries
      new_queries = nil

      @mutex.synchronize do
        new_queries = @new_queries.dup
        @new_queries.clear
      end

      queries = []
      @top_queries.each do |k, v|
        if new_queries.include?(k) && v[:total_time] >= @min_time && v[:calls] >= @min_calls
          query = Query.new(v[:query], k)
          query.total_time = v[:total_time]
          query.calls = v[:calls]
          queries << query
        end
      end

      queries.sort_by(&:fingerprint)
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
pgdexter-0.4.3 lib/dexter/collector.rb
pgdexter-0.4.2 lib/dexter/collector.rb
pgdexter-0.4.1 lib/dexter/collector.rb
pgdexter-0.4.0 lib/dexter/collector.rb
pgdexter-0.3.10 lib/dexter/collector.rb
pgdexter-0.3.9 lib/dexter/collector.rb
pgdexter-0.3.8 lib/dexter/collector.rb
pgdexter-0.3.7 lib/dexter/collector.rb
pgdexter-0.3.6 lib/dexter/collector.rb
pgdexter-0.3.5 lib/dexter/collector.rb
pgdexter-0.3.4 lib/dexter/collector.rb
pgdexter-0.3.3 lib/dexter/collector.rb
pgdexter-0.3.2 lib/dexter/collector.rb
pgdexter-0.3.1 lib/dexter/collector.rb
pgdexter-0.3.0 lib/dexter/collector.rb