QueryWise 0.2.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 +7 -0
- data/CHANGELOG.md +45 -0
- data/CLOUD_RUN_README.md +263 -0
- data/DOCKER_README.md +327 -0
- data/Dockerfile +69 -0
- data/Dockerfile.cloudrun +76 -0
- data/Dockerfile.dev +36 -0
- data/GEM_Gemfile +16 -0
- data/GEM_README.md +421 -0
- data/GEM_Rakefile +10 -0
- data/GEM_gitignore +137 -0
- data/LICENSE.txt +21 -0
- data/PUBLISHING_GUIDE.md +269 -0
- data/README.md +392 -0
- data/app/controllers/api/v1/analysis_controller.rb +340 -0
- data/app/controllers/api/v1/api_keys_controller.rb +83 -0
- data/app/controllers/api/v1/base_controller.rb +93 -0
- data/app/controllers/api/v1/health_controller.rb +86 -0
- data/app/controllers/application_controller.rb +2 -0
- data/app/controllers/concerns/.keep +0 -0
- data/app/jobs/application_job.rb +7 -0
- data/app/mailers/application_mailer.rb +4 -0
- data/app/models/app_profile.rb +18 -0
- data/app/models/application_record.rb +3 -0
- data/app/models/concerns/.keep +0 -0
- data/app/models/optimization_suggestion.rb +44 -0
- data/app/models/query_analysis.rb +47 -0
- data/app/models/query_pattern.rb +55 -0
- data/app/services/missing_index_detector_service.rb +244 -0
- data/app/services/n_plus_one_detector_service.rb +177 -0
- data/app/services/slow_query_analyzer_service.rb +225 -0
- data/app/services/sql_parser_service.rb +352 -0
- data/app/validators/query_data_validator.rb +96 -0
- data/app/views/layouts/mailer.html.erb +13 -0
- data/app/views/layouts/mailer.text.erb +1 -0
- data/app.yaml +109 -0
- data/cloudbuild.yaml +47 -0
- data/config/application.rb +32 -0
- data/config/boot.rb +4 -0
- data/config/cable.yml +17 -0
- data/config/cache.yml +16 -0
- data/config/credentials.yml.enc +1 -0
- data/config/database.yml +69 -0
- data/config/deploy.yml +116 -0
- data/config/environment.rb +5 -0
- data/config/environments/development.rb +70 -0
- data/config/environments/production.rb +87 -0
- data/config/environments/test.rb +53 -0
- data/config/initializers/cors.rb +16 -0
- data/config/initializers/filter_parameter_logging.rb +8 -0
- data/config/initializers/inflections.rb +16 -0
- data/config/locales/en.yml +31 -0
- data/config/master.key +1 -0
- data/config/puma.rb +41 -0
- data/config/puma_cloudrun.rb +48 -0
- data/config/queue.yml +18 -0
- data/config/recurring.yml +15 -0
- data/config/routes.rb +28 -0
- data/config/storage.yml +34 -0
- data/config.ru +6 -0
- data/db/cable_schema.rb +11 -0
- data/db/cache_schema.rb +14 -0
- data/db/migrate/20250818214709_create_app_profiles.rb +13 -0
- data/db/migrate/20250818214731_create_query_analyses.rb +22 -0
- data/db/migrate/20250818214740_create_query_patterns.rb +22 -0
- data/db/migrate/20250818214805_create_optimization_suggestions.rb +20 -0
- data/db/queue_schema.rb +129 -0
- data/db/schema.rb +79 -0
- data/db/seeds.rb +9 -0
- data/init.sql +9 -0
- data/lib/query_optimizer_client/client.rb +176 -0
- data/lib/query_optimizer_client/configuration.rb +43 -0
- data/lib/query_optimizer_client/generators/install_generator.rb +43 -0
- data/lib/query_optimizer_client/generators/templates/README +46 -0
- data/lib/query_optimizer_client/generators/templates/analysis_job.rb +84 -0
- data/lib/query_optimizer_client/generators/templates/initializer.rb +30 -0
- data/lib/query_optimizer_client/middleware.rb +126 -0
- data/lib/query_optimizer_client/railtie.rb +37 -0
- data/lib/query_optimizer_client/tasks.rake +228 -0
- data/lib/query_optimizer_client/version.rb +5 -0
- data/lib/query_optimizer_client.rb +48 -0
- data/lib/tasks/.keep +0 -0
- data/public/robots.txt +1 -0
- data/query_optimizer_client.gemspec +60 -0
- data/script/.keep +0 -0
- data/storage/.keep +0 -0
- data/storage/development.sqlite3 +0 -0
- data/storage/test.sqlite3 +0 -0
- data/vendor/.keep +0 -0
- metadata +265 -0
data/PUBLISHING_GUIDE.md
ADDED
@@ -0,0 +1,269 @@
|
|
1
|
+
# Publishing Query Optimizer Client to RubyGems
|
2
|
+
|
3
|
+
This guide walks you through publishing the Query Optimizer Client gem to RubyGems.
|
4
|
+
|
5
|
+
## Prerequisites
|
6
|
+
|
7
|
+
1. **RubyGems Account**: Create an account at [rubygems.org](https://rubygems.org)
|
8
|
+
2. **API Key**: Get your API key from your RubyGems profile
|
9
|
+
3. **Git Repository**: Set up a GitHub repository for the gem
|
10
|
+
|
11
|
+
## Pre-Publishing Checklist
|
12
|
+
|
13
|
+
### 1. Update Gem Information
|
14
|
+
|
15
|
+
Edit `query_optimizer_client.gemspec`:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
spec.authors = ["Your Name"]
|
19
|
+
spec.email = ["your.email@example.com"]
|
20
|
+
spec.homepage = "https://github.com/yourusername/query_optimizer_client"
|
21
|
+
spec.metadata["source_code_uri"] = "https://github.com/yourusername/query_optimizer_client"
|
22
|
+
```
|
23
|
+
|
24
|
+
### 2. Verify Version
|
25
|
+
|
26
|
+
Check `lib/query_optimizer_client/version.rb`:
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
module QueryOptimizerClient
|
30
|
+
VERSION = "0.1.0" # Update as needed
|
31
|
+
end
|
32
|
+
```
|
33
|
+
|
34
|
+
### 3. Update Documentation
|
35
|
+
|
36
|
+
- [ ] Update `README.md` with correct GitHub URLs
|
37
|
+
- [ ] Update `CHANGELOG.md` with release notes
|
38
|
+
- [ ] Verify all code examples work
|
39
|
+
- [ ] Check that documentation is comprehensive
|
40
|
+
|
41
|
+
### 4. Run Tests
|
42
|
+
|
43
|
+
```bash
|
44
|
+
# Install dependencies
|
45
|
+
bundle install
|
46
|
+
|
47
|
+
# Run all tests
|
48
|
+
bundle exec rspec
|
49
|
+
|
50
|
+
# Run RuboCop for code style
|
51
|
+
bundle exec rubocop
|
52
|
+
|
53
|
+
# Fix any RuboCop issues
|
54
|
+
bundle exec rubocop -a
|
55
|
+
```
|
56
|
+
|
57
|
+
### 5. Test the Gem Locally
|
58
|
+
|
59
|
+
```bash
|
60
|
+
# Build the gem
|
61
|
+
gem build query_optimizer_client.gemspec
|
62
|
+
|
63
|
+
# Install locally to test
|
64
|
+
gem install ./query_optimizer_client-0.1.0.gem
|
65
|
+
|
66
|
+
# Test in a Rails app
|
67
|
+
cd /path/to/test/rails/app
|
68
|
+
echo 'gem "query_optimizer_client", path: "/path/to/gem"' >> Gemfile
|
69
|
+
bundle install
|
70
|
+
rails generate query_optimizer_client:install
|
71
|
+
```
|
72
|
+
|
73
|
+
## Publishing Steps
|
74
|
+
|
75
|
+
### 1. Set Up RubyGems Credentials
|
76
|
+
|
77
|
+
```bash
|
78
|
+
# Add your RubyGems API key
|
79
|
+
gem signin
|
80
|
+
|
81
|
+
# Or manually create credentials file
|
82
|
+
mkdir -p ~/.gem
|
83
|
+
echo "---
|
84
|
+
:rubygems_api_key: your_api_key_here" > ~/.gem/credentials
|
85
|
+
chmod 0600 ~/.gem/credentials
|
86
|
+
```
|
87
|
+
|
88
|
+
### 2. Build and Publish
|
89
|
+
|
90
|
+
```bash
|
91
|
+
# Build the gem
|
92
|
+
gem build query_optimizer_client.gemspec
|
93
|
+
|
94
|
+
# Push to RubyGems
|
95
|
+
gem push query_optimizer_client-0.1.0.gem
|
96
|
+
```
|
97
|
+
|
98
|
+
### 3. Verify Publication
|
99
|
+
|
100
|
+
- Check [rubygems.org/gems/query_optimizer_client](https://rubygems.org/gems/query_optimizer_client)
|
101
|
+
- Test installation: `gem install query_optimizer_client`
|
102
|
+
|
103
|
+
## Post-Publishing Tasks
|
104
|
+
|
105
|
+
### 1. Tag the Release
|
106
|
+
|
107
|
+
```bash
|
108
|
+
git tag v0.1.0
|
109
|
+
git push origin v0.1.0
|
110
|
+
```
|
111
|
+
|
112
|
+
### 2. Create GitHub Release
|
113
|
+
|
114
|
+
1. Go to your GitHub repository
|
115
|
+
2. Click "Releases" → "Create a new release"
|
116
|
+
3. Choose the tag you just created
|
117
|
+
4. Add release notes from CHANGELOG.md
|
118
|
+
5. Publish the release
|
119
|
+
|
120
|
+
### 3. Update Documentation
|
121
|
+
|
122
|
+
- [ ] Update README badges with correct gem version
|
123
|
+
- [ ] Update any documentation that references installation
|
124
|
+
- [ ] Consider creating a documentation website
|
125
|
+
|
126
|
+
## Gem Structure Overview
|
127
|
+
|
128
|
+
```
|
129
|
+
query_optimizer_client/
|
130
|
+
├── lib/
|
131
|
+
│ ├── query_optimizer_client.rb # Main entry point
|
132
|
+
│ └── query_optimizer_client/
|
133
|
+
│ ├── version.rb # Version constant
|
134
|
+
│ ├── configuration.rb # Configuration class
|
135
|
+
│ ├── client.rb # HTTP client
|
136
|
+
│ ├── middleware.rb # Rails middleware
|
137
|
+
│ ├── railtie.rb # Rails integration
|
138
|
+
│ ├── tasks.rake # Rake tasks
|
139
|
+
│ └── generators/
|
140
|
+
│ ├── install_generator.rb # Rails generator
|
141
|
+
│ └── templates/
|
142
|
+
│ ├── initializer.rb # Config template
|
143
|
+
│ ├── analysis_job.rb # Job template
|
144
|
+
│ └── README # Post-install instructions
|
145
|
+
├── spec/ # Test files
|
146
|
+
├── query_optimizer_client.gemspec # Gem specification
|
147
|
+
├── README.md # Main documentation
|
148
|
+
├── CHANGELOG.md # Version history
|
149
|
+
├── LICENSE.txt # MIT license
|
150
|
+
├── Gemfile # Development dependencies
|
151
|
+
└── Rakefile # Build tasks
|
152
|
+
```
|
153
|
+
|
154
|
+
## Usage After Publishing
|
155
|
+
|
156
|
+
Once published, Rails developers can use your gem like this:
|
157
|
+
|
158
|
+
### Installation
|
159
|
+
|
160
|
+
```ruby
|
161
|
+
# Gemfile
|
162
|
+
gem 'query_optimizer_client'
|
163
|
+
```
|
164
|
+
|
165
|
+
```bash
|
166
|
+
bundle install
|
167
|
+
```
|
168
|
+
|
169
|
+
### Setup
|
170
|
+
|
171
|
+
```bash
|
172
|
+
rails generate query_optimizer_client:install
|
173
|
+
```
|
174
|
+
|
175
|
+
### Configuration
|
176
|
+
|
177
|
+
```bash
|
178
|
+
# .env
|
179
|
+
QUERY_OPTIMIZER_API_URL=http://localhost:3000/api/v1
|
180
|
+
QUERY_OPTIMIZER_API_KEY=your_api_key_here
|
181
|
+
QUERY_OPTIMIZER_ENABLED=true
|
182
|
+
```
|
183
|
+
|
184
|
+
### Usage
|
185
|
+
|
186
|
+
```ruby
|
187
|
+
# Manual analysis
|
188
|
+
queries = [
|
189
|
+
{ sql: "SELECT * FROM users", duration_ms: 50 }
|
190
|
+
]
|
191
|
+
|
192
|
+
result = QueryOptimizerClient.analyze_queries(queries)
|
193
|
+
puts "Score: #{result['data']['summary']['optimization_score']}%"
|
194
|
+
|
195
|
+
# CI integration
|
196
|
+
rails query_optimizer:ci[85]
|
197
|
+
```
|
198
|
+
|
199
|
+
## Maintenance
|
200
|
+
|
201
|
+
### Updating the Gem
|
202
|
+
|
203
|
+
1. Make changes to the code
|
204
|
+
2. Update version in `lib/query_optimizer_client/version.rb`
|
205
|
+
3. Update `CHANGELOG.md`
|
206
|
+
4. Run tests: `bundle exec rspec`
|
207
|
+
5. Build and publish: `gem build && gem push query_optimizer_client-x.x.x.gem`
|
208
|
+
6. Tag the release: `git tag vx.x.x && git push origin vx.x.x`
|
209
|
+
|
210
|
+
### Semantic Versioning
|
211
|
+
|
212
|
+
Follow [Semantic Versioning](https://semver.org/):
|
213
|
+
|
214
|
+
- **MAJOR** (1.0.0): Breaking changes
|
215
|
+
- **MINOR** (0.1.0): New features, backward compatible
|
216
|
+
- **PATCH** (0.0.1): Bug fixes, backward compatible
|
217
|
+
|
218
|
+
### Support
|
219
|
+
|
220
|
+
- Monitor GitHub issues
|
221
|
+
- Respond to user questions
|
222
|
+
- Keep dependencies updated
|
223
|
+
- Maintain compatibility with new Rails versions
|
224
|
+
|
225
|
+
## Security Considerations
|
226
|
+
|
227
|
+
- Never commit API keys or sensitive data
|
228
|
+
- Use `.gitignore` to exclude sensitive files
|
229
|
+
- Consider signing your gem releases
|
230
|
+
- Monitor for security vulnerabilities in dependencies
|
231
|
+
|
232
|
+
## Marketing and Community
|
233
|
+
|
234
|
+
- Announce on Ruby/Rails forums
|
235
|
+
- Write blog posts about the gem
|
236
|
+
- Present at Ruby meetups or conferences
|
237
|
+
- Engage with users on GitHub issues
|
238
|
+
- Consider creating video tutorials
|
239
|
+
|
240
|
+
## Example GitHub Actions for CI
|
241
|
+
|
242
|
+
```yaml
|
243
|
+
# .github/workflows/ci.yml
|
244
|
+
name: CI
|
245
|
+
|
246
|
+
on: [push, pull_request]
|
247
|
+
|
248
|
+
jobs:
|
249
|
+
test:
|
250
|
+
runs-on: ubuntu-latest
|
251
|
+
strategy:
|
252
|
+
matrix:
|
253
|
+
ruby-version: ['3.0', '3.1', '3.2']
|
254
|
+
rails-version: ['6.1', '7.0', '7.1']
|
255
|
+
|
256
|
+
steps:
|
257
|
+
- uses: actions/checkout@v3
|
258
|
+
- name: Set up Ruby ${{ matrix.ruby-version }}
|
259
|
+
uses: ruby/setup-ruby@v1
|
260
|
+
with:
|
261
|
+
ruby-version: ${{ matrix.ruby-version }}
|
262
|
+
bundler-cache: true
|
263
|
+
- name: Run tests
|
264
|
+
run: bundle exec rspec
|
265
|
+
- name: Run RuboCop
|
266
|
+
run: bundle exec rubocop
|
267
|
+
```
|
268
|
+
|
269
|
+
This comprehensive guide should help you successfully publish and maintain the Query Optimizer Client gem on RubyGems! 🚀
|
data/README.md
ADDED
@@ -0,0 +1,392 @@
|
|
1
|
+
# QueryWise
|
2
|
+
|
3
|
+
A Ruby gem for integrating with the Rails Database Query Optimizer API to detect N+1 queries, slow queries, and missing indexes in your Rails applications.
|
4
|
+
|
5
|
+
[](https://badge.fury.io/rb/QueryWise)
|
6
|
+
[](https://github.com/BlairLane22/QueryWise/actions)
|
7
|
+
|
8
|
+
## Features
|
9
|
+
|
10
|
+
- 🔍 **N+1 Query Detection** - Automatically detect N+1 query patterns
|
11
|
+
- 🐌 **Slow Query Analysis** - Identify slow queries with optimization suggestions
|
12
|
+
- 📊 **Missing Index Detection** - Get recommendations for database indexes
|
13
|
+
- 🚀 **Rails Integration** - Seamless integration with Rails applications
|
14
|
+
- 📈 **CI/CD Support** - Built-in support for continuous integration
|
15
|
+
- 🔧 **Configurable** - Flexible configuration options
|
16
|
+
- 📝 **Comprehensive Logging** - Detailed logging and error handling
|
17
|
+
|
18
|
+
## Step-by-Step Implementation Guide
|
19
|
+
|
20
|
+
Follow this guide to implement Query Optimizer Client in your Rails project in under an hour:
|
21
|
+
|
22
|
+
### Step 1: Install the Gem (2 minutes)
|
23
|
+
|
24
|
+
Add to your `Gemfile`:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
gem 'QueryWise'
|
28
|
+
```
|
29
|
+
|
30
|
+
Run bundle install:
|
31
|
+
|
32
|
+
```bash
|
33
|
+
bundle install
|
34
|
+
```
|
35
|
+
|
36
|
+
### Step 2: Generate Configuration Files (1 minute)
|
37
|
+
|
38
|
+
Run the installation generator:
|
39
|
+
|
40
|
+
```bash
|
41
|
+
rails generate QueryWise:install
|
42
|
+
```
|
43
|
+
|
44
|
+
This creates:
|
45
|
+
- `config/initializers/query_optimizer_client.rb` - Configuration file
|
46
|
+
- `app/jobs/query_optimizer_client/analysis_job.rb` - Background job
|
47
|
+
- `.env` file with configuration template
|
48
|
+
|
49
|
+
### Step 3: Set Up API Connection (5 minutes)
|
50
|
+
|
51
|
+
#### Option A: Use Hosted API (Recommended)
|
52
|
+
```bash
|
53
|
+
# Add to your .env file
|
54
|
+
QUERY_OPTIMIZER_API_URL=https://your-hosted-api.com/api/v1
|
55
|
+
QUERY_OPTIMIZER_API_KEY=your_api_key_here
|
56
|
+
QUERY_OPTIMIZER_ENABLED=true
|
57
|
+
```
|
58
|
+
|
59
|
+
#### Option B: Run API Locally
|
60
|
+
```bash
|
61
|
+
# Clone and start the API server
|
62
|
+
git clone https://github.com/yourusername/query-optimizer-api
|
63
|
+
cd query-optimizer-api
|
64
|
+
bundle install
|
65
|
+
rails db:create db:migrate
|
66
|
+
rails server
|
67
|
+
|
68
|
+
# In your app's .env file
|
69
|
+
QUERY_OPTIMIZER_API_URL=http://localhost:3000/api/v1
|
70
|
+
QUERY_OPTIMIZER_ENABLED=true
|
71
|
+
```
|
72
|
+
|
73
|
+
### Step 4: Generate API Key (2 minutes)
|
74
|
+
|
75
|
+
```bash
|
76
|
+
# Generate an API key for your application
|
77
|
+
rails query_optimizer:generate_key["My Rails App"]
|
78
|
+
|
79
|
+
# Copy the generated key to your .env file
|
80
|
+
QUERY_OPTIMIZER_API_KEY=abc123def456...
|
81
|
+
```
|
82
|
+
|
83
|
+
### Step 5: Test Your Setup (2 minutes)
|
84
|
+
|
85
|
+
```bash
|
86
|
+
# Verify configuration and connectivity
|
87
|
+
rails query_optimizer:check
|
88
|
+
|
89
|
+
# Should output:
|
90
|
+
# ✅ API connection successful
|
91
|
+
# Version: 1.0.0
|
92
|
+
# Services: {"database":"ok","sql_parser":"ok"}
|
93
|
+
```
|
94
|
+
|
95
|
+
### Step 6: Run Your First Analysis (5 minutes)
|
96
|
+
|
97
|
+
```bash
|
98
|
+
# Analyze sample queries from your application
|
99
|
+
rails query_optimizer:analyze
|
100
|
+
|
101
|
+
# Example output:
|
102
|
+
# 📊 Analysis Results
|
103
|
+
# Overall Score: 75/100
|
104
|
+
# Issues Found: 2
|
105
|
+
# 🔍 N+1 Query Issues:
|
106
|
+
# ⚠️ posts.user_id
|
107
|
+
# 💡 Use includes(:user) to preload associations
|
108
|
+
```
|
109
|
+
|
110
|
+
### Step 7: Set Up Automatic Monitoring (10 minutes)
|
111
|
+
|
112
|
+
The middleware is automatically enabled. Test it by:
|
113
|
+
|
114
|
+
1. **Start your Rails server:**
|
115
|
+
```bash
|
116
|
+
rails server
|
117
|
+
```
|
118
|
+
|
119
|
+
2. **Visit a page with database queries:**
|
120
|
+
```bash
|
121
|
+
# Navigate to a page like /users or /posts
|
122
|
+
curl http://localhost:3000/users
|
123
|
+
```
|
124
|
+
|
125
|
+
3. **Check your logs for analysis results:**
|
126
|
+
```bash
|
127
|
+
tail -f log/development.log | grep "Query analysis"
|
128
|
+
```
|
129
|
+
|
130
|
+
You should see logs like:
|
131
|
+
```
|
132
|
+
Query analysis completed on GET /users: Score 85%, 1 issues found
|
133
|
+
N+1 queries detected on GET /users:
|
134
|
+
- posts.user_id: Use includes(:posts) to preload associations
|
135
|
+
```
|
136
|
+
|
137
|
+
### Step 8: Configure for Your Needs (15 minutes)
|
138
|
+
|
139
|
+
Edit `config/initializers/query_optimizer_client.rb`:
|
140
|
+
|
141
|
+
```ruby
|
142
|
+
QueryOptimizerClient.configure do |config|
|
143
|
+
# Basic settings
|
144
|
+
config.enabled = Rails.env.production? || Rails.env.development?
|
145
|
+
config.default_threshold = 80 # Minimum acceptable score
|
146
|
+
|
147
|
+
# Performance settings
|
148
|
+
config.timeout = 30 # API request timeout
|
149
|
+
config.batch_size = 50 # Max queries per request
|
150
|
+
config.retries = 3 # Retry failed requests
|
151
|
+
|
152
|
+
# Monitoring settings - customize what gets analyzed
|
153
|
+
Rails.application.config.query_optimizer_client.min_queries = 5 # Only analyze requests with 5+ queries
|
154
|
+
Rails.application.config.query_optimizer_client.skip_paths = [
|
155
|
+
'/assets', '/health', '/admin' # Skip these paths
|
156
|
+
]
|
157
|
+
end
|
158
|
+
```
|
159
|
+
|
160
|
+
### Step 9: Set Up CI/CD Integration (10 minutes)
|
161
|
+
|
162
|
+
Add to your CI pipeline (e.g., `.github/workflows/ci.yml`):
|
163
|
+
|
164
|
+
```yaml
|
165
|
+
- name: Database Performance Check
|
166
|
+
run: bundle exec rails query_optimizer:ci[85]
|
167
|
+
env:
|
168
|
+
QUERY_OPTIMIZER_API_KEY: ${{ secrets.QUERY_OPTIMIZER_API_KEY }}
|
169
|
+
QUERY_OPTIMIZER_ENABLED: true
|
170
|
+
```
|
171
|
+
|
172
|
+
Or add to your test suite:
|
173
|
+
|
174
|
+
```ruby
|
175
|
+
# spec/performance/query_performance_spec.rb
|
176
|
+
RSpec.describe "Query Performance" do
|
177
|
+
it "maintains good performance" do
|
178
|
+
# Your test queries here
|
179
|
+
queries = [
|
180
|
+
{ sql: User.where(active: true).to_sql, duration_ms: 45 }
|
181
|
+
]
|
182
|
+
|
183
|
+
result = QueryOptimizerClient.analyze_for_ci(queries, threshold: 85)
|
184
|
+
expect(result['data']['passed']).to be true
|
185
|
+
end
|
186
|
+
end
|
187
|
+
```
|
188
|
+
|
189
|
+
### Step 10: Customize Alerts and Actions (10 minutes)
|
190
|
+
|
191
|
+
Edit `app/jobs/query_optimizer_client/analysis_job.rb` to add custom behavior:
|
192
|
+
|
193
|
+
```ruby
|
194
|
+
def send_alerts(data, endpoint, score)
|
195
|
+
if score < 50
|
196
|
+
# Send to Slack
|
197
|
+
SlackNotifier.ping("🚨 Critical performance issue on #{endpoint}: #{score}%")
|
198
|
+
|
199
|
+
# Send email alert
|
200
|
+
PerformanceMailer.critical_alert(endpoint, data).deliver_now
|
201
|
+
|
202
|
+
# Create GitHub issue
|
203
|
+
create_github_issue(endpoint, data) if Rails.env.production?
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
def store_analysis_results(data, endpoint)
|
208
|
+
# Store in your database for trending
|
209
|
+
PerformanceMetric.create!(
|
210
|
+
endpoint: endpoint,
|
211
|
+
score: data['summary']['optimization_score'],
|
212
|
+
issues: data['summary']['issues_found'],
|
213
|
+
analysis_data: data,
|
214
|
+
measured_at: Time.current
|
215
|
+
)
|
216
|
+
end
|
217
|
+
```
|
218
|
+
|
219
|
+
### 🎉 You're Done! (Total time: ~45 minutes)
|
220
|
+
|
221
|
+
Your Rails application now has:
|
222
|
+
|
223
|
+
✅ **Automatic N+1 query detection**
|
224
|
+
✅ **Slow query identification**
|
225
|
+
✅ **Missing index recommendations**
|
226
|
+
✅ **Real-time performance monitoring**
|
227
|
+
✅ **CI/CD integration**
|
228
|
+
✅ **Custom alerts and actions**
|
229
|
+
|
230
|
+
## Next Steps
|
231
|
+
|
232
|
+
- **Monitor your dashboard** for performance insights
|
233
|
+
- **Review the analysis results** and implement suggested optimizations
|
234
|
+
- **Set up alerts** for critical performance issues
|
235
|
+
- **Track performance trends** over time
|
236
|
+
- **Share results** with your team
|
237
|
+
|
238
|
+
## Installation
|
239
|
+
|
240
|
+
Add this line to your application's Gemfile:
|
241
|
+
|
242
|
+
```ruby
|
243
|
+
gem 'query_optimizer_client'
|
244
|
+
```
|
245
|
+
|
246
|
+
And then execute:
|
247
|
+
|
248
|
+
```bash
|
249
|
+
bundle install
|
250
|
+
```
|
251
|
+
|
252
|
+
## Usage
|
253
|
+
|
254
|
+
### Basic Analysis
|
255
|
+
|
256
|
+
```ruby
|
257
|
+
# Analyze queries manually
|
258
|
+
queries = [
|
259
|
+
{
|
260
|
+
sql: "SELECT * FROM users WHERE id = 1",
|
261
|
+
duration_ms: 50
|
262
|
+
},
|
263
|
+
{
|
264
|
+
sql: "SELECT * FROM posts WHERE user_id = 1",
|
265
|
+
duration_ms: 200
|
266
|
+
}
|
267
|
+
]
|
268
|
+
|
269
|
+
result = QueryOptimizerClient.analyze_queries(queries)
|
270
|
+
|
271
|
+
if result['success']
|
272
|
+
puts "Optimization Score: #{result['data']['summary']['optimization_score']}%"
|
273
|
+
puts "Issues Found: #{result['data']['summary']['issues_found']}"
|
274
|
+
end
|
275
|
+
```
|
276
|
+
|
277
|
+
### Automatic Monitoring
|
278
|
+
|
279
|
+
The gem automatically monitors your Rails application when enabled:
|
280
|
+
|
281
|
+
```ruby
|
282
|
+
# config/initializers/query_optimizer_client.rb
|
283
|
+
QueryOptimizerClient.configure do |config|
|
284
|
+
config.enabled = true
|
285
|
+
config.api_key = ENV['QUERY_OPTIMIZER_API_KEY']
|
286
|
+
# Middleware will automatically collect and analyze queries
|
287
|
+
end
|
288
|
+
```
|
289
|
+
|
290
|
+
### CI/CD Integration
|
291
|
+
|
292
|
+
Add to your CI pipeline:
|
293
|
+
|
294
|
+
```bash
|
295
|
+
# Check if your app meets the 85% performance threshold
|
296
|
+
rails query_optimizer:ci[85]
|
297
|
+
```
|
298
|
+
|
299
|
+
Or in your test suite:
|
300
|
+
|
301
|
+
```ruby
|
302
|
+
RSpec.describe "Performance" do
|
303
|
+
it "maintains good query performance" do
|
304
|
+
queries = collect_test_queries
|
305
|
+
result = QueryOptimizerClient.analyze_for_ci(queries, threshold: 85)
|
306
|
+
|
307
|
+
expect(result['data']['passed']).to be true
|
308
|
+
end
|
309
|
+
end
|
310
|
+
```
|
311
|
+
|
312
|
+
### GitHub Actions Example
|
313
|
+
|
314
|
+
```yaml
|
315
|
+
name: Performance Check
|
316
|
+
on: [push, pull_request]
|
317
|
+
|
318
|
+
jobs:
|
319
|
+
performance:
|
320
|
+
runs-on: ubuntu-latest
|
321
|
+
steps:
|
322
|
+
- uses: actions/checkout@v3
|
323
|
+
- uses: ruby/setup-ruby@v1
|
324
|
+
with:
|
325
|
+
bundler-cache: true
|
326
|
+
- name: Setup Database
|
327
|
+
run: |
|
328
|
+
bundle exec rails db:create db:migrate
|
329
|
+
- name: Run Performance Check
|
330
|
+
run: bundle exec rails query_optimizer:ci[85]
|
331
|
+
env:
|
332
|
+
QUERY_OPTIMIZER_API_KEY: ${{ secrets.QUERY_OPTIMIZER_API_KEY }}
|
333
|
+
```
|
334
|
+
|
335
|
+
## Configuration
|
336
|
+
|
337
|
+
### Environment Variables
|
338
|
+
|
339
|
+
```bash
|
340
|
+
# Required
|
341
|
+
QUERY_OPTIMIZER_API_URL=http://localhost:3000/api/v1
|
342
|
+
QUERY_OPTIMIZER_API_KEY=your_64_character_api_key
|
343
|
+
|
344
|
+
# Optional
|
345
|
+
QUERY_OPTIMIZER_ENABLED=true
|
346
|
+
QUERY_OPTIMIZER_TIMEOUT=30
|
347
|
+
QUERY_OPTIMIZER_RETRIES=3
|
348
|
+
QUERY_OPTIMIZER_THRESHOLD=80
|
349
|
+
QUERY_OPTIMIZER_BATCH_SIZE=50
|
350
|
+
QUERY_OPTIMIZER_RATE_LIMIT_RETRY=true
|
351
|
+
```
|
352
|
+
|
353
|
+
### Initializer Configuration
|
354
|
+
|
355
|
+
```ruby
|
356
|
+
# config/initializers/query_optimizer_client.rb
|
357
|
+
QueryOptimizerClient.configure do |config|
|
358
|
+
config.api_url = 'https://your-api-server.com/api/v1'
|
359
|
+
config.api_key = ENV['QUERY_OPTIMIZER_API_KEY']
|
360
|
+
config.enabled = Rails.env.production?
|
361
|
+
config.timeout = 30
|
362
|
+
config.retries = 3
|
363
|
+
config.default_threshold = 80
|
364
|
+
config.batch_size = 50
|
365
|
+
config.rate_limit_retry = true
|
366
|
+
config.logger = Rails.logger
|
367
|
+
end
|
368
|
+
```
|
369
|
+
|
370
|
+
## Rake Tasks
|
371
|
+
|
372
|
+
```bash
|
373
|
+
# Check configuration and connectivity
|
374
|
+
rails query_optimizer:check
|
375
|
+
|
376
|
+
# Analyze sample queries from your application
|
377
|
+
rails query_optimizer:analyze
|
378
|
+
|
379
|
+
# Run CI performance check with threshold
|
380
|
+
rails query_optimizer:ci[85]
|
381
|
+
|
382
|
+
# Generate a new API key
|
383
|
+
rails query_optimizer:generate_key["App Name"]
|
384
|
+
```
|
385
|
+
|
386
|
+
## Contributing
|
387
|
+
|
388
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/yourusername/query_optimizer_client.
|
389
|
+
|
390
|
+
## License
|
391
|
+
|
392
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|