mysql_genius-core 0.6.0 → 0.7.1

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: 01d397f796109a83914008b801549df18cec6c6b9363c7878f0aaa5b04b5a160
4
- data.tar.gz: 4bdd6cfbee56cffd41feffcadc287ae104b5d5f71fbb2d59a4e85dbcb0da567e
3
+ metadata.gz: dfc2a8ccac7f377e556a0c5f51b843e97b4882f20a49b24ba41b5ab9e4623268
4
+ data.tar.gz: 170b7fcfbdfab6e9aa20db0fef7dcbcf3d86018c8b41c35a0cbe927c3a8961c5
5
5
  SHA512:
6
- metadata.gz: bc74723d3f13499a57b115d857732f3061787d1b442c509ba89b690e2614522b3a921a5c39f3b52e2e420a2f0513de1dc41acb7b34613445b85e30f9c30730e6
7
- data.tar.gz: ec977e844a1fd46189560c332a6d46e5b9482c2e83f9f6acd2646ae5e4eedf631847b34360f8ded330832f2ae1f0aa92f01247b168e9d33b27f2db9ac6a87e6a
6
+ metadata.gz: 4111e15ebb74f4139c854b7710f745509939228e5b8f55013fec9b69b0ed40249453686bee6c17254205c0fe907ed544104fdc049971a618d7376ff122019388
7
+ data.tar.gz: b2ddb49da235bfaac6ab6e75f9725d1f0162d6ded16ebf43dbd7e119d647c26cab3f12994213576636c5c8ee512722eb05ca3ee5ca9f229c594858b252051ff6
data/CHANGELOG.md CHANGED
@@ -1,5 +1,20 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.7.1
4
+
5
+ ### Fixed
6
+ - **ERB templates missing from gem package.** The `spec.files` glob in the gemspec only matched `*.rb` files, excluding the shared ERB templates at `lib/mysql_genius/core/views/`. The `mysql_genius-desktop` sidecar crashed with `Errno::ENOENT` when installed from RubyGems (vs path dependency). Fixed by changing the glob to `*.{rb,erb}`.
7
+
8
+ ## 0.7.0
9
+
10
+ ### Added
11
+ - `MysqlGenius::Core::Analysis::StatsHistory` — thread-safe in-memory ring buffer storing per-digest query performance snapshots. Supports `record`, `series_for`, `digests`, `clear`. Max 1440 samples per digest (24 hours at 60-second intervals).
12
+ - `MysqlGenius::Core::Analysis::StatsCollector` — background thread that samples `performance_schema.events_statements_summary_by_digest` at a configurable interval, computes per-interval deltas, and records them into a `StatsHistory` instance. Handles server restarts (negative deltas clamped to 0) and performance_schema unavailability (stops gracefully).
13
+ - `MysqlGenius::Core::Analysis::QueryStats` now includes `digest:` (the `DIGEST` hex hash from performance_schema) in its return value for stable URL keys.
14
+ - `capability?(name)` template helper contract — shared templates gate Redis-backed UI via `<% if capability?(:slow_queries) %>` guards. Rails adapter returns `true` for all names; the desktop sidecar returns `true` only for `:ai`.
15
+ - Query detail shared template (`query_detail.html.erb`) with SQL display, Explain button, stats cards, and inline SVG time-series charts.
16
+ - Query Stats dashboard tab now renders SQL cells as clickable links to `/queries/:digest`.
17
+
3
18
  ## 0.6.0
4
19
 
5
20
  No functional changes in `mysql_genius-core`. Version bumped to maintain lockstep with `mysql_genius 0.6.0`, which drops Rails 5.2 support from the Rails adapter. See the root `CHANGELOG.md` for the full change list.
@@ -42,6 +42,7 @@ module MysqlGenius
42
42
  def build_sql(order_clause, limit)
43
43
  <<~SQL
44
44
  SELECT
45
+ DIGEST,
45
46
  DIGEST_TEXT,
46
47
  COUNT_STAR AS calls,
47
48
  ROUND(SUM_TIMER_WAIT / 1000000000, 1) AS total_time_ms,
@@ -77,6 +78,7 @@ module MysqlGenius
77
78
  rows_sent = (row["rows_sent"] || row["ROWS_SENT"] || 0).to_i
78
79
 
79
80
  {
81
+ digest: (row["DIGEST"] || row["digest"] || "").to_s,
80
82
  sql: truncate(digest, 500),
81
83
  calls: calls,
82
84
  total_time_ms: (row["total_time_ms"] || 0).to_f,
@@ -0,0 +1,137 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MysqlGenius
4
+ module Core
5
+ module Analysis
6
+ # Background sampler that periodically queries performance_schema for the
7
+ # top 50 digests by SUM_TIMER_WAIT, computes per-interval deltas, and
8
+ # records snapshots into a StatsHistory ring buffer.
9
+ #
10
+ # The +connection_provider+ is a callable (lambda/proc) that returns a
11
+ # Core::Connection on each invocation. This allows each adapter to supply
12
+ # its own connection strategy:
13
+ #
14
+ # Rails: -> { ActiveRecordAdapter.new(ActiveRecord::Base.connection) }
15
+ # Desktop: -> { session.checkout { |c| c } }
16
+ class StatsCollector
17
+ DEFAULT_INTERVAL = 60
18
+ STOP_JOIN_TIMEOUT = 5
19
+ TOP_N = 50
20
+
21
+ def initialize(connection_provider:, history:, interval: DEFAULT_INTERVAL)
22
+ @connection_provider = connection_provider
23
+ @history = history
24
+ @interval = interval
25
+ @mutex = Mutex.new
26
+ @cv = ConditionVariable.new
27
+ @stop_signal = false
28
+ @running = false
29
+ @thread = nil
30
+ @previous = {}
31
+ end
32
+
33
+ def start
34
+ return self if @running
35
+
36
+ @stop_signal = false
37
+ @running = true
38
+ @thread = Thread.new { run_loop }
39
+ self
40
+ end
41
+
42
+ def stop
43
+ @mutex.synchronize do
44
+ @stop_signal = true
45
+ @cv.signal
46
+ end
47
+ @thread&.join(STOP_JOIN_TIMEOUT)
48
+ @thread = nil
49
+ end
50
+
51
+ def running?
52
+ @running
53
+ end
54
+
55
+ private
56
+
57
+ def run_loop
58
+ loop do
59
+ tick
60
+ break if wait_or_stop(@interval)
61
+ end
62
+ rescue StandardError => e
63
+ warn("[MysqlGenius] StatsCollector stopped: #{e.message}")
64
+ ensure
65
+ @running = false
66
+ end
67
+
68
+ def tick
69
+ connection = @connection_provider.call
70
+ result = connection.exec_query(build_sql(connection))
71
+ current = {}
72
+
73
+ result.to_hashes.each do |row|
74
+ digest_text = (row["DIGEST_TEXT"] || row["digest_text"]).to_s
75
+ next if digest_text.empty?
76
+
77
+ calls = (row["COUNT_STAR"] || row["count_star"]).to_i
78
+ total_time_ms = (row["total_time_ms"] || row["TOTAL_TIME_MS"] || 0).to_f
79
+
80
+ current[digest_text] = { calls: calls, total_time_ms: total_time_ms }
81
+
82
+ next unless @previous.key?(digest_text)
83
+
84
+ record_delta(digest_text, calls, total_time_ms)
85
+ end
86
+
87
+ @previous = current
88
+ end
89
+
90
+ def record_delta(digest_text, calls, total_time_ms)
91
+ prev = @previous[digest_text]
92
+ delta_calls = [calls - prev[:calls], 0].max
93
+ delta_total_ms = [(total_time_ms - prev[:total_time_ms]).round(1), 0.0].max
94
+
95
+ @history.record(digest_text, {
96
+ timestamp: Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ"),
97
+ calls: delta_calls,
98
+ total_time_ms: delta_total_ms,
99
+ avg_time_ms: delta_calls.positive? ? (delta_total_ms / delta_calls).round(1) : 0.0,
100
+ })
101
+ end
102
+
103
+ def build_sql(connection)
104
+ <<~SQL
105
+ SELECT
106
+ DIGEST_TEXT,
107
+ COUNT_STAR,
108
+ ROUND(SUM_TIMER_WAIT / 1000000000, 1) AS total_time_ms
109
+ FROM performance_schema.events_statements_summary_by_digest
110
+ WHERE SCHEMA_NAME = #{connection.quote(connection.current_database)}
111
+ AND DIGEST_TEXT IS NOT NULL
112
+ AND DIGEST_TEXT NOT LIKE 'EXPLAIN%'
113
+ AND DIGEST_TEXT NOT LIKE '%`information_schema`%'
114
+ AND DIGEST_TEXT NOT LIKE '%`performance_schema`%'
115
+ AND DIGEST_TEXT NOT LIKE '%information_schema.%'
116
+ AND DIGEST_TEXT NOT LIKE '%performance_schema.%'
117
+ AND DIGEST_TEXT NOT LIKE 'SHOW %'
118
+ AND DIGEST_TEXT NOT LIKE 'SET STATEMENT %'
119
+ AND DIGEST_TEXT NOT LIKE 'SELECT VERSION ( )%'
120
+ AND DIGEST_TEXT NOT LIKE 'SELECT @@%'
121
+ ORDER BY SUM_TIMER_WAIT DESC
122
+ LIMIT #{TOP_N}
123
+ SQL
124
+ end
125
+
126
+ def wait_or_stop(seconds)
127
+ @mutex.synchronize do
128
+ return true if @stop_signal
129
+
130
+ @cv.wait(@mutex, seconds)
131
+ @stop_signal
132
+ end
133
+ end
134
+ end
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MysqlGenius
4
+ module Core
5
+ module Analysis
6
+ # Thread-safe in-memory ring buffer that stores per-digest query stats
7
+ # snapshots. Each digest key maps to an array of snapshots capped at
8
+ # +max_samples+. Oldest entries are dropped when the cap is reached.
9
+ class StatsHistory
10
+ DEFAULT_MAX_SAMPLES = 1440
11
+
12
+ def initialize(max_samples: DEFAULT_MAX_SAMPLES)
13
+ @max_samples = max_samples
14
+ @mutex = Mutex.new
15
+ @data = {}
16
+ end
17
+
18
+ def record(digest_text, snapshot)
19
+ @mutex.synchronize do
20
+ buf = (@data[digest_text] ||= [])
21
+ buf << snapshot
22
+ buf.shift if buf.length > @max_samples
23
+ end
24
+ end
25
+
26
+ def series_for(digest_text)
27
+ @mutex.synchronize do
28
+ (@data[digest_text] || []).dup
29
+ end
30
+ end
31
+
32
+ def digests
33
+ @mutex.synchronize { @data.keys.dup }
34
+ end
35
+
36
+ def clear
37
+ @mutex.synchronize { @data.clear }
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module MysqlGenius
4
4
  module Core
5
- VERSION = "0.6.0"
5
+ VERSION = "0.7.1"
6
6
  end
7
7
  end
@@ -0,0 +1,56 @@
1
+ <!-- Explain Results -->
2
+ <div id="explain-results" class="mg-mt mg-hidden">
3
+ <div class="mg-card">
4
+ <div class="mg-card-header">
5
+ <span><strong>&#128270; EXPLAIN Output</strong></span>
6
+ <div>
7
+ <% if @ai_enabled %>
8
+ <button id="explain-optimize" class="mg-btn mg-btn-outline mg-btn-sm">&#9889; AI Optimization</button>
9
+ <button id="explain-index-advisor" class="mg-btn mg-btn-outline mg-btn-sm">&#9889; Index Advisor</button>
10
+ <% end %>
11
+ <button id="explain-close" class="mg-btn mg-btn-outline-secondary mg-btn-sm">&#10005; Close</button>
12
+ </div>
13
+ </div>
14
+ <div class="mg-card-body">
15
+ <div class="mg-table-wrap">
16
+ <table class="mg-table">
17
+ <thead id="explain-thead"></thead>
18
+ <tbody id="explain-tbody"></tbody>
19
+ </table>
20
+ </div>
21
+ <div id="optimize-results" class="mg-hidden mg-mt">
22
+ <div id="optimize-content" class="mg-alert mg-alert-info"></div>
23
+ </div>
24
+ </div>
25
+ </div>
26
+ </div>
27
+
28
+ <!-- Results Area -->
29
+ <div id="query-results" class="mg-mt">
30
+ <div id="results-alert" class="mg-hidden"></div>
31
+ <div id="results-stats" class="mg-mb mg-hidden">
32
+ <span id="results-row-count" class="mg-badge mg-badge-info"></span>
33
+ <span id="results-time" class="mg-badge mg-badge-secondary"></span>
34
+ <span id="results-truncated" class="mg-badge mg-badge-warning mg-hidden">Results truncated</span>
35
+ </div>
36
+ <div id="results-table-wrapper" class="mg-table-wrap mg-hidden">
37
+ <table class="mg-table">
38
+ <thead id="results-thead"></thead>
39
+ <tbody id="results-tbody"></tbody>
40
+ </table>
41
+ </div>
42
+ <div id="results-empty" class="mg-text-center mg-text-muted mg-hidden">No rows returned.</div>
43
+ </div>
44
+
45
+ <!-- AI Query Analysis Results -->
46
+ <div id="ai-query-result" class="mg-mt mg-hidden">
47
+ <div class="mg-card">
48
+ <div class="mg-card-header">
49
+ <span id="ai-query-title"><strong>&#9889; AI Analysis</strong></span>
50
+ <button id="ai-query-close" class="mg-btn mg-btn-outline-secondary mg-btn-sm">&#10005;</button>
51
+ </div>
52
+ <div class="mg-card-body">
53
+ <div id="ai-query-content" style="font-size:13px;"></div>
54
+ </div>
55
+ </div>
56
+ </div>
@@ -0,0 +1,43 @@
1
+ <!-- AI Tools Tab -->
2
+ <div class="mg-tab-content" id="tab-aitools">
3
+ <!-- Schema Review -->
4
+ <div class="mg-card mg-mb">
5
+ <div class="mg-card-header"><strong>&#9889; Schema Review</strong></div>
6
+ <div class="mg-card-body">
7
+ <div class="mg-text-muted mg-mb" style="font-size:12px;">Analyze your schema for anti-patterns: inappropriate column types, missing indexes, naming inconsistencies, and more.</div>
8
+ <div class="mg-row" style="align-items:flex-end;">
9
+ <div class="mg-col-4 mg-field">
10
+ <label for="schema-table">Table (leave blank for all)</label>
11
+ <select id="schema-table">
12
+ <option value="">All tables (top 20)</option>
13
+ <% @all_tables.each do |table| %>
14
+ <option value="<%= table %>"><%= table %></option>
15
+ <% end %>
16
+ </select>
17
+ </div>
18
+ <div class="mg-field">
19
+ <button id="schema-review-btn" class="mg-btn mg-btn-primary mg-btn-sm">&#9889; Analyze Schema</button>
20
+ </div>
21
+ </div>
22
+ <div id="schema-result" class="mg-mt mg-hidden">
23
+ <div id="schema-result-content" style="font-size:13px;"></div>
24
+ </div>
25
+ </div>
26
+ </div>
27
+
28
+ <!-- Migration Risk Assessment -->
29
+ <div class="mg-card">
30
+ <div class="mg-card-header"><strong>&#9889; Migration Risk Assessment</strong></div>
31
+ <div class="mg-card-body">
32
+ <div class="mg-text-muted mg-mb" style="font-size:12px;">Paste a Rails migration or DDL and get a risk assessment: lock duration, impact on active queries, deployment strategy.</div>
33
+ <div class="mg-field">
34
+ <textarea id="migration-input" rows="8" placeholder="class AddIndexToUsers < ActiveRecord::Migration[7.0]&#10; def change&#10; add_index :users, :email, unique: true&#10; end&#10;end"></textarea>
35
+ </div>
36
+ <button id="migration-assess-btn" class="mg-btn mg-btn-primary mg-btn-sm">&#9889; Assess Risk</button>
37
+ <div id="migration-result" class="mg-mt mg-hidden">
38
+ <div id="migration-risk-badge" style="margin-bottom:8px;"></div>
39
+ <div id="migration-result-content" style="font-size:13px;"></div>
40
+ </div>
41
+ </div>
42
+ </div>
43
+ </div>
@@ -0,0 +1,97 @@
1
+ <!-- Dashboard Tab -->
2
+ <div class="mg-tab-content active" id="tab-dashboard">
3
+ <div id="dash-loading" class="mg-text-center"><span class="mg-spinner"></span> Loading dashboard...</div>
4
+ <div id="dash-error" class="mg-hidden"></div>
5
+ <div id="dash-content" class="mg-hidden">
6
+
7
+ <!-- Server Summary -->
8
+ <div class="mg-card mg-mb">
9
+ <div class="mg-card-header" style="display:flex;justify-content:space-between;align-items:center;">
10
+ <strong>Server</strong>
11
+ <button class="mg-btn mg-btn-outline-secondary mg-btn-sm dash-jump-tab" data-target="server">Details &rarr;</button>
12
+ </div>
13
+ </div>
14
+ <div style="display:grid;grid-template-columns:repeat(auto-fill,minmax(260px,1fr));gap:12px;margin-bottom:16px;">
15
+ <div class="mg-card">
16
+ <div class="mg-card-header"><strong>Overview</strong></div>
17
+ <div class="mg-card-body">
18
+ <div class="mg-stat-grid" id="dash-server-info"></div>
19
+ </div>
20
+ </div>
21
+ <div class="mg-card">
22
+ <div class="mg-card-header"><strong>Connections</strong></div>
23
+ <div class="mg-card-body">
24
+ <div id="dash-conn-bar" style="margin-bottom:8px;"></div>
25
+ <div class="mg-stat-grid" id="dash-conn-info"></div>
26
+ </div>
27
+ </div>
28
+ <div class="mg-card">
29
+ <div class="mg-card-header"><strong>InnoDB Buffer Pool</strong></div>
30
+ <div class="mg-card-body">
31
+ <div id="dash-innodb-bar" style="margin-bottom:8px;"></div>
32
+ <div class="mg-stat-grid" id="dash-innodb-info"></div>
33
+ </div>
34
+ </div>
35
+ <div class="mg-card">
36
+ <div class="mg-card-header"><strong>Query Activity</strong></div>
37
+ <div class="mg-card-body">
38
+ <div class="mg-stat-grid" id="dash-query-info"></div>
39
+ </div>
40
+ </div>
41
+ </div>
42
+
43
+ <% if capability?(:slow_queries) %>
44
+ <!-- Top 5 Slow Queries -->
45
+ <div class="mg-card mg-mb">
46
+ <div class="mg-card-header" style="display:flex;justify-content:space-between;align-items:center;">
47
+ <strong>Slow Queries</strong>
48
+ <button class="mg-btn mg-btn-outline-secondary mg-btn-sm dash-jump-tab" data-target="slow">View all &rarr;</button>
49
+ </div>
50
+ <div class="mg-card-body">
51
+ <div id="dash-slow-empty" class="mg-text-muted mg-hidden"></div>
52
+ <div id="dash-slow-table" class="mg-table-wrap mg-hidden">
53
+ <table class="mg-table">
54
+ <thead><tr><th style="width:100px">Duration</th><th style="width:160px">Time</th><th>SQL</th></tr></thead>
55
+ <tbody id="dash-slow-tbody"></tbody>
56
+ </table>
57
+ </div>
58
+ </div>
59
+ </div>
60
+ <% end %>
61
+
62
+ <!-- Top 5 Expensive Queries -->
63
+ <div class="mg-card mg-mb">
64
+ <div class="mg-card-header" style="display:flex;justify-content:space-between;align-items:center;">
65
+ <strong>Most Expensive Queries</strong>
66
+ <button class="mg-btn mg-btn-outline-secondary mg-btn-sm dash-jump-tab" data-target="qstats">View all &rarr;</button>
67
+ </div>
68
+ <div class="mg-card-body">
69
+ <div id="dash-qstats-empty" class="mg-text-muted mg-hidden"></div>
70
+ <div id="dash-qstats-error" class="mg-hidden"></div>
71
+ <div id="dash-qstats-table" class="mg-table-wrap mg-hidden">
72
+ <table class="mg-table">
73
+ <thead><tr><th>Query</th><th style="text-align:right">Calls</th><th style="text-align:right">Total Time</th><th style="text-align:right">Avg Time</th></tr></thead>
74
+ <tbody id="dash-qstats-tbody"></tbody>
75
+ </table>
76
+ </div>
77
+ </div>
78
+ </div>
79
+
80
+ <!-- Index Alerts -->
81
+ <div style="display:grid;grid-template-columns:repeat(auto-fill,minmax(260px,1fr));gap:12px;">
82
+ <div class="mg-card dash-jump-tab" data-target="indexes" style="cursor:pointer;">
83
+ <div class="mg-card-body mg-text-center">
84
+ <div id="dash-dup-count" style="font-size:24px;font-weight:700;">--</div>
85
+ <div class="mg-text-muted">Duplicate Indexes</div>
86
+ </div>
87
+ </div>
88
+ <div class="mg-card dash-jump-tab" data-target="unused" style="cursor:pointer;">
89
+ <div class="mg-card-body mg-text-center">
90
+ <div id="dash-unused-count" style="font-size:24px;font-weight:700;">--</div>
91
+ <div class="mg-text-muted">Unused Indexes</div>
92
+ </div>
93
+ </div>
94
+ </div>
95
+
96
+ </div>
97
+ </div>
@@ -0,0 +1,35 @@
1
+ <!-- Duplicate Indexes Tab -->
2
+ <div class="mg-tab-content" id="tab-indexes">
3
+ <div class="mg-row" style="justify-content:space-between;align-items:center;margin-bottom:12px;">
4
+ <div class="mg-text-muted">Indexes whose columns are a left-prefix of another index on the same table. These are redundant and can safely be dropped. <span id="dup-count" class="mg-badge mg-badge-secondary"></span></div>
5
+ <button id="dup-refresh" class="mg-btn mg-btn-outline-secondary mg-btn-sm">&#8635; Refresh</button>
6
+ </div>
7
+ <div id="dup-loading" class="mg-text-center mg-hidden"><span class="mg-spinner"></span> Scanning indexes...</div>
8
+ <div id="dup-empty" class="mg-text-center mg-text-muted mg-hidden">No duplicate indexes found.</div>
9
+ <div id="dup-table-wrapper" class="mg-table-wrap mg-hidden">
10
+ <table class="mg-table">
11
+ <thead>
12
+ <tr>
13
+ <th>Table</th>
14
+ <th>Duplicate Index</th>
15
+ <th>Columns</th>
16
+ <th>Covered By</th>
17
+ <th>Covered Columns</th>
18
+ <th>DROP Statement</th>
19
+ </tr>
20
+ </thead>
21
+ <tbody id="dup-tbody"></tbody>
22
+ </table>
23
+ </div>
24
+ <div id="dup-migration" class="mg-hidden" style="margin-top:16px;">
25
+ <div class="mg-card">
26
+ <div class="mg-card-header" style="display:flex;justify-content:space-between;align-items:center;">
27
+ <strong>Suggested Migration</strong>
28
+ <button id="dup-copy-migration" class="mg-btn mg-btn-outline-secondary mg-btn-sm">&#128203; Copy</button>
29
+ </div>
30
+ <div class="mg-card-body">
31
+ <pre id="dup-migration-code" style="font-size:12px;margin:0;white-space:pre-wrap;user-select:all;"></pre>
32
+ </div>
33
+ </div>
34
+ </div>
35
+ </div>
@@ -0,0 +1,110 @@
1
+ <!-- Query Explorer Tab -->
2
+ <div class="mg-tab-content" id="tab-explorer">
3
+ <div style="margin-bottom:12px;">
4
+ <button class="mg-btn mg-btn-sm qe-mode active" data-mode="visual">Visual Builder</button>
5
+ <button class="mg-btn mg-btn-sm mg-btn-outline qe-mode" data-mode="sql">SQL Editor</button>
6
+ </div>
7
+
8
+ <!-- Visual Builder Mode -->
9
+ <div id="qe-visual">
10
+ <div class="mg-row mg-mb">
11
+ <div class="mg-col-4 mg-field">
12
+ <label for="vb-table">Table</label>
13
+ <select id="vb-table">
14
+ <option value="">-- Select a table --</option>
15
+ <% if @featured_tables != @all_tables %>
16
+ <optgroup label="Featured">
17
+ <% @featured_tables.each do |table| %>
18
+ <option value="<%= table %>"><%= table %></option>
19
+ <% end %>
20
+ </optgroup>
21
+ <optgroup label="All Tables">
22
+ <% (@all_tables - @featured_tables).each do |table| %>
23
+ <option value="<%= table %>"><%= table %></option>
24
+ <% end %>
25
+ </optgroup>
26
+ <% else %>
27
+ <% @all_tables.each do |table| %>
28
+ <option value="<%= table %>"><%= table %></option>
29
+ <% end %>
30
+ <% end %>
31
+ </select>
32
+ </div>
33
+ <div class="mg-col-2 mg-field">
34
+ <label for="vb-row-limit">Row Limit</label>
35
+ <input type="number" id="vb-row-limit" value="25" min="1" max="<%= MysqlGenius.configuration.max_row_limit %>">
36
+ </div>
37
+ <div class="mg-field">
38
+ <label>&nbsp;</label>
39
+ <button id="vb-run" class="mg-btn mg-btn-primary" disabled>&#9654; Run Query</button>
40
+ <button id="vb-explain" class="mg-btn mg-btn-outline" disabled>&#128270; Explain</button>
41
+ </div>
42
+ </div>
43
+
44
+ <div id="vb-columns-section" class="mg-mb mg-hidden">
45
+ <label>Columns
46
+ <span id="vb-toggle-all" class="mg-link">Toggle All</span>
47
+ <span id="vb-show-defaults" class="mg-link">Reset Defaults</span>
48
+ </label>
49
+ <div id="vb-columns" class="mg-checks"></div>
50
+ </div>
51
+
52
+ <div id="vb-filters-section" class="mg-mb mg-hidden">
53
+ <label>Filters</label>
54
+ <div id="vb-filters"></div>
55
+ <button id="vb-add-filter" class="mg-btn mg-btn-outline-secondary mg-btn-sm" style="margin-top:4px;">+ Add Filter</button>
56
+ </div>
57
+
58
+ <div id="vb-order-section" class="mg-mb mg-hidden">
59
+ <label>Order By</label>
60
+ <div id="vb-orders"></div>
61
+ <button id="vb-add-order" class="mg-btn mg-btn-outline-secondary mg-btn-sm" style="margin-top:4px;">+ Add Sort</button>
62
+ </div>
63
+
64
+ <div id="vb-generated-sql" class="mg-mb mg-hidden">
65
+ <label>Generated SQL</label>
66
+ <textarea id="vb-sql-preview" rows="2" readonly></textarea>
67
+ </div>
68
+ </div>
69
+
70
+ <!-- SQL Editor Mode -->
71
+ <div id="qe-sql" class="mg-hidden">
72
+ <% if @ai_enabled %>
73
+ <div class="mg-card mg-mb">
74
+ <div class="mg-card-header">
75
+ <span class="mg-card-toggle" id="ai-toggle">&#9889; AI Assistant <span class="mg-text-muted">- Describe what you want in plain English</span></span>
76
+ </div>
77
+ <div class="mg-card-body mg-hidden" id="ai-panel">
78
+ <div class="mg-field">
79
+ <textarea id="ai-prompt" rows="2" placeholder="e.g. Show me all users created in the last 30 days"></textarea>
80
+ <div class="mg-text-muted" style="margin-top:2px;">Only table names and column names are sent to the AI. No row data leaves the system.</div>
81
+ </div>
82
+ <button id="ai-suggest" class="mg-btn mg-btn-primary mg-btn-sm mg-mb">&#9889; Suggest Query</button>
83
+ <div id="ai-result" class="mg-hidden">
84
+ <div id="ai-explanation" class="mg-alert mg-alert-info"></div>
85
+ </div>
86
+ </div>
87
+ </div>
88
+ <% end %>
89
+
90
+ <div class="mg-field">
91
+ <label for="sql-input">SQL Query</label>
92
+ <textarea id="sql-input" rows="5" placeholder="SELECT * FROM users LIMIT 10"></textarea>
93
+ </div>
94
+ <div class="mg-row">
95
+ <div class="mg-col-2 mg-field">
96
+ <label for="sql-row-limit">Row Limit</label>
97
+ <input type="number" id="sql-row-limit" value="25" min="1" max="<%= MysqlGenius.configuration.max_row_limit %>">
98
+ </div>
99
+ <div class="mg-field">
100
+ <label>&nbsp;</label>
101
+ <button id="sql-run" class="mg-btn mg-btn-primary">&#9654; Run Query</button>
102
+ <button id="sql-explain" class="mg-btn mg-btn-outline">&#128270; Explain</button>
103
+ <% if @ai_enabled %>
104
+ <button id="sql-describe" class="mg-btn mg-btn-outline mg-btn-sm" style="margin-left:8px;">&#9889; Describe</button>
105
+ <button id="sql-rewrite" class="mg-btn mg-btn-outline mg-btn-sm">&#9889; Rewrite</button>
106
+ <% end %>
107
+ </div>
108
+ </div>
109
+ </div>
110
+ </div>
@@ -0,0 +1,26 @@
1
+ <!-- Query Stats Tab -->
2
+ <div class="mg-tab-content" id="tab-qstats">
3
+ <div class="mg-row" style="justify-content:space-between;align-items:center;margin-bottom:12px;">
4
+ <div class="mg-text-muted">Top queries from <code>performance_schema.events_statements_summary_by_digest</code>. <span id="qstats-count" class="mg-badge mg-badge-secondary"></span></div>
5
+ </div>
6
+ <div id="qstats-loading" class="mg-text-center mg-hidden"><span class="mg-spinner"></span> Loading...</div>
7
+ <div id="qstats-error" class="mg-hidden"></div>
8
+ <div id="qstats-empty" class="mg-text-center mg-text-muted mg-hidden">No query statistics available.</div>
9
+ <div id="qstats-table-wrapper" class="mg-table-wrap mg-hidden">
10
+ <table class="mg-table">
11
+ <thead>
12
+ <tr>
13
+ <th>Query</th>
14
+ <th style="text-align:right">Calls</th>
15
+ <th style="text-align:right">Total Time</th>
16
+ <th style="text-align:right">Avg Time</th>
17
+ <th style="text-align:right">Max Time</th>
18
+ <th style="text-align:right">Rows Examined</th>
19
+ <th style="text-align:right">Rows Sent</th>
20
+ <th style="text-align:right">Exam/Sent</th>
21
+ </tr>
22
+ </thead>
23
+ <tbody id="qstats-tbody"></tbody>
24
+ </table>
25
+ </div>
26
+ </div>