davanstrien HF staff commited on
Commit
c882e97
·
1 Parent(s): 8baf70b

chore: Add logging to main.py for error handling and debugging

Browse files
Files changed (1) hide show
  1. main.py +13 -2
main.py CHANGED
@@ -7,6 +7,9 @@ from pydantic import BaseModel
7
  from data_loader import refresh_data
8
  import numpy as np
9
  from pandas import Timestamp
 
 
 
10
 
11
 
12
  def get_db_connection():
@@ -18,7 +21,8 @@ def get_db_connection():
18
  def setup_database():
19
  conn = get_db_connection()
20
  c = conn.cursor()
21
- c.execute("""CREATE TABLE IF NOT EXISTS datasets
 
22
  (hub_id TEXT PRIMARY KEY,
23
  likes INTEGER,
24
  downloads INTEGER,
@@ -29,7 +33,8 @@ def setup_database():
29
  language TEXT,
30
  config_name TEXT,
31
  column_names TEXT,
32
- features TEXT)""")
 
33
  c.execute("CREATE INDEX IF NOT EXISTS idx_column_names ON datasets (column_names)")
34
  conn.commit()
35
  conn.close()
@@ -44,6 +49,7 @@ def serialize_numpy(obj):
44
  return float(obj)
45
  if isinstance(obj, Timestamp):
46
  return int(obj.timestamp())
 
47
  raise TypeError(f"Object of type {type(obj)} is not JSON serializable")
48
 
49
 
@@ -85,11 +91,15 @@ def insert_data(conn, data):
85
  async def lifespan(app: FastAPI):
86
  # Startup: Load data into the database
87
  setup_database()
 
88
  conn = get_db_connection()
 
89
  datasets = refresh_data()
 
90
  for data in datasets:
91
  insert_data(conn, data)
92
  conn.close()
 
93
  yield
94
  # Shutdown: You can add any cleanup operations here if needed
95
  # For example, closing database connections, clearing caches, etc.
@@ -169,6 +179,7 @@ async def search_datasets(
169
  )
170
 
171
  except sqlite3.Error as e:
 
172
  raise HTTPException(status_code=500, detail=f"Database error: {str(e)}") from e
173
  finally:
174
  conn.close()
 
7
  from data_loader import refresh_data
8
  import numpy as np
9
  from pandas import Timestamp
10
+ import logging
11
+
12
+ logger = logging.getLogger(__name__)
13
 
14
 
15
  def get_db_connection():
 
21
  def setup_database():
22
  conn = get_db_connection()
23
  c = conn.cursor()
24
+ c.execute(
25
+ """CREATE TABLE IF NOT EXISTS datasets
26
  (hub_id TEXT PRIMARY KEY,
27
  likes INTEGER,
28
  downloads INTEGER,
 
33
  language TEXT,
34
  config_name TEXT,
35
  column_names TEXT,
36
+ features TEXT)"""
37
+ )
38
  c.execute("CREATE INDEX IF NOT EXISTS idx_column_names ON datasets (column_names)")
39
  conn.commit()
40
  conn.close()
 
49
  return float(obj)
50
  if isinstance(obj, Timestamp):
51
  return int(obj.timestamp())
52
+ logger.error(f"Object of type {type(obj)} is not JSON serializable")
53
  raise TypeError(f"Object of type {type(obj)} is not JSON serializable")
54
 
55
 
 
91
  async def lifespan(app: FastAPI):
92
  # Startup: Load data into the database
93
  setup_database()
94
+ logger.info("Creating database connection")
95
  conn = get_db_connection()
96
+ logger.info("Refreshing data")
97
  datasets = refresh_data()
98
+
99
  for data in datasets:
100
  insert_data(conn, data)
101
  conn.close()
102
+ logger.info("Data refreshed")
103
  yield
104
  # Shutdown: You can add any cleanup operations here if needed
105
  # For example, closing database connections, clearing caches, etc.
 
179
  )
180
 
181
  except sqlite3.Error as e:
182
+ logger.error(f"Database error: {str(e)}")
183
  raise HTTPException(status_code=500, detail=f"Database error: {str(e)}") from e
184
  finally:
185
  conn.close()