Spaces:
Sleeping
Sleeping
amaye15
commited on
Commit
·
ad86a2b
1
Parent(s):
5c144a1
Debug - Data format
Browse files- src/api/database.py +36 -4
- src/main.py +1 -1
src/api/database.py
CHANGED
|
@@ -110,16 +110,41 @@ class Database:
|
|
| 110 |
with self.lock:
|
| 111 |
self.pool.append(conn)
|
| 112 |
|
| 113 |
-
async def fetch(self, query: str, *args) -> List[Dict]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
"""
|
| 115 |
-
Execute a SELECT query and return the results as a
|
| 116 |
|
| 117 |
Args:
|
| 118 |
query (str): The SQL query to execute.
|
| 119 |
*args: Query parameters.
|
| 120 |
|
| 121 |
Returns:
|
| 122 |
-
List
|
| 123 |
|
| 124 |
Raises:
|
| 125 |
QueryExecutionError: If the query execution fails.
|
|
@@ -130,7 +155,14 @@ class Database:
|
|
| 130 |
cursor.execute(query, args)
|
| 131 |
rows = cursor.fetchall()
|
| 132 |
columns = [desc[0] for desc in cursor.description]
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
except Pg8000DatabaseError as e:
|
| 135 |
logger.error(f"Query execution failed: {e}")
|
| 136 |
raise QueryExecutionError(f"Failed to execute query: {query}") from e
|
|
|
|
| 110 |
with self.lock:
|
| 111 |
self.pool.append(conn)
|
| 112 |
|
| 113 |
+
# async def fetch(self, query: str, *args) -> List[Dict]:
|
| 114 |
+
# """
|
| 115 |
+
# Execute a SELECT query and return the results as a list of dictionaries.
|
| 116 |
+
|
| 117 |
+
# Args:
|
| 118 |
+
# query (str): The SQL query to execute.
|
| 119 |
+
# *args: Query parameters.
|
| 120 |
+
|
| 121 |
+
# Returns:
|
| 122 |
+
# List[Dict]: A list of dictionaries where keys are column names and values are column values.
|
| 123 |
+
|
| 124 |
+
# Raises:
|
| 125 |
+
# QueryExecutionError: If the query execution fails.
|
| 126 |
+
# """
|
| 127 |
+
# try:
|
| 128 |
+
# async with self.get_connection() as conn:
|
| 129 |
+
# cursor = conn.cursor()
|
| 130 |
+
# cursor.execute(query, args)
|
| 131 |
+
# rows = cursor.fetchall()
|
| 132 |
+
# columns = [desc[0] for desc in cursor.description]
|
| 133 |
+
# return [dict(zip(columns, row)) for row in rows]
|
| 134 |
+
# except Pg8000DatabaseError as e:
|
| 135 |
+
# logger.error(f"Query execution failed: {e}")
|
| 136 |
+
# raise QueryExecutionError(f"Failed to execute query: {query}") from e
|
| 137 |
+
|
| 138 |
+
async def fetch(self, query: str, *args) -> Dict[str, List]:
|
| 139 |
"""
|
| 140 |
+
Execute a SELECT query and return the results as a dictionary of lists.
|
| 141 |
|
| 142 |
Args:
|
| 143 |
query (str): The SQL query to execute.
|
| 144 |
*args: Query parameters.
|
| 145 |
|
| 146 |
Returns:
|
| 147 |
+
Dict[str, List]: A dictionary where keys are column names and values are lists of column values.
|
| 148 |
|
| 149 |
Raises:
|
| 150 |
QueryExecutionError: If the query execution fails.
|
|
|
|
| 155 |
cursor.execute(query, args)
|
| 156 |
rows = cursor.fetchall()
|
| 157 |
columns = [desc[0] for desc in cursor.description]
|
| 158 |
+
|
| 159 |
+
# Convert the list of dictionaries into a dictionary of lists
|
| 160 |
+
data_dict = {column: [] for column in columns}
|
| 161 |
+
for row in rows:
|
| 162 |
+
for i, value in enumerate(row):
|
| 163 |
+
data_dict[columns[i]].append(value)
|
| 164 |
+
|
| 165 |
+
return data_dict
|
| 166 |
except Pg8000DatabaseError as e:
|
| 167 |
logger.error(f"Query execution failed: {e}")
|
| 168 |
raise QueryExecutionError(f"Failed to execute query: {query}") from e
|
src/main.py
CHANGED
|
@@ -367,7 +367,7 @@ async def create_embedding(
|
|
| 367 |
logger.info("Fetching data from the database...")
|
| 368 |
result = await db.fetch(request.query)
|
| 369 |
|
| 370 |
-
logger.info(f"{result}")
|
| 371 |
|
| 372 |
dataset = Dataset.from_dict(result)
|
| 373 |
|
|
|
|
| 367 |
logger.info("Fetching data from the database...")
|
| 368 |
result = await db.fetch(request.query)
|
| 369 |
|
| 370 |
+
# logger.info(f"{result}")
|
| 371 |
|
| 372 |
dataset = Dataset.from_dict(result)
|
| 373 |
|