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
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 54b05c02cde4d1128bd14c5411cddd7f8914d9a0927b31f0e29fe8d79c5fc2c3
|
4
|
+
data.tar.gz: 397dbad0080d557015e899e7947ede50abd6838148d7310a271af8f9c88f846a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bae2762aa237b5aa1e6f3c15507375563af9d0dbfe0695bbada5fae4964b90b253d1fab7b6342645e71a7f43ff77cc78d39212b3cb14ba0432fcfd8f97f472a7
|
7
|
+
data.tar.gz: 872c2502a91532593df69231669e13af7276c41d4ffb72ee947cf9e9e0c15cb2bed5fac6c4cf1c2eaefaf47a195a22f65b9a17c1f5b9404600457eaf31446154
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
All notable changes to this project will be documented in this file.
|
4
|
+
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
7
|
+
|
8
|
+
## [Unreleased]
|
9
|
+
|
10
|
+
## [0.1.0] - 2024-01-15
|
11
|
+
|
12
|
+
### Added
|
13
|
+
- Initial release of Query Optimizer Client gem
|
14
|
+
- Basic API client for Rails Database Query Optimizer API
|
15
|
+
- N+1 query detection integration
|
16
|
+
- Slow query analysis integration
|
17
|
+
- Missing index detection integration
|
18
|
+
- Rails middleware for automatic query monitoring
|
19
|
+
- CI/CD integration with threshold-based pass/fail
|
20
|
+
- Comprehensive configuration options
|
21
|
+
- Rake tasks for analysis and configuration checking
|
22
|
+
- Rails generator for easy installation
|
23
|
+
- Background job for asynchronous analysis
|
24
|
+
- Error handling with specific exception classes
|
25
|
+
- Rate limiting support with automatic retry
|
26
|
+
- Comprehensive logging and debugging support
|
27
|
+
|
28
|
+
### Features
|
29
|
+
- **Client**: HTTP client with retry logic and error handling
|
30
|
+
- **Configuration**: Flexible configuration via environment variables and initializers
|
31
|
+
- **Middleware**: Automatic query collection and analysis
|
32
|
+
- **Rake Tasks**: Command-line tools for analysis and CI integration
|
33
|
+
- **Generator**: Rails generator for easy setup
|
34
|
+
- **Jobs**: Background job for asynchronous processing
|
35
|
+
- **Logging**: Detailed logging with configurable levels
|
36
|
+
|
37
|
+
### Supported Ruby Versions
|
38
|
+
- Ruby 3.0+
|
39
|
+
- Rails 6.0+
|
40
|
+
|
41
|
+
### API Endpoints Supported
|
42
|
+
- `POST /analyze` - Query analysis with optimization suggestions
|
43
|
+
- `POST /analyze_ci` - CI/CD integration with pass/fail scoring
|
44
|
+
- `GET /health` - Health check
|
45
|
+
- `POST /api_keys` - API key generation
|
data/CLOUD_RUN_README.md
ADDED
@@ -0,0 +1,263 @@
|
|
1
|
+
# ☁️ Google Cloud Run Deployment
|
2
|
+
|
3
|
+
Deploy the Query Optimizer API to Google Cloud Run with automatic scaling and 2 million free requests per month!
|
4
|
+
|
5
|
+
## 🚀 One-Command Deployment
|
6
|
+
|
7
|
+
```bash
|
8
|
+
# 1. Setup Google Cloud (one-time)
|
9
|
+
./scripts/setup-gcp.sh
|
10
|
+
|
11
|
+
# 2. Deploy to Cloud Run
|
12
|
+
./scripts/deploy-cloudrun.sh
|
13
|
+
|
14
|
+
# 3. Test your deployment
|
15
|
+
./scripts/test-cloudrun.sh
|
16
|
+
```
|
17
|
+
|
18
|
+
**That's it!** Your API will be live at `https://query-optimizer-api-xxx-uc.a.run.app`
|
19
|
+
|
20
|
+
## 💰 Free Tier Benefits
|
21
|
+
|
22
|
+
Google Cloud Run free tier includes:
|
23
|
+
- ✅ **2 million requests/month** (enough for 30,000+ daily user interactions)
|
24
|
+
- ✅ **400,000 GB-seconds memory**
|
25
|
+
- ✅ **200,000 vCPU-seconds**
|
26
|
+
- ✅ **Automatic scaling** (scales to zero when not used)
|
27
|
+
- ✅ **Global CDN** and HTTPS included
|
28
|
+
- ✅ **No server management** required
|
29
|
+
|
30
|
+
## 📋 What You Get
|
31
|
+
|
32
|
+
After deployment, you'll have:
|
33
|
+
|
34
|
+
- 🌐 **Public API endpoint** with HTTPS
|
35
|
+
- 🗄️ **Managed PostgreSQL database** (Cloud SQL)
|
36
|
+
- 🔐 **Secure secrets management**
|
37
|
+
- 📊 **Automatic monitoring and logging**
|
38
|
+
- 🚀 **Auto-scaling** (0 to 10 instances)
|
39
|
+
- 💳 **Cost-effective** (free tier covers most usage)
|
40
|
+
|
41
|
+
## 🔧 Prerequisites
|
42
|
+
|
43
|
+
1. **Google Cloud Account** (free tier available)
|
44
|
+
2. **Google Cloud SDK** installed
|
45
|
+
3. **Billing enabled** (required for Cloud Run, but free tier covers most usage)
|
46
|
+
|
47
|
+
### Install Google Cloud SDK
|
48
|
+
|
49
|
+
```bash
|
50
|
+
# macOS
|
51
|
+
brew install google-cloud-sdk
|
52
|
+
|
53
|
+
# Linux
|
54
|
+
curl https://sdk.cloud.google.com | bash
|
55
|
+
|
56
|
+
# Windows
|
57
|
+
# Download from: https://cloud.google.com/sdk/docs/install
|
58
|
+
```
|
59
|
+
|
60
|
+
## 📊 Usage Examples
|
61
|
+
|
62
|
+
Once deployed, your API will be available at your Cloud Run URL:
|
63
|
+
|
64
|
+
### Get API Key
|
65
|
+
|
66
|
+
```bash
|
67
|
+
# The deployment script automatically creates one, or create manually:
|
68
|
+
curl -X POST https://your-service-url/api/v1/api_keys \
|
69
|
+
-H 'Content-Type: application/json' \
|
70
|
+
-d '{"app_name":"My Rails App"}'
|
71
|
+
```
|
72
|
+
|
73
|
+
### Analyze Queries
|
74
|
+
|
75
|
+
```bash
|
76
|
+
curl -X POST https://your-service-url/api/v1/analyze \
|
77
|
+
-H 'Content-Type: application/json' \
|
78
|
+
-H 'X-API-Key: your_api_key_here' \
|
79
|
+
-d '{
|
80
|
+
"queries": [
|
81
|
+
{
|
82
|
+
"sql": "SELECT * FROM users WHERE id = 1",
|
83
|
+
"duration_ms": 50
|
84
|
+
}
|
85
|
+
]
|
86
|
+
}'
|
87
|
+
```
|
88
|
+
|
89
|
+
### CI Integration
|
90
|
+
|
91
|
+
```bash
|
92
|
+
curl -X POST https://your-service-url/api/v1/analyze_ci \
|
93
|
+
-H 'Content-Type: application/json' \
|
94
|
+
-H 'X-API-Key: your_api_key_here' \
|
95
|
+
-d '{
|
96
|
+
"queries": [{"sql": "SELECT 1", "duration_ms": 10}],
|
97
|
+
"threshold_score": 80
|
98
|
+
}'
|
99
|
+
```
|
100
|
+
|
101
|
+
## 🔍 Monitoring
|
102
|
+
|
103
|
+
### View Logs
|
104
|
+
|
105
|
+
```bash
|
106
|
+
# Real-time logs
|
107
|
+
gcloud run services logs read query-optimizer-api --region=us-central1 --follow
|
108
|
+
|
109
|
+
# Recent errors
|
110
|
+
gcloud run services logs read query-optimizer-api --region=us-central1 --filter="severity>=ERROR"
|
111
|
+
```
|
112
|
+
|
113
|
+
### Monitor Usage
|
114
|
+
|
115
|
+
- **Service Dashboard**: https://console.cloud.google.com/run
|
116
|
+
- **Billing Dashboard**: https://console.cloud.google.com/billing
|
117
|
+
- **Logs**: https://console.cloud.google.com/logs
|
118
|
+
|
119
|
+
## 🔧 Configuration
|
120
|
+
|
121
|
+
### Environment Variables
|
122
|
+
|
123
|
+
The deployment automatically configures:
|
124
|
+
|
125
|
+
```bash
|
126
|
+
RAILS_ENV=production
|
127
|
+
DATABASE_URL=postgresql://... # Managed Cloud SQL
|
128
|
+
RAILS_MASTER_KEY=... # Stored in Secret Manager
|
129
|
+
```
|
130
|
+
|
131
|
+
### Resource Limits
|
132
|
+
|
133
|
+
```yaml
|
134
|
+
Memory: 512Mi
|
135
|
+
CPU: 1 vCPU
|
136
|
+
Max Instances: 10
|
137
|
+
Timeout: 300 seconds
|
138
|
+
```
|
139
|
+
|
140
|
+
### Custom Configuration
|
141
|
+
|
142
|
+
Edit `cloudbuild.yaml` to customize:
|
143
|
+
|
144
|
+
```yaml
|
145
|
+
# Increase memory
|
146
|
+
--memory=1Gi
|
147
|
+
|
148
|
+
# Increase CPU
|
149
|
+
--cpu=2
|
150
|
+
|
151
|
+
# Change region
|
152
|
+
--region=europe-west1
|
153
|
+
|
154
|
+
# Set max instances
|
155
|
+
--max-instances=20
|
156
|
+
```
|
157
|
+
|
158
|
+
## 💡 Cost Optimization
|
159
|
+
|
160
|
+
### Monitor Costs
|
161
|
+
|
162
|
+
```bash
|
163
|
+
# Set up billing alerts
|
164
|
+
gcloud billing budgets create \
|
165
|
+
--billing-account=YOUR_BILLING_ACCOUNT \
|
166
|
+
--display-name="Query Optimizer Budget" \
|
167
|
+
--budget-amount=10USD
|
168
|
+
```
|
169
|
+
|
170
|
+
### Optimize Usage
|
171
|
+
|
172
|
+
1. **Batch requests** - Send multiple queries in one request
|
173
|
+
2. **Cache results** - Cache analysis results for identical queries
|
174
|
+
3. **Smart thresholds** - Only analyze requests with 5+ queries
|
175
|
+
4. **Monitor usage** - Track request patterns and optimize
|
176
|
+
|
177
|
+
## 🚀 Scaling
|
178
|
+
|
179
|
+
### Automatic Scaling
|
180
|
+
|
181
|
+
Cloud Run automatically scales based on:
|
182
|
+
- **Request volume** - Scales up with more requests
|
183
|
+
- **CPU usage** - Scales up when CPU is high
|
184
|
+
- **Memory usage** - Scales up when memory is high
|
185
|
+
- **Zero scaling** - Scales to zero when no requests
|
186
|
+
|
187
|
+
### Manual Scaling
|
188
|
+
|
189
|
+
```bash
|
190
|
+
# Set minimum instances (reduces cold starts)
|
191
|
+
gcloud run services update query-optimizer-api \
|
192
|
+
--region=us-central1 \
|
193
|
+
--min-instances=1
|
194
|
+
|
195
|
+
# Set maximum instances
|
196
|
+
gcloud run services update query-optimizer-api \
|
197
|
+
--region=us-central1 \
|
198
|
+
--max-instances=20
|
199
|
+
```
|
200
|
+
|
201
|
+
## 🔒 Security
|
202
|
+
|
203
|
+
### Built-in Security
|
204
|
+
|
205
|
+
- ✅ **HTTPS by default** - All traffic encrypted
|
206
|
+
- ✅ **IAM integration** - Fine-grained access control
|
207
|
+
- ✅ **Secret management** - Secure credential storage
|
208
|
+
- ✅ **VPC support** - Network isolation available
|
209
|
+
- ✅ **Container security** - Runs in secure sandbox
|
210
|
+
|
211
|
+
### API Security
|
212
|
+
|
213
|
+
- ✅ **API key authentication** - Secure access control
|
214
|
+
- ✅ **Rate limiting** - Prevents abuse
|
215
|
+
- ✅ **Input validation** - SQL injection protection
|
216
|
+
- ✅ **CORS support** - Cross-origin request handling
|
217
|
+
|
218
|
+
## 🐛 Troubleshooting
|
219
|
+
|
220
|
+
### Common Issues
|
221
|
+
|
222
|
+
**Deployment failed:**
|
223
|
+
```bash
|
224
|
+
# Check build logs
|
225
|
+
gcloud builds list --limit=5
|
226
|
+
gcloud builds log BUILD_ID
|
227
|
+
```
|
228
|
+
|
229
|
+
**Service not responding:**
|
230
|
+
```bash
|
231
|
+
# Check service status
|
232
|
+
gcloud run services describe query-optimizer-api --region=us-central1
|
233
|
+
|
234
|
+
# Check logs
|
235
|
+
gcloud run services logs read query-optimizer-api --region=us-central1 --limit=50
|
236
|
+
```
|
237
|
+
|
238
|
+
**Database connection issues:**
|
239
|
+
```bash
|
240
|
+
# Check Cloud SQL instance
|
241
|
+
gcloud sql instances describe query-optimizer-db
|
242
|
+
|
243
|
+
# Test connection
|
244
|
+
gcloud sql connect query-optimizer-db --user=rails
|
245
|
+
```
|
246
|
+
|
247
|
+
### Get Help
|
248
|
+
|
249
|
+
- 📖 **Full Guide**: See `docs/CLOUD_RUN_GUIDE.md`
|
250
|
+
- 🌐 **Google Cloud Docs**: https://cloud.google.com/run/docs
|
251
|
+
- 💬 **Community**: https://stackoverflow.com/questions/tagged/google-cloud-run
|
252
|
+
|
253
|
+
## 🎯 Next Steps
|
254
|
+
|
255
|
+
1. **Deploy**: Run `./scripts/setup-gcp.sh` then `./scripts/deploy-cloudrun.sh`
|
256
|
+
2. **Test**: Run `./scripts/test-cloudrun.sh` to verify everything works
|
257
|
+
3. **Integrate**: Use the API URL in your Rails applications
|
258
|
+
4. **Monitor**: Set up billing alerts and monitor usage
|
259
|
+
5. **Scale**: Adjust resources as your usage grows
|
260
|
+
|
261
|
+
---
|
262
|
+
|
263
|
+
**Ready to go serverless?** Your Query Optimizer API will be running on Google Cloud Run in minutes! ☁️🚀
|
data/DOCKER_README.md
ADDED
@@ -0,0 +1,327 @@
|
|
1
|
+
# 🐳 Docker Deployment - Query Optimizer API
|
2
|
+
|
3
|
+
Get the Query Optimizer API running with Docker in minutes!
|
4
|
+
|
5
|
+
## 🚀 Quick Start
|
6
|
+
|
7
|
+
### Option 1: Automated Setup (Recommended)
|
8
|
+
|
9
|
+
```bash
|
10
|
+
# Development environment with live reloading
|
11
|
+
./scripts/docker-dev.sh
|
12
|
+
|
13
|
+
# Production environment
|
14
|
+
./scripts/docker-deploy.sh
|
15
|
+
```
|
16
|
+
|
17
|
+
### Option 2: Manual Setup
|
18
|
+
|
19
|
+
```bash
|
20
|
+
# 1. Start services
|
21
|
+
docker-compose up --build -d
|
22
|
+
|
23
|
+
# 2. Test the deployment
|
24
|
+
./scripts/test-docker.sh
|
25
|
+
```
|
26
|
+
|
27
|
+
## 📋 What You Get
|
28
|
+
|
29
|
+
- ✅ **Rails API** running on port 3000
|
30
|
+
- ✅ **PostgreSQL** database with automatic setup
|
31
|
+
- ✅ **Redis** for caching and rate limiting
|
32
|
+
- ✅ **Health checks** and monitoring
|
33
|
+
- ✅ **Automatic database migrations**
|
34
|
+
- ✅ **Production-ready configuration**
|
35
|
+
|
36
|
+
## 🛠️ Development vs Production
|
37
|
+
|
38
|
+
### Development Mode
|
39
|
+
```bash
|
40
|
+
# Start development environment
|
41
|
+
docker-compose -f docker-compose.dev.yml up --build
|
42
|
+
|
43
|
+
# Features:
|
44
|
+
# - Live code reloading
|
45
|
+
# - Development gems included
|
46
|
+
# - Detailed logging
|
47
|
+
# - Easy debugging
|
48
|
+
```
|
49
|
+
|
50
|
+
### Production Mode
|
51
|
+
```bash
|
52
|
+
# Start production environment
|
53
|
+
docker-compose up --build -d
|
54
|
+
|
55
|
+
# Features:
|
56
|
+
# - Optimized multi-stage build
|
57
|
+
# - Minimal image size
|
58
|
+
# - Security hardening
|
59
|
+
# - Performance optimizations
|
60
|
+
```
|
61
|
+
|
62
|
+
## 🔧 Configuration
|
63
|
+
|
64
|
+
### Environment Variables
|
65
|
+
|
66
|
+
Create a `.env` file:
|
67
|
+
|
68
|
+
```bash
|
69
|
+
# Database
|
70
|
+
DATABASE_URL=postgresql://postgres:password@db:5432/query_optimizer_production
|
71
|
+
|
72
|
+
# Security
|
73
|
+
RAILS_MASTER_KEY=your_master_key_here
|
74
|
+
SECRET_KEY_BASE=your_secret_key_here
|
75
|
+
|
76
|
+
# API Settings
|
77
|
+
API_RATE_LIMIT=1000
|
78
|
+
API_RATE_LIMIT_WINDOW=3600
|
79
|
+
```
|
80
|
+
|
81
|
+
### Custom Configuration
|
82
|
+
|
83
|
+
Create `docker-compose.override.yml`:
|
84
|
+
|
85
|
+
```yaml
|
86
|
+
version: '3.8'
|
87
|
+
services:
|
88
|
+
app:
|
89
|
+
ports:
|
90
|
+
- "8080:80" # Custom port
|
91
|
+
environment:
|
92
|
+
- CUSTOM_SETTING=value
|
93
|
+
```
|
94
|
+
|
95
|
+
## 📊 Usage Examples
|
96
|
+
|
97
|
+
### 1. Get API Key
|
98
|
+
|
99
|
+
```bash
|
100
|
+
# Create API key
|
101
|
+
docker-compose exec app rails runner "
|
102
|
+
app = AppProfile.create!(name: 'My App')
|
103
|
+
puts 'API Key: ' + app.generate_api_key!
|
104
|
+
"
|
105
|
+
```
|
106
|
+
|
107
|
+
### 2. Test the API
|
108
|
+
|
109
|
+
```bash
|
110
|
+
# Health check
|
111
|
+
curl http://localhost:3000/api/v1/health
|
112
|
+
|
113
|
+
# Analyze queries
|
114
|
+
curl -X POST http://localhost:3000/api/v1/analyze \
|
115
|
+
-H 'Content-Type: application/json' \
|
116
|
+
-H 'X-API-Key: your_api_key_here' \
|
117
|
+
-d '{
|
118
|
+
"queries": [
|
119
|
+
{
|
120
|
+
"sql": "SELECT * FROM users WHERE id = 1",
|
121
|
+
"duration_ms": 50
|
122
|
+
}
|
123
|
+
]
|
124
|
+
}'
|
125
|
+
```
|
126
|
+
|
127
|
+
### 3. CI Integration
|
128
|
+
|
129
|
+
```bash
|
130
|
+
# Test CI endpoint
|
131
|
+
curl -X POST http://localhost:3000/api/v1/analyze_ci \
|
132
|
+
-H 'Content-Type: application/json' \
|
133
|
+
-H 'X-API-Key: your_api_key_here' \
|
134
|
+
-d '{
|
135
|
+
"queries": [{"sql": "SELECT 1", "duration_ms": 10}],
|
136
|
+
"threshold_score": 80
|
137
|
+
}'
|
138
|
+
```
|
139
|
+
|
140
|
+
## 🔍 Monitoring
|
141
|
+
|
142
|
+
### Health Checks
|
143
|
+
|
144
|
+
```bash
|
145
|
+
# Check service status
|
146
|
+
docker-compose ps
|
147
|
+
|
148
|
+
# Check API health
|
149
|
+
curl http://localhost:3000/api/v1/health
|
150
|
+
|
151
|
+
# View logs
|
152
|
+
docker-compose logs -f app
|
153
|
+
```
|
154
|
+
|
155
|
+
### Resource Usage
|
156
|
+
|
157
|
+
```bash
|
158
|
+
# Monitor resource usage
|
159
|
+
docker stats
|
160
|
+
|
161
|
+
# Check container details
|
162
|
+
docker-compose exec app top
|
163
|
+
```
|
164
|
+
|
165
|
+
## 🚀 Deployment Options
|
166
|
+
|
167
|
+
### 1. Railway
|
168
|
+
|
169
|
+
```bash
|
170
|
+
# Deploy to Railway
|
171
|
+
railway login
|
172
|
+
railway init
|
173
|
+
railway up
|
174
|
+
```
|
175
|
+
|
176
|
+
### 2. Render
|
177
|
+
|
178
|
+
```yaml
|
179
|
+
# render.yaml
|
180
|
+
services:
|
181
|
+
- type: web
|
182
|
+
name: query-optimizer-api
|
183
|
+
env: docker
|
184
|
+
dockerfilePath: ./Dockerfile
|
185
|
+
```
|
186
|
+
|
187
|
+
### 3. Google Cloud Run
|
188
|
+
|
189
|
+
```bash
|
190
|
+
# Build and deploy
|
191
|
+
docker build -t gcr.io/project/query-optimizer-api .
|
192
|
+
docker push gcr.io/project/query-optimizer-api
|
193
|
+
gcloud run deploy --image gcr.io/project/query-optimizer-api
|
194
|
+
```
|
195
|
+
|
196
|
+
### 4. AWS ECS
|
197
|
+
|
198
|
+
```bash
|
199
|
+
# Push to ECR
|
200
|
+
aws ecr get-login-password | docker login --username AWS --password-stdin
|
201
|
+
docker build -t query-optimizer-api .
|
202
|
+
docker tag query-optimizer-api:latest account.dkr.ecr.region.amazonaws.com/query-optimizer-api:latest
|
203
|
+
docker push account.dkr.ecr.region.amazonaws.com/query-optimizer-api:latest
|
204
|
+
```
|
205
|
+
|
206
|
+
## 🛠️ Useful Commands
|
207
|
+
|
208
|
+
### Development
|
209
|
+
|
210
|
+
```bash
|
211
|
+
# Rails console
|
212
|
+
docker-compose -f docker-compose.dev.yml exec app rails console
|
213
|
+
|
214
|
+
# Run tests
|
215
|
+
docker-compose -f docker-compose.dev.yml exec app rspec
|
216
|
+
|
217
|
+
# Generate migration
|
218
|
+
docker-compose -f docker-compose.dev.yml exec app rails generate migration AddIndexToUsers
|
219
|
+
|
220
|
+
# Run migration
|
221
|
+
docker-compose -f docker-compose.dev.yml exec app rails db:migrate
|
222
|
+
```
|
223
|
+
|
224
|
+
### Production
|
225
|
+
|
226
|
+
```bash
|
227
|
+
# Scale application
|
228
|
+
docker-compose up -d --scale app=3
|
229
|
+
|
230
|
+
# Update application
|
231
|
+
docker-compose pull
|
232
|
+
docker-compose up -d
|
233
|
+
|
234
|
+
# Backup database
|
235
|
+
docker-compose exec db pg_dump -U postgres query_optimizer_production > backup.sql
|
236
|
+
|
237
|
+
# View application logs
|
238
|
+
docker-compose logs -f app
|
239
|
+
```
|
240
|
+
|
241
|
+
### Debugging
|
242
|
+
|
243
|
+
```bash
|
244
|
+
# Enter container
|
245
|
+
docker-compose exec app bash
|
246
|
+
|
247
|
+
# Check environment
|
248
|
+
docker-compose exec app env
|
249
|
+
|
250
|
+
# Debug database connection
|
251
|
+
docker-compose exec app rails runner "puts ActiveRecord::Base.connection.execute('SELECT 1')"
|
252
|
+
```
|
253
|
+
|
254
|
+
## 🔒 Security
|
255
|
+
|
256
|
+
### Production Security Checklist
|
257
|
+
|
258
|
+
- ✅ **Non-root user**: Containers run as user 1000
|
259
|
+
- ✅ **Secrets management**: Master key stored as Docker secret
|
260
|
+
- ✅ **Health checks**: Automatic container health monitoring
|
261
|
+
- ✅ **Resource limits**: Memory and CPU limits configured
|
262
|
+
- ✅ **Network isolation**: Services communicate via internal network
|
263
|
+
- ✅ **Minimal image**: Multi-stage build reduces attack surface
|
264
|
+
|
265
|
+
### Security Best Practices
|
266
|
+
|
267
|
+
```yaml
|
268
|
+
# docker-compose.yml security example
|
269
|
+
services:
|
270
|
+
app:
|
271
|
+
deploy:
|
272
|
+
resources:
|
273
|
+
limits:
|
274
|
+
memory: 512M
|
275
|
+
cpus: '0.5'
|
276
|
+
security_opt:
|
277
|
+
- no-new-privileges:true
|
278
|
+
read_only: true
|
279
|
+
tmpfs:
|
280
|
+
- /tmp
|
281
|
+
- /rails/tmp
|
282
|
+
```
|
283
|
+
|
284
|
+
## 🐛 Troubleshooting
|
285
|
+
|
286
|
+
### Common Issues
|
287
|
+
|
288
|
+
**Port already in use:**
|
289
|
+
```bash
|
290
|
+
# Find and kill process
|
291
|
+
lsof -i :3000
|
292
|
+
kill -9 <PID>
|
293
|
+
```
|
294
|
+
|
295
|
+
**Database connection failed:**
|
296
|
+
```bash
|
297
|
+
# Check database status
|
298
|
+
docker-compose ps db
|
299
|
+
docker-compose logs db
|
300
|
+
|
301
|
+
# Restart database
|
302
|
+
docker-compose restart db
|
303
|
+
```
|
304
|
+
|
305
|
+
**Permission denied:**
|
306
|
+
```bash
|
307
|
+
# Fix permissions
|
308
|
+
sudo chown -R $USER:$USER .
|
309
|
+
chmod +x scripts/*.sh
|
310
|
+
```
|
311
|
+
|
312
|
+
**Out of disk space:**
|
313
|
+
```bash
|
314
|
+
# Clean up Docker
|
315
|
+
docker system prune -a
|
316
|
+
docker volume prune
|
317
|
+
```
|
318
|
+
|
319
|
+
## 📞 Support
|
320
|
+
|
321
|
+
- 📖 **Full Documentation**: See `docs/DOCKER_GUIDE.md`
|
322
|
+
- 🐛 **Issues**: Report issues on GitHub
|
323
|
+
- 💬 **Discussions**: Join our community discussions
|
324
|
+
|
325
|
+
---
|
326
|
+
|
327
|
+
**Ready to deploy?** Run `./scripts/docker-deploy.sh` and you'll have a production-ready Query Optimizer API running in minutes! 🚀
|
data/Dockerfile
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
# syntax=docker/dockerfile:1
|
2
|
+
# check=error=true
|
3
|
+
|
4
|
+
# This Dockerfile is designed for production, not development. Use with Kamal or build'n'run by hand:
|
5
|
+
# docker build -t query_optimizer_api .
|
6
|
+
# docker run -d -p 80:80 -e RAILS_MASTER_KEY=<value from config/master.key> --name query_optimizer_api query_optimizer_api
|
7
|
+
|
8
|
+
# For a containerized dev environment, see Dev Containers: https://guides.rubyonrails.org/getting_started_with_devcontainer.html
|
9
|
+
|
10
|
+
# Make sure RUBY_VERSION matches the Ruby version in .ruby-version
|
11
|
+
ARG RUBY_VERSION=3.2.2
|
12
|
+
FROM docker.io/library/ruby:$RUBY_VERSION-slim AS base
|
13
|
+
|
14
|
+
# Rails app lives here
|
15
|
+
WORKDIR /rails
|
16
|
+
|
17
|
+
# Install base packages
|
18
|
+
RUN apt-get update -qq && \
|
19
|
+
apt-get install --no-install-recommends -y curl libjemalloc2 libvips postgresql-client && \
|
20
|
+
rm -rf /var/lib/apt/lists /var/cache/apt/archives
|
21
|
+
|
22
|
+
# Set production environment
|
23
|
+
ENV RAILS_ENV="production" \
|
24
|
+
BUNDLE_DEPLOYMENT="1" \
|
25
|
+
BUNDLE_PATH="/usr/local/bundle" \
|
26
|
+
BUNDLE_WITHOUT="development"
|
27
|
+
|
28
|
+
# Throw-away build stage to reduce size of final image
|
29
|
+
FROM base AS build
|
30
|
+
|
31
|
+
# Install packages needed to build gems
|
32
|
+
RUN apt-get update -qq && \
|
33
|
+
apt-get install --no-install-recommends -y build-essential git libpq-dev libyaml-dev pkg-config && \
|
34
|
+
rm -rf /var/lib/apt/lists /var/cache/apt/archives
|
35
|
+
|
36
|
+
# Install application gems
|
37
|
+
COPY Gemfile Gemfile.lock ./
|
38
|
+
RUN bundle install && \
|
39
|
+
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \
|
40
|
+
bundle exec bootsnap precompile --gemfile
|
41
|
+
|
42
|
+
# Copy application code
|
43
|
+
COPY . .
|
44
|
+
|
45
|
+
# Precompile bootsnap code for faster boot times
|
46
|
+
RUN bundle exec bootsnap precompile app/ lib/
|
47
|
+
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
# Final stage for app image
|
52
|
+
FROM base
|
53
|
+
|
54
|
+
# Copy built artifacts: gems, application
|
55
|
+
COPY --from=build "${BUNDLE_PATH}" "${BUNDLE_PATH}"
|
56
|
+
COPY --from=build /rails /rails
|
57
|
+
|
58
|
+
# Run and own only the runtime files as a non-root user for security
|
59
|
+
RUN groupadd --system --gid 1000 rails && \
|
60
|
+
useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \
|
61
|
+
chown -R rails:rails db log storage tmp
|
62
|
+
USER 1000:1000
|
63
|
+
|
64
|
+
# Entrypoint prepares the database.
|
65
|
+
ENTRYPOINT ["/rails/bin/docker-entrypoint"]
|
66
|
+
|
67
|
+
# Start server via Thruster by default, this can be overwritten at runtime
|
68
|
+
EXPOSE 80
|
69
|
+
CMD ["./bin/thrust", "./bin/rails", "server"]
|