Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions mflix/server/python-fastapi/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from src.routers import movies
from src.utils.errorHandler import register_error_handlers
from src.database.mongo_client import db, get_collection

import os
Expand All @@ -15,8 +14,9 @@
@asynccontextmanager
async def lifespan(app: FastAPI):
# Startup: Create search indexes
await ensure_search_index()
await vector_search_index()
await ensure_mongodb_search_index()
await ensure_vector_search_index()
await ensure_standard_index()

# Print server information
print(f"\n{'='*60}")
Expand All @@ -30,10 +30,9 @@ async def lifespan(app: FastAPI):
# Add any cleanup code here


async def ensure_search_index():
async def ensure_mongodb_search_index():
try:
movies_collection = db.get_collection("movies")
comments_collection = db.get_collection("comments")

# Check and create search index for movies collection
result = await movies_collection.list_search_indexes()
Expand Down Expand Up @@ -71,7 +70,7 @@ async def ensure_search_index():
)


async def vector_search_index():
async def ensure_vector_search_index():
"""
Creates vector search index on application startup if it doesn't already exist.
This ensures the index is ready before any vector search requests are made.
Expand Down Expand Up @@ -114,6 +113,26 @@ async def vector_search_index():
f"and verify the 'embedded_movies' collection exists with the required embedding field."
)

async def ensure_standard_index():
"""
Creates a standard MongoDB index on the comments collection on application startup.
This improves performance for queries filtering by movie_id such as ReportingByComments().
"""

try:
comments_collection = db.get_collection("comments")

existing_indexes_cursor = await comments_collection.list_search_indexes()
Comment thread
tmcneil-mdb marked this conversation as resolved.
Outdated
existing_indexes = await existing_indexes_cursor.to_list(length=None)
index_names = [index.get("name") for index in existing_indexes]

if "movie_id" not in index_names:
await comments_collection.create_index("movie_id")
Comment thread
tmcneil-mdb marked this conversation as resolved.
Outdated

except Exception as e:
print(f"Failed to create standard index on 'comments' collection: {str(e)}. ")
print(f"Performance may be degraded. Please check your MongoDB configuration.")


app = FastAPI(lifespan=lifespan)

Expand All @@ -127,6 +146,5 @@ async def vector_search_index():
allow_headers=["*"],
)

register_error_handlers(app)
app.include_router(movies.router, prefix="/api/movies", tags=["movies"])

13 changes: 0 additions & 13 deletions mflix/server/python-fastapi/src/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,22 +127,9 @@ class SuccessResponse(BaseModel, Generic[T]):
timestamp: str
pagination: Optional[Pagination] = None


class ErrorDetails(BaseModel):
message: str
code: Optional[str]
details: Optional[Any] = None

class BatchUpdateRequest(BaseModel):
filter: MovieFilter
update: UpdateMovieRequest

class BatchDeleteRequest(BaseModel):
filter: MovieFilter

class ErrorResponse(BaseModel):
success: bool = False
message: str
error: ErrorDetails
timestamp: str

Loading
Loading