mirror of
https://github.com/elisiariocouto/leggen.git
synced 2025-12-17 00:59:30 +00:00
refactor(api): Improve database connection management and reduce boilerplate.
- Add context manager for database connections with proper cleanup - Add @require_sqlite decorator to eliminate duplicate checks - Refactor 9 core CRUD methods to use managed connections - Reduce code by 50 lines while improving resource management - All 114 tests passing
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
import json
|
import json
|
||||||
import sqlite3
|
import sqlite3
|
||||||
|
from contextlib import contextmanager
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from functools import wraps
|
||||||
from typing import Any, Dict, List, Optional
|
from typing import Any, Dict, List, Optional
|
||||||
|
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
@@ -14,6 +16,25 @@ from leggen.utils.config import config
|
|||||||
from leggen.utils.paths import path_manager
|
from leggen.utils.paths import path_manager
|
||||||
|
|
||||||
|
|
||||||
|
def require_sqlite(func):
|
||||||
|
"""Decorator to check if SQLite is enabled before executing method"""
|
||||||
|
|
||||||
|
@wraps(func)
|
||||||
|
async def wrapper(self, *args, **kwargs):
|
||||||
|
if not self.sqlite_enabled:
|
||||||
|
logger.warning(f"SQLite database disabled, skipping {func.__name__}")
|
||||||
|
# Return appropriate default based on return type hints
|
||||||
|
return_type = func.__annotations__.get("return")
|
||||||
|
if return_type is int:
|
||||||
|
return 0
|
||||||
|
elif return_type in (list, List[Dict[str, Any]]):
|
||||||
|
return []
|
||||||
|
return None
|
||||||
|
return await func(self, *args, **kwargs)
|
||||||
|
|
||||||
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
class DatabaseService:
|
class DatabaseService:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.db_config = config.database_config
|
self.db_config = config.database_config
|
||||||
@@ -24,24 +45,33 @@ class DatabaseService:
|
|||||||
self.balance_transformer = BalanceTransformer()
|
self.balance_transformer = BalanceTransformer()
|
||||||
self.analytics_processor = AnalyticsProcessor()
|
self.analytics_processor = AnalyticsProcessor()
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def _get_db_connection(self, row_factory: bool = False):
|
||||||
|
"""Context manager for database connections with proper cleanup"""
|
||||||
|
db_path = path_manager.get_database_path()
|
||||||
|
conn = sqlite3.connect(str(db_path))
|
||||||
|
if row_factory:
|
||||||
|
conn.row_factory = sqlite3.Row
|
||||||
|
try:
|
||||||
|
yield conn
|
||||||
|
except Exception as e:
|
||||||
|
conn.rollback()
|
||||||
|
raise e
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
@require_sqlite
|
||||||
async def persist_balance(
|
async def persist_balance(
|
||||||
self, account_id: str, balance_data: Dict[str, Any]
|
self, account_id: str, balance_data: Dict[str, Any]
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Persist account balance data"""
|
"""Persist account balance data"""
|
||||||
if not self.sqlite_enabled:
|
|
||||||
logger.warning("SQLite database disabled, skipping balance persistence")
|
|
||||||
return
|
|
||||||
|
|
||||||
await self._persist_balance_sqlite(account_id, balance_data)
|
await self._persist_balance_sqlite(account_id, balance_data)
|
||||||
|
|
||||||
|
@require_sqlite
|
||||||
async def persist_transactions(
|
async def persist_transactions(
|
||||||
self, account_id: str, transactions: List[Dict[str, Any]]
|
self, account_id: str, transactions: List[Dict[str, Any]]
|
||||||
) -> List[Dict[str, Any]]:
|
) -> List[Dict[str, Any]]:
|
||||||
"""Persist transactions and return new transactions"""
|
"""Persist transactions and return new transactions"""
|
||||||
if not self.sqlite_enabled:
|
|
||||||
logger.warning("SQLite database disabled, skipping transaction persistence")
|
|
||||||
return transactions
|
|
||||||
|
|
||||||
return await self._persist_transactions_sqlite(account_id, transactions)
|
return await self._persist_transactions_sqlite(account_id, transactions)
|
||||||
|
|
||||||
def process_transactions(
|
def process_transactions(
|
||||||
@@ -55,6 +85,7 @@ class DatabaseService:
|
|||||||
account_id, account_info, transaction_data
|
account_id, account_info, transaction_data
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@require_sqlite
|
||||||
async def get_transactions_from_db(
|
async def get_transactions_from_db(
|
||||||
self,
|
self,
|
||||||
account_id: Optional[str] = None,
|
account_id: Optional[str] = None,
|
||||||
@@ -67,10 +98,6 @@ class DatabaseService:
|
|||||||
search: Optional[str] = None,
|
search: Optional[str] = None,
|
||||||
) -> List[Dict[str, Any]]:
|
) -> List[Dict[str, Any]]:
|
||||||
"""Get transactions from SQLite database"""
|
"""Get transactions from SQLite database"""
|
||||||
if not self.sqlite_enabled:
|
|
||||||
logger.warning("SQLite database disabled, cannot read transactions")
|
|
||||||
return []
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
transactions = self._get_transactions(
|
transactions = self._get_transactions(
|
||||||
account_id=account_id,
|
account_id=account_id,
|
||||||
@@ -88,6 +115,7 @@ class DatabaseService:
|
|||||||
logger.error(f"Failed to get transactions from database: {e}")
|
logger.error(f"Failed to get transactions from database: {e}")
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
@require_sqlite
|
||||||
async def get_transaction_count_from_db(
|
async def get_transaction_count_from_db(
|
||||||
self,
|
self,
|
||||||
account_id: Optional[str] = None,
|
account_id: Optional[str] = None,
|
||||||
@@ -98,9 +126,6 @@ class DatabaseService:
|
|||||||
search: Optional[str] = None,
|
search: Optional[str] = None,
|
||||||
) -> int:
|
) -> int:
|
||||||
"""Get total count of transactions from SQLite database"""
|
"""Get total count of transactions from SQLite database"""
|
||||||
if not self.sqlite_enabled:
|
|
||||||
return 0
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
filters = {
|
filters = {
|
||||||
"date_from": date_from,
|
"date_from": date_from,
|
||||||
@@ -119,14 +144,11 @@ class DatabaseService:
|
|||||||
logger.error(f"Failed to get transaction count from database: {e}")
|
logger.error(f"Failed to get transaction count from database: {e}")
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
@require_sqlite
|
||||||
async def get_balances_from_db(
|
async def get_balances_from_db(
|
||||||
self, account_id: Optional[str] = None
|
self, account_id: Optional[str] = None
|
||||||
) -> List[Dict[str, Any]]:
|
) -> List[Dict[str, Any]]:
|
||||||
"""Get balances from SQLite database"""
|
"""Get balances from SQLite database"""
|
||||||
if not self.sqlite_enabled:
|
|
||||||
logger.warning("SQLite database disabled, cannot read balances")
|
|
||||||
return []
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
balances = self._get_balances(account_id=account_id)
|
balances = self._get_balances(account_id=account_id)
|
||||||
logger.debug(f"Retrieved {len(balances)} balances from database")
|
logger.debug(f"Retrieved {len(balances)} balances from database")
|
||||||
@@ -135,14 +157,11 @@ class DatabaseService:
|
|||||||
logger.error(f"Failed to get balances from database: {e}")
|
logger.error(f"Failed to get balances from database: {e}")
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
@require_sqlite
|
||||||
async def get_historical_balances_from_db(
|
async def get_historical_balances_from_db(
|
||||||
self, account_id: Optional[str] = None, days: int = 365
|
self, account_id: Optional[str] = None, days: int = 365
|
||||||
) -> List[Dict[str, Any]]:
|
) -> List[Dict[str, Any]]:
|
||||||
"""Get historical balance progression from SQLite database"""
|
"""Get historical balance progression from SQLite database"""
|
||||||
if not self.sqlite_enabled:
|
|
||||||
logger.warning("SQLite database disabled, cannot read historical balances")
|
|
||||||
return []
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
db_path = path_manager.get_database_path()
|
db_path = path_manager.get_database_path()
|
||||||
balances = self.analytics_processor.calculate_historical_balances(
|
balances = self.analytics_processor.calculate_historical_balances(
|
||||||
@@ -156,13 +175,11 @@ class DatabaseService:
|
|||||||
logger.error(f"Failed to get historical balances from database: {e}")
|
logger.error(f"Failed to get historical balances from database: {e}")
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
@require_sqlite
|
||||||
async def get_account_summary_from_db(
|
async def get_account_summary_from_db(
|
||||||
self, account_id: str
|
self, account_id: str
|
||||||
) -> Optional[Dict[str, Any]]:
|
) -> Optional[Dict[str, Any]]:
|
||||||
"""Get basic account info from SQLite database (avoids GoCardless call)"""
|
"""Get basic account info from SQLite database (avoids GoCardless call)"""
|
||||||
if not self.sqlite_enabled:
|
|
||||||
return None
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
summary = self._get_account_summary(account_id)
|
summary = self._get_account_summary(account_id)
|
||||||
if summary:
|
if summary:
|
||||||
@@ -174,22 +191,16 @@ class DatabaseService:
|
|||||||
logger.error(f"Failed to get account summary from database: {e}")
|
logger.error(f"Failed to get account summary from database: {e}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@require_sqlite
|
||||||
async def persist_account_details(self, account_data: Dict[str, Any]) -> None:
|
async def persist_account_details(self, account_data: Dict[str, Any]) -> None:
|
||||||
"""Persist account details to database"""
|
"""Persist account details to database"""
|
||||||
if not self.sqlite_enabled:
|
|
||||||
logger.warning("SQLite database disabled, skipping account persistence")
|
|
||||||
return
|
|
||||||
|
|
||||||
await self._persist_account_details_sqlite(account_data)
|
await self._persist_account_details_sqlite(account_data)
|
||||||
|
|
||||||
|
@require_sqlite
|
||||||
async def get_accounts_from_db(
|
async def get_accounts_from_db(
|
||||||
self, account_ids: Optional[List[str]] = None
|
self, account_ids: Optional[List[str]] = None
|
||||||
) -> List[Dict[str, Any]]:
|
) -> List[Dict[str, Any]]:
|
||||||
"""Get account details from database"""
|
"""Get account details from database"""
|
||||||
if not self.sqlite_enabled:
|
|
||||||
logger.warning("SQLite database disabled, cannot read accounts")
|
|
||||||
return []
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
accounts = self._get_accounts(account_ids=account_ids)
|
accounts = self._get_accounts(account_ids=account_ids)
|
||||||
logger.debug(f"Retrieved {len(accounts)} accounts from database")
|
logger.debug(f"Retrieved {len(accounts)} accounts from database")
|
||||||
@@ -198,14 +209,11 @@ class DatabaseService:
|
|||||||
logger.error(f"Failed to get accounts from database: {e}")
|
logger.error(f"Failed to get accounts from database: {e}")
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
@require_sqlite
|
||||||
async def get_account_details_from_db(
|
async def get_account_details_from_db(
|
||||||
self, account_id: str
|
self, account_id: str
|
||||||
) -> Optional[Dict[str, Any]]:
|
) -> Optional[Dict[str, Any]]:
|
||||||
"""Get specific account details from database"""
|
"""Get specific account details from database"""
|
||||||
if not self.sqlite_enabled:
|
|
||||||
logger.warning("SQLite database disabled, cannot read account")
|
|
||||||
return None
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
account = self._get_account(account_id)
|
account = self._get_account(account_id)
|
||||||
if account:
|
if account:
|
||||||
@@ -729,10 +737,7 @@ class DatabaseService:
|
|||||||
) -> None:
|
) -> None:
|
||||||
"""Persist balance to SQLite"""
|
"""Persist balance to SQLite"""
|
||||||
try:
|
try:
|
||||||
import sqlite3
|
with self._get_db_connection() as conn:
|
||||||
|
|
||||||
db_path = path_manager.get_database_path()
|
|
||||||
conn = sqlite3.connect(str(db_path))
|
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
|
|
||||||
# Create the balances table if it doesn't exist
|
# Create the balances table if it doesn't exist
|
||||||
@@ -788,7 +793,6 @@ class DatabaseService:
|
|||||||
logger.warning(f"Skipped duplicate balance for {account_id}")
|
logger.warning(f"Skipped duplicate balance for {account_id}")
|
||||||
|
|
||||||
conn.commit()
|
conn.commit()
|
||||||
conn.close()
|
|
||||||
|
|
||||||
logger.info(f"Persisted balances to SQLite for account {account_id}")
|
logger.info(f"Persisted balances to SQLite for account {account_id}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -800,11 +804,7 @@ class DatabaseService:
|
|||||||
) -> List[Dict[str, Any]]:
|
) -> List[Dict[str, Any]]:
|
||||||
"""Persist transactions to SQLite"""
|
"""Persist transactions to SQLite"""
|
||||||
try:
|
try:
|
||||||
import json
|
with self._get_db_connection() as conn:
|
||||||
import sqlite3
|
|
||||||
|
|
||||||
db_path = path_manager.get_database_path()
|
|
||||||
conn = sqlite3.connect(str(db_path))
|
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
|
|
||||||
# The table should already exist with the new schema from migration
|
# The table should already exist with the new schema from migration
|
||||||
@@ -899,7 +899,6 @@ class DatabaseService:
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
conn.commit()
|
conn.commit()
|
||||||
conn.close()
|
|
||||||
|
|
||||||
logger.info(
|
logger.info(
|
||||||
f"Persisted {len(new_transactions)} new transactions to SQLite for account {account_id}"
|
f"Persisted {len(new_transactions)} new transactions to SQLite for account {account_id}"
|
||||||
@@ -939,8 +938,8 @@ class DatabaseService:
|
|||||||
db_path = path_manager.get_database_path()
|
db_path = path_manager.get_database_path()
|
||||||
if not db_path.exists():
|
if not db_path.exists():
|
||||||
return []
|
return []
|
||||||
conn = sqlite3.connect(str(db_path))
|
|
||||||
conn.row_factory = sqlite3.Row # Enable dict-like access
|
with self._get_db_connection(row_factory=True) as conn:
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
|
|
||||||
# Build query with filters
|
# Build query with filters
|
||||||
@@ -982,7 +981,6 @@ class DatabaseService:
|
|||||||
query += " OFFSET ?"
|
query += " OFFSET ?"
|
||||||
params.append(offset)
|
params.append(offset)
|
||||||
|
|
||||||
try:
|
|
||||||
cursor.execute(query, params)
|
cursor.execute(query, params)
|
||||||
rows = cursor.fetchall()
|
rows = cursor.fetchall()
|
||||||
|
|
||||||
@@ -996,20 +994,15 @@ class DatabaseService:
|
|||||||
)
|
)
|
||||||
transactions.append(transaction)
|
transactions.append(transaction)
|
||||||
|
|
||||||
conn.close()
|
|
||||||
return transactions
|
return transactions
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
conn.close()
|
|
||||||
raise e
|
|
||||||
|
|
||||||
def _get_balances(self, account_id=None):
|
def _get_balances(self, account_id=None):
|
||||||
"""Get latest balances from SQLite database"""
|
"""Get latest balances from SQLite database"""
|
||||||
db_path = path_manager.get_database_path()
|
db_path = path_manager.get_database_path()
|
||||||
if not db_path.exists():
|
if not db_path.exists():
|
||||||
return []
|
return []
|
||||||
conn = sqlite3.connect(str(db_path))
|
|
||||||
conn.row_factory = sqlite3.Row
|
with self._get_db_connection(row_factory=True) as conn:
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
|
|
||||||
# Get latest balance for each account_id and type combination
|
# Get latest balance for each account_id and type combination
|
||||||
@@ -1029,28 +1022,20 @@ class DatabaseService:
|
|||||||
|
|
||||||
query += " ORDER BY b1.account_id, b1.type"
|
query += " ORDER BY b1.account_id, b1.type"
|
||||||
|
|
||||||
try:
|
|
||||||
cursor.execute(query, params)
|
cursor.execute(query, params)
|
||||||
rows = cursor.fetchall()
|
rows = cursor.fetchall()
|
||||||
|
|
||||||
balances = [dict(row) for row in rows]
|
return [dict(row) for row in rows]
|
||||||
conn.close()
|
|
||||||
return balances
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
conn.close()
|
|
||||||
raise e
|
|
||||||
|
|
||||||
def _get_account_summary(self, account_id):
|
def _get_account_summary(self, account_id):
|
||||||
"""Get basic account info from transactions table (avoids GoCardless API call)"""
|
"""Get basic account info from transactions table (avoids GoCardless API call)"""
|
||||||
db_path = path_manager.get_database_path()
|
db_path = path_manager.get_database_path()
|
||||||
if not db_path.exists():
|
if not db_path.exists():
|
||||||
return None
|
return None
|
||||||
conn = sqlite3.connect(str(db_path))
|
|
||||||
conn.row_factory = sqlite3.Row
|
with self._get_db_connection(row_factory=True) as conn:
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
|
|
||||||
try:
|
|
||||||
# Get account info from most recent transaction
|
# Get account info from most recent transaction
|
||||||
cursor.execute(
|
cursor.execute(
|
||||||
"""
|
"""
|
||||||
@@ -1064,22 +1049,17 @@ class DatabaseService:
|
|||||||
)
|
)
|
||||||
|
|
||||||
row = cursor.fetchone()
|
row = cursor.fetchone()
|
||||||
conn.close()
|
|
||||||
|
|
||||||
if row:
|
if row:
|
||||||
return dict(row)
|
return dict(row)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
conn.close()
|
|
||||||
raise e
|
|
||||||
|
|
||||||
def _get_transaction_count(self, account_id=None, **filters):
|
def _get_transaction_count(self, account_id=None, **filters):
|
||||||
"""Get total count of transactions matching filters"""
|
"""Get total count of transactions matching filters"""
|
||||||
db_path = path_manager.get_database_path()
|
db_path = path_manager.get_database_path()
|
||||||
if not db_path.exists():
|
if not db_path.exists():
|
||||||
return 0
|
return 0
|
||||||
conn = sqlite3.connect(str(db_path))
|
|
||||||
|
with self._get_db_connection() as conn:
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
|
|
||||||
query = "SELECT COUNT(*) FROM transactions WHERE 1=1"
|
query = "SELECT COUNT(*) FROM transactions WHERE 1=1"
|
||||||
@@ -1110,20 +1090,12 @@ class DatabaseService:
|
|||||||
query += " AND description LIKE ?"
|
query += " AND description LIKE ?"
|
||||||
params.append(f"%{filters['search']}%")
|
params.append(f"%{filters['search']}%")
|
||||||
|
|
||||||
try:
|
|
||||||
cursor.execute(query, params)
|
cursor.execute(query, params)
|
||||||
count = cursor.fetchone()[0]
|
return cursor.fetchone()[0]
|
||||||
conn.close()
|
|
||||||
return count
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
conn.close()
|
|
||||||
raise e
|
|
||||||
|
|
||||||
def _persist_account(self, account_data: dict):
|
def _persist_account(self, account_data: dict):
|
||||||
"""Persist account details to SQLite database"""
|
"""Persist account details to SQLite database"""
|
||||||
db_path = path_manager.get_database_path()
|
with self._get_db_connection() as conn:
|
||||||
conn = sqlite3.connect(str(db_path))
|
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
|
|
||||||
# Create the accounts table if it doesn't exist
|
# Create the accounts table if it doesn't exist
|
||||||
@@ -1153,7 +1125,6 @@ class DatabaseService:
|
|||||||
ON accounts(status)"""
|
ON accounts(status)"""
|
||||||
)
|
)
|
||||||
|
|
||||||
try:
|
|
||||||
# First, check if account exists and preserve display_name
|
# First, check if account exists and preserve display_name
|
||||||
cursor.execute(
|
cursor.execute(
|
||||||
"SELECT display_name FROM accounts WHERE id = ?", (account_data["id"],)
|
"SELECT display_name FROM accounts WHERE id = ?", (account_data["id"],)
|
||||||
@@ -1194,21 +1165,16 @@ class DatabaseService:
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
conn.commit()
|
conn.commit()
|
||||||
conn.close()
|
|
||||||
|
|
||||||
return account_data
|
return account_data
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
conn.close()
|
|
||||||
raise e
|
|
||||||
|
|
||||||
def _get_accounts(self, account_ids=None):
|
def _get_accounts(self, account_ids=None):
|
||||||
"""Get account details from SQLite database"""
|
"""Get account details from SQLite database"""
|
||||||
db_path = path_manager.get_database_path()
|
db_path = path_manager.get_database_path()
|
||||||
if not db_path.exists():
|
if not db_path.exists():
|
||||||
return []
|
return []
|
||||||
conn = sqlite3.connect(str(db_path))
|
|
||||||
conn.row_factory = sqlite3.Row
|
with self._get_db_connection(row_factory=True) as conn:
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
|
|
||||||
query = "SELECT * FROM accounts"
|
query = "SELECT * FROM accounts"
|
||||||
@@ -1221,40 +1187,28 @@ class DatabaseService:
|
|||||||
|
|
||||||
query += " ORDER BY created DESC"
|
query += " ORDER BY created DESC"
|
||||||
|
|
||||||
try:
|
|
||||||
cursor.execute(query, params)
|
cursor.execute(query, params)
|
||||||
rows = cursor.fetchall()
|
rows = cursor.fetchall()
|
||||||
|
|
||||||
accounts = [dict(row) for row in rows]
|
return [dict(row) for row in rows]
|
||||||
conn.close()
|
|
||||||
return accounts
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
conn.close()
|
|
||||||
raise e
|
|
||||||
|
|
||||||
def _get_account(self, account_id: str):
|
def _get_account(self, account_id: str):
|
||||||
"""Get specific account details from SQLite database"""
|
"""Get specific account details from SQLite database"""
|
||||||
db_path = path_manager.get_database_path()
|
db_path = path_manager.get_database_path()
|
||||||
if not db_path.exists():
|
if not db_path.exists():
|
||||||
return None
|
return None
|
||||||
conn = sqlite3.connect(str(db_path))
|
|
||||||
conn.row_factory = sqlite3.Row
|
with self._get_db_connection(row_factory=True) as conn:
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
|
|
||||||
try:
|
|
||||||
cursor.execute("SELECT * FROM accounts WHERE id = ?", (account_id,))
|
cursor.execute("SELECT * FROM accounts WHERE id = ?", (account_id,))
|
||||||
row = cursor.fetchone()
|
row = cursor.fetchone()
|
||||||
conn.close()
|
|
||||||
|
|
||||||
if row:
|
if row:
|
||||||
return dict(row)
|
return dict(row)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
except Exception as e:
|
@require_sqlite
|
||||||
conn.close()
|
|
||||||
raise e
|
|
||||||
|
|
||||||
async def get_monthly_transaction_stats_from_db(
|
async def get_monthly_transaction_stats_from_db(
|
||||||
self,
|
self,
|
||||||
account_id: Optional[str] = None,
|
account_id: Optional[str] = None,
|
||||||
@@ -1262,10 +1216,6 @@ class DatabaseService:
|
|||||||
date_to: Optional[str] = None,
|
date_to: Optional[str] = None,
|
||||||
) -> List[Dict[str, Any]]:
|
) -> List[Dict[str, Any]]:
|
||||||
"""Get monthly transaction statistics aggregated by the database"""
|
"""Get monthly transaction statistics aggregated by the database"""
|
||||||
if not self.sqlite_enabled:
|
|
||||||
logger.warning("SQLite database disabled, cannot read monthly stats")
|
|
||||||
return []
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
db_path = path_manager.get_database_path()
|
db_path = path_manager.get_database_path()
|
||||||
monthly_stats = self.analytics_processor.calculate_monthly_stats(
|
monthly_stats = self.analytics_processor.calculate_monthly_stats(
|
||||||
|
|||||||
Reference in New Issue
Block a user