Updated app.py to query faster
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ from typing import Optional
|
|
| 5 |
import psycopg2
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
from psycopg2.extras import RealDictCursor
|
|
|
|
| 8 |
import numpy as np
|
| 9 |
import os
|
| 10 |
|
|
@@ -43,15 +44,48 @@ print(f"Loaded untrained: {untrained_features.shape}")
|
|
| 43 |
# Database Connection
|
| 44 |
# -------------------------
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
def get_db():
|
| 47 |
-
return
|
| 48 |
-
host=os.getenv("DB_HOST"),
|
| 49 |
-
database=os.getenv("DB_NAME"),
|
| 50 |
-
user=os.getenv("DB_USER"),
|
| 51 |
-
password=os.getenv("DB_PASSWORD"),
|
| 52 |
-
port=6543,
|
| 53 |
-
cursor_factory=RealDictCursor
|
| 54 |
-
)
|
| 55 |
|
| 56 |
|
| 57 |
# -------------------------
|
|
@@ -172,7 +206,7 @@ async def root():
|
|
| 172 |
|
| 173 |
|
| 174 |
@app.get("/test-db")
|
| 175 |
-
|
| 176 |
try:
|
| 177 |
conn = get_db()
|
| 178 |
cur = conn.cursor()
|
|
@@ -185,7 +219,7 @@ async def test_db():
|
|
| 185 |
|
| 186 |
|
| 187 |
@app.get("/movies/search")
|
| 188 |
-
|
| 189 |
conn = get_db()
|
| 190 |
cur = conn.cursor()
|
| 191 |
cur.execute("""
|
|
@@ -202,7 +236,7 @@ async def search_movies(q: str, n: int = 10):
|
|
| 202 |
|
| 203 |
|
| 204 |
@app.get("/movies/{movie_id}")
|
| 205 |
-
|
| 206 |
conn = get_db()
|
| 207 |
cur = conn.cursor()
|
| 208 |
cur.execute("""
|
|
|
|
| 5 |
import psycopg2
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
from psycopg2.extras import RealDictCursor
|
| 8 |
+
from psycopg2 import pool
|
| 9 |
import numpy as np
|
| 10 |
import os
|
| 11 |
|
|
|
|
| 44 |
# Database Connection
|
| 45 |
# -------------------------
|
| 46 |
|
| 47 |
+
db_pool = None
|
| 48 |
+
|
| 49 |
+
@app.on_event("startup")
|
| 50 |
+
def startup_event():
|
| 51 |
+
global db_pool
|
| 52 |
+
try:
|
| 53 |
+
db_pool = pool.SimpleConnectionPool(
|
| 54 |
+
1, 20,
|
| 55 |
+
host=os.getenv("DB_HOST"),
|
| 56 |
+
database=os.getenv("DB_NAME"),
|
| 57 |
+
user=os.getenv("DB_USER"),
|
| 58 |
+
password=os.getenv("DB_PASSWORD"),
|
| 59 |
+
port=6543,
|
| 60 |
+
cursor_factory=RealDictCursor
|
| 61 |
+
)
|
| 62 |
+
print("Database connection pool created")
|
| 63 |
+
except Exception as e:
|
| 64 |
+
print(f"Error creating connection pool: {e}")
|
| 65 |
+
|
| 66 |
+
@app.on_event("shutdown")
|
| 67 |
+
def shutdown_event():
|
| 68 |
+
global db_pool
|
| 69 |
+
if db_pool:
|
| 70 |
+
db_pool.closeall()
|
| 71 |
+
print("Database connection pool closed")
|
| 72 |
+
|
| 73 |
+
class PooledConnection:
|
| 74 |
+
def __init__(self, pool):
|
| 75 |
+
self.pool = pool
|
| 76 |
+
self.conn = pool.getconn()
|
| 77 |
+
|
| 78 |
+
def close(self):
|
| 79 |
+
self.pool.putconn(self.conn)
|
| 80 |
+
|
| 81 |
+
def cursor(self, *args, **kwargs):
|
| 82 |
+
return self.conn.cursor(*args, **kwargs)
|
| 83 |
+
|
| 84 |
+
def __getattr__(self, name):
|
| 85 |
+
return getattr(self.conn, name)
|
| 86 |
+
|
| 87 |
def get_db():
|
| 88 |
+
return PooledConnection(db_pool)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
|
| 91 |
# -------------------------
|
|
|
|
| 206 |
|
| 207 |
|
| 208 |
@app.get("/test-db")
|
| 209 |
+
def test_db():
|
| 210 |
try:
|
| 211 |
conn = get_db()
|
| 212 |
cur = conn.cursor()
|
|
|
|
| 219 |
|
| 220 |
|
| 221 |
@app.get("/movies/search")
|
| 222 |
+
def search_movies(q: str, n: int = 10):
|
| 223 |
conn = get_db()
|
| 224 |
cur = conn.cursor()
|
| 225 |
cur.execute("""
|
|
|
|
| 236 |
|
| 237 |
|
| 238 |
@app.get("/movies/{movie_id}")
|
| 239 |
+
def get_movie(movie_id: int):
|
| 240 |
conn = get_db()
|
| 241 |
cur = conn.cursor()
|
| 242 |
cur.execute("""
|