Spaces:
Sleeping
Sleeping
Initial WorkWise backend deployment metric routes fix
Browse files
app/routes/__init__.py
CHANGED
|
@@ -1 +1,75 @@
|
|
| 1 |
-
"""API route modules"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""API route modules"""
|
| 2 |
+
"""Routes for aggregate metrics"""
|
| 3 |
+
from fastapi import APIRouter, HTTPException
|
| 4 |
+
from app.models.jira_schema import MetricsResponse
|
| 5 |
+
from app.services.vector_store import vector_store
|
| 6 |
+
from app.utils.logger import setup_logger
|
| 7 |
+
import pandas as pd
|
| 8 |
+
|
| 9 |
+
logger = setup_logger(__name__)
|
| 10 |
+
router = APIRouter()
|
| 11 |
+
|
| 12 |
+
@router.get("/metrics", response_model=MetricsResponse)
|
| 13 |
+
async def get_metrics():
|
| 14 |
+
"""
|
| 15 |
+
Get aggregate metrics from Jira data
|
| 16 |
+
- Average resolution time
|
| 17 |
+
- Open/closed ticket counts
|
| 18 |
+
- SLA compliance percentage
|
| 19 |
+
"""
|
| 20 |
+
try:
|
| 21 |
+
logger.info("Calculating metrics...")
|
| 22 |
+
|
| 23 |
+
info = vector_store.get_collection_info()
|
| 24 |
+
total_tickets = info.get('vectors_count', 0)
|
| 25 |
+
if total_tickets == 0:
|
| 26 |
+
raise HTTPException(status_code=404, detail="No data available. Please ingest data first.")
|
| 27 |
+
|
| 28 |
+
payloads = vector_store.get_payloads_sample(limit=100)
|
| 29 |
+
if not payloads:
|
| 30 |
+
raise HTTPException(status_code=404, detail="No tickets found in Faiss payloads")
|
| 31 |
+
|
| 32 |
+
# Calculate metrics
|
| 33 |
+
open_statuses = {'Open', 'In Progress', 'To Do'}
|
| 34 |
+
closed_statuses = {'Closed', 'Done', 'Resolved'}
|
| 35 |
+
|
| 36 |
+
open_tickets = sum(1 for p in payloads if (p.get('status') or '') in open_statuses)
|
| 37 |
+
closed_tickets = sum(1 for p in payloads if (p.get('status') or '') in closed_statuses)
|
| 38 |
+
|
| 39 |
+
# Average resolution time (days)
|
| 40 |
+
resolution_times = []
|
| 41 |
+
for p in payloads:
|
| 42 |
+
created = p.get('created_date')
|
| 43 |
+
resolved = p.get('resolved_date')
|
| 44 |
+
if created and resolved:
|
| 45 |
+
try:
|
| 46 |
+
c = pd.to_datetime(created)
|
| 47 |
+
r = pd.to_datetime(resolved)
|
| 48 |
+
delta = (r - c).days
|
| 49 |
+
if delta >= 0:
|
| 50 |
+
resolution_times.append(delta)
|
| 51 |
+
except Exception:
|
| 52 |
+
pass
|
| 53 |
+
|
| 54 |
+
avg_resolution = (sum(resolution_times) / len(resolution_times)) if resolution_times else 0.0
|
| 55 |
+
avg_resolution_str = f"{avg_resolution:.1f} days"
|
| 56 |
+
|
| 57 |
+
# SLA compliance: resolved within 5 days
|
| 58 |
+
sla_threshold = 5
|
| 59 |
+
sla_compliant = sum(1 for t in resolution_times if t <= sla_threshold)
|
| 60 |
+
sla_pct = (sla_compliant / len(resolution_times) * 100) if resolution_times else 0.0
|
| 61 |
+
sla_compliance_str = f"{sla_pct:.0f}%"
|
| 62 |
+
|
| 63 |
+
return MetricsResponse(
|
| 64 |
+
avg_resolution_time=avg_resolution_str,
|
| 65 |
+
open_tickets=open_tickets,
|
| 66 |
+
closed_tickets=closed_tickets,
|
| 67 |
+
sla_compliance=sla_compliance_str,
|
| 68 |
+
total_tickets=total_tickets
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
except HTTPException:
|
| 72 |
+
raise
|
| 73 |
+
except Exception as e:
|
| 74 |
+
logger.error(f"Metrics calculation failed: {str(e)}")
|
| 75 |
+
raise HTTPException(status_code=500, detail=str(e))
|
app/routes/{metrics_routes copy.py → metrics_routes.py}
RENAMED
|
File without changes
|