Skip to content

Commit 948346a

Browse files
author
lukovicaleksa
committed
ADD: Initialization check
1 parent 531417b commit 948346a

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

main.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from database.collections import mongodb_connection
77
from routes.movies import movies_router
8-
from utils import initialize_db_movies_collection_from_dataset
8+
from utils import is_db_movies_collection_initialized, initialize_db_movies_collection_from_dataset
99

1010

1111
logger = logging.getLogger("uvicorn")
@@ -17,11 +17,14 @@ def app_startup_event_handler():
1717
"""
1818
logger.info('Initializing Movies collection from dataset ...')
1919

20-
start_time = time.time()
21-
movies_inserted = initialize_db_movies_collection_from_dataset()
20+
if is_db_movies_collection_initialized():
21+
logger.info('Collection already initialized')
22+
else:
23+
start_time = time.time()
24+
movies_inserted = initialize_db_movies_collection_from_dataset()
2225

23-
logger.info(f'Successfully initialized the collection with {movies_inserted} movies, '
24-
f'elapsed time: {time.time() - start_time:.2f} seconds')
26+
logger.info(f'Successfully initialized the collection with {movies_inserted} movies, '
27+
f'elapsed time: {time.time() - start_time:.2f} seconds')
2528

2629

2730
def app_shutdown_event_handler():

utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@
88
from language_model import embedding_model
99

1010

11+
def is_db_movies_collection_initialized() -> bool:
12+
"""
13+
Checks if the Movies collection is initialized.\n
14+
For the sake of simplicity, collection is considered as initialized if it contains documents.
15+
16+
:return: True/False
17+
"""
18+
return db_movies_collection.count_documents({}) > 0
19+
20+
1121
def initialize_db_movies_collection_from_dataset() -> int:
1222
"""
1323
Initialize Movies collection using TMDB 5000 Movie Dataset from Kaggle (https://www.kaggle.com/datasets/tmdb/tmdb-movie-metadata)\n

0 commit comments

Comments
 (0)