gudb 0.1.0__tar.gz
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.
- gudb-0.1.0/.env.example +23 -0
- gudb-0.1.0/.gitignore +21 -0
- gudb-0.1.0/PKG-INFO +238 -0
- gudb-0.1.0/README.md +198 -0
- gudb-0.1.0/SDK_README.md +93 -0
- gudb-0.1.0/api/analysis.py +25 -0
- gudb-0.1.0/api/notifications.py +39 -0
- gudb-0.1.0/examples/enterprise_demo.py +41 -0
- gudb-0.1.0/examples/hackathon_demo.py +158 -0
- gudb-0.1.0/main.py +613 -0
- gudb-0.1.0/pyproject.toml +56 -0
- gudb-0.1.0/requirements.txt +16 -0
- gudb-0.1.0/services/graph.py +24 -0
- gudb-0.1.0/services/models.py +54 -0
- gudb-0.1.0/services/nodes.py +495 -0
- gudb-0.1.0/services/preventive_analyzer.py +407 -0
- gudb-0.1.0/services/prompts.py +184 -0
- gudb-0.1.0/services/state.py +30 -0
- gudb-0.1.0/setup_test_db.sql +37 -0
- gudb-0.1.0/src/gudb/__init__.py +32 -0
- gudb-0.1.0/src/gudb/config.py +52 -0
- gudb-0.1.0/src/gudb/core/guardian.py +99 -0
- gudb-0.1.0/src/gudb/core/interceptor.py +96 -0
- gudb-0.1.0/src/gudb/core/redactor.py +16 -0
- gudb-0.1.0/src/gudb/exceptions.py +42 -0
- gudb-0.1.0/src/gudb/middlewares/fastapi.py +25 -0
- gudb-0.1.0/src/gudb/middlewares/flask.py +28 -0
- gudb-0.1.0/src/gudb/providers/base.py +22 -0
- gudb-0.1.0/src/gudb/providers/remote.py +40 -0
- gudb-0.1.0/src/gudb/utils/context.py +40 -0
- gudb-0.1.0/src/gudb/utils/logging.py +18 -0
- gudb-0.1.0/static/app.js +407 -0
- gudb-0.1.0/static/index.html +145 -0
- gudb-0.1.0/static/styles.css +946 -0
- gudb-0.1.0/test_system.sh +42 -0
- gudb-0.1.0/utils/benchmark.py +204 -0
- gudb-0.1.0/utils/bouncer.py +73 -0
- gudb-0.1.0/utils/config.py +56 -0
- gudb-0.1.0/utils/db.py +48 -0
- gudb-0.1.0/utils/economics.py +67 -0
- gudb-0.1.0/utils/git_tracker.py +325 -0
- gudb-0.1.0/utils/notifier.py +32 -0
- gudb-0.1.0/utils/schema_tracker.py +502 -0
- gudb-0.1.0/vercel.json +13 -0
gudb-0.1.0/.env.example
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# AI-DB-Sentinel Configuration
|
|
2
|
+
|
|
3
|
+
# Database Connection
|
|
4
|
+
DB_URL=postgresql://user:password@localhost/db
|
|
5
|
+
|
|
6
|
+
# Gemini API
|
|
7
|
+
GEMINI_API_KEY=your_gemini_api_key_here
|
|
8
|
+
GEMINI_MODEL=gemini-2.0-flash-exp
|
|
9
|
+
|
|
10
|
+
# Discord Webhook (Optional - for Discord notifications)
|
|
11
|
+
# Get your webhook URL from Discord Server Settings > Integrations > Webhooks
|
|
12
|
+
DISCORD_WEBHOOK_URL=
|
|
13
|
+
|
|
14
|
+
# Query Monitoring Thresholds (milliseconds)
|
|
15
|
+
SLOW_QUERY_THRESHOLD_MS=500
|
|
16
|
+
CRITICAL_THRESHOLD_MS=2000
|
|
17
|
+
|
|
18
|
+
# Feature Flags
|
|
19
|
+
ENABLE_AUTO_ANALYSIS=true
|
|
20
|
+
ENABLE_NOTIFICATIONS=true
|
|
21
|
+
|
|
22
|
+
# Notification Settings
|
|
23
|
+
MAX_STORED_ALERTS=100
|
gudb-0.1.0/.gitignore
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
.env
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.pyc
|
|
4
|
+
venv/
|
|
5
|
+
env/
|
|
6
|
+
*.sqlite3
|
|
7
|
+
*.log
|
|
8
|
+
.DS_Store
|
|
9
|
+
.idea/
|
|
10
|
+
.vscode/
|
|
11
|
+
migrations/
|
|
12
|
+
instance/
|
|
13
|
+
*.egg-info/
|
|
14
|
+
dist/
|
|
15
|
+
build/
|
|
16
|
+
.coverage
|
|
17
|
+
htmlcov/
|
|
18
|
+
.tox/
|
|
19
|
+
.pytest_cache/
|
|
20
|
+
celerybeat-schedule
|
|
21
|
+
*.sqlite3-journal
|
gudb-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: gudb
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: gudb: The Database Seatbelt 🛡️ - A professional database guardian SDK with AI-backed query interception and safety guardrails.
|
|
5
|
+
Project-URL: Homepage, https://gudb.ai
|
|
6
|
+
Project-URL: Documentation, https://docs.gudb.ai
|
|
7
|
+
Project-URL: Repository, https://github.com/gudb-ai/gudb.git
|
|
8
|
+
Project-URL: Issues, https://github.com/gudb-ai/gudb/issues
|
|
9
|
+
Author-email: Gudb Team <hello@gudb.ai>
|
|
10
|
+
License-Expression: MIT
|
|
11
|
+
Keywords: ai,database,gudb,monitoring,safety,sentinel,sql,sre
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Topic :: Database
|
|
19
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
20
|
+
Requires-Python: >=3.9
|
|
21
|
+
Requires-Dist: psycopg2-binary>=2.9.0
|
|
22
|
+
Requires-Dist: pydantic-settings>=2.0.0
|
|
23
|
+
Requires-Dist: pydantic>=2.0.0
|
|
24
|
+
Requires-Dist: python-dotenv>=1.0.0
|
|
25
|
+
Requires-Dist: requests>=2.31.0
|
|
26
|
+
Requires-Dist: sqlparse>=0.4.4
|
|
27
|
+
Provides-Extra: dev
|
|
28
|
+
Requires-Dist: black; extra == 'dev'
|
|
29
|
+
Requires-Dist: build; extra == 'dev'
|
|
30
|
+
Requires-Dist: httpx>=0.24.0; extra == 'dev'
|
|
31
|
+
Requires-Dist: isort; extra == 'dev'
|
|
32
|
+
Requires-Dist: pytest-asyncio>=0.21.0; extra == 'dev'
|
|
33
|
+
Requires-Dist: pytest>=7.0.0; extra == 'dev'
|
|
34
|
+
Requires-Dist: twine; extra == 'dev'
|
|
35
|
+
Provides-Extra: fastapi
|
|
36
|
+
Requires-Dist: fastapi>=0.100.0; extra == 'fastapi'
|
|
37
|
+
Provides-Extra: flask
|
|
38
|
+
Requires-Dist: flask>=2.0.0; extra == 'flask'
|
|
39
|
+
Description-Content-Type: text/markdown
|
|
40
|
+
|
|
41
|
+
# gudb: The Database Seatbelt 🛡️
|
|
42
|
+
|
|
43
|
+
**"You never notice a seatbelt until it saves your life. We do the same for your production database."**
|
|
44
|
+
|
|
45
|
+
gudb is a high-performance safety layer that prevents "Career-Ending" database disasters. It intercepts unconstrained `DELETE`, `DROP`, and `TRUNCATE` operations in real-time with zero latency, while using Gemini AI as an asynchronous "Senior DRE Advisor" to suggest performance optimizations.
|
|
46
|
+
|
|
47
|
+
## 🎬 Killer Demo (Hackathon Special)
|
|
48
|
+
Check out our high-impact terminal demo that showcases the seatbelt in action:
|
|
49
|
+
```bash
|
|
50
|
+
python3 examples/hackathon_demo.py
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Features
|
|
54
|
+
- ⚡ **Zero-Latency Seatbelt**: Hardcoded safety rules block disasters in <1ms.
|
|
55
|
+
- 🤖 **AI Advisor**: Asynchronous query analysis suggests indexes and refactors.
|
|
56
|
+
- 🔔 **Command Center**: A beautiful dashboard for real-time observability.
|
|
57
|
+
- 🔧 **One-Line Integration**: `conn = monitor(raw_psycopg2_conn)`
|
|
58
|
+
|
|
59
|
+
## Architecture
|
|
60
|
+
|
|
61
|
+
```
|
|
62
|
+
User Request → Middleware (Detects Slow Query) → Creates Alert → Triggers AI Analysis
|
|
63
|
+
↓
|
|
64
|
+
Notification Badge
|
|
65
|
+
↓
|
|
66
|
+
User Clicks → Shows Details
|
|
67
|
+
↓
|
|
68
|
+
AI Recommendations + Fix
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Installation
|
|
72
|
+
|
|
73
|
+
1. Clone the repository:
|
|
74
|
+
```bash
|
|
75
|
+
git clone <your-repo-url>
|
|
76
|
+
cd gudb
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
2. Create virtual environment:
|
|
80
|
+
```bash
|
|
81
|
+
python -m venv venv
|
|
82
|
+
source venv/bin/activate # On Windows: venv\Scripts\activate
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
3. Install dependencies:
|
|
86
|
+
```bash
|
|
87
|
+
pip install -r requirements.txt
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
4. Configure environment variables:
|
|
91
|
+
```bash
|
|
92
|
+
cp .env.example .env
|
|
93
|
+
# Edit .env with your database URL and Gemini API key
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## Configuration
|
|
97
|
+
|
|
98
|
+
Edit `.env` file:
|
|
99
|
+
|
|
100
|
+
```env
|
|
101
|
+
# Database Connection
|
|
102
|
+
DB_URL=postgresql://user:password@localhost:5432/your_database
|
|
103
|
+
|
|
104
|
+
# Gemini API
|
|
105
|
+
GEMINI_API_KEY=your_gemini_api_key_here
|
|
106
|
+
|
|
107
|
+
# Thresholds (milliseconds)
|
|
108
|
+
SLOW_QUERY_THRESHOLD_MS=500
|
|
109
|
+
CRITICAL_THRESHOLD_MS=2000
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Usage
|
|
113
|
+
|
|
114
|
+
### Start the Server
|
|
115
|
+
|
|
116
|
+
```bash
|
|
117
|
+
uvicorn main:app --reload
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Access the Dashboard
|
|
121
|
+
|
|
122
|
+
Open your browser and navigate to:
|
|
123
|
+
- **Production Dashboard**: [https://gudb.ai/dashboard](https://gudb.ai/dashboard)
|
|
124
|
+
- **Local Dashboard**: http://localhost:8000/dashboard
|
|
125
|
+
- **Local API Docs**: http://localhost:8000/docs
|
|
126
|
+
|
|
127
|
+
### Test Slow Query Detection
|
|
128
|
+
|
|
129
|
+
Trigger a test slow query:
|
|
130
|
+
```bash
|
|
131
|
+
curl http://localhost:8000/test/slow
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
Watch the notification badge update and click to see AI analysis!
|
|
135
|
+
|
|
136
|
+
## API Endpoints
|
|
137
|
+
|
|
138
|
+
### Notifications
|
|
139
|
+
- `GET /api/notifications/` - Get all alerts
|
|
140
|
+
- `GET /api/notifications/?severity=critical` - Filter by severity
|
|
141
|
+
- `GET /api/notifications/count` - Get notification count
|
|
142
|
+
- `GET /api/notifications/{alert_id}` - Get specific alert
|
|
143
|
+
|
|
144
|
+
### Analysis
|
|
145
|
+
- `GET /api/analysis/{alert_id}` - Get detailed AI analysis for an alert
|
|
146
|
+
|
|
147
|
+
### Health
|
|
148
|
+
- `GET /health` - Health check
|
|
149
|
+
- `GET /` - Service info
|
|
150
|
+
|
|
151
|
+
## How It Works
|
|
152
|
+
|
|
153
|
+
1. **Detection**: Middleware measures query execution time
|
|
154
|
+
2. **Alert Creation**: If time exceeds threshold, creates an alert with severity level
|
|
155
|
+
3. **Background Analysis**: Triggers AI analysis using LangGraph workflow:
|
|
156
|
+
- **Detective Node**: Runs EXPLAIN ANALYZE and gathers schema info
|
|
157
|
+
- **Architect Node**: Uses Gemini to identify bottlenecks and suggest fixes
|
|
158
|
+
- **Validator Node**: Stores analysis results
|
|
159
|
+
4. **User Interaction**: User sees notification badge, clicks to view detailed analysis
|
|
160
|
+
5. **Action**: User can copy the suggested SQL fix and apply it
|
|
161
|
+
|
|
162
|
+
## LangGraph Workflow
|
|
163
|
+
|
|
164
|
+
```
|
|
165
|
+
Detective → Architect → Validator
|
|
166
|
+
↓ ↓ ↓
|
|
167
|
+
EXPLAIN Gemini AI Store
|
|
168
|
+
ANALYZE Analysis Results
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
## Project Structure
|
|
172
|
+
|
|
173
|
+
```
|
|
174
|
+
gudb/
|
|
175
|
+
├── main.py # FastAPI app with middleware
|
|
176
|
+
├── services/
|
|
177
|
+
├── src/
|
|
178
|
+
│ └── gudb/ # The SDK Package
|
|
179
|
+
│ ├── core/
|
|
180
|
+
│ ├── providers/
|
|
181
|
+
│ └── middlewares/
|
|
182
|
+
├── static/ # Dashboard UI
|
|
183
|
+
└── requirements.txt
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
## Use as SDK/Middleware
|
|
187
|
+
|
|
188
|
+
To integrate into your own FastAPI app:
|
|
189
|
+
|
|
190
|
+
```python
|
|
191
|
+
from fastapi import FastAPI
|
|
192
|
+
from gudb.middlewares.fastapi import SafeDBMiddleware
|
|
193
|
+
|
|
194
|
+
app = FastAPI()
|
|
195
|
+
app.add_middleware(SafeDBMiddleware)
|
|
196
|
+
|
|
197
|
+
# Your routes here...
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
## Customization
|
|
201
|
+
|
|
202
|
+
### Adjust Thresholds
|
|
203
|
+
Edit `.env`:
|
|
204
|
+
```env
|
|
205
|
+
SLOW_QUERY_THRESHOLD_MS=300 # More sensitive
|
|
206
|
+
CRITICAL_THRESHOLD_MS=1000 # Lower critical threshold
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
### Disable Auto-Analysis
|
|
210
|
+
```env
|
|
211
|
+
ENABLE_AUTO_ANALYSIS=false
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
## 🌐 Vercel Deployment
|
|
215
|
+
|
|
216
|
+
To deploy the gudb Command Center on Vercel as a static site:
|
|
217
|
+
|
|
218
|
+
1. **Project Root**: Ensure you are in the repository root.
|
|
219
|
+
2. **Configuration**: The included `vercel.json` automatically handles routing to the `static/` directory.
|
|
220
|
+
3. **Deployment**:
|
|
221
|
+
```bash
|
|
222
|
+
vercel --prod
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
> [!TIP]
|
|
226
|
+
> This deployment mode is for the **frontend only**. To protect your production database, ensure the gudb SDK is deployed within your application cluster.
|
|
227
|
+
|
|
228
|
+
## License
|
|
229
|
+
|
|
230
|
+
MIT
|
|
231
|
+
|
|
232
|
+
## Contributing
|
|
233
|
+
|
|
234
|
+
Contributions welcome! Please open an issue or PR.
|
|
235
|
+
|
|
236
|
+
---
|
|
237
|
+
|
|
238
|
+
Built with ❤️ using FastAPI, LangGraph, and Google Gemini
|
gudb-0.1.0/README.md
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
# gudb: The Database Seatbelt 🛡️
|
|
2
|
+
|
|
3
|
+
**"You never notice a seatbelt until it saves your life. We do the same for your production database."**
|
|
4
|
+
|
|
5
|
+
gudb is a high-performance safety layer that prevents "Career-Ending" database disasters. It intercepts unconstrained `DELETE`, `DROP`, and `TRUNCATE` operations in real-time with zero latency, while using Gemini AI as an asynchronous "Senior DRE Advisor" to suggest performance optimizations.
|
|
6
|
+
|
|
7
|
+
## 🎬 Killer Demo (Hackathon Special)
|
|
8
|
+
Check out our high-impact terminal demo that showcases the seatbelt in action:
|
|
9
|
+
```bash
|
|
10
|
+
python3 examples/hackathon_demo.py
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Features
|
|
14
|
+
- ⚡ **Zero-Latency Seatbelt**: Hardcoded safety rules block disasters in <1ms.
|
|
15
|
+
- 🤖 **AI Advisor**: Asynchronous query analysis suggests indexes and refactors.
|
|
16
|
+
- 🔔 **Command Center**: A beautiful dashboard for real-time observability.
|
|
17
|
+
- 🔧 **One-Line Integration**: `conn = monitor(raw_psycopg2_conn)`
|
|
18
|
+
|
|
19
|
+
## Architecture
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
User Request → Middleware (Detects Slow Query) → Creates Alert → Triggers AI Analysis
|
|
23
|
+
↓
|
|
24
|
+
Notification Badge
|
|
25
|
+
↓
|
|
26
|
+
User Clicks → Shows Details
|
|
27
|
+
↓
|
|
28
|
+
AI Recommendations + Fix
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Installation
|
|
32
|
+
|
|
33
|
+
1. Clone the repository:
|
|
34
|
+
```bash
|
|
35
|
+
git clone <your-repo-url>
|
|
36
|
+
cd gudb
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
2. Create virtual environment:
|
|
40
|
+
```bash
|
|
41
|
+
python -m venv venv
|
|
42
|
+
source venv/bin/activate # On Windows: venv\Scripts\activate
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
3. Install dependencies:
|
|
46
|
+
```bash
|
|
47
|
+
pip install -r requirements.txt
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
4. Configure environment variables:
|
|
51
|
+
```bash
|
|
52
|
+
cp .env.example .env
|
|
53
|
+
# Edit .env with your database URL and Gemini API key
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Configuration
|
|
57
|
+
|
|
58
|
+
Edit `.env` file:
|
|
59
|
+
|
|
60
|
+
```env
|
|
61
|
+
# Database Connection
|
|
62
|
+
DB_URL=postgresql://user:password@localhost:5432/your_database
|
|
63
|
+
|
|
64
|
+
# Gemini API
|
|
65
|
+
GEMINI_API_KEY=your_gemini_api_key_here
|
|
66
|
+
|
|
67
|
+
# Thresholds (milliseconds)
|
|
68
|
+
SLOW_QUERY_THRESHOLD_MS=500
|
|
69
|
+
CRITICAL_THRESHOLD_MS=2000
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Usage
|
|
73
|
+
|
|
74
|
+
### Start the Server
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
uvicorn main:app --reload
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Access the Dashboard
|
|
81
|
+
|
|
82
|
+
Open your browser and navigate to:
|
|
83
|
+
- **Production Dashboard**: [https://gudb.ai/dashboard](https://gudb.ai/dashboard)
|
|
84
|
+
- **Local Dashboard**: http://localhost:8000/dashboard
|
|
85
|
+
- **Local API Docs**: http://localhost:8000/docs
|
|
86
|
+
|
|
87
|
+
### Test Slow Query Detection
|
|
88
|
+
|
|
89
|
+
Trigger a test slow query:
|
|
90
|
+
```bash
|
|
91
|
+
curl http://localhost:8000/test/slow
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
Watch the notification badge update and click to see AI analysis!
|
|
95
|
+
|
|
96
|
+
## API Endpoints
|
|
97
|
+
|
|
98
|
+
### Notifications
|
|
99
|
+
- `GET /api/notifications/` - Get all alerts
|
|
100
|
+
- `GET /api/notifications/?severity=critical` - Filter by severity
|
|
101
|
+
- `GET /api/notifications/count` - Get notification count
|
|
102
|
+
- `GET /api/notifications/{alert_id}` - Get specific alert
|
|
103
|
+
|
|
104
|
+
### Analysis
|
|
105
|
+
- `GET /api/analysis/{alert_id}` - Get detailed AI analysis for an alert
|
|
106
|
+
|
|
107
|
+
### Health
|
|
108
|
+
- `GET /health` - Health check
|
|
109
|
+
- `GET /` - Service info
|
|
110
|
+
|
|
111
|
+
## How It Works
|
|
112
|
+
|
|
113
|
+
1. **Detection**: Middleware measures query execution time
|
|
114
|
+
2. **Alert Creation**: If time exceeds threshold, creates an alert with severity level
|
|
115
|
+
3. **Background Analysis**: Triggers AI analysis using LangGraph workflow:
|
|
116
|
+
- **Detective Node**: Runs EXPLAIN ANALYZE and gathers schema info
|
|
117
|
+
- **Architect Node**: Uses Gemini to identify bottlenecks and suggest fixes
|
|
118
|
+
- **Validator Node**: Stores analysis results
|
|
119
|
+
4. **User Interaction**: User sees notification badge, clicks to view detailed analysis
|
|
120
|
+
5. **Action**: User can copy the suggested SQL fix and apply it
|
|
121
|
+
|
|
122
|
+
## LangGraph Workflow
|
|
123
|
+
|
|
124
|
+
```
|
|
125
|
+
Detective → Architect → Validator
|
|
126
|
+
↓ ↓ ↓
|
|
127
|
+
EXPLAIN Gemini AI Store
|
|
128
|
+
ANALYZE Analysis Results
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## Project Structure
|
|
132
|
+
|
|
133
|
+
```
|
|
134
|
+
gudb/
|
|
135
|
+
├── main.py # FastAPI app with middleware
|
|
136
|
+
├── services/
|
|
137
|
+
├── src/
|
|
138
|
+
│ └── gudb/ # The SDK Package
|
|
139
|
+
│ ├── core/
|
|
140
|
+
│ ├── providers/
|
|
141
|
+
│ └── middlewares/
|
|
142
|
+
├── static/ # Dashboard UI
|
|
143
|
+
└── requirements.txt
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
## Use as SDK/Middleware
|
|
147
|
+
|
|
148
|
+
To integrate into your own FastAPI app:
|
|
149
|
+
|
|
150
|
+
```python
|
|
151
|
+
from fastapi import FastAPI
|
|
152
|
+
from gudb.middlewares.fastapi import SafeDBMiddleware
|
|
153
|
+
|
|
154
|
+
app = FastAPI()
|
|
155
|
+
app.add_middleware(SafeDBMiddleware)
|
|
156
|
+
|
|
157
|
+
# Your routes here...
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## Customization
|
|
161
|
+
|
|
162
|
+
### Adjust Thresholds
|
|
163
|
+
Edit `.env`:
|
|
164
|
+
```env
|
|
165
|
+
SLOW_QUERY_THRESHOLD_MS=300 # More sensitive
|
|
166
|
+
CRITICAL_THRESHOLD_MS=1000 # Lower critical threshold
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
### Disable Auto-Analysis
|
|
170
|
+
```env
|
|
171
|
+
ENABLE_AUTO_ANALYSIS=false
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
## 🌐 Vercel Deployment
|
|
175
|
+
|
|
176
|
+
To deploy the gudb Command Center on Vercel as a static site:
|
|
177
|
+
|
|
178
|
+
1. **Project Root**: Ensure you are in the repository root.
|
|
179
|
+
2. **Configuration**: The included `vercel.json` automatically handles routing to the `static/` directory.
|
|
180
|
+
3. **Deployment**:
|
|
181
|
+
```bash
|
|
182
|
+
vercel --prod
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
> [!TIP]
|
|
186
|
+
> This deployment mode is for the **frontend only**. To protect your production database, ensure the gudb SDK is deployed within your application cluster.
|
|
187
|
+
|
|
188
|
+
## License
|
|
189
|
+
|
|
190
|
+
MIT
|
|
191
|
+
|
|
192
|
+
## Contributing
|
|
193
|
+
|
|
194
|
+
Contributions welcome! Please open an issue or PR.
|
|
195
|
+
|
|
196
|
+
---
|
|
197
|
+
|
|
198
|
+
Built with ❤️ using FastAPI, LangGraph, and Google Gemini
|
gudb-0.1.0/SDK_README.md
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# gudb: The Database Seatbelt 🛡️
|
|
2
|
+
|
|
3
|
+
**gudb** is a high-performance database safety layer designed to prevent production disasters *before* they happen. It acts as a "seatbelt" for your data, intercepting dangerous queries at the driver level with zero-latency heuristics and Senior DRE-level AI advice.
|
|
4
|
+
|
|
5
|
+
## ✨ Why a Seatbelt?
|
|
6
|
+
- **Zero-Latency Protection**: Local heuristics block `DELETE` or `DROP` without `WHERE` in <1ms.
|
|
7
|
+
- **Fail-Safe by Design**: If the AI or network is down, the seatbelt stays on.
|
|
8
|
+
- **AI Advisor**: Deep query analysis and optimization tips provided by Gemini AI.
|
|
9
|
+
- **Driver-Level Hook**: Drops into your `psycopg2` connection in one line of code.
|
|
10
|
+
- **Privacy First**: SQL redaction ensures sensitive PII never leaves your network.
|
|
11
|
+
|
|
12
|
+
## 📦 Installation
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
pip install gudb
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
To install with framework-specific extras:
|
|
19
|
+
```bash
|
|
20
|
+
pip install "gudb[fastapi]"
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## 🚀 Quick Start
|
|
24
|
+
|
|
25
|
+
### 1. The One-Liner (DB Driver Level)
|
|
26
|
+
Wrap your database connection to start guarding immediately.
|
|
27
|
+
|
|
28
|
+
```python
|
|
29
|
+
import psycopg2
|
|
30
|
+
from gudb import monitor
|
|
31
|
+
|
|
32
|
+
# Create your connection
|
|
33
|
+
raw_conn = psycopg2.connect("postgres://...")
|
|
34
|
+
|
|
35
|
+
# --- GUARD IT ---
|
|
36
|
+
conn = monitor(raw_conn)
|
|
37
|
+
# -----------------
|
|
38
|
+
|
|
39
|
+
cur = conn.cursor()
|
|
40
|
+
cur.execute("DELETE FROM orders;") # 🛑 Raises DisasterBlockedError
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### 2. FastAPI Integration
|
|
44
|
+
```python
|
|
45
|
+
from fastapi import FastAPI
|
|
46
|
+
from gudb.middlewares.fastapi import SafeDBMiddleware
|
|
47
|
+
|
|
48
|
+
app = FastAPI()
|
|
49
|
+
app.add_middleware(SafeDBMiddleware)
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## ⚙️ Configuration
|
|
53
|
+
The SDK can be configured via environment variables:
|
|
54
|
+
|
|
55
|
+
| Variable | Description | Default |
|
|
56
|
+
|----------|-------------|---------|
|
|
57
|
+
| `GUDB_API_ENDPOINT` | The evaluation backend URL | `https://ai-db-sentinel.onrender.com/api/sdk/evaluate` |
|
|
58
|
+
| `GUDB_ENVIRONMENT` | `prod`, `stage`, or `dev` | `production` |
|
|
59
|
+
| `GUDB_FAIL_OPEN` | Allow queries if AI is down | `True` |
|
|
60
|
+
| `GUDB_REDACT_PII` | Scrub SQL literals | `True` |
|
|
61
|
+
|
|
62
|
+
## 🛠️ Advanced Usage (Extensibility)
|
|
63
|
+
You can implement your own AI provider if you want to use a local model or a different API.
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
from gudb.providers.base import BaseProvider
|
|
67
|
+
from gudb import Gudb
|
|
68
|
+
|
|
69
|
+
class MyCustomProvider(BaseProvider):
|
|
70
|
+
def evaluate(self, query, context):
|
|
71
|
+
if "DROP" in query.upper():
|
|
72
|
+
return {"verdict": "STOP", "issue": "No drops allowed"}
|
|
73
|
+
return {"verdict": "GO"}
|
|
74
|
+
|
|
75
|
+
# Initialize with custom provider
|
|
76
|
+
gudb = Gudb(provider=MyCustomProvider())
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## 🏗️ Development & Publishing
|
|
80
|
+
To test locally:
|
|
81
|
+
```bash
|
|
82
|
+
pip install -e .[dev]
|
|
83
|
+
pytest tests/
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
To publish to PyPI:
|
|
87
|
+
```bash
|
|
88
|
+
python -m build
|
|
89
|
+
twine upload dist/*
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
---
|
|
93
|
+
Built with ❤️ by the gudb Team. Ensuring your database sleeps soundly.
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
from fastapi import APIRouter, HTTPException
|
|
2
|
+
from services.models import QueryAnalysis
|
|
3
|
+
from utils.notifier import notification_manager
|
|
4
|
+
|
|
5
|
+
router = APIRouter(prefix="/api/analysis", tags=["analysis"])
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@router.get("/{alert_id}", response_model=QueryAnalysis)
|
|
9
|
+
async def get_analysis(alert_id: str):
|
|
10
|
+
"""Get detailed AI analysis for a specific alert"""
|
|
11
|
+
# First check if alert exists
|
|
12
|
+
alert = notification_manager.get_alert(alert_id)
|
|
13
|
+
if not alert:
|
|
14
|
+
raise HTTPException(status_code=404, detail="Alert not found")
|
|
15
|
+
|
|
16
|
+
# Get analysis results
|
|
17
|
+
analysis = notification_manager.get_analysis(alert_id)
|
|
18
|
+
if not analysis:
|
|
19
|
+
# Alert exists but analysis not ready yet
|
|
20
|
+
raise HTTPException(
|
|
21
|
+
status_code=202,
|
|
22
|
+
detail=f"Analysis in progress (status: {alert.status})"
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
return analysis
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
from fastapi import APIRouter, HTTPException, Query
|
|
2
|
+
from typing import List, Optional
|
|
3
|
+
from services.models import QueryAlert
|
|
4
|
+
from utils.notifier import notification_manager
|
|
5
|
+
|
|
6
|
+
router = APIRouter(prefix="/api/notifications", tags=["notifications"])
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@router.get("/", response_model=List[QueryAlert])
|
|
10
|
+
async def get_notifications(
|
|
11
|
+
limit: int = Query(50, ge=1, le=100),
|
|
12
|
+
severity: Optional[str] = Query(None, regex="^(info|warning|critical)$")
|
|
13
|
+
):
|
|
14
|
+
"""Get recent query alerts, optionally filtered by severity"""
|
|
15
|
+
alerts = notification_manager.get_alerts(limit=limit, severity=severity)
|
|
16
|
+
return alerts
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@router.get("/count")
|
|
20
|
+
async def get_notification_count():
|
|
21
|
+
"""Get count of unread notifications"""
|
|
22
|
+
count = notification_manager.get_unread_count()
|
|
23
|
+
return {"count": count}
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@router.get("/{alert_id}", response_model=QueryAlert)
|
|
27
|
+
async def get_notification(alert_id: str):
|
|
28
|
+
"""Get a specific alert by ID"""
|
|
29
|
+
alert = notification_manager.get_alert(alert_id)
|
|
30
|
+
if not alert:
|
|
31
|
+
raise HTTPException(status_code=404, detail="Alert not found")
|
|
32
|
+
return alert
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@router.delete("/clear")
|
|
36
|
+
async def clear_notifications():
|
|
37
|
+
"""Clear all notifications (for testing)"""
|
|
38
|
+
notification_manager.clear_all()
|
|
39
|
+
return {"message": "All notifications cleared"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
import os
|
|
3
|
+
from pprint import pprint
|
|
4
|
+
|
|
5
|
+
# Add src to path for demo
|
|
6
|
+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'src')))
|
|
7
|
+
|
|
8
|
+
from ai_db_sentinel import monitor, sentinel, settings
|
|
9
|
+
from ai_db_sentinel.exceptions import DisasterBlockedError
|
|
10
|
+
|
|
11
|
+
def demo_driver_interception():
|
|
12
|
+
print("--- 🛡️ Driver Interception Demo ---")
|
|
13
|
+
|
|
14
|
+
# Mocking a PEP 249 connection
|
|
15
|
+
class MockCursor:
|
|
16
|
+
def execute(self, q, v=None): print(f"Executing SQL: {q}")
|
|
17
|
+
class MockConn:
|
|
18
|
+
def cursor(self): return MockCursor()
|
|
19
|
+
|
|
20
|
+
conn = monitor(MockConn())
|
|
21
|
+
cur = conn.cursor()
|
|
22
|
+
|
|
23
|
+
print("\n1. Testing Safe Query...")
|
|
24
|
+
cur.execute("SELECT * FROM users WHERE id = 1")
|
|
25
|
+
|
|
26
|
+
print("\n2. Testing Disaster Query (Heuristic Block)...")
|
|
27
|
+
try:
|
|
28
|
+
cur.execute("DELETE FROM orders;")
|
|
29
|
+
except DisasterBlockedError as e:
|
|
30
|
+
print(f"✅ Intercepted Disaster: {e.issue}")
|
|
31
|
+
print(f"💥 Impact: {e.impact}")
|
|
32
|
+
|
|
33
|
+
def demo_configuration():
|
|
34
|
+
print("\n--- ⚙️ Configuration Demo ---")
|
|
35
|
+
print(f"Default Endpoint: {settings.api_endpoint}")
|
|
36
|
+
print(f"Environment: {settings.environment}")
|
|
37
|
+
print(f"Fail Open: {settings.fail_open}")
|
|
38
|
+
|
|
39
|
+
if __name__ == "__main__":
|
|
40
|
+
demo_configuration()
|
|
41
|
+
demo_driver_interception()
|