Spaces:
Paused
Paused
dushyantsingh0 commited on
Commit ·
0652eed
1
Parent(s): 054ee7d
backend stabilization
Browse files
api/v1/tickets.py
CHANGED
|
@@ -5,7 +5,7 @@ Ticket endpoints: CRUD, status transitions, and assignments. (Refactored to use
|
|
| 5 |
import asyncio
|
| 6 |
from typing import Any, List, Optional, Union
|
| 7 |
from uuid import UUID
|
| 8 |
-
from fastapi import APIRouter, Depends, status, HTTPException
|
| 9 |
from sqlalchemy.ext.asyncio import AsyncSession
|
| 10 |
from sqlalchemy import select
|
| 11 |
from sqlalchemy.orm import joinedload
|
|
@@ -82,12 +82,25 @@ async def list_tickets_paginated(
|
|
| 82 |
@router.post("/", response_model=Union[TicketInternalResponse, TicketResponse], status_code=status.HTTP_201_CREATED, dependencies=[Depends(require_roles(ALL_CUSTOMER_ROLES + WORKSPACE_ADMIN_ROLES))])
|
| 83 |
async def create_ticket(
|
| 84 |
payload: TicketCreate,
|
|
|
|
| 85 |
current_user: User = Depends(get_current_user),
|
| 86 |
db: AsyncSession = Depends(get_db),
|
| 87 |
arq_pool: Any = Depends(get_arq_pool)
|
| 88 |
) -> Any:
|
| 89 |
"""Create a new ticket."""
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
@router.get("/", response_model=List[Union[TicketInternalResponse, TicketResponse]])
|
| 93 |
async def list_tickets(
|
|
|
|
| 5 |
import asyncio
|
| 6 |
from typing import Any, List, Optional, Union
|
| 7 |
from uuid import UUID
|
| 8 |
+
from fastapi import APIRouter, Depends, status, HTTPException, BackgroundTasks
|
| 9 |
from sqlalchemy.ext.asyncio import AsyncSession
|
| 10 |
from sqlalchemy import select
|
| 11 |
from sqlalchemy.orm import joinedload
|
|
|
|
| 82 |
@router.post("/", response_model=Union[TicketInternalResponse, TicketResponse], status_code=status.HTTP_201_CREATED, dependencies=[Depends(require_roles(ALL_CUSTOMER_ROLES + WORKSPACE_ADMIN_ROLES))])
|
| 83 |
async def create_ticket(
|
| 84 |
payload: TicketCreate,
|
| 85 |
+
background_tasks: BackgroundTasks,
|
| 86 |
current_user: User = Depends(get_current_user),
|
| 87 |
db: AsyncSession = Depends(get_db),
|
| 88 |
arq_pool: Any = Depends(get_arq_pool)
|
| 89 |
) -> Any:
|
| 90 |
"""Create a new ticket."""
|
| 91 |
+
ticket_data = await TicketService.create_ticket(payload, current_user, db, arq_pool)
|
| 92 |
+
|
| 93 |
+
# Offload non-critical side effects to background tasks
|
| 94 |
+
# This includes WebSocket broadcasts and cache purges
|
| 95 |
+
ticket_id = ticket_data.get("id")
|
| 96 |
+
if ticket_id:
|
| 97 |
+
background_tasks.add_task(
|
| 98 |
+
TicketService.run_post_creation_sync,
|
| 99 |
+
ticket_id=ticket_id,
|
| 100 |
+
workspace_id=current_user.workspace_id
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
return ticket_data
|
| 104 |
|
| 105 |
@router.get("/", response_model=List[Union[TicketInternalResponse, TicketResponse]])
|
| 106 |
async def list_tickets(
|
frontend/apps/internal/src/components/TopBar.module.css
CHANGED
|
@@ -100,21 +100,35 @@
|
|
| 100 |
display: flex;
|
| 101 |
align-items: center;
|
| 102 |
gap: 24px;
|
|
|
|
|
|
|
| 103 |
}
|
| 104 |
|
| 105 |
-
@media (max-width:
|
| 106 |
.right {
|
| 107 |
-
gap:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
}
|
| 109 |
-
|
| 110 |
.statusIndicator {
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
| 112 |
}
|
| 113 |
}
|
| 114 |
|
| 115 |
-
@media (max-width:
|
| 116 |
.right {
|
| 117 |
-
gap:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
}
|
| 119 |
}
|
| 120 |
|
|
@@ -122,18 +136,20 @@
|
|
| 122 |
.searchWrapper {
|
| 123 |
position: relative;
|
| 124 |
width: 280px;
|
|
|
|
|
|
|
| 125 |
transition: width 0.3s ease;
|
| 126 |
}
|
| 127 |
|
| 128 |
@media (max-width: 1200px) {
|
| 129 |
.searchWrapper {
|
| 130 |
-
width:
|
| 131 |
}
|
| 132 |
}
|
| 133 |
|
| 134 |
-
@media (max-width:
|
| 135 |
.searchWrapper {
|
| 136 |
-
|
| 137 |
}
|
| 138 |
}
|
| 139 |
|
|
@@ -414,6 +430,13 @@
|
|
| 414 |
cursor: pointer;
|
| 415 |
transition: all 0.2s;
|
| 416 |
box-shadow: 0 4px 10px rgba(239, 68, 68, 0.2);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 417 |
}
|
| 418 |
|
| 419 |
.logoutBtn:hover {
|
|
|
|
| 100 |
display: flex;
|
| 101 |
align-items: center;
|
| 102 |
gap: 24px;
|
| 103 |
+
flex-shrink: 1;
|
| 104 |
+
min-width: 0;
|
| 105 |
}
|
| 106 |
|
| 107 |
+
@media (max-width: 1300px) {
|
| 108 |
.right {
|
| 109 |
+
gap: 16px;
|
| 110 |
+
}
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
@media (max-width: 1100px) {
|
| 114 |
+
.right {
|
| 115 |
+
gap: 12px;
|
| 116 |
}
|
|
|
|
| 117 |
.statusIndicator {
|
| 118 |
+
padding: 6px 10px;
|
| 119 |
+
}
|
| 120 |
+
.timeText {
|
| 121 |
+
display: none;
|
| 122 |
}
|
| 123 |
}
|
| 124 |
|
| 125 |
+
@media (max-width: 768px) {
|
| 126 |
.right {
|
| 127 |
+
gap: 8px;
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
.statusIndicator {
|
| 131 |
+
display: none !important;
|
| 132 |
}
|
| 133 |
}
|
| 134 |
|
|
|
|
| 136 |
.searchWrapper {
|
| 137 |
position: relative;
|
| 138 |
width: 280px;
|
| 139 |
+
flex-shrink: 1;
|
| 140 |
+
min-width: 140px;
|
| 141 |
transition: width 0.3s ease;
|
| 142 |
}
|
| 143 |
|
| 144 |
@media (max-width: 1200px) {
|
| 145 |
.searchWrapper {
|
| 146 |
+
width: 220px;
|
| 147 |
}
|
| 148 |
}
|
| 149 |
|
| 150 |
+
@media (max-width: 1024px) {
|
| 151 |
.searchWrapper {
|
| 152 |
+
width: 180px;
|
| 153 |
}
|
| 154 |
}
|
| 155 |
|
|
|
|
| 430 |
cursor: pointer;
|
| 431 |
transition: all 0.2s;
|
| 432 |
box-shadow: 0 4px 10px rgba(239, 68, 68, 0.2);
|
| 433 |
+
white-space: nowrap;
|
| 434 |
+
}
|
| 435 |
+
|
| 436 |
+
@media (max-width: 1200px) {
|
| 437 |
+
.logoutBtn {
|
| 438 |
+
padding: 8px 12px;
|
| 439 |
+
}
|
| 440 |
}
|
| 441 |
|
| 442 |
.logoutBtn:hover {
|
frontend/packages/shared/src/hooks/useTickets.ts
CHANGED
|
@@ -75,8 +75,25 @@ export const useTickets = (workspaceId?: string) => {
|
|
| 75 |
const createTicket = async (data: any) => {
|
| 76 |
try {
|
| 77 |
const response = await api.post('tickets/', data);
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
} catch (err: any) {
|
| 81 |
if (err.response?.status === 422) {
|
| 82 |
const details = err.response.data.detail;
|
|
|
|
| 75 |
const createTicket = async (data: any) => {
|
| 76 |
try {
|
| 77 |
const response = await api.post('tickets/', data);
|
| 78 |
+
const newTicket = response.data;
|
| 79 |
+
|
| 80 |
+
// --- OPTIMISTIC UI UPDATE ---
|
| 81 |
+
// Immediately inject the new ticket into the global cache to make the transition feel instant.
|
| 82 |
+
if (!globalTicketsMap[wsKey]) {
|
| 83 |
+
globalTicketsMap[wsKey] = [];
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
// Check if it already exists (e.g. if WS broadcast already arrived)
|
| 87 |
+
if (!globalTicketsMap[wsKey].find(t => t.id === newTicket.id)) {
|
| 88 |
+
globalTicketsMap[wsKey] = [newTicket, ...globalTicketsMap[wsKey]];
|
| 89 |
+
sessionStorage.setItem(TICKET_CACHE_KEY, JSON.stringify(globalTicketsMap));
|
| 90 |
+
notifyTickets();
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
// Trigger background refresh (NON-AWAITED) to ensure eventual consistency
|
| 94 |
+
fetchTickets(true);
|
| 95 |
+
|
| 96 |
+
return newTicket;
|
| 97 |
} catch (err: any) {
|
| 98 |
if (err.response?.status === 422) {
|
| 99 |
const details = err.response.data.detail;
|
services/ticket_service.py
CHANGED
|
@@ -243,14 +243,18 @@ class TicketService:
|
|
| 243 |
payload_str = f"{payload.title.strip()}|{payload.description.strip()}"
|
| 244 |
c_hash = hashlib.sha256(payload_str.encode('utf-8')).hexdigest()
|
| 245 |
|
| 246 |
-
|
|
|
|
| 247 |
Ticket.workspace_id == current_user.workspace_id,
|
| 248 |
Ticket.content_hash == c_hash,
|
| 249 |
Ticket.status.in_([TicketStatus.OPEN, TicketStatus.ACKNOWLEDGED, TicketStatus.IN_PROGRESS]),
|
| 250 |
Ticket.deleted_at.is_(None)
|
| 251 |
).limit(1)
|
| 252 |
-
|
| 253 |
-
if
|
|
|
|
|
|
|
|
|
|
| 254 |
return await TicketService.sanitize_ticket_response(existing_dup, current_user, db)
|
| 255 |
|
| 256 |
# SLA Calculation
|
|
@@ -288,14 +292,33 @@ class TicketService:
|
|
| 288 |
refetch_stmt = select(Ticket).options(joinedload(Ticket.creator), joinedload(Ticket.assignee), joinedload(Ticket.resolver), joinedload(Ticket.feedback)).where(Ticket.id == new_ticket.id)
|
| 289 |
new_ticket = (await db.execute(refetch_stmt)).scalar_one()
|
| 290 |
|
|
|
|
|
|
|
| 291 |
await enqueue_task(arq_pool, "run_ai_workflow", new_ticket.id, new_ticket.workspace_id)
|
| 292 |
-
await manager.broadcast_to_workspace(current_user.workspace_id, {"event": "ticket_created", "ticket_id": str(new_ticket.id)})
|
| 293 |
|
| 294 |
-
|
| 295 |
-
|
| 296 |
|
| 297 |
return await TicketService.sanitize_ticket_response(new_ticket, current_user, db)
|
| 298 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
@staticmethod
|
| 300 |
async def update_ticket(
|
| 301 |
ticket_id: UUID,
|
|
|
|
| 243 |
payload_str = f"{payload.title.strip()}|{payload.description.strip()}"
|
| 244 |
c_hash = hashlib.sha256(payload_str.encode('utf-8')).hexdigest()
|
| 245 |
|
| 246 |
+
# Deduplication: Optimized to avoid N+1 joinedload when just checking existence
|
| 247 |
+
dup_stmt = select(Ticket).where(
|
| 248 |
Ticket.workspace_id == current_user.workspace_id,
|
| 249 |
Ticket.content_hash == c_hash,
|
| 250 |
Ticket.status.in_([TicketStatus.OPEN, TicketStatus.ACKNOWLEDGED, TicketStatus.IN_PROGRESS]),
|
| 251 |
Ticket.deleted_at.is_(None)
|
| 252 |
).limit(1)
|
| 253 |
+
existing_dup_id = (await db.execute(dup_stmt)).scalars().first()
|
| 254 |
+
if existing_dup_id:
|
| 255 |
+
# Full fetch only on hit
|
| 256 |
+
full_stmt = select(Ticket).options(joinedload(Ticket.creator), joinedload(Ticket.assignee)).where(Ticket.id == existing_dup_id)
|
| 257 |
+
existing_dup = (await db.execute(full_stmt)).scalar_one()
|
| 258 |
return await TicketService.sanitize_ticket_response(existing_dup, current_user, db)
|
| 259 |
|
| 260 |
# SLA Calculation
|
|
|
|
| 292 |
refetch_stmt = select(Ticket).options(joinedload(Ticket.creator), joinedload(Ticket.assignee), joinedload(Ticket.resolver), joinedload(Ticket.feedback)).where(Ticket.id == new_ticket.id)
|
| 293 |
new_ticket = (await db.execute(refetch_stmt)).scalar_one()
|
| 294 |
|
| 295 |
+
# CRITICAL SIDE EFFECTS: Enqueue AI Task immediately (Awaited for integrity)
|
| 296 |
+
# We await this to ensure the neural handshake is at least committed to the queue.
|
| 297 |
await enqueue_task(arq_pool, "run_ai_workflow", new_ticket.id, new_ticket.workspace_id)
|
|
|
|
| 298 |
|
| 299 |
+
# NON-CRITICAL SIDE EFFECTS: Offloaded to BackgroundTasks (managed by caller)
|
| 300 |
+
# This includes WS broadcasts and cache purges which are high-latency on remote Redis.
|
| 301 |
|
| 302 |
return await TicketService.sanitize_ticket_response(new_ticket, current_user, db)
|
| 303 |
|
| 304 |
+
@staticmethod
|
| 305 |
+
async def run_post_creation_sync(ticket_id: UUID, workspace_id: UUID):
|
| 306 |
+
"""
|
| 307 |
+
Background task to handle non-critical post-creation synchronization.
|
| 308 |
+
Offloading this from the main request loop saves ~2-5s in high-latency Redis environments.
|
| 309 |
+
"""
|
| 310 |
+
try:
|
| 311 |
+
# 1. Broadcast to workspace (WebSocket)
|
| 312 |
+
await manager.broadcast_to_workspace(workspace_id, {"event": "ticket_created", "ticket_id": str(ticket_id)})
|
| 313 |
+
|
| 314 |
+
# 2. Invalidate Caches (Parallelized)
|
| 315 |
+
await asyncio.gather(
|
| 316 |
+
delete_by_pattern(f"tickets:list:{workspace_id}*"),
|
| 317 |
+
invalidate_workspace_semantic_cache(str(workspace_id))
|
| 318 |
+
)
|
| 319 |
+
except Exception as e:
|
| 320 |
+
logger.error(f"Failed to complete post-creation sync for ticket {ticket_id}: {e}")
|
| 321 |
+
|
| 322 |
@staticmethod
|
| 323 |
async def update_ticket(
|
| 324 |
ticket_id: UUID,
|