Class: ActiveTracker::Plugin::Query

Inherits:
Base
  • Object
show all
Defined in:
lib/active_tracker/plugin/query.rb

Class Method Summary collapse

Methods inherited from Base

nav_url

Class Method Details

.capture_query(event) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/active_tracker/plugin/query.rb', line 78

def self.capture_query(event)
  tags = {
    sql: event.payload[:sql],
    name: event.payload[:name],
  }

  return if filter_query?(tags)

  ActiveTracker::Model.find_or_create("Query", tags:tags, data_type: "full") do |obj|
    if obj.persisted
      ActiveTracker::Model.delete(obj.key)
    end
    obj.data ||= {}
    obj.data["last_duration"] = event.duration
    obj.data["count"] = (obj.data["count"] || 0) + 1
    # Enough for most git commits to be referenced
    # so should be fine for SQL query hashes within an application
    obj.id = "Q" + Digest::SHA2.hexdigest(tags.inspect)[0,8]
    obj.expiry = 7.days
    obj.log_at = Time.now

    obj.data["at_requests"] ||= []
    if ActiveTracker::Plugin::Request.registered?
      begin
        id = ActiveTracker::Plugin::Request.current_tags[:id] rescue nil
        obj.data["at_requests"].prepend(id) if id.present?
        obj.data["at_requests"] = obj.data["at_requests"][0,20]
        ActiveTracker::Plugin::Request.current_tags[:at_queries] ||= []
        ActiveTracker::Plugin::Request.current_tags[:at_queries] << obj.id
      rescue Exception, ActiveRecord::StatementInvalid, NoMethodError
        # Sometimes during initial DB migration this will fail to insert
        # the current object
      end
    end
  end
end

.filter_query?(details) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/active_tracker/plugin/query.rb', line 115

def self.filter_query?(details)
  ActiveTracker::Plugin::Query.filters.each do |filter|
    if filter.is_a?(Regexp)
      if filter.match(details[:sql] || "") || filter.match(details[:name] || "")
        return true
      end
    else
      if (details[:sql] || "").include?(filter) || (details[:name] || "").include?(filter)
        return true
      end
    end
  end

  false
end

.filtersObject



66
67
68
# File 'lib/active_tracker/plugin/query.rb', line 66

def self.filters
  @filters ||= ["SCHEMA", /^$/]
end

.filters=(value) ⇒ Object



62
63
64
# File 'lib/active_tracker/plugin/query.rb', line 62

def self.filters=(value)
  @filters = value
end

.min_slow_duration_msObject



74
75
76
# File 'lib/active_tracker/plugin/query.rb', line 74

def self.min_slow_duration_ms
  @min_slow_duration_ms ||= 100
end

.min_slow_duration_ms=(value) ⇒ Object



70
71
72
# File 'lib/active_tracker/plugin/query.rb', line 70

def self.min_slow_duration_ms=(value)
  @min_slow_duration_ms = value
end


23
24
25
26
27
28
# File 'lib/active_tracker/plugin/query.rb', line 23

def self.nav_svg
  svg = <<~EOF
  <svg aria-hidden="true" focusable="false" data-prefix="fas" data-icon="database" class="fill-current" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512" width="16" height="16"><path d="M448 73.143v45.714C448 159.143 347.667 192 224 192S0 159.143 0 118.857V73.143C0 32.857 100.333 0 224 0s224 32.857 224 73.143zM448 176v102.857C448 319.143 347.667 352 224 352S0 319.143 0 278.857V176c48.125 33.143 136.208 48.572 224 48.572S399.874 209.143 448 176zm0 160v102.857C448 479.143 347.667 512 224 512S0 479.143 0 438.857V336c48.125 33.143 136.208 48.572 224 48.572S399.874 369.143 448 336z"></path></svg>
  EOF
  svg.html_safe
end


30
31
32
# File 'lib/active_tracker/plugin/query.rb', line 30

def self.nav_title
  "Queries"
end

.registerObject



6
7
8
9
10
11
12
13
# File 'lib/active_tracker/plugin/query.rb', line 6

def self.register
  ActiveSupport::Notifications.subscribe "sql.active_record" do |*args|
    event = ActiveSupport::Notifications::Event.new(*args)
    capture_query(event)
  end

  @@registered = true
end

.registered?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/active_tracker/plugin/query.rb', line 15

def self.registered?
  @@registered rescue false
end

.resources_nameObject



19
20
21
# File 'lib/active_tracker/plugin/query.rb', line 19

def self.resources_name
  :queries
end

.statisticsObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/active_tracker/plugin/query.rb', line 34

def self.statistics
  ret = []
  queries = ActiveTracker::Model.all("Query")
  queries = queries.select {|e| e.log_at >= 60.minutes.ago}

  num_queries = 0
  slow_queries = 0
  total_duration = 0

  queries.each do |query|
    actual_query = ActiveTracker::Model.find(query.key)
    next unless actual_query
    slow_queries += actual_query.count if actual_query.last_duration > self.min_slow_duration_ms
    num_queries += actual_query.count
    total_duration += actual_query.last_duration * actual_query.count
  end

  ret << {plugin: self, label: "Queries/hour", value: num_queries}
  if slow_queries == 0
    ret << {plugin: self, label: "Slow queries/hour", value: slow_queries}
  else
    ret << {plugin: self, label: "Slow queries/hour", value: slow_queries, error: true}
  end
  ret << {plugin: self, label: "Avg time/query", value: "%.2fms" % (total_duration/num_queries)} if num_queries > 0

  ret
end