Spaces:
Running

axeronis commited on
Commit
3118010
·
1 Parent(s): 1326b8d

perf: fix N+1 query in leads API, add smooth scroll to ClientCard

Browse files
backend/app/api/leads.py CHANGED
@@ -110,17 +110,40 @@ async def get_leads(
110
  conversations = conv_result.scalars().all()
111
  conv_map = {c.lead_id: c.id for c in conversations}
112
 
113
- # Get last message for each conversation
114
  last_messages = {}
115
- for lead_id, conv_id in conv_map.items():
116
- msg_stmt = select(Message).where(
117
- Message.conversation_id == conv_id
118
- ).order_by(Message.created_at.desc()).limit(1)
119
- msg_result = await session.execute(msg_stmt)
120
- msg = msg_result.scalar_one_or_none()
121
- if msg:
122
- # Truncate for preview
123
- last_messages[lead_id] = msg.content[:100] if msg.content else None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
 
125
  return [
126
  LeadResponse(
 
110
  conversations = conv_result.scalars().all()
111
  conv_map = {c.lead_id: c.id for c in conversations}
112
 
113
+ # Get last message for each conversation - OPTIMIZED: Single query instead of N+1
114
  last_messages = {}
115
+ if conv_map:
116
+ # Get all conversation IDs
117
+ conv_ids = list(conv_map.values())
118
+
119
+ # Bulk fetch last messages using window function
120
+ from sqlalchemy import func, desc
121
+ from sqlalchemy.orm import aliased
122
+
123
+ # Subquery to get latest message per conversation
124
+ subq = (
125
+ select(
126
+ Message.conversation_id,
127
+ Message.content,
128
+ func.row_number().over(
129
+ partition_by=Message.conversation_id,
130
+ order_by=desc(Message.created_at)
131
+ ).label('rn')
132
+ )
133
+ .where(Message.conversation_id.in_(conv_ids))
134
+ .subquery()
135
+ )
136
+
137
+ # Select only the first row (rn=1) for each conversation
138
+ last_msg_stmt = select(subq.c.conversation_id, subq.c.content).where(subq.c.rn == 1)
139
+ last_msg_result = await session.execute(last_msg_stmt)
140
+
141
+ # Build reverse map: lead_id -> last_message
142
+ conv_to_lead = {v: k for k, v in conv_map.items()}
143
+ for conv_id, content in last_msg_result:
144
+ lead_id = conv_to_lead.get(conv_id)
145
+ if lead_id and content:
146
+ last_messages[lead_id] = content[:100]
147
 
148
  return [
149
  LeadResponse(
frontend/components/ClientCard.tsx CHANGED
@@ -207,8 +207,8 @@ export function ClientCard({ lead, onTakeover, isAnalyzing, onRefresh, conversat
207
 
208
  {/* Content */}
209
  <div
210
- className="flex-1 overflow-y-auto p-4 md:p-5 space-y-4 md:space-y-5 scrollbar-thin scrollbar-thumb-white/10"
211
- style={{ overscrollBehavior: 'contain' }}
212
  >
213
  {/* QUALIFICATION GRID - Premium View */}
214
  {(lead as any).qualification_data && Object.keys((lead as any).qualification_data).length > 0 && (
 
207
 
208
  {/* Content */}
209
  <div
210
+ className="flex-1 overflow-y-auto p-4 md:p-5 space-y-4 md:space-y-5 scrollbar-thin scrollbar-thumb-white/10 scroll-smooth"
211
+ style={{ overscrollBehavior: 'contain', willChange: 'scroll-position', WebkitOverflowScrolling: 'touch' }}
212
  >
213
  {/* QUALIFICATION GRID - Premium View */}
214
  {(lead as any).qualification_data && Object.keys((lead as any).qualification_data).length > 0 && (