Spaces:
Paused

File size: 2,136 Bytes
cde49b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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())