query_owl 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6a44534cc84c3ea8089418405a3291098f3c35fb6ee3cb6b67d1ebea3cce4a62
4
- data.tar.gz: 40272edc4665655ae1fda469819eab1158951fc4a75ea6e417132d5ae80c6103
3
+ metadata.gz: 5db617fa85bce1a43d851809139bd43015afd92263bd203ac0114921dd94c525
4
+ data.tar.gz: 4ebf287c62f2779448c2277d416147d7b426332a2dd927f6a1a8485498c76638
5
5
  SHA512:
6
- metadata.gz: 8f97a828e36d1fe5a8acee9e6dc42198895e12a92a63d02dfb0b91aa35541a5062d5e439ef4035b0ca486655c2e0ea90fbe5efd31eaa8eb93c3b7d26d3184cc6
7
- data.tar.gz: 943ad7e59234e171da3831b3b4bfbdd9f43668d413cbd17efba1803f4b225e198d7e047723c4c987c6b85720ad1fc13f1574a210b31c38e6e6f051cc6a3f7c1d
6
+ metadata.gz: e3ec2642c611837a83dc96fdf8fc7eb669a99c6ae140fc3ad5bb1439a5b2db4349a9038a87a4af8e61fe1156e2d6fcbe43535641a2c51dd3bbcc41f9652ec88c
7
+ data.tar.gz: 2b2fe57db0a34c3289ef76a6170c4ea0d36d441eb733571ce87d8b506e1772b54d1e63841a8719d009a7ffd52a708b8ce5ad19c36e22839ceb3d33111700ae8b
data/README.md CHANGED
@@ -14,6 +14,7 @@ A leaner alternative to Bullet. QueryOwl detects N+1 queries, slow queries, and
14
14
  - [Installation](#installation)
15
15
  - [Configuration](#configuration)
16
16
  - [Log Output](#log-output)
17
+ - [Dashboard Endpoint](#dashboard-endpoint)
17
18
  - [Manual Testing in the Dummy App](#manual-testing-in-the-dummy-app)
18
19
  - [Roadmap](#roadmap)
19
20
  - [Contributing](#contributing)
@@ -67,6 +68,8 @@ QueryOwl.configure do |config|
67
68
  config.backtrace_lines = 5 # number of backtrace frames to capture
68
69
  config.backtrace_filter = ->(line) { line.start_with?("app/") } # optional custom filter
69
70
  config.raise_on_n_plus_one = false # set true in CI to raise instead of log
71
+ config.event_store_size = 100 # ring buffer capacity
72
+ config.dashboard_enabled = Rails.env.development? # HTML view on/off
70
73
  end
71
74
  ```
72
75
 
@@ -89,6 +92,45 @@ When a problem is detected, QueryOwl writes a structured line to `Rails.logger`:
89
92
 
90
93
  ---
91
94
 
95
+ ## Dashboard Endpoint
96
+
97
+ Mount the engine in your host app's routes to enable the JSON endpoint:
98
+
99
+ ```ruby
100
+ # config/routes.rb
101
+ mount QueryOwl::Engine => "/rails"
102
+ ```
103
+
104
+ Then browse the HTML dashboard or query JSON at `GET /rails/slow_queries`:
105
+
106
+ ```
107
+ GET /rails/slow_queries # HTML dashboard (browser)
108
+ GET /rails/slow_queries.json # JSON array
109
+ GET /rails/slow_queries?type=n_plus_one
110
+ GET /rails/slow_queries?type=slow_query
111
+ GET /rails/slow_queries?type=unused_eager_load
112
+ ```
113
+
114
+ The HTML view is enabled when `config.dashboard_enabled` is `true` (default in development); returns `403` otherwise. The JSON endpoint is always available.
115
+
116
+ The JSON response is an array of event objects, newest first, up to `config.event_store_size` entries:
117
+
118
+ ```json
119
+ [
120
+ {
121
+ "type": "n_plus_one",
122
+ "sql": "SELECT * FROM posts WHERE user_id = ?",
123
+ "count": 5,
124
+ "backtrace": ["app/controllers/posts_controller.rb:12"],
125
+ "recorded_at": "2026-06-15T18:00:00.000Z"
126
+ }
127
+ ]
128
+ ```
129
+
130
+ [↑ Back to top](#table-of-contents)
131
+
132
+ ---
133
+
92
134
  ## Manual Testing in the Dummy App
93
135
 
94
136
  The gem ships with a minimal Rails app in `spec/dummy/` for manual verification.
@@ -0,0 +1,23 @@
1
+ *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
2
+
3
+ :root {
4
+ --bg: #f8f9fa;
5
+ --surface: #ffffff;
6
+ --border: #dee2e6;
7
+ --text: #212529;
8
+ --muted: #6c757d;
9
+ --radius: 6px;
10
+ --shadow: 0 1px 3px rgba(0,0,0,.08);
11
+ }
12
+
13
+ body {
14
+ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
15
+ font-size: 14px;
16
+ line-height: 1.5;
17
+ color: var(--text);
18
+ background: var(--bg);
19
+ padding: 2rem;
20
+ }
21
+
22
+ .qo-monospace { font-family: ui-monospace, "SFMono-Regular", Menlo, monospace; font-size: 12px; }
23
+ .qo-muted { color: var(--muted); font-size: 12px; }
@@ -0,0 +1,5 @@
1
+ .qo-header { margin-bottom: 1.5rem; }
2
+ .qo-header h1 { font-size: 1.4rem; font-weight: 600; }
3
+ .qo-header p { color: var(--muted); margin-top: 0.2rem; }
4
+
5
+ .qo-empty { color: var(--muted); font-style: italic; margin-top: 1rem; }
@@ -0,0 +1,29 @@
1
+ .qo-table {
2
+ width: 100%;
3
+ border-collapse: collapse;
4
+ background: var(--surface);
5
+ border-radius: var(--radius);
6
+ overflow: hidden;
7
+ box-shadow: var(--shadow);
8
+ }
9
+
10
+ .qo-table th {
11
+ text-align: left;
12
+ padding: 0.6rem 1rem;
13
+ background: #f0f0f0;
14
+ font-weight: 600;
15
+ font-size: 11px;
16
+ text-transform: uppercase;
17
+ letter-spacing: .04em;
18
+ color: var(--muted);
19
+ border-bottom: 1px solid var(--border);
20
+ }
21
+
22
+ .qo-table td {
23
+ padding: 0.6rem 1rem;
24
+ border-bottom: 1px solid #eee;
25
+ vertical-align: top;
26
+ }
27
+
28
+ .qo-table tr:last-child td { border-bottom: none; }
29
+ .qo-table tr:hover td { background: #fafafa; }
@@ -0,0 +1,13 @@
1
+ .qo-badge {
2
+ display: inline-block;
3
+ padding: 2px 8px;
4
+ border-radius: 3px;
5
+ font-size: 11px;
6
+ font-weight: 600;
7
+ text-transform: uppercase;
8
+ white-space: nowrap;
9
+ }
10
+
11
+ .qo-badge--n_plus_one { background: #fde8e8; color: #c0392b; }
12
+ .qo-badge--slow_query { background: #fef3e2; color: #e67e22; }
13
+ .qo-badge--unused_eager_load { background: #e8f4fd; color: #2980b9; }
@@ -0,0 +1,28 @@
1
+ module QueryOwl
2
+ class SlowQueriesController < ActionController::Base
3
+ protect_from_forgery with: :null_session
4
+ layout "query_owl/application"
5
+ helper QueryOwl::ApplicationHelper
6
+
7
+ before_action :check_dashboard_enabled, if: -> { request.format.html? }
8
+
9
+ def index
10
+ filters = request.query_parameters
11
+ events = EventStore.all
12
+ events = events.select { |e| e[:type].to_s == filters["type"] } if filters["type"].present?
13
+ events = events.select { |e| e[:controller] == filters["controller"] } if filters["controller"].present?
14
+ events = events.select { |e| e[:action] == filters["action"] } if filters["action"].present?
15
+
16
+ respond_to do |format|
17
+ format.json { render json: events }
18
+ format.html { @events = events.reverse }
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def check_dashboard_enabled
25
+ head :forbidden unless QueryOwl.config.dashboard_enabled
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,9 @@
1
+ module QueryOwl
2
+ module ApplicationHelper
3
+ def inline_styles
4
+ dir = QueryOwl::Engine.root.join("app/assets/stylesheets/query_owl")
5
+ css = dir.glob("_*.css").sort.map(&:read).join("\n")
6
+ content_tag(:style, css.html_safe)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,13 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1">
6
+ <title>QueryOwl</title>
7
+ <link rel="icon" href="data:,">
8
+ <%= inline_styles %>
9
+ </head>
10
+ <body>
11
+ <%= yield %>
12
+ </body>
13
+ </html>
@@ -0,0 +1,34 @@
1
+ <div class="qo-header">
2
+ <h1>QueryOwl</h1>
3
+ <p>Last <%= @events.length %> detected event<%= "s" unless @events.length == 1 %> (newest first)</p>
4
+ </div>
5
+
6
+ <% if @events.empty? %>
7
+ <p class="qo-empty">No events detected yet.</p>
8
+ <% else %>
9
+ <table class="qo-table">
10
+ <thead>
11
+ <tr>
12
+ <th>Type</th>
13
+ <th>SQL / Details</th>
14
+ <th>Info</th>
15
+ <th>Recorded At</th>
16
+ <th>Backtrace</th>
17
+ </tr>
18
+ </thead>
19
+ <tbody>
20
+ <% @events.each do |event| %>
21
+ <tr>
22
+ <td><span class="qo-badge qo-badge--<%= event[:type] %>"><%= event[:type].to_s.tr("_", " ") %></span></td>
23
+ <td class="qo-monospace"><%= event[:sql] || "#{event[:model]}##{event[:association]}" %></td>
24
+ <td class="qo-muted">
25
+ <% if event[:count] %>count: <%= event[:count] %><% end %>
26
+ <% if event[:duration_ms] %><%= event[:duration_ms] %>ms<% end %>
27
+ </td>
28
+ <td class="qo-muted"><%= event[:recorded_at]&.strftime("%H:%M:%S") %></td>
29
+ <td class="qo-monospace qo-muted"><%= Array(event[:backtrace]).first %></td>
30
+ </tr>
31
+ <% end %>
32
+ </tbody>
33
+ </table>
34
+ <% end %>
data/config/routes.rb CHANGED
@@ -1,2 +1,3 @@
1
1
  QueryOwl::Engine.routes.draw do
2
+ get "slow_queries", to: "slow_queries#index"
2
3
  end
@@ -5,7 +5,7 @@ module QueryOwl
5
5
 
6
6
  attr_reader :log_level, :backtrace_filter
7
7
  attr_accessor :enabled, :slow_query_threshold_ms, :n_plus_one_threshold, :backtrace_lines,
8
- :raise_on_n_plus_one
8
+ :raise_on_n_plus_one, :event_store_size, :dashboard_enabled
9
9
 
10
10
  def initialize
11
11
  @enabled = Rails.env.development?
@@ -15,6 +15,8 @@ module QueryOwl
15
15
  @backtrace_lines = 5
16
16
  @backtrace_filter = DEFAULT_BACKTRACE_FILTER
17
17
  @raise_on_n_plus_one = false
18
+ @event_store_size = 100
19
+ @dashboard_enabled = Rails.env.development?
18
20
  end
19
21
 
20
22
  def log_level=(level)
@@ -0,0 +1,59 @@
1
+ module QueryOwl
2
+ module EventStore
3
+ class << self
4
+ def push(event)
5
+ mutex.synchronize do
6
+ ensure_buffer_size
7
+ buffer[@write_pos] = event.merge(recorded_at: Time.now)
8
+ @write_pos = (@write_pos + 1) % capacity
9
+ @stored = [@stored + 1, capacity].min
10
+ end
11
+ end
12
+
13
+ def all
14
+ mutex.synchronize do
15
+ stored = @stored || 0
16
+ return [] if stored.zero?
17
+
18
+ if stored < capacity
19
+ buffer.first(stored)
20
+ else
21
+ buffer[@write_pos..] + buffer[0...@write_pos]
22
+ end
23
+ end
24
+ end
25
+
26
+ def clear
27
+ mutex.synchronize { reset! }
28
+ end
29
+
30
+ def size
31
+ mutex.synchronize { @stored || 0 }
32
+ end
33
+
34
+ private
35
+
36
+ def mutex
37
+ @mutex ||= Mutex.new
38
+ end
39
+
40
+ def capacity
41
+ QueryOwl.config.event_store_size
42
+ end
43
+
44
+ def ensure_buffer_size
45
+ reset! if @write_pos.nil? || @buffer.nil? || @buffer.size != capacity
46
+ end
47
+
48
+ def buffer
49
+ @buffer ||= Array.new(capacity)
50
+ end
51
+
52
+ def reset!
53
+ @buffer = Array.new(capacity)
54
+ @write_pos = 0
55
+ @stored = 0
56
+ end
57
+ end
58
+ end
59
+ end
@@ -25,6 +25,7 @@ module QueryOwl
25
25
  Detector.detect_unused_eager_loads(eager_data)
26
26
  Logger.log_events(events)
27
27
  Logger.log_summary(events)
28
+ events.each { |e| EventStore.push(e) }
28
29
  raise_on_n_plus_one!(events) if QueryOwl.config.raise_on_n_plus_one
29
30
  end
30
31
  end
@@ -1,3 +1,3 @@
1
1
  module QueryOwl
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/query_owl.rb CHANGED
@@ -2,6 +2,7 @@ require "query_owl/version"
2
2
  require "query_owl/configuration"
3
3
  require "query_owl/query_tracker"
4
4
  require "query_owl/eager_load_tracker"
5
+ require "query_owl/event_store"
5
6
  require "query_owl/detector"
6
7
  require "query_owl/logger"
7
8
  require "query_owl/middleware"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: query_owl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chuck Smith
@@ -34,16 +34,25 @@ files:
34
34
  - MIT-LICENSE
35
35
  - README.md
36
36
  - Rakefile
37
+ - app/assets/stylesheets/query_owl/_01_base.css
38
+ - app/assets/stylesheets/query_owl/_02_layout.css
39
+ - app/assets/stylesheets/query_owl/_03_table.css
40
+ - app/assets/stylesheets/query_owl/_04_badges.css
37
41
  - app/controllers/query_owl/application_controller.rb
42
+ - app/controllers/query_owl/slow_queries_controller.rb
43
+ - app/helpers/query_owl/application_helper.rb
38
44
  - app/jobs/query_owl/application_job.rb
39
45
  - app/mailers/query_owl/application_mailer.rb
40
46
  - app/models/query_owl/application_record.rb
47
+ - app/views/layouts/query_owl/application.html.erb
48
+ - app/views/query_owl/slow_queries/index.html.erb
41
49
  - config/routes.rb
42
50
  - lib/query_owl.rb
43
51
  - lib/query_owl/configuration.rb
44
52
  - lib/query_owl/detector.rb
45
53
  - lib/query_owl/eager_load_tracker.rb
46
54
  - lib/query_owl/engine.rb
55
+ - lib/query_owl/event_store.rb
47
56
  - lib/query_owl/logger.rb
48
57
  - lib/query_owl/middleware.rb
49
58
  - lib/query_owl/query_tracker.rb