Sha256: 084b7ce8e3f57fd2e407c94679648c0b480c9cdd0d59d0664b8d855e707fad3c
Contents?: true
Size: 1.27 KB
Versions: 6
Compression:
Stored size: 1.27 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, total_time, calls = 1) 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] += calls @top_queries[fingerprint][:total_time] += total_time @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
6 entries across 6 versions & 1 rubygems