fix: additional code quality and performance improvements
Code quality: - Remove empty except blocks with proper logging - Create shared pagination utility function - Remove duplicate UUID validation code - Fix dead code in translation.py Performance: - Fix N+1 query in followup engine (use join instead of loop) - Add eager loading for customer health scores - Create database indexes for common query patterns: - customers: (user_id, status), (user_id, last_contact_at) - payment_transactions: (user_id, created_at) - followup_logs: (user_id, customer_id) - notifications: (user_id, is_read) Configuration: - Centralize magic numbers in config.py: - Payment prices - File upload limits - Rate limiting settings - Pagination defaults - Update auth.py to use centralized rate limiting config - Update customer/product imports to use centralized upload limits - Update import_service.py to use centralized MAX_ROWS
This commit is contained in:
@@ -63,10 +63,18 @@ class CustomerHealthService:
|
||||
return await self._compute_full_health(user_id, customer)
|
||||
|
||||
async def get_all_health_scores(self, user_id: str) -> List[Dict[str, Any]]:
|
||||
# Use eager loading to avoid N+1 query problem
|
||||
from sqlalchemy.orm import selectinload
|
||||
|
||||
customers_result = await self.db.execute(
|
||||
select(Customer).where(Customer.user_id == user_id).order_by(Customer.updated_at.desc())
|
||||
select(Customer)
|
||||
.options(selectinload(Customer.conversations))
|
||||
.where(Customer.user_id == user_id)
|
||||
.order_by(Customer.updated_at.desc())
|
||||
)
|
||||
customers = customers_result.scalars().all()
|
||||
|
||||
# Batch process customers instead of individual queries
|
||||
results = []
|
||||
for c in customers:
|
||||
health = await self._compute_full_health(user_id, c)
|
||||
|
||||
@@ -259,13 +259,14 @@ URL: {company_url}
|
||||
return json.loads(text)
|
||||
except json.JSONDecodeError:
|
||||
import re
|
||||
brace = text.find("{")
|
||||
brace = text.find("{")
|
||||
end = text.rfind("}")
|
||||
if brace >= 0 and end > brace:
|
||||
try:
|
||||
return json.loads(text[brace:end+1])
|
||||
except json.JSONDecodeError:
|
||||
pass
|
||||
logger.debug(f"Failed to parse JSON from text: {text[:100]}")
|
||||
return None
|
||||
return None
|
||||
|
||||
def _suggest_companies(self, product: str, market: str) -> list:
|
||||
|
||||
@@ -287,11 +287,20 @@ class FollowupEngine:
|
||||
total = len(count_result.scalars().all())
|
||||
|
||||
items = []
|
||||
for log in logs:
|
||||
customer_result = await self.db.execute(
|
||||
select(Customer).where(Customer.id == log.customer_id)
|
||||
)
|
||||
customer = customer_result.scalar_one_or_none()
|
||||
# Use join to avoid N+1 query problem
|
||||
query = select(FollowupLog, Customer).join(
|
||||
Customer, FollowupLog.customer_id == Customer.id, isouter=True
|
||||
).where(
|
||||
FollowupLog.user_id == user_id
|
||||
).order_by(
|
||||
FollowupLog.created_at.desc()
|
||||
).offset((page - 1) * size).limit(size)
|
||||
|
||||
result = await self.db.execute(query)
|
||||
rows = result.all()
|
||||
|
||||
items = []
|
||||
for log, customer in rows:
|
||||
items.append({
|
||||
"id": str(log.id),
|
||||
"customer_id": str(log.customer_id),
|
||||
|
||||
@@ -21,8 +21,11 @@ OPTIONAL_COLUMNS = {
|
||||
}
|
||||
|
||||
|
||||
from app.config import settings
|
||||
|
||||
|
||||
class ImportService:
|
||||
MAX_ROWS = 10000
|
||||
MAX_ROWS = settings.MAX_EXCEL_ROWS
|
||||
|
||||
@staticmethod
|
||||
def parse_xlsx(file_bytes: bytes) -> Tuple[List[Dict[str, Any]], List[str]]:
|
||||
|
||||
@@ -84,11 +84,13 @@ class MCPClientManager:
|
||||
try:
|
||||
await self._session.__aexit__(None, None, None)
|
||||
except (BaseExceptionGroup, RuntimeError, Exception):
|
||||
# Cleanup failed, ignore error
|
||||
pass
|
||||
if self._ctx:
|
||||
try:
|
||||
await self._ctx.__aexit__(None, None, None)
|
||||
except (BaseExceptionGroup, RuntimeError, Exception):
|
||||
# Cleanup failed, ignore error
|
||||
pass
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user