Spaces:
Paused

miracle / scratch /debug_sla.py
dushyantsingh0
backend stabilisation
cde49b7
Raw History Blame Contribute Delete
2.14 kB
import asyncio
import os
import sys
# Windows async compatibility
if sys.platform == 'win32':
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
# Add project root to sys.path
sys.path.append(os.getcwd())
from datetime import datetime, timedelta
from sqlalchemy import select, func, and_, cast, String
from core.database import async_session_factory
from models.ticket import Ticket
from models.escalation_rule import EscalationRule
async def dump():
async with async_session_factory() as db:
since = datetime.utcnow() - timedelta(days=30)
stmt = (
select(Ticket, EscalationRule.threshold_minutes)
.join(EscalationRule, and_(
EscalationRule.workspace_id == Ticket.workspace_id,
EscalationRule.priority_level == cast(Ticket.priority, String)
), isouter=True)
.where(
Ticket.created_at >= since,
Ticket.deleted_at.is_(None)
)
)
res = await db.execute(stmt)
rows = res.all()
daily = {}
now = datetime.utcnow()
for t, thresh in rows:
day = t.created_at.date().isoformat()
if day not in daily: daily[day] = {'total': 0, 'breached': 0, 'ids': []}
daily[day]['total'] += 1
limit = thresh if thresh is not None else 1440
# Fix: duration calculation needs to handle datetime - datetime
effective_end = t.resolved_at if t.resolved_at else now
duration = (effective_end - t.created_at).total_seconds() / 60
if duration > limit:
daily[day]['breached'] += 1
daily[day]['ids'].append(str(t.id)[:8])
print("Date | Total | Breached | Compliance | IDs")
print("-" * 50)
for d in sorted(daily.keys()):
s = daily[d]
comp = (s['total'] - s['breached']) / s['total'] * 100
print(f"{d} | {s['total']:5} | {s['breached']:8} | {comp:9.1f}% | {s['ids']}")
if __name__ == '__main__':
asyncio.run(dump())