-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworking_api_with_rate_limiting.py
More file actions
494 lines (423 loc) · 16.8 KB
/
working_api_with_rate_limiting.py
File metadata and controls
494 lines (423 loc) · 16.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
#!/usr/bin/env python3
"""
Working API with schema-adapted search and rate limiting
Free: 100 searches/day | Paid: $29/month for 10k searches/day
"""
from flask import Flask, request, jsonify, send_file
from flask_cors import CORS
import sqlite3
import time
import json
import os
import hashlib
import secrets
from datetime import datetime, timedelta
from functools import wraps
from collections import defaultdict
app = Flask(__name__)
CORS(app)
DB_PATH = "/mnt/databases/SELF_HEALING_AGI.db"
RATE_LIMIT_DB = "/tmp/rate_limits.db"
# Rate limit tiers
RATE_LIMITS = {
'free': {
'daily_limit': 100,
'price': 0,
'name': 'Free Tier'
},
'paid': {
'daily_limit': 10000,
'price': 29,
'name': 'Pro Tier ($29/month)'
}
}
def init_rate_limit_db():
"""Initialize rate limiting database"""
conn = sqlite3.connect(RATE_LIMIT_DB)
cursor = conn.cursor()
# Create tables
cursor.execute("""
CREATE TABLE IF NOT EXISTS api_keys (
key_id TEXT PRIMARY KEY,
tier TEXT NOT NULL DEFAULT 'free',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
last_used TIMESTAMP,
total_searches INTEGER DEFAULT 0
)
""")
cursor.execute("""
CREATE TABLE IF NOT EXISTS daily_usage (
key_id TEXT,
date TEXT,
search_count INTEGER DEFAULT 0,
PRIMARY KEY (key_id, date)
)
""")
cursor.execute("""
CREATE TABLE IF NOT EXISTS ip_limits (
ip_address TEXT,
date TEXT,
search_count INTEGER DEFAULT 0,
PRIMARY KEY (ip_address, date)
)
""")
# Create default anonymous key for free users
today = datetime.now().strftime('%Y-%m-%d')
cursor.execute("INSERT OR IGNORE INTO api_keys (key_id, tier) VALUES (?, ?)", ('anonymous', 'free'))
cursor.execute("INSERT OR IGNORE INTO daily_usage (key_id, date, search_count) VALUES (?, ?, ?)", ('anonymous', today, 0))
conn.commit()
conn.close()
def generate_api_key():
"""Generate a secure API key"""
return 'fixit_' + secrets.token_urlsafe(32)
def get_client_identifier(request):
"""Get client identifier (API key or IP address)"""
# Check for API key in headers
api_key = request.headers.get('X-API-Key') or request.headers.get('Authorization', '').replace('Bearer ', '')
if api_key and api_key.startswith('fixit_'):
return api_key, 'api_key'
# Fall back to IP address for anonymous users
ip_address = request.headers.get('X-Forwarded-For', request.remote_addr)
if ip_address:
ip_address = ip_address.split(',')[0].strip() # Handle multiple IPs
return ip_address or 'unknown', 'ip_address'
def check_rate_limit(client_id, client_type):
"""Check if client has exceeded rate limit"""
conn = sqlite3.connect(RATE_LIMIT_DB)
cursor = conn.cursor()
today = datetime.now().strftime('%Y-%m-%d')
try:
if client_type == 'api_key':
# Check API key tier and usage
cursor.execute("SELECT tier FROM api_keys WHERE key_id = ?", (client_id,))
result = cursor.fetchone()
if not result:
# Invalid API key, treat as anonymous
tier = 'free'
client_id = 'anonymous'
else:
tier = result[0]
# Get daily usage
cursor.execute("SELECT search_count FROM daily_usage WHERE key_id = ? AND date = ?", (client_id, today))
usage_result = cursor.fetchone()
current_usage = usage_result[0] if usage_result else 0
else:
# IP-based rate limiting (free tier only)
tier = 'free'
cursor.execute("SELECT search_count FROM ip_limits WHERE ip_address = ? AND date = ?", (client_id, today))
usage_result = cursor.fetchone()
current_usage = usage_result[0] if usage_result else 0
# Check limits
daily_limit = RATE_LIMITS[tier]['daily_limit']
remaining = max(0, daily_limit - current_usage)
rate_limit_info = {
'tier': tier,
'daily_limit': daily_limit,
'current_usage': current_usage,
'remaining': remaining,
'exceeded': current_usage >= daily_limit
}
conn.close()
return rate_limit_info
except Exception as e:
conn.close()
# On error, allow request but log it
return {
'tier': 'free',
'daily_limit': 100,
'current_usage': 0,
'remaining': 100,
'exceeded': False
}
def increment_usage(client_id, client_type):
"""Increment usage counter for client"""
conn = sqlite3.connect(RATE_LIMIT_DB)
cursor = conn.cursor()
today = datetime.now().strftime('%Y-%m-%d')
try:
if client_type == 'api_key':
# Update API key usage
cursor.execute("""
INSERT OR REPLACE INTO daily_usage (key_id, date, search_count)
VALUES (?, ?, COALESCE((SELECT search_count FROM daily_usage WHERE key_id = ? AND date = ?), 0) + 1)
""", (client_id, today, client_id, today))
# Update last used timestamp
cursor.execute("UPDATE api_keys SET last_used = CURRENT_TIMESTAMP, total_searches = total_searches + 1 WHERE key_id = ?", (client_id,))
else:
# Update IP usage
cursor.execute("""
INSERT OR REPLACE INTO ip_limits (ip_address, date, search_count)
VALUES (?, ?, COALESCE((SELECT search_count FROM ip_limits WHERE ip_address = ? AND date = ?), 0) + 1)
""", (client_id, today, client_id, today))
conn.commit()
conn.close()
except Exception as e:
conn.close()
# Log error but don't fail the request
print(f"Error incrementing usage: {e}")
def rate_limit_decorator(f):
"""Rate limiting decorator"""
@wraps(f)
def decorated_function(*args, **kwargs):
client_id, client_type = get_client_identifier(request)
rate_info = check_rate_limit(client_id, client_type)
if rate_info['exceeded']:
return jsonify({
'error': 'Rate limit exceeded',
'details': f"You've used {rate_info['current_usage']}/{rate_info['daily_limit']} searches today",
'tier': rate_info['tier'],
'upgrade_info': {
'message': 'Upgrade to Pro for 10,000 searches/day',
'price': '$29/month',
'endpoint': '/upgrade'
} if rate_info['tier'] == 'free' else None,
'reset_time': 'Daily limits reset at midnight UTC'
}), 429
# Execute the original function
result = f(*args, **kwargs)
# Increment usage counter (only if search was successful)
if isinstance(result, tuple):
response, status_code = result
if status_code == 200:
increment_usage(client_id, client_type)
else:
# Assume success if no status code specified
increment_usage(client_id, client_type)
# Add rate limit headers to response
if isinstance(result, tuple):
response_data, status_code = result
else:
response_data = result
status_code = 200
# Add rate limit info to response
if hasattr(response_data, 'json') or isinstance(response_data, dict):
updated_rate_info = check_rate_limit(client_id, client_type)
if isinstance(response_data, dict):
response_data['rate_limit'] = {
'tier': updated_rate_info['tier'],
'remaining': updated_rate_info['remaining'],
'daily_limit': updated_rate_info['daily_limit'],
'current_usage': updated_rate_info['current_usage'] + 1
}
return response_data, status_code
return decorated_function
def adapted_search(query, limit=10):
"""Schema-adapted search for the actual database structure"""
try:
start_time = time.time()
print(f"[DEBUG] Search started for: {query}")
conn = sqlite3.connect(DB_PATH, check_same_thread=False)
conn.row_factory = sqlite3.Row
cursor = conn.cursor()
print(f"[DEBUG] DB connection took: {(time.time() - start_time)*1000:.1f}ms")
search_query = """
SELECT
s.id,
s.question_title as title,
s.answer_body as solution,
s.question_tags as tags,
s.error_pattern,
s.fix_command,
s.answer_score,
s.is_accepted,
fts.rank
FROM solutions_fts fts
JOIN solutions s ON s.id = fts.rowid
WHERE solutions_fts MATCH ?
ORDER BY rank
LIMIT ?
"""
query_start = time.time()
cursor.execute(search_query, (query, limit))
print(f"[DEBUG] Query execution took: {(time.time() - query_start)*1000:.1f}ms")
fetch_start = time.time()
results = []
for row in cursor.fetchall():
result = {
'id': row['id'],
'title': row['title'] or 'No title',
'solution': row['solution'] or 'No solution',
'tags': row['tags'] or '',
'error_pattern': row['error_pattern'] or '',
'fix_command': row['fix_command'] or '',
'answer_score': row['answer_score'] or 0,
'is_accepted': bool(row['is_accepted']),
'similarity': 1.0,
'confidence': row['answer_score'] / 100.0 if row['answer_score'] else 0.5,
'search_strategy': 'fts_adapted'
}
results.append(result)
print(f"[DEBUG] Result processing took: {(time.time() - fetch_start)*1000:.1f}ms")
conn.close()
search_time = (time.time() - start_time) * 1000 # Convert to ms
print(f"[DEBUG] Total search function took: {search_time:.1f}ms")
return {
'results': results,
'query': query,
'count': len(results),
'search_time_ms': round(search_time, 1),
'database_size': '83GB',
'total_records': '18.5M+'
}
except Exception as e:
return {
'error': f'Search failed: {str(e)}',
'results': [],
'count': 0
}
@app.route('/search', methods=['POST'])
@rate_limit_decorator
def search():
"""Search endpoint with rate limiting"""
try:
data = request.get_json()
query = data.get('query', '')
limit = data.get('limit', 10)
if not query:
return jsonify({'error': 'Query required'}), 400
if limit > 50:
return jsonify({'error': 'Limit cannot exceed 50'}), 400
results = adapted_search(query, limit)
return jsonify(results)
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/upgrade', methods=['POST'])
def upgrade():
"""Upgrade to paid tier (simplified - no actual payment processing)"""
try:
data = request.get_json() or {}
# Generate new API key for paid tier
new_api_key = generate_api_key()
conn = sqlite3.connect(RATE_LIMIT_DB)
cursor = conn.cursor()
cursor.execute("INSERT INTO api_keys (key_id, tier) VALUES (?, ?)", (new_api_key, 'paid'))
today = datetime.now().strftime('%Y-%m-%d')
cursor.execute("INSERT INTO daily_usage (key_id, date, search_count) VALUES (?, ?, ?)", (new_api_key, today, 0))
conn.commit()
conn.close()
return jsonify({
'success': True,
'api_key': new_api_key,
'tier': 'paid',
'daily_limit': 10000,
'price': '$29/month',
'message': 'Upgrade successful! Use this API key for 10,000 searches/day',
'usage_instructions': {
'header': 'X-API-Key',
'example': f'curl -H "X-API-Key: {new_api_key}" ...'
},
'note': 'This is a demo upgrade - no actual payment required'
})
except Exception as e:
return jsonify({'error': f'Upgrade failed: {str(e)}'}), 500
@app.route('/usage', methods=['GET'])
def usage():
"""Check current usage"""
try:
client_id, client_type = get_client_identifier(request)
rate_info = check_rate_limit(client_id, client_type)
return jsonify({
'client_id': client_id[:10] + '...' if len(client_id) > 10 else client_id,
'client_type': client_type,
'tier': rate_info['tier'],
'daily_limit': rate_info['daily_limit'],
'current_usage': rate_info['current_usage'],
'remaining': rate_info['remaining'],
'percentage_used': round((rate_info['current_usage'] / rate_info['daily_limit']) * 100, 1),
'tier_info': RATE_LIMITS[rate_info['tier']]
})
except Exception as e:
return jsonify({'error': f'Usage check failed: {str(e)}'}), 500
@app.route('/health', methods=['GET'])
def health():
"""Health check with live database stats"""
try:
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
# Get actual record count
cursor.execute("SELECT COUNT(*) FROM solutions")
total_records = cursor.fetchone()[0]
# Get database size
cursor.execute("SELECT page_count * page_size as size FROM pragma_page_count(), pragma_page_size()")
db_size_bytes = cursor.fetchone()[0]
db_size_gb = round(db_size_bytes / (1024**3), 1)
conn.close()
return jsonify({
'status': 'healthy',
'database': 'connected',
'database_path': DB_PATH,
'live_stats': {
'total_records': f"{total_records:,}",
'database_size': f"{db_size_gb}GB",
'database_size_bytes': db_size_bytes
},
'rate_limiting': {
'enabled': True,
'tiers': RATE_LIMITS
},
'timestamp': time.time()
})
except Exception as e:
return jsonify({
'status': 'error',
'database': 'error',
'error': str(e),
'timestamp': time.time()
})
@app.route('/', methods=['GET'])
def root():
"""Serve HTML interface or API info"""
# Check if request wants HTML
accept_header = request.headers.get('Accept', '')
if 'text/html' in accept_header:
# Serve the HTML interface
html_path = '/var/www/talon-api/fixit_frontend.html'
if os.path.exists(html_path):
return send_file(html_path)
else:
return '''
<html><body style="font-family: Arial; padding: 40px; background: #f5f5f5;">
<div style="max-width: 800px; margin: 0 auto; background: white; padding: 40px; border-radius: 10px;">
<h1 style="color: #e74c3c;">🔧 FixIt API</h1>
<p>HTML interface not found. API endpoints available:</p>
<ul>
<li><strong>POST /search</strong> - Search Stack Overflow solutions</li>
<li><strong>GET /health</strong> - Health check</li>
<li><strong>GET /usage</strong> - Check your usage</li>
<li><strong>POST /upgrade</strong> - Upgrade to Pro tier</li>
</ul>
<h3>Rate Limits:</h3>
<ul>
<li><strong>Free:</strong> 100 searches/day (no API key needed)</li>
<li><strong>Pro:</strong> 10,000 searches/day ($29/month)</li>
</ul>
<p>Try: <code>curl -X POST -H "Content-Type: application/json" -d '{"query":"python error","limit":5}' https://fixit.built-simple.ai/search</code></p>
</div>
</body></html>
'''
else:
# Return JSON API info
return jsonify({
'name': 'FixIt - Stack Overflow Solution Search API',
'database': '83GB SELF_HEALING_AGI.db',
'records': '18.5M+ Stack Overflow solutions',
'endpoints': {
'/search': 'POST - Search solutions',
'/health': 'GET - Health check',
'/usage': 'GET - Check your usage',
'/upgrade': 'POST - Upgrade to Pro tier',
'/': 'GET - Web interface (HTML) or API info (JSON)'
},
'rate_limits': {
'free': '100 searches/day (no API key required)',
'pro': '10,000 searches/day ($29/month)',
'upgrade': 'POST /upgrade to get Pro API key'
}
})
# Initialize rate limiting database on startup
if __name__ == '__main__':
init_rate_limit_db()
print("🚀 Starting FixIt API with Rate Limiting...")
print(f"📊 Database: {DB_PATH}")
print("💳 Free: 100 searches/day | Pro: 10k searches/day ($29/month)")
app.run(host='0.0.0.0', port=5001, debug=True)