-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
571 lines (496 loc) · 19.7 KB
/
database.py
File metadata and controls
571 lines (496 loc) · 19.7 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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
import os
import logging
from datetime import datetime
import psycopg2
from psycopg2.extras import RealDictCursor
import uuid
from dotenv import load_dotenv
import requests
import slack_message
# Configure logging
logger = logging.getLogger(__name__)
# Load environment variables
load_dotenv()
def get_db_connection():
"""Get a connection to the PostgreSQL database"""
try:
conn = psycopg2.connect(
host=os.getenv('DB_HOST', 'localhost'),
database=os.getenv('DB_NAME', 'zeus_customs'),
user=os.getenv('DB_USER', 'postgres'),
password=os.getenv('DB_PASSWORD', 'postgres'),
port=os.getenv('DB_PORT', '5432')
)
conn.autocommit = True
return conn
except Exception as e:
logger.error(f"Database connection error: {str(e)}")
raise
def get_ip_location(ip_address):
"""Get location information from IP address using ip-api.com (free tier)"""
try:
if ip_address in ('127.0.0.1', 'localhost', '::1'):
return {
'country': 'Local',
'regionName': 'Development',
'city': 'Local',
'latitude': 0,
'longitude': 0
}
# Use ip-api.com free tier (no API key needed)
response = requests.get(f'http://ip-api.com/json/{ip_address}')
if response.status_code == 200:
data = response.json()
if data.get('status') == 'success':
return {
'country': data.get('country'),
'region': data.get('regionName'),
'city': data.get('city'),
'latitude': data.get('lat'),
'longitude': data.get('lon')
}
return None
except Exception as e:
logger.error(f"Error getting IP location: {str(e)}")
return None
def get_client_ip(request):
"""Get the client's real IP address, accounting for proxies"""
# Check X-Forwarded-For header first (standard for proxies)
forwarded_for = request.headers.get('X-Forwarded-For')
if forwarded_for:
# The leftmost IP is typically the original client
return forwarded_for.split(',')[0].strip()
# Check other common proxy headers
for header in ['X-Real-IP', 'CF-Connecting-IP', 'True-Client-IP']:
if header in request.headers:
return request.headers[header]
# Fall back to remote_addr if no proxy headers are found
return request.remote_addr
def log_api_usage(endpoint, user_id=None, request=None, success=None, message=None):
"""Log individual API usage to the database"""
try:
# Connect to the database
conn = get_db_connection()
cur = conn.cursor()
# Get IP address and user agent
#ip_address = request.remote_addr if request else None
ip_address = get_client_ip(request) if request else None
user_agent = request.headers.get('User-Agent', '') if request else None
# Get location data if IP address is available
location_data = {}
if ip_address:
location = get_ip_location(ip_address)
if location:
location_data = {
'country': location.get('country'),
'region': location.get('region'),
'city': location.get('city'),
'latitude': location.get('latitude'),
'longitude': location.get('longitude')
}
# Insert a new record for each API call - no aggregation
cur.execute(
"""
INSERT INTO api_usage
(user_id, endpoint, date, timestamp, ip_address, user_agent, country, region, city,
latitude, longitude, success, message)
VALUES (%s, %s, CURRENT_DATE, CURRENT_TIMESTAMP, %s, %s, %s, %s, %s, %s, %s, %s, %s)
RETURNING id
""",
(user_id, endpoint, ip_address, user_agent,
location_data.get('country'),
location_data.get('region'),
location_data.get('city'),
location_data.get('latitude'),
location_data.get('longitude'),
success, message)
)
# Close cursor and connection
cur.close()
conn.close()
#country = location_data.get('country')
#city = location_data.get('city')
#message = f"api usage {endpoint} {country} {city} ip {ip_address} user id {user_id} agent {user_agent}"
#slack_message.post("#general", message)
return True
except Exception as e:
logger.error(f"Error logging API usage: {str(e)}")
return False
def log_lookup(user_id, lookup_type, house_bill=None, voc_scac=None, master_bill=None,
status='success', error_message=None, request=None, batch_id=None,
batch_size=None, batch_index=None):
"""
Log a lookup to the database, supporting both individual and batch lookups
Args:
user_id: User ID performing the lookup
lookup_type: Type of lookup ('single' or 'batch')
house_bill: House bill number (can be None for batch summary records)
voc_scac: VOC SCAC code (can be None for batch summary records)
master_bill: Master bill number if successful (can be None)
status: 'success' or 'error'
error_message: Error message if applicable
request: Flask request object for IP and user agent info
batch_id: UUID for the batch (to link related lookups)
batch_size: Total size of the batch
batch_index: Index of this lookup within the batch
"""
try:
# Connect to the database
conn = get_db_connection()
cur = conn.cursor()
# Get client IP and user agent
#ip_address = request.remote_addr if request else None
ip_address = get_client_ip(request) if request else None
user_agent = request.headers.get('User-Agent', '') if request else None
# Get location data if IP address is available
location_data = {}
if ip_address:
location = get_ip_location(ip_address)
if location:
location_data = {
'country': location.get('country'),
'region': location.get('region'),
'city': location.get('city'),
'latitude': location.get('latitude'),
'longitude': location.get('longitude')
}
# Insert lookup log with location data and batch info
cur.execute(
"""
INSERT INTO lookup_logs
(user_id, lookup_type, house_bill, voc_scac, master_bill, status, error_message,
ip_address, user_agent, country, region, city, latitude, longitude,
batch_id, batch_size, batch_index)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
""",
(user_id, lookup_type, house_bill, voc_scac, master_bill, status, error_message,
ip_address, user_agent,
location_data.get('country'),
location_data.get('region'),
location_data.get('city'),
location_data.get('latitude'),
location_data.get('longitude'),
batch_id, batch_size, batch_index)
)
# If successful, update the mapping table
if status == 'success' and master_bill and house_bill and voc_scac:
cur.execute(
"""
INSERT INTO hbl_mbl_mappings (house_bill, voc_scac, master_bill)
VALUES (%s, %s, %s)
ON CONFLICT (house_bill, voc_scac)
DO UPDATE SET
master_bill = EXCLUDED.master_bill,
last_lookup_at = CURRENT_TIMESTAMP,
lookup_count = hbl_mbl_mappings.lookup_count + 1
""",
(house_bill, voc_scac, master_bill)
)
# Commit changes
conn.commit()
# Close cursor and connection
cur.close()
conn.close()
return True
except Exception as e:
logger.error(f"Error logging lookup: {str(e)}")
return False
def create_token_record(user_id, jti, expires_at, request=None):
"""Create a token record in the database with location data"""
try:
# Connect to the database
conn = get_db_connection()
cur = conn.cursor()
# Get IP address and user agent
#ip_address = request.remote_addr if request else None
ip_address = get_client_ip(request) if request else None
user_agent = request.headers.get('User-Agent', '') if request else None
# Get location data if IP address is available
location_data = {}
if ip_address:
location = get_ip_location(ip_address)
if location:
location_data = {
'country': location.get('country'),
'region': location.get('region'),
'city': location.get('city'),
'latitude': location.get('latitude'), # Note the column name difference
'longitude': location.get('longitude') # Note the column name difference
}
# Insert token record with location data
cur.execute(
"""
INSERT INTO access_tokens
(user_id, token_id, expires_at, ip_address, user_agent,
country, region, city, latitude, longitude)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
""",
(user_id, jti, expires_at, ip_address, user_agent,
location_data.get('country'),
location_data.get('region'),
location_data.get('city'),
location_data.get('latitude'),
location_data.get('longitude'))
)
# Close cursor and connection
cur.close()
conn.close()
return True
except Exception as e:
logger.error(f"Error creating token record: {str(e)}")
return False
def is_token_revoked(jti):
"""Check if a token has been revoked"""
try:
# Connect to the database
conn = get_db_connection()
cur = conn.cursor()
# Check if token is revoked
cur.execute(
"""
SELECT is_revoked FROM access_tokens
WHERE token_id = %s
""",
(jti,)
)
result = cur.fetchone()
# Close cursor and connection
cur.close()
conn.close()
if result and result[0]:
return True
return False
except Exception as e:
logger.error(f"Error checking token revocation: {str(e)}")
# Fail-closed: If there's an error, treat the token as revoked for security
return True
def get_user_by_username(username):
"""Get user information by username"""
try:
conn = get_db_connection()
cur = conn.cursor(cursor_factory=RealDictCursor)
# Get user from database
cur.execute(
"""
SELECT id, username, password_hash, role, active
FROM users
WHERE username = %s
""",
(username,)
)
user = cur.fetchone()
# Close cursor and connection
cur.close()
conn.close()
return user
except Exception as e:
logger.error(f"Error getting user by username: {str(e)}")
return None
def revoke_token_by_id(token_id):
"""Revoke a token by its ID"""
try:
# Connect to the database
conn = get_db_connection()
cur = conn.cursor()
# Revoke the token
cur.execute(
"""
UPDATE access_tokens
SET is_revoked = TRUE, revoked_at = CURRENT_TIMESTAMP
WHERE token_id = %s
""",
(token_id,)
)
# Check if any rows were affected
rows_affected = cur.rowcount
# Close cursor and connection
cur.close()
conn.close()
return rows_affected > 0
except Exception as e:
logger.error(f"Error revoking token: {str(e)}")
return False
def get_all_tokens():
"""Get all tokens with user information and location data"""
try:
# Connect to the database
conn = get_db_connection()
cur = conn.cursor(cursor_factory=RealDictCursor)
# Get all tokens with location data
cur.execute(
"""
SELECT t.id, t.user_id, u.username, t.token_id, t.is_revoked,
t.created_at, t.expires_at, t.revoked_at,
t.ip_address, t.user_agent, t.country, t.region, t.city,
t.latitude, t.longitude
FROM access_tokens t
JOIN users u ON t.user_id = u.id
ORDER BY t.created_at DESC
"""
)
tokens = cur.fetchall()
# Convert timestamps to strings
for token in tokens:
token['created_at'] = token['created_at'].isoformat() if token['created_at'] else None
token['expires_at'] = token['expires_at'].isoformat() if token['expires_at'] else None
token['revoked_at'] = token['revoked_at'].isoformat() if token['revoked_at'] else None
# Close cursor and connection
cur.close()
conn.close()
return tokens
except Exception as e:
logger.error(f"Error getting tokens: {str(e)}")
return None
def get_all_users():
"""Get all users"""
try:
# Connect to the database
conn = get_db_connection()
cur = conn.cursor(cursor_factory=RealDictCursor)
# Get all users
cur.execute(
"""
SELECT id, username, email, role, api_quota, active, created_at, updated_at
FROM users
ORDER BY username
"""
)
users = cur.fetchall()
# Convert timestamps to strings
for user in users:
user['created_at'] = user['created_at'].isoformat() if user['created_at'] else None
user['updated_at'] = user['updated_at'].isoformat() if user['updated_at'] else None
# Close cursor and connection
cur.close()
conn.close()
return users
except Exception as e:
logger.error(f"Error getting users: {str(e)}")
return None
def get_usage_statistics():
"""Get API usage statistics with location data"""
try:
# Connect to the database
conn = get_db_connection()
cur = conn.cursor(cursor_factory=RealDictCursor)
# Get usage stats by endpoint with location data
cur.execute(
"""
SELECT u.username, a.endpoint, COUNT(*) as request_count,
a.country, a.region, a.city, a.latitude, a.longitude
FROM api_usage a
LEFT JOIN users u ON a.user_id = u.id
GROUP BY u.username, a.endpoint, a.country, a.region, a.city, a.latitude, a.longitude
ORDER BY u.username, a.endpoint
"""
)
results = cur.fetchall()
# Organize results by user
stats = {}
for row in results:
username = row['username'] or 'anonymous' # Handle unauthenticated requests
if username not in stats:
stats[username] = {
"total_requests": 0,
"endpoints": {},
"locations": {}
}
# Add endpoint stats
endpoint = row['endpoint']
if endpoint not in stats[username]["endpoints"]:
stats[username]["endpoints"][endpoint] = 0
stats[username]["endpoints"][endpoint] += row['request_count']
# Add location stats
location_key = f"{row.get('country', 'Unknown')}/{row.get('city', 'Unknown')}"
if location_key not in stats[username]["locations"]:
stats[username]["locations"][location_key] = {
"count": 0,
"country": row.get('country'),
"region": row.get('region'),
"city": row.get('city'),
"coordinates": [row.get('longitude'), row.get('latitude')] if row.get('latitude') and row.get('longitude') else None
}
stats[username]["locations"][location_key]["count"] += row['request_count']
stats[username]["total_requests"] += row['request_count']
# Get lookup stats with location data
cur.execute(
"""
SELECT u.username, COUNT(*) as lookup_count,
COUNT(*) FILTER (WHERE l.status = 'success') as success_count,
COUNT(*) FILTER (WHERE l.status = 'error') as error_count,
l.country, l.region, l.city, l.latitude, l.longitude,
COUNT(*) as location_count
FROM lookup_logs l
LEFT JOIN users u ON l.user_id = u.id
GROUP BY u.username, l.country, l.region, l.city, l.latitude, l.longitude
ORDER BY lookup_count DESC
"""
)
lookup_stats = cur.fetchall()
# Get geographic distribution
cur.execute(
"""
SELECT country, COUNT(*) as country_count
FROM api_usage
WHERE country IS NOT NULL
GROUP BY country
ORDER BY country_count DESC
"""
)
country_stats = cur.fetchall()
# Close cursor and connection
cur.close()
conn.close()
return {
"user_stats": stats,
"lookup_stats": lookup_stats,
"country_stats": country_stats,
"period": "all time"
}
except Exception as e:
logger.error(f"Error getting usage stats: {str(e)}")
return None
def get_cached_master_bill(house_bill, voc_scac):
"""Get cached master bill from database if available"""
try:
conn = get_db_connection()
cur = conn.cursor(cursor_factory=RealDictCursor)
cur.execute(
"""
SELECT master_bill, last_lookup_at, lookup_count
FROM hbl_mbl_mappings
WHERE house_bill = %s AND voc_scac = %s
""",
(house_bill, voc_scac)
)
result = cur.fetchone()
# Close cursor and connection
cur.close()
conn.close()
return result
except Exception as e:
logger.error(f"Error getting cached master bill: {str(e)}")
return None
def get_user_by_api_key(api_key):
"""Get user by API key"""
try:
with get_db_connection() as conn:
with conn.cursor() as cur:
cur.execute(
"SELECT id, username, password_hash, email, role, active FROM users WHERE api_key = %s",
(api_key,)
)
user = cur.fetchone()
if user:
return {
'id': user[0],
'username': user[1],
'password_hash': user[2],
'email': user[3],
'role': user[4],
'active': user[5]
}
return None
except Exception as e:
logger.error(f"Database error when getting user by API key: {str(e)}")
return None